Prenormalize substituted expressions (#765)

Fixes https://github.com/dhall-lang/dhall-haskell/issues/764

This pre-normalizes expressions before substituting them to
avoid duplicated work.  This also fixes the space leak from the
above issue.
This commit is contained in:
Gabriel Gonzalez 2018-12-24 09:35:56 -06:00 committed by GitHub
parent ca78d7977d
commit 0b3fd4b624
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1587,14 +1587,16 @@ normalizeWithM ctx e0 = loop (denote e0)
Just e1 -> loop e1
Nothing -> do
f' <- loop f
a' <- loop a
case f' of
Lam x _A b -> loop b''
where
a' = shift 1 (V x 0) a
b' = subst (V x 0) a' b
b'' = shift (-1) (V x 0) b'
Lam x _A b -> do
let a = shift 1 (V x 0) a'
let b = subst (V x 0) a b
let b = shift (-1) (V x 0) b
loop b
_ -> do
a' <- loop a
case App f' a' of
-- build/fold fusion for `List`
App (App ListBuild _) (App (App ListFold _) e') -> loop e'
@ -1714,15 +1716,18 @@ normalizeWithM ctx e0 = loop (denote e0)
case res2 of
Nothing -> pure (App f' a')
Just app' -> loop app'
Let (Binding x _ a :| ls) b -> loop b
where
rest = case ls of
[] -> b
l : ls -> Let (l :| ls) b
Let (Binding x _ a :| ls) b -> do
a <- loop a
a = shift 1 (V x 0) a
b = subst (V x 0) a rest
b = shift (-1) (V x 0) b
rest <- case ls of
[] -> loop b
l : ls -> loop (Let (l :| ls) b)
let a = shift 1 (V x 0) a
let b = subst (V x 0) a rest
let b = shift (-1) (V x 0) b
loop b
Annot x _ -> loop x
Bool -> pure Bool
BoolLit b -> pure (BoolLit b)