Rename tree types

This commit is contained in:
George Thomas 2026-02-19 21:11:58 +00:00
parent 261acca8a4
commit ca063b1dd6
7 changed files with 109 additions and 109 deletions

View File

@ -37,10 +37,10 @@ convertShape = \case
data BTree a
= Leaf a
| Fork (BTree a) (BTree a)
withBTree :: BTree Int64 -> (Raw.BTree -> IO a) -> IO a
withBTree :: BTree Int64 -> (Raw.BTreeC -> IO a) -> IO a
withBTree = flip \f -> \case
Leaf v ->
f $ Raw.BTree Raw.Leaf $ Raw.set_bTree_body_leaf $ Raw.Leaf_Body v
f $ Raw.BTreeC Raw.Leaf $ Raw.set_bTreeC_body_leaf $ Raw.Leaf_Body v
Fork l r ->
withBTree l \lRaw ->
withBTree r \rRaw ->
@ -49,8 +49,8 @@ withBTree = flip \f -> \case
poke lPtr lRaw
poke rPtr rRaw
f
. Raw.BTree Raw.Fork
. Raw.set_bTree_body_fork
. Raw.BTreeC Raw.Fork
. Raw.set_bTreeC_body_fork
$ Raw.Fork_Body (unsafeFromPtr lPtr) (unsafeFromPtr rPtr)
hello :: ByteString -> IO ()

View File

@ -526,50 +526,50 @@ instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType Shape) "body")
getField =
HsBindgen.Runtime.HasCField.fromPtr (Data.Proxy.Proxy @"body")
{-| __C declaration:__ @enum BTree_Tag@
{-| __C declaration:__ @enum BTreeC_Tag@
__defined at:__ @garnet_rs.h 33:6@
__exported by:__ @garnet_rs.h@
-}
newtype BTree_Tag = BTree_Tag
newtype BTreeC_Tag = BTreeC_Tag
{ unwrap :: FC.CUInt
}
deriving stock (GHC.Generics.Generic)
deriving stock (Eq, Ord)
deriving newtype (HsBindgen.Runtime.Internal.HasFFIType.HasFFIType)
instance HsBindgen.Runtime.Marshal.StaticSize BTree_Tag where
instance HsBindgen.Runtime.Marshal.StaticSize BTreeC_Tag where
staticSizeOf = \_ -> (4 :: Int)
staticAlignment = \_ -> (4 :: Int)
instance HsBindgen.Runtime.Marshal.ReadRaw BTree_Tag where
instance HsBindgen.Runtime.Marshal.ReadRaw BTreeC_Tag where
readRaw =
\ptr0 ->
pure BTree_Tag
pure BTreeC_Tag
<*> HsBindgen.Runtime.Marshal.readRawByteOff ptr0 (0 :: Int)
instance HsBindgen.Runtime.Marshal.WriteRaw BTree_Tag where
instance HsBindgen.Runtime.Marshal.WriteRaw BTreeC_Tag where
writeRaw =
\ptr0 ->
\s1 ->
case s1 of
BTree_Tag unwrap2 ->
BTreeC_Tag unwrap2 ->
HsBindgen.Runtime.Marshal.writeRawByteOff ptr0 (0 :: Int) unwrap2
deriving via HsBindgen.Runtime.Marshal.EquivStorable BTree_Tag instance F.Storable BTree_Tag
deriving via HsBindgen.Runtime.Marshal.EquivStorable BTreeC_Tag instance F.Storable BTreeC_Tag
deriving via FC.CUInt instance Data.Primitive.Types.Prim BTree_Tag
deriving via FC.CUInt instance Data.Primitive.Types.Prim BTreeC_Tag
instance HsBindgen.Runtime.CEnum.CEnum BTree_Tag where
instance HsBindgen.Runtime.CEnum.CEnum BTreeC_Tag where
type CEnumZ BTree_Tag = FC.CUInt
type CEnumZ BTreeC_Tag = FC.CUInt
toCEnum = BTree_Tag
toCEnum = BTreeC_Tag
fromCEnum = GHC.Records.getField @"unwrap"
@ -580,26 +580,26 @@ instance HsBindgen.Runtime.CEnum.CEnum BTree_Tag where
]
showsUndeclared =
HsBindgen.Runtime.CEnum.showsWrappedUndeclared "BTree_Tag"
HsBindgen.Runtime.CEnum.showsWrappedUndeclared "BTreeC_Tag"
readPrecUndeclared =
HsBindgen.Runtime.CEnum.readPrecWrappedUndeclared "BTree_Tag"
HsBindgen.Runtime.CEnum.readPrecWrappedUndeclared "BTreeC_Tag"
isDeclared = HsBindgen.Runtime.CEnum.seqIsDeclared
mkDeclared = HsBindgen.Runtime.CEnum.seqMkDeclared
instance HsBindgen.Runtime.CEnum.SequentialCEnum BTree_Tag where
instance HsBindgen.Runtime.CEnum.SequentialCEnum BTreeC_Tag where
minDeclaredValue = Leaf
maxDeclaredValue = Fork
instance Show BTree_Tag where
instance Show BTreeC_Tag where
showsPrec = HsBindgen.Runtime.CEnum.shows
instance Read BTree_Tag where
instance Read BTreeC_Tag where
readPrec = HsBindgen.Runtime.CEnum.readPrec
@ -607,15 +607,15 @@ instance Read BTree_Tag where
readListPrec = Text.Read.readListPrecDefault
instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType BTree_Tag) "unwrap")
) => GHC.Records.HasField "unwrap" (Ptr.Ptr BTree_Tag) (Ptr.Ptr ty) where
instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType BTreeC_Tag) "unwrap")
) => GHC.Records.HasField "unwrap" (Ptr.Ptr BTreeC_Tag) (Ptr.Ptr ty) where
getField =
HsBindgen.Runtime.HasCField.fromPtr (Data.Proxy.Proxy @"unwrap")
instance HsBindgen.Runtime.HasCField.HasCField BTree_Tag "unwrap" where
instance HsBindgen.Runtime.HasCField.HasCField BTreeC_Tag "unwrap" where
type CFieldType BTree_Tag "unwrap" = FC.CUInt
type CFieldType BTreeC_Tag "unwrap" = FC.CUInt
offset# = \_ -> \_ -> 0
@ -625,8 +625,8 @@ instance HsBindgen.Runtime.HasCField.HasCField BTree_Tag "unwrap" where
__exported by:__ @garnet_rs.h@
-}
pattern Leaf :: BTree_Tag
pattern Leaf = BTree_Tag 0
pattern Leaf :: BTreeC_Tag
pattern Leaf = BTreeC_Tag 0
{-| __C declaration:__ @Fork@
@ -634,8 +634,8 @@ pattern Leaf = BTree_Tag 0
__exported by:__ @garnet_rs.h@
-}
pattern Fork :: BTree_Tag
pattern Fork = BTree_Tag 1
pattern Fork :: BTreeC_Tag
pattern Fork = BTreeC_Tag 1
{-| __C declaration:__ @struct Leaf_Body@
@ -699,17 +699,17 @@ instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType Leaf_Body) "value")
__exported by:__ @garnet_rs.h@
-}
data Fork_Body = Fork_Body
{ left :: HsBindgen.Runtime.PtrConst.PtrConst BTree
{ left :: HsBindgen.Runtime.PtrConst.PtrConst BTreeC
{- ^ __C declaration:__ @left@
__defined at:__ @garnet_rs.h 43:23@
__defined at:__ @garnet_rs.h 43:24@
__exported by:__ @garnet_rs.h@
-}
, right :: HsBindgen.Runtime.PtrConst.PtrConst BTree
, right :: HsBindgen.Runtime.PtrConst.PtrConst BTreeC
{- ^ __C declaration:__ @right@
__defined at:__ @garnet_rs.h 44:23@
__defined at:__ @garnet_rs.h 44:24@
__exported by:__ @garnet_rs.h@
-}
@ -746,7 +746,7 @@ deriving via HsBindgen.Runtime.Marshal.EquivStorable Fork_Body instance F.Storab
instance HsBindgen.Runtime.HasCField.HasCField Fork_Body "left" where
type CFieldType Fork_Body "left" =
HsBindgen.Runtime.PtrConst.PtrConst BTree
HsBindgen.Runtime.PtrConst.PtrConst BTreeC
offset# = \_ -> \_ -> 0
@ -759,7 +759,7 @@ instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType Fork_Body) "left")
instance HsBindgen.Runtime.HasCField.HasCField Fork_Body "right" where
type CFieldType Fork_Body "right" =
HsBindgen.Runtime.PtrConst.PtrConst BTree
HsBindgen.Runtime.PtrConst.PtrConst BTreeC
offset# = \_ -> \_ -> 8
@ -769,28 +769,28 @@ instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType Fork_Body) "right")
getField =
HsBindgen.Runtime.HasCField.fromPtr (Data.Proxy.Proxy @"right")
{-| __C declaration:__ @union \@BTree_body@
{-| __C declaration:__ @union \@BTreeC_body@
__defined at:__ @garnet_rs.h 49:3@
__exported by:__ @garnet_rs.h@
-}
newtype BTree_body = BTree_body
newtype BTreeC_body = BTreeC_body
{ unwrap :: Data.Array.Byte.ByteArray
}
deriving stock (GHC.Generics.Generic)
deriving via (HsBindgen.Runtime.Internal.SizedByteArray.SizedByteArray 16) 8 instance HsBindgen.Runtime.Marshal.StaticSize BTree_body
deriving via (HsBindgen.Runtime.Internal.SizedByteArray.SizedByteArray 16) 8 instance HsBindgen.Runtime.Marshal.StaticSize BTreeC_body
deriving via (HsBindgen.Runtime.Internal.SizedByteArray.SizedByteArray 16) 8 instance HsBindgen.Runtime.Marshal.ReadRaw BTree_body
deriving via (HsBindgen.Runtime.Internal.SizedByteArray.SizedByteArray 16) 8 instance HsBindgen.Runtime.Marshal.ReadRaw BTreeC_body
deriving via (HsBindgen.Runtime.Internal.SizedByteArray.SizedByteArray 16) 8 instance HsBindgen.Runtime.Marshal.WriteRaw BTree_body
deriving via (HsBindgen.Runtime.Internal.SizedByteArray.SizedByteArray 16) 8 instance HsBindgen.Runtime.Marshal.WriteRaw BTreeC_body
deriving via HsBindgen.Runtime.Marshal.EquivStorable BTree_body instance F.Storable BTree_body
deriving via HsBindgen.Runtime.Marshal.EquivStorable BTreeC_body instance F.Storable BTreeC_body
{-|
__See:__ 'set_bTree_body_leaf'
__See:__ 'set_bTreeC_body_leaf'
__C declaration:__ @leaf@
@ -798,26 +798,26 @@ __defined at:__ @garnet_rs.h 50:22@
__exported by:__ @garnet_rs.h@
-}
get_bTree_body_leaf ::
BTree_body
get_bTreeC_body_leaf ::
BTreeC_body
-> Leaf_Body
get_bTree_body_leaf =
get_bTreeC_body_leaf =
HsBindgen.Runtime.Internal.ByteArray.getUnionPayload
{-|
__See:__ 'get_bTree_body_leaf'
__See:__ 'get_bTreeC_body_leaf'
-}
set_bTree_body_leaf ::
set_bTreeC_body_leaf ::
Leaf_Body
-> BTree_body
set_bTree_body_leaf =
-> BTreeC_body
set_bTreeC_body_leaf =
HsBindgen.Runtime.Internal.ByteArray.setUnionPayload
{-|
__See:__ 'set_bTree_body_fork'
__See:__ 'set_bTreeC_body_fork'
__C declaration:__ @fork@
@ -825,62 +825,62 @@ __defined at:__ @garnet_rs.h 51:22@
__exported by:__ @garnet_rs.h@
-}
get_bTree_body_fork ::
BTree_body
get_bTreeC_body_fork ::
BTreeC_body
-> Fork_Body
get_bTree_body_fork =
get_bTreeC_body_fork =
HsBindgen.Runtime.Internal.ByteArray.getUnionPayload
{-|
__See:__ 'get_bTree_body_fork'
__See:__ 'get_bTreeC_body_fork'
-}
set_bTree_body_fork ::
set_bTreeC_body_fork ::
Fork_Body
-> BTree_body
set_bTree_body_fork =
-> BTreeC_body
set_bTreeC_body_fork =
HsBindgen.Runtime.Internal.ByteArray.setUnionPayload
instance HsBindgen.Runtime.HasCField.HasCField BTree_body "leaf" where
instance HsBindgen.Runtime.HasCField.HasCField BTreeC_body "leaf" where
type CFieldType BTree_body "leaf" = Leaf_Body
type CFieldType BTreeC_body "leaf" = Leaf_Body
offset# = \_ -> \_ -> 0
instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType BTree_body) "leaf")
) => GHC.Records.HasField "leaf" (Ptr.Ptr BTree_body) (Ptr.Ptr ty) where
instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType BTreeC_body) "leaf")
) => GHC.Records.HasField "leaf" (Ptr.Ptr BTreeC_body) (Ptr.Ptr ty) where
getField =
HsBindgen.Runtime.HasCField.fromPtr (Data.Proxy.Proxy @"leaf")
instance HsBindgen.Runtime.HasCField.HasCField BTree_body "fork" where
instance HsBindgen.Runtime.HasCField.HasCField BTreeC_body "fork" where
type CFieldType BTree_body "fork" = Fork_Body
type CFieldType BTreeC_body "fork" = Fork_Body
offset# = \_ -> \_ -> 0
instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType BTree_body) "fork")
) => GHC.Records.HasField "fork" (Ptr.Ptr BTree_body) (Ptr.Ptr ty) where
instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType BTreeC_body) "fork")
) => GHC.Records.HasField "fork" (Ptr.Ptr BTreeC_body) (Ptr.Ptr ty) where
getField =
HsBindgen.Runtime.HasCField.fromPtr (Data.Proxy.Proxy @"fork")
{-| __C declaration:__ @struct BTree@
{-| __C declaration:__ @struct BTreeC@
__defined at:__ @garnet_rs.h 47:8@
__exported by:__ @garnet_rs.h@
-}
data BTree = BTree
{ tag :: BTree_Tag
data BTreeC = BTreeC
{ tag :: BTreeC_Tag
{- ^ __C declaration:__ @tag@
__defined at:__ @garnet_rs.h 48:18@
__defined at:__ @garnet_rs.h 48:19@
__exported by:__ @garnet_rs.h@
-}
, body :: BTree_body
, body :: BTreeC_body
{- ^ __C declaration:__ @body@
__defined at:__ @garnet_rs.h 52:5@
@ -890,52 +890,52 @@ data BTree = BTree
}
deriving stock (GHC.Generics.Generic)
instance HsBindgen.Runtime.Marshal.StaticSize BTree where
instance HsBindgen.Runtime.Marshal.StaticSize BTreeC where
staticSizeOf = \_ -> (24 :: Int)
staticAlignment = \_ -> (8 :: Int)
instance HsBindgen.Runtime.Marshal.ReadRaw BTree where
instance HsBindgen.Runtime.Marshal.ReadRaw BTreeC where
readRaw =
\ptr0 ->
pure BTree
pure BTreeC
<*> HsBindgen.Runtime.HasCField.readRaw (Data.Proxy.Proxy @"tag") ptr0
<*> HsBindgen.Runtime.HasCField.readRaw (Data.Proxy.Proxy @"body") ptr0
instance HsBindgen.Runtime.Marshal.WriteRaw BTree where
instance HsBindgen.Runtime.Marshal.WriteRaw BTreeC where
writeRaw =
\ptr0 ->
\s1 ->
case s1 of
BTree tag2 body3 ->
BTreeC tag2 body3 ->
HsBindgen.Runtime.HasCField.writeRaw (Data.Proxy.Proxy @"tag") ptr0 tag2
>> HsBindgen.Runtime.HasCField.writeRaw (Data.Proxy.Proxy @"body") ptr0 body3
deriving via HsBindgen.Runtime.Marshal.EquivStorable BTree instance F.Storable BTree
deriving via HsBindgen.Runtime.Marshal.EquivStorable BTreeC instance F.Storable BTreeC
instance HsBindgen.Runtime.HasCField.HasCField BTree "tag" where
instance HsBindgen.Runtime.HasCField.HasCField BTreeC "tag" where
type CFieldType BTree "tag" = BTree_Tag
type CFieldType BTreeC "tag" = BTreeC_Tag
offset# = \_ -> \_ -> 0
instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType BTree) "tag")
) => GHC.Records.HasField "tag" (Ptr.Ptr BTree) (Ptr.Ptr ty) where
instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType BTreeC) "tag")
) => GHC.Records.HasField "tag" (Ptr.Ptr BTreeC) (Ptr.Ptr ty) where
getField =
HsBindgen.Runtime.HasCField.fromPtr (Data.Proxy.Proxy @"tag")
instance HsBindgen.Runtime.HasCField.HasCField BTree "body" where
instance HsBindgen.Runtime.HasCField.HasCField BTreeC "body" where
type CFieldType BTree "body" = BTree_body
type CFieldType BTreeC "body" = BTreeC_body
offset# = \_ -> \_ -> 8
instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType BTree) "body")
) => GHC.Records.HasField "body" (Ptr.Ptr BTree) (Ptr.Ptr ty) where
instance ( TyEq ty ((HsBindgen.Runtime.HasCField.CFieldType BTreeC) "body")
) => GHC.Records.HasField "body" (Ptr.Ptr BTreeC) (Ptr.Ptr ty) where
getField =
HsBindgen.Runtime.HasCField.fromPtr (Data.Proxy.Proxy @"body")

View File

@ -54,7 +54,7 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un
, "/* com_garnet_GarnetRs_get_sum_tree */"
, "__attribute__ ((const))"
, "int64_t (*hs_bindgen_473a3e791275b06d (void)) ("
, " struct BTree arg1"
, " struct BTreeC arg1"
, ")"
, "{"
, " return &sum_tree;"
@ -146,7 +146,7 @@ foreign import ccall unsafe "hs_bindgen_473a3e791275b06d" hs_bindgen_473a3e79127
IO (Ptr.FunPtr Void)
-- __unique:__ @com_garnet_GarnetRs_get_sum_tree@
hs_bindgen_473a3e791275b06d :: IO (Ptr.FunPtr (BTree -> IO HsBindgen.Runtime.LibC.Int64))
hs_bindgen_473a3e791275b06d :: IO (Ptr.FunPtr (BTreeC -> IO HsBindgen.Runtime.LibC.Int64))
hs_bindgen_473a3e791275b06d =
HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_473a3e791275b06d_base
@ -157,6 +157,6 @@ hs_bindgen_473a3e791275b06d =
__exported by:__ @garnet_rs.h@
-}
sum_tree :: Ptr.FunPtr (BTree -> IO HsBindgen.Runtime.LibC.Int64)
sum_tree :: Ptr.FunPtr (BTreeC -> IO HsBindgen.Runtime.LibC.Int64)
sum_tree =
GHC.IO.Unsafe.unsafePerformIO hs_bindgen_473a3e791275b06d

View File

@ -45,7 +45,7 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un
, " return add(arg1, arg2);"
, "}"
, "int64_t hs_bindgen_9602640c06f0c62f ("
, " struct BTree *arg1"
, " struct BTreeC *arg1"
, ")"
, "{"
, " return sum_tree(*arg1);"
@ -167,7 +167,7 @@ foreign import ccall safe "hs_bindgen_9602640c06f0c62f" hs_bindgen_9602640c06f0c
-- __unique:__ @com_garnet_GarnetRs_Safe_sum_tree@
hs_bindgen_9602640c06f0c62f ::
Ptr.Ptr BTree
Ptr.Ptr BTreeC
-> IO HsBindgen.Runtime.LibC.Int64
hs_bindgen_9602640c06f0c62f =
HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_9602640c06f0c62f_base
@ -179,7 +179,7 @@ hs_bindgen_9602640c06f0c62f =
__exported by:__ @garnet_rs.h@
-}
sum_tree ::
BTree
BTreeC
-- ^ __C declaration:__ @t@
-> IO HsBindgen.Runtime.LibC.Int64
sum_tree =

View File

@ -45,7 +45,7 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un
, " return add(arg1, arg2);"
, "}"
, "int64_t hs_bindgen_c2ced4f3ba39c8ff ("
, " struct BTree *arg1"
, " struct BTreeC *arg1"
, ")"
, "{"
, " return sum_tree(*arg1);"
@ -167,7 +167,7 @@ foreign import ccall unsafe "hs_bindgen_c2ced4f3ba39c8ff" hs_bindgen_c2ced4f3ba3
-- __unique:__ @com_garnet_GarnetRs_Unsafe_sum_tree@
hs_bindgen_c2ced4f3ba39c8ff ::
Ptr.Ptr BTree
Ptr.Ptr BTreeC
-> IO HsBindgen.Runtime.LibC.Int64
hs_bindgen_c2ced4f3ba39c8ff =
HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_c2ced4f3ba39c8ff_base
@ -179,7 +179,7 @@ hs_bindgen_c2ced4f3ba39c8ff =
__exported by:__ @garnet_rs.h@
-}
sum_tree ::
BTree
BTreeC
-- ^ __C declaration:__ @t@
-> IO HsBindgen.Runtime.LibC.Int64
sum_tree =

View File

@ -30,7 +30,7 @@ struct Shape {
} body;
};
enum BTree_Tag {
enum BTreeC_Tag {
Leaf,
Fork,
};
@ -40,12 +40,12 @@ struct Leaf_Body {
};
struct Fork_Body {
const struct BTree *left;
const struct BTree *right;
const struct BTreeC *left;
const struct BTreeC *right;
};
struct BTree {
enum BTree_Tag tag;
struct BTreeC {
enum BTreeC_Tag tag;
union {
struct Leaf_Body leaf;
struct Fork_Body fork;
@ -60,4 +60,4 @@ void hello_shape(struct Shape s);
__attribute__((const)) int64_t add(int64_t a, int64_t b);
int64_t sum_tree(struct BTree t);
int64_t sum_tree(struct BTreeC t);

View File

@ -42,31 +42,31 @@ extern "C" fn add(a: i64, b: i64) -> i64 {
}
#[repr(C)]
enum BTreeBox {
enum BTree {
Leaf { value: i64 },
Fork { left: Box<BTreeBox>, right: Box<BTreeBox> },
Fork { left: Box<BTree>, right: Box<BTree> },
}
impl BTreeBox {
impl BTree {
fn sum(&self) -> i64 {
match self {
BTreeBox::Leaf { value } => *value,
BTreeBox::Fork { left, right } => left.sum() + right.sum(),
BTree::Leaf { value } => *value,
BTree::Fork { left, right } => left.sum() + right.sum(),
}
}
}
#[repr(C)]
enum BTree {
enum BTreeC {
Leaf {
value: i64,
},
Fork {
left: *const BTree,
right: *const BTree,
left: *const BTreeC,
right: *const BTreeC,
},
}
#[unsafe(no_mangle)]
extern "C" fn sum_tree(t: BTree) -> i64 {
(unsafe { std::mem::transmute::<_, &BTreeBox>(&t) }).sum()
extern "C" fn sum_tree(t: BTreeC) -> i64 {
(unsafe { std::mem::transmute::<_, &BTree>(&t) }).sum()
}