Skip to content

Commit fcca883

Browse files
AndrasKovacsGabriella439
authored andcommitted
Add a new environment machine normalizer (#876)
- Dhall.Eval: new evaluator, conversion checker and normalizer. There is no standalone alpha normalizer yet. - There is a new option "new-normalize" for dhall executable, which uses the new normalizer. - Type checker is unchanged. - new implementation: alphaNormalize, judgmentallyEqual, normalize - normalizeWith takes a Maybe ReifiedNormalizer argument now, and switches to the new evaluator whenever the input normalizer is Nothing - QuickCheck test for isNormalized removed, because we don't support evaluation of ill-typed terms, which the test would require.
1 parent 91f3cae commit fcca883

File tree

18 files changed

+1163
-344
lines changed

18 files changed

+1163
-344
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
let Nat = (N : Type) (N N) N N
3+
let n2 = λ(N : Type) λ(s : N N) λ(z : N) s (s z)
4+
let n5 = λ(N : Type) λ(s : N N) λ(z : N) s (s (s (s (s z))))
5+
6+
let mul =
7+
λ(a : Nat) λ(b : Nat) λ(N : Type) λ(s : N N) λ(z : N) a N (b N s) z
8+
9+
let add =
10+
λ(a : Nat) λ(b : Nat) λ(N : Type) λ(s : N N) λ(z : N) a N s (b N s z)
11+
12+
let n10 = mul n2 n5
13+
let n100 = mul n10 n10
14+
let n1k = mul n10 n100
15+
let n10k = mul n100 n100
16+
let n100k = mul n10 n10k
17+
let n1M = mul n10k n100
18+
let n5M = mul n1M n5
19+
let n10M = mul n1M n10
20+
let n20M = mul n10M n2
21+
22+
in n1M Natural (λ (x:Natural) x + 1) 0
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
let compose
3+
: (a : Type) (b : Type) (c : Type) (a b) (b c) a c
4+
= λ(A : Type)
5+
λ(B : Type)
6+
λ(C : Type)
7+
λ(f : A B)
8+
λ(g : B C)
9+
λ(x : A)
10+
g (f x)
11+
12+
let composeN : (a : Type) Natural (a a) a a
13+
= λ (a : Type)
14+
λ (n : Natural)
15+
λ (f : a a)
16+
Natural/fold n (a a) (compose a a a f) (λ (x : a) x)
17+
18+
in composeN Natural 100000 (λ (x : Natural) x + 1) 0
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
let iterate
3+
= λ(n : Natural)
4+
λ(a : Type)
5+
λ(f : a a)
6+
λ(x : a)
7+
List/build
8+
a
9+
( λ(list : Type)
10+
λ(cons : a list list)
11+
List/fold
12+
{ index : Natural, value : {} }
13+
( List/indexed
14+
{}
15+
( List/build
16+
{}
17+
( λ(list : Type)
18+
λ(cons : {} list list)
19+
Natural/fold n list (cons {=})
20+
)
21+
)
22+
)
23+
list
24+
( λ(y : { index : Natural, value : {} })
25+
cons (Natural/fold y.index a f x)
26+
)
27+
)
28+
29+
let countTo =
30+
λ (x : Natural)
31+
iterate x Natural (λ (x : Natural) x + 1) 0
32+
33+
let sum =
34+
λ (xs : List Natural)
35+
List/fold Natural xs Natural (λ (x : Natural) λ (acc : Natural) x + acc) 0
36+
37+
38+
let map
39+
: (a : Type) (b : Type) (a b) List a List b
40+
= λ(a : Type)
41+
λ(b : Type)
42+
λ(f : a b)
43+
λ(xs : List a)
44+
List/build
45+
b
46+
( λ(list : Type)
47+
λ(cons : b list list)
48+
List/fold a xs list (λ(x : a) cons (f x))
49+
)
50+
51+
let any
52+
: (a : Type) (a Bool) List a Bool
53+
= λ(a : Type)
54+
λ(f : a Bool)
55+
λ(xs : List a)
56+
List/fold a xs Bool (λ(x : a) λ(r : Bool) f x || r) False
57+
58+
let filter
59+
: (a : Type) (a Bool) List a List a
60+
= λ(a : Type)
61+
λ(f : a Bool)
62+
λ(xs : List a)
63+
List/build
64+
a
65+
( λ(list : Type)
66+
λ(cons : a list list)
67+
List/fold
68+
a
69+
xs
70+
list
71+
(λ(x : a) λ(xs : list) if f x then cons x xs else xs)
72+
)
73+
74+
in sum (filter Natural Natural/even
75+
(map Natural Natural (λ(x:Natural) x + 10) (countTo 1000)))
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
let iterate =
3+
λ(n : Natural)
4+
λ(a : Type)
5+
λ(f : a a)
6+
λ(x : a)
7+
(Natural/fold n
8+
(a List a {fst:a, snd:List a})
9+
(λ (hyp : a List a {fst:a, snd:List a})
10+
λ (x : a) λ (xs : List a)
11+
let tup = hyp x xs
12+
in {fst = f (tup.fst), snd = tup.snd # [tup.fst]})
13+
(λ (x : a) λ (xs : List a) {fst=x, snd=xs})
14+
x ([] : List a)).snd
15+
16+
let countTo =
17+
λ (x : Natural)
18+
iterate x Natural (λ (x : Natural) x + 1) 0
19+
20+
let sum =
21+
λ (xs : List Natural)
22+
List/fold Natural xs Natural (λ (x : Natural) λ (acc : Natural) x + acc) 0
23+
24+
let map
25+
= λ(a : Type)
26+
λ(b : Type)
27+
λ(f : a b)
28+
λ(xs : List a)
29+
List/fold a xs (List b) (λ (x : a) λ (xs : List b) [f x] # xs) ([] : List b)
30+
31+
let any
32+
= λ(a : Type)
33+
λ(f : a Bool)
34+
λ(xs : List a)
35+
List/fold a xs Bool (λ(x : a) λ(r : Bool) f x || r) False
36+
37+
let filter
38+
= λ(a : Type)
39+
λ(f : a Bool)
40+
λ(xs : List a)
41+
List/fold a xs (List a)
42+
(λ (x : a) λ (xs : List a) if f x then [x] # xs else xs) ([] : List a)
43+
44+
in sum (filter Natural Natural/even
45+
(map Natural Natural (λ(x:Natural) x + 10) (countTo 1000)))

dhall/dhall-lang

Submodule dhall-lang updated 647 files

dhall/dhall.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Category: Compiler
2525
Extra-Source-Files:
2626
benchmark/deep-nested-large-record/*.dhall
2727
benchmark/examples/*.dhall
28+
benchmark/examples/normalize/*.dhall
2829
CHANGELOG.md
2930
dhall-lang/Prelude/Bool/and
3031
dhall-lang/Prelude/Bool/build
@@ -448,6 +449,7 @@ Library
448449
Dhall.Parser.Combinators,
449450
Dhall.Parser.Token,
450451
Dhall.Import.Types,
452+
Dhall.Eval,
451453
Dhall.Util,
452454
Paths_dhall
453455
if flag(with-http)

dhall/src/Dhall.hs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ instance (Pretty s, Pretty a, Typeable s, Typeable a) => Show (InvalidType s a)
185185
where
186186
txt0 = Dhall.Util.insert invalidTypeExpected
187187
txt1 = Dhall.Util.insert invalidTypeExpression
188-
188+
189189

190190
instance (Pretty s, Pretty a, Typeable s, Typeable a) => Exception (InvalidType s a)
191191

@@ -231,7 +231,7 @@ sourceName k s =
231231
-- | @since 1.16
232232
data EvaluateSettings = EvaluateSettings
233233
{ _startingContext :: Dhall.Context.Context (Expr Src X)
234-
, _normalizer :: Dhall.Core.ReifiedNormalizer X
234+
, _normalizer :: Maybe (Dhall.Core.ReifiedNormalizer X)
235235
, _standardVersion :: StandardVersion
236236
}
237237

@@ -242,7 +242,7 @@ data EvaluateSettings = EvaluateSettings
242242
defaultEvaluateSettings :: EvaluateSettings
243243
defaultEvaluateSettings = EvaluateSettings
244244
{ _startingContext = Dhall.Context.empty
245-
, _normalizer = Dhall.Core.ReifiedNormalizer (const (pure Nothing))
245+
, _normalizer = Nothing
246246
, _standardVersion = Dhall.Binary.defaultStandardVersion
247247
}
248248

@@ -263,11 +263,11 @@ startingContext = evaluateSettings . l
263263
-- @since 1.16
264264
normalizer
265265
:: (Functor f, HasEvaluateSettings s)
266-
=> LensLike' f s (Dhall.Core.ReifiedNormalizer X)
266+
=> LensLike' f s (Maybe (Dhall.Core.ReifiedNormalizer X))
267267
normalizer = evaluateSettings . l
268268
where
269269
l :: (Functor f)
270-
=> LensLike' f EvaluateSettings (Dhall.Core.ReifiedNormalizer X)
270+
=> LensLike' f EvaluateSettings (Maybe (Dhall.Core.ReifiedNormalizer X))
271271
l k s = fmap (\x -> s { _normalizer = x }) (k (_normalizer s))
272272

273273
-- | Access the standard version (used primarily when encoding or decoding
@@ -360,10 +360,8 @@ inputWithSettings settings (Type {..}) txt = do
360360
_ ->
361361
Annot expr' expected
362362
_ <- throws (Dhall.TypeCheck.typeWith (view startingContext settings) annot)
363-
let normExpr = Dhall.Core.normalizeWith
364-
(Dhall.Core.getReifiedNormalizer
365-
(view normalizer settings))
366-
expr'
363+
let normExpr = Dhall.Core.normalizeWith (view normalizer settings) expr'
364+
367365
case extract normExpr of
368366
Just x -> return x
369367
Nothing -> Control.Exception.throwIO
@@ -450,7 +448,7 @@ inputExprWithSettings settings txt = do
450448
expr' <- State.evalStateT (Dhall.Import.loadWith expr) status
451449

452450
_ <- throws (Dhall.TypeCheck.typeWith (view startingContext settings) expr')
453-
pure (Dhall.Core.normalizeWith (Dhall.Core.getReifiedNormalizer (view normalizer settings)) expr')
451+
pure (Dhall.Core.normalizeWith (view normalizer settings) expr')
454452

455453
-- | Use this function to extract Haskell values directly from Dhall AST.
456454
-- The intended use case is to allow easy extraction of Dhall values for
@@ -834,7 +832,7 @@ instance Interpret a => Interpret (Vector a) where
834832
instance (Inject a, Interpret b) => Interpret (a -> b) where
835833
autoWith opts = Type extractOut expectedOut
836834
where
837-
normalizer_ = Dhall.Core.getReifiedNormalizer (inputNormalizer opts)
835+
normalizer_ = Just (inputNormalizer opts)
838836

839837
extractOut e = Just (\i -> case extractIn (Dhall.Core.normalizeWith normalizer_ (App e (embed i))) of
840838
Just o -> o

0 commit comments

Comments
 (0)