Skip to content

Commit

Permalink
update generic-data-functions
Browse files Browse the repository at this point in the history
Much cleaner interface.
  • Loading branch information
raehik committed Apr 4, 2024
1 parent 15b68cc commit 1ec5303
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 37 deletions.
9 changes: 6 additions & 3 deletions binrep.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ library
, bytezap
, deepseq >=1.4.6.1 && <1.6
, flatparse >=0.5.0.2 && <0.6
, generic-data-functions >=0.3.0 && <0.4
, generic-data-asserts >=0.1.0 && <0.2
, generic-data-functions >=0.4.0 && <0.5
, megaparsec >=9.2.0 && <9.7
, parser-combinators >=1.3.0 && <1.4
, primitive >=0.8.0.0 && <0.10.0.0
Expand Down Expand Up @@ -138,7 +139,8 @@ test-suite spec
, bytezap
, deepseq >=1.4.6.1 && <1.6
, flatparse >=0.5.0.2 && <0.6
, generic-data-functions >=0.3.0 && <0.4
, generic-data-asserts >=0.1.0 && <0.2
, generic-data-functions >=0.4.0 && <0.5
, generic-random >=1.5.0.1 && <1.6
, hspec >=2.7 && <2.12
, megaparsec >=9.2.0 && <9.7
Expand Down Expand Up @@ -184,7 +186,8 @@ benchmark bench
, deepseq >=1.4.6.1 && <1.6
, flatparse >=0.5.0.2 && <0.6
, gauge
, generic-data-functions >=0.3.0 && <0.4
, generic-data-asserts >=0.1.0 && <0.2
, generic-data-functions >=0.4.0 && <0.5
, megaparsec >=9.2.0 && <9.7
, parser-combinators >=1.3.0 && <1.4
, primitive >=0.8.0.0 && <0.10.0.0
Expand Down
23 changes: 20 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
strongweak.flake = false;
generic-data-functions.url = "github:raehik/generic-data-functions";
generic-data-functions.flake = false;
generic-data-asserts.url = "github:raehik/generic-data-asserts";
generic-data-asserts.flake = false;
};
outputs = inputs:
let
Expand All @@ -37,6 +39,7 @@
packages.flatparse.source = inputs.flatparse;
packages.strongweak.source = inputs.strongweak;
packages.generic-data-functions.source = inputs.generic-data-functions;
packages.generic-data-asserts.source = inputs.generic-data-asserts;
devShell = nondevDevShell "ghc98";
};
haskellProjects.ghc96 = {
Expand All @@ -45,6 +48,7 @@
packages.flatparse.source = inputs.flatparse;
packages.strongweak.source = inputs.strongweak;
packages.generic-data-functions.source = inputs.generic-data-functions;
packages.generic-data-asserts.source = inputs.generic-data-asserts;
devShell.mkShellArgs.name = "ghc96-binrep";
devShell.tools = _: {
haskell-language-server = null; # 2024-03-06: broken
Expand All @@ -56,6 +60,7 @@
packages.flatparse.source = inputs.flatparse;
packages.strongweak.source = inputs.strongweak;
packages.generic-data-functions.source = inputs.generic-data-functions;
packages.generic-data-asserts.source = inputs.generic-data-asserts;
devShell = nondevDevShell "ghc94";
};
haskellProjects.ghc92 = {
Expand All @@ -64,6 +69,7 @@
packages.flatparse.source = inputs.flatparse;
packages.strongweak.source = inputs.strongweak;
packages.generic-data-functions.source = inputs.generic-data-functions;
packages.generic-data-asserts.source = inputs.generic-data-asserts;
devShell = nondevDevShell "ghc92";
};
};
Expand Down
3 changes: 2 additions & 1 deletion package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ dependencies:
- bytezap
- primitive >= 0.8.0.0 && < 0.10.0.0
- flatparse >= 0.5.0.2 && < 0.6
- generic-data-functions ^>= 0.3.0
- generic-data-functions ^>= 0.4.0
- generic-data-asserts ^>= 0.1.0
- parser-combinators ^>= 1.3.0
- bytestring >= 0.11 && < 0.13
- strongweak ^>= 0.6.0
Expand Down
20 changes: 10 additions & 10 deletions src/Binrep/BLen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ instance GenericFoldMap BLen where
-- | Measure the byte length of a term of the non-sum type @a@ via its 'Generic'
-- instance.
blenGenericNonSum
:: forall {cd} {gf} {asserts} a
. ( Generic a, Rep a ~ D1 cd gf, GFoldMapNonSum BLen gf
, asserts ~ '[ 'NoEmpty, 'NoSum], ApplyGCAsserts asserts gf)
=> a -> Int
blenGenericNonSum = Monoid.getSum . genericFoldMapNonSum @asserts @BLen
:: forall a
. ( Generic a, GFoldMapNonSum BLen (Rep a)
, GAssertNotVoid a, GAssertNotSum a
) => a -> Int
blenGenericNonSum = Monoid.getSum . genericFoldMapNonSum @BLen

-- | Measure the byte length of a term of the sum type @a@ via its 'Generic'
-- instance.
Expand All @@ -72,12 +72,12 @@ blenGenericNonSum = Monoid.getSum . genericFoldMapNonSum @asserts @BLen
-- inspecting the reified constructor names. This is regrettably inefficient.
-- Alas. Do write your own instance if you want better performance!
blenGenericSum
:: forall {cd} {gf} {asserts} a
. (Generic a, Rep a ~ D1 cd gf, GFoldMapSum 'SumOnly BLen gf
, asserts ~ '[ 'NoEmpty, 'NeedSum], ApplyGCAsserts asserts gf)
=> (String -> Int) -> a -> Int
:: forall a
. ( Generic a, GFoldMapSum BLen 'SumOnly (Rep a)
, GAssertNotVoid a, GAssertSum a
) => (String -> Int) -> a -> Int
blenGenericSum f =
Monoid.getSum . genericFoldMapSum @'SumOnly @asserts @BLen (Monoid.Sum <$> f)
Monoid.getSum . genericFoldMapSum @BLen @'SumOnly (Monoid.Sum <$> f)

instance TypeError ENoEmpty => BLen Void where blen = undefined
instance TypeError ENoSum => BLen (Either a b) where blen = undefined
Expand Down
20 changes: 10 additions & 10 deletions src/Binrep/Get.hs
Original file line number Diff line number Diff line change
Expand Up @@ -179,19 +179,19 @@ instance GenericTraverseSum Get where
FP.err $ E 0 $ EGeneric cd $ EGenericSum $ EGenericSumTagNoMatch cstrs ptText

getGenericNonSum
:: forall {cd} {gf} {asserts} a
. (Generic a, Rep a ~ D1 cd gf, GTraverseNonSum cd Get gf
, asserts ~ '[ 'NoEmpty, 'NoSum], ApplyGCAsserts asserts gf)
=> Getter a
getGenericNonSum = genericTraverseNonSum @asserts @Get
:: forall a
. (Generic a, GTraverseNonSum Get (Rep a)
, GAssertNotVoid a, GAssertNotSum a
) => Getter a
getGenericNonSum = genericTraverseNonSum @Get

getGenericSum
:: forall {cd} {gf} {asserts} pt a
. ( Generic a, Rep a ~ D1 cd gf, GTraverseSum 'SumOnly cd Get gf
:: forall pt a
. ( Generic a, GTraverseSum Get 'SumOnly (Rep a)
, Get pt
, asserts ~ '[ 'NoEmpty, 'NeedSum], ApplyGCAsserts asserts gf)
=> PfxTagCfg pt -> Getter a
getGenericSum = genericTraverseSum @'SumOnly @asserts @Get
, GAssertNotVoid a, GAssertSum a
) => PfxTagCfg pt -> Getter a
getGenericSum = genericTraverseSum @Get @'SumOnly

instance TypeError ENoEmpty => Get Void where get = undefined
instance TypeError ENoSum => Get (Either a b) where get = undefined
Expand Down
20 changes: 10 additions & 10 deletions src/Binrep/Put.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ instance GenericFoldMap Put where

-- | Serialize a term of the non-sum type @a@ via its 'Generic' instance.
putGenericNonSum
:: forall {cd} {gf} {asserts} a
. ( Generic a, Rep a ~ D1 cd gf, GFoldMapNonSum Put gf
, asserts ~ '[ 'NoEmpty, 'NoSum], ApplyGCAsserts asserts gf)
=> a -> Putter
putGenericNonSum = genericFoldMapNonSum @asserts @Put
:: forall a
. ( Generic a, GFoldMapNonSum Put (Rep a)
, GAssertNotVoid a, GAssertNotSum a
) => a -> Putter
putGenericNonSum = genericFoldMapNonSum @Put

-- | Serialize a term of the sum type @a@ via its 'Generic' instance.
--
-- You must provide a serializer for @a@'s constructors. This is regrettably
-- inefficient due to having to use 'String's. Alas. Do write your own instance
-- if you want better performance!
putGenericSum
:: forall {cd} {gf} {asserts} a
. ( Generic a, Rep a ~ D1 cd gf, GFoldMapSum 'SumOnly Put gf
, asserts ~ '[ 'NoEmpty, 'NeedSum], ApplyGCAsserts asserts gf)
=> (String -> Putter) -> a -> Putter
putGenericSum = genericFoldMapSum @'SumOnly @asserts @Put
:: forall a
. ( Generic a, GFoldMapSum Put 'SumOnly (Rep a)
, GAssertNotVoid a, GAssertSum a
) => (String -> Putter) -> a -> Putter
putGenericSum = genericFoldMapSum @Put @'SumOnly

instance Prim' a => Put (ViaPrim a) where
put = fromStructPoke (sizeOf (undefined :: a)) . putC
Expand Down

0 comments on commit 1ec5303

Please sign in to comment.