Skip to content

Commit 3b684cf

Browse files
committed
Switched Resolve to TTG
1 parent 359aac4 commit 3b684cf

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
lines changed

src/Codec/CBOR/Cuddle/CDDL/CTree.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Codec.CBOR.Cuddle.CDDL (
1111
)
1212
import Codec.CBOR.Cuddle.CDDL.CtlOp
1313
import Codec.CBOR.Cuddle.CDDL.Postlude (PTerm)
14+
import Data.Hashable (Hashable)
1415
import Data.List.NonEmpty qualified as NE
1516
import Data.Map.Strict qualified as Map
1617
import Data.Word (Word64)
@@ -32,7 +33,9 @@ data Parametrisation a = Parametrisation
3233
{ parameters :: [Name]
3334
, underlying :: a
3435
}
35-
deriving (Generic, Functor)
36+
deriving (Generic, Functor, Show, Eq, Foldable, Traversable)
37+
38+
instance Hashable a => Hashable (Parametrisation a)
3639

3740
data Parametrised
3841

src/Codec/CBOR/Cuddle/CDDL/Resolve.hs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ module Codec.CBOR.Cuddle.CDDL.Resolve (
3131
asMap,
3232
buildMonoCTree,
3333
fullResolveCDDL,
34-
MonoRef (..),
3534
NameResolutionFailure (..),
35+
MonoReferenced,
36+
MonoRef (..),
3637
)
3738
where
3839

@@ -55,7 +56,6 @@ import Codec.CBOR.Cuddle.CDDL.Postlude (PTerm (..))
5556
import Control.Monad.Except (ExceptT (..), runExceptT)
5657
import Control.Monad.Reader (Reader, ReaderT (..), runReader)
5758
import Control.Monad.State.Strict (StateT (..))
58-
import Data.Functor.Identity (Identity (..))
5959
import Data.Generics.Product
6060
import Data.Generics.Sum
6161
import Data.Hashable
@@ -342,7 +342,7 @@ data DistRef a
342342
RuleRef Name [CTree.Node DistReferenced]
343343
deriving (Eq, Generic, Functor, Show)
344344

345-
instance Hashable (DistRef a)
345+
instance Hashable a => Hashable (DistRef a)
346346

347347
deriving instance Show (CTree DistReferenced)
348348

@@ -529,32 +529,23 @@ resolveGenericCTree = CTree.traverseCTree resolveGenericRef resolveGenericCTree
529529
-- Concretely, for each reference in the tree to a generic rule, we synthesize a
530530
-- new monomorphic instance of that rule at top-level with the correct
531531
-- parameters applied.
532-
monoCTree ::
533-
CTreeRoot DistReferenced ->
534-
MonoM (CTreeRoot MonoReferenced)
535-
monoCTree (CTreeRoot ct) = CTreeRoot <$> traverse go ct
536-
where
537-
go = traverse resolveGenericRef
538-
539532
buildMonoCTree ::
540533
CTreeRoot DistReferenced ->
541534
Either NameResolutionFailure (Map.Map Name (CTree.Node MonoReferenced))
542535
buildMonoCTree (CTreeRoot ct) = do
543-
let a1 = runExceptT $ runMonoM (monoCTree monoC)
536+
let a1 = runExceptT $ runMonoM (traverse resolveGenericRef monoC)
544537
a2 = runStateT a1 mempty
545-
(er, newBindings) = runReader a2 initBindingEnv
546-
CTreeRoot r <- er
547-
pure $ Map.union r $ newBindings
538+
(r, newBindings) = runReader a2 initBindingEnv
539+
(`Map.union` newBindings) <$> r
548540
where
549541
initBindingEnv = BindingEnv ct mempty
550542
monoC =
551-
CTreeRoot $
552-
Map.mapMaybe
553-
( \case
554-
CTree.Parametrisation [] f -> Just $ Identity f
555-
CTree.Parametrisation _ _ -> Nothing
556-
)
557-
ct
543+
Map.mapMaybe
544+
( \case
545+
Parametrisation [] f -> Just f
546+
Parametrisation _ _ -> Nothing
547+
)
548+
ct
558549

559550
--------------------------------------------------------------------------------
560551
-- Combined resolution

0 commit comments

Comments
 (0)