Skip to content

Commit d3d7baf

Browse files
authored
dhall-nix: Fix field-based union access (#907)
Fixes #906. This adds new translation rules: * <Foo : {} | Bar>.Foo => x: { Bar, Foo }: Foo x * <Foo : {} | Bar>.Foo {=} => { Bar, Foo }: Foo {} * <Foo : {} | Bar>.Bar => { Bar, Foo }: Bar
1 parent 21da6f2 commit d3d7baf

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

dhall-nix/src/Dhall/Nix.hs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@ dhallToNix e = loop (Dhall.Core.normalize e)
208208
-- None needs a type to convert to an Optional
209209
loop (App None _) = do
210210
return (Fix (NConstant NNull))
211+
loop (App (Field (Union kts) k) v) = do
212+
v' <- loop v
213+
let e0 = do
214+
k' <- Dhall.Map.keys kts
215+
return (k', Nothing)
216+
let e2 = Fix (NBinary NApp (Fix (NSym k)) v')
217+
return (Fix (NAbs (ParamSet e0 False Nothing) e2))
211218
loop (App a b) = do
212219
a' <- loop a
213220
b' <- loop b
@@ -488,6 +495,26 @@ dhallToNix e = loop (Dhall.Core.normalize e)
488495
a' <- loop a
489496
b' <- loop b
490497
return (Fix (NBinary NUpdate a' b'))
498+
loop (Field (Union kts) k) =
499+
case Dhall.Map.lookup k kts of
500+
-- If the selected alternative has an associated payload, then we
501+
-- need introduce the partial application through an extra abstraction
502+
-- (here "x").
503+
--
504+
-- This translates `< Foo : T >.Foo` to `x: { Foo }: Foo x`
505+
Just ( Just _ ) -> do
506+
let e0 = do
507+
k' <- Dhall.Map.keys kts
508+
return (k', Nothing)
509+
let e2 = Fix (NBinary NApp (Fix (NSym k)) (Fix (NSym "x")))
510+
return (Fix (NAbs (Param "x") (Fix (NAbs (ParamSet e0 False Nothing) e2))))
511+
512+
_ -> do
513+
let e0 = do
514+
k' <- Dhall.Map.keys kts
515+
return (k', Nothing)
516+
let e2 = Fix (NSym k)
517+
return (Fix (NAbs (ParamSet e0 False Nothing) e2))
491518
loop (Field a b) = do
492519
a' <- loop a
493520
return (Fix (NSelect a' [StaticKey b] Nothing))

0 commit comments

Comments
 (0)