Skip to content

Fix issue with calling generics #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions example/cddl-files/issue80-min.cddl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
main = test<int, int>
set<x> = [x]
test<x, x1> = [
1,
x,
set<x>
]

28 changes: 14 additions & 14 deletions src/Codec/CBOR/Cuddle/CDDL/Resolve.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
-- generic arguments bound.
module Codec.CBOR.Cuddle.CDDL.Resolve (
buildResolvedCTree,
monoCTree,
buildRefCTree,
asMap,
buildMonoCTree,
Expand Down Expand Up @@ -323,10 +322,10 @@ postludeBinding =
, (Name "null" mempty, PTNil)
]

data BindingEnv poly f = BindingEnv
data BindingEnv poly f g = BindingEnv
{ global :: Map.Map Name (poly (CTree.Node f))
-- ^ Global name bindings via 'RuleDef'
, local :: Map.Map Name (CTree.Node f)
, local :: Map.Map Name (CTree.Node g)
-- ^ Local bindings for generic parameters
}
deriving (Generic)
Expand Down Expand Up @@ -354,7 +353,7 @@ deriving instance Eq (CTreeRoot DistRef)
instance Hashable (CTreeRoot DistRef)

resolveRef ::
BindingEnv (ParametrisedWith [Name]) OrRef ->
BindingEnv (ParametrisedWith [Name]) OrRef OrRef ->
CTree.Node OrRef ->
Either NameResolutionFailure (DistRef (CTree DistRef))
resolveRef env (It a) = DIt <$> resolveCTree env a
Expand All @@ -375,7 +374,7 @@ resolveRef env (Ref n args) = case Map.lookup n postludeBinding of
Nothing -> Left $ UnboundReference n

resolveCTree ::
BindingEnv (ParametrisedWith [Name]) OrRef ->
BindingEnv (ParametrisedWith [Name]) OrRef OrRef ->
CTree OrRef ->
Either NameResolutionFailure (CTree DistRef)
resolveCTree e = CTree.traverseCTree (resolveRef e)
Expand Down Expand Up @@ -407,7 +406,7 @@ deriving instance
Show (poly (CTree.Node MonoRef)) =>
Show (CTreeRoot' poly MonoRef)

type MonoEnv = BindingEnv (ParametrisedWith [Name]) DistRef
type MonoEnv = BindingEnv (ParametrisedWith [Name]) DistRef MonoRef

-- | We introduce additional bindings in the state
type MonoState = Map.Map Name (CTree.Node MonoRef)
Expand All @@ -432,10 +431,10 @@ newtype MonoM a = MonoM
deriving
( HasSource
"local"
(Map.Map Name (CTree.Node DistRef))
(Map.Map Name (CTree.Node MonoRef))
, HasReader
"local"
(Map.Map Name (CTree.Node DistRef))
(Map.Map Name (CTree.Node MonoRef))
)
via Field
"local"
Expand Down Expand Up @@ -490,11 +489,12 @@ synthMono n@(Name origName _) args =
Just (Unparametrised _) -> throwNR $ MismatchingArgs n []
Just (Parametrised r params') ->
if length params' == length args
then
let localBinds = Map.fromList $ zip params' args
in Reader.local @"local" (Map.union localBinds) $ do
foo <- resolveGenericRef r
modify @"synth" $ Map.insert fresh foo
then do
rargs <- traverse resolveGenericRef args
let localBinds = Map.fromList $ zip params' rargs
Reader.local @"local" (Map.union localBinds) $ do
foo <- resolveGenericRef r
modify @"synth" $ Map.insert fresh foo
else throwNR $ MismatchingArgs n params'
Nothing -> throwNR $ UnboundReference n
pure fresh
Expand All @@ -512,7 +512,7 @@ resolveGenericRef (RuleRef n margs) =
resolveGenericRef (GenericRef n) = do
localBinds <- ask @"local"
case Map.lookup n localBinds of
Just node -> resolveGenericRef node
Just node -> pure node
Nothing -> throwNR $ UnboundReference n

resolveGenericCTree ::
Expand Down