Skip to content
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

dhall-to-{json,yaml,yaml-ng}: support for recursive types #2133

Open
WillSewell opened this issue Jan 11, 2021 · 0 comments
Open

dhall-to-{json,yaml,yaml-ng}: support for recursive types #2133

WillSewell opened this issue Jan 11, 2021 · 0 comments

Comments

@WillSewell
Copy link
Collaborator

Support recursive types as described in https://docs.dhall-lang.org/howtos/How-to-translate-recursive-code-to-Dhall.html.

dhall-to-{json,yaml,yaml-ng} already has special case support for the recursive JSON type:

Core.Lam _ (Core.functionBindingAnnotation -> Core.Const Core.Type)
(Core.Lam _ (Core.functionBindingAnnotation ->
(Core.Record
[ ("array" , Core.recordFieldValue -> Core.Pi _ _ (Core.App Core.List (V 0)) (V 1))
, ("bool" , Core.recordFieldValue -> Core.Pi _ _ Core.Bool (V 1))
, ("null" , Core.recordFieldValue -> V 0)
, ("number", Core.recordFieldValue -> Core.Pi _ _ Core.Double (V 1))
, ("object", Core.recordFieldValue ->
Core.Pi _ _ (Core.App Core.List (Core.Record
[ ("mapKey", Core.recordFieldValue -> Core.Text)
, ("mapValue", Core.recordFieldValue -> V 0)])) (V 1))
, ("string", Core.recordFieldValue -> Core.Pi _ _ Core.Text (V 1))
]
))
value
) -> do
let outer (Core.Field (V 0) (FA "null")) = return Aeson.Null
outer (Core.App (Core.Field (V 0) (FA "bool")) (Core.BoolLit b)) =
return (Aeson.Bool b)
outer (Core.App (Core.Field (V 0) (FA "array")) (Core.ListLit _ xs)) = do
ys <- traverse outer (Foldable.toList xs)
return (Aeson.Array (Vector.fromList ys))
outer (Core.App (Core.Field (V 0) (FA "object")) (Core.ListLit _ xs)) = do
let inner (Core.RecordLit
[ ("mapKey", Core.recordFieldValue -> Core.TextLit (Core.Chunks [] mapKey))
, ("mapValue", Core.recordFieldValue -> mapExpression)]) = do
mapValue <- outer mapExpression
return (mapKey, mapValue)
inner _ = Left (Unsupported e)
ys <- traverse inner (Foldable.toList xs)
return (Aeson.Object (HashMap.fromList ys))
outer (Core.App (Core.Field (V 0) (FA "number")) (Core.DoubleLit (DhallDouble n))) =
return (Aeson.toJSON n)
outer (Core.App (Core.Field (V 0) (FA "string")) (Core.TextLit (Core.Chunks [] text))) =
return (toJSON text)
outer _ = Left (Unsupported e)
outer value
Core.Lam _ (Core.functionBindingAnnotation -> Core.Const Core.Type)
(Core.Lam _ (Core.functionBindingAnnotation ->
(Core.Record
[ ("array" , Core.recordFieldValue -> Core.Pi _ _ (Core.App Core.List (V 0)) (V 1))
, ("bool" , Core.recordFieldValue -> Core.Pi _ _ Core.Bool (V 1))
, ("double", Core.recordFieldValue -> Core.Pi _ _ Core.Double (V 1))
, ("integer", Core.recordFieldValue -> Core.Pi _ _ Core.Integer (V 1))
, ("null" , Core.recordFieldValue -> V 0)
, ("object", Core.recordFieldValue ->
Core.Pi _ _ (Core.App Core.List (Core.Record
[ ("mapKey", Core.recordFieldValue -> Core.Text)
, ("mapValue", Core.recordFieldValue -> V 0)
])) (V 1))
, ("string", Core.recordFieldValue -> Core.Pi _ _ Core.Text (V 1))
]
))
value
) -> do
let outer (Core.Field (V 0) (FA "null")) =
return Aeson.Null
outer (Core.App (Core.Field (V 0) (FA "bool")) (Core.BoolLit b)) =
return (Aeson.Bool b)
outer (Core.App (Core.Field (V 0) (FA "array")) (Core.ListLit _ xs)) = do
ys <- traverse outer (Foldable.toList xs)
return (Aeson.Array (Vector.fromList ys))
outer (Core.App (Core.Field (V 0) (FA "object")) (Core.ListLit _ xs)) = do
let inner (Core.RecordLit
[ ("mapKey", Core.recordFieldValue -> Core.TextLit (Core.Chunks [] mapKey))
, ("mapValue", Core.recordFieldValue -> mapExpression)]) = do
mapValue <- outer mapExpression
return (mapKey, mapValue)
inner _ = Left (Unsupported e)
ys <- traverse inner (Foldable.toList xs)
return (Aeson.Object (HashMap.fromList ys))
outer (Core.App (Core.Field (V 0) (FA "double")) (Core.DoubleLit (DhallDouble n))) =
return (Aeson.toJSON n)
outer (Core.App (Core.Field (V 0) (FA "integer")) (Core.IntegerLit n)) =
return (Aeson.toJSON n)
outer (Core.App (Core.Field (V 0) (FA "string")) (Core.TextLit (Core.Chunks [] text))) =
return (toJSON text)
outer _ = Left (Unsupported e)

In dhall-lang/dhall-kubernetes#111 (comment), @Gabriel439 suggested adding support for arbitrary recursive types. I thought it was better to create a separate issue to track this because the limitation is more general than that particular instance of the issue.

I would like to help with this, but to be honest I'm not sure how successful I would be given my current Dhall experience (understanding how to model recursive types was tricky enough!). However if anyone could give any hints/pointers I could give it a go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants