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

optimize Natural/fold in the strict case #2585

Merged
merged 9 commits into from
Jun 12, 2024
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
12 changes: 8 additions & 4 deletions dhall/src/Dhall/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ instance Semigroup (VChunks a) where
VChunks xys z <> VChunks [] z' = VChunks xys (z <> z')
VChunks xys z <> VChunks ((x', y'):xys') z' = VChunks (xys ++ (z <> x', y'):xys') z'

instance Monoid (VChunks b) where
instance Monoid (VChunks a) where
mempty = VChunks [] mempty

{-| Some information is lost when `eval` converts a `Lam` or a built-in function
Expand Down Expand Up @@ -527,9 +527,13 @@ eval !env t0 =
-- following issue:
--
-- https://github.com/ghcjs/ghcjs/issues/782
let go !acc 0 = acc
go acc m = go (vApp succ acc) (m - 1)
in go zero (fromIntegral n' :: Integer)
go zero (fromIntegral n' :: Integer) where
go !acc 0 = acc
go (VNaturalLit x) m =
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, we detect the shortcut only when the accumulator is a Natural literal. However, we would like to detect the shortcut in all cases. I could not find any way of comparing Val values. What kind of code would need to be written here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dhall.Eval.conv is what you want

case vApp succ (VNaturalLit x) of
VNaturalLit y | x == y -> VNaturalLit x
notNaturalLit -> go notNaturalLit (m - 1)
go acc m = go (vApp succ acc) (m - 1)
_ -> inert
NaturalBuild ->
VPrim $ \case
Expand Down
12 changes: 10 additions & 2 deletions dhall/src/Dhall/Normalize.hs
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,16 @@ normalizeWithM ctx e0 = loop (Syntax.denote e0)
strict = strictLoop (fromIntegral n0 :: Integer)
lazy = loop ( lazyLoop (fromIntegral n0 :: Integer))

strictLoop 0 = loop zero
strictLoop !n = App succ' <$> strictLoop (n - 1) >>= loop
strictLoop !n = do
z <- loop zero
strictLoopShortcut n z

strictLoopShortcut 0 !previous = pure previous
strictLoopShortcut !n !previous = do
current <- loop (App succ' previous)
if judgmentallyEqual previous current
then pure previous
else strictLoopShortcut (n - 1) current

lazyLoop 0 = zero
lazyLoop !n = App succ' (lazyLoop (n - 1))
Expand Down
Loading