Skip to content

Commit

Permalink
lambdaMultiplier in FreeVarsInfo computation
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcz committed Sep 13, 2024
1 parent 87fc7fe commit e0cafd6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 60 deletions.
24 changes: 18 additions & 6 deletions src/Juvix/Compiler/Core/Info/FreeVarsInfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,38 @@ makeLenses ''FreeVarsInfo
-- | Computes free variable info for each subnode. Assumption: no subnode is a
-- closure.
computeFreeVarsInfo :: Node -> Node
computeFreeVarsInfo = umap go
computeFreeVarsInfo = computeFreeVarsInfo' 1

computeFreeVarsInfo' :: Int -> Node -> Node
computeFreeVarsInfo' lambdaMultiplier = umap go
where
go :: Node -> Node
go node = case node of
NVar Var {..} ->
mkVar (Info.insert fvi _varInfo) _varIndex
where
fvi = FreeVarsInfo (Map.singleton _varIndex 1)
NLam Lambda {..} ->
modifyInfo (Info.insert fvi) node
where
fvi =
FreeVarsInfo
. fmap (* lambdaMultiplier)
. Map.mapKeysMonotonic (\idx -> idx - 1)
. Map.filterWithKey (\idx _ -> idx >= 1)
$ getFreeVarsInfo _lambdaBody ^. infoFreeVars
_ ->
modifyInfo (Info.insert fvi) node
where
fvi =
FreeVarsInfo $
foldr
( \NodeChild {..} acc ->
Map.unionWith (+) acc $
Map.mapKeysMonotonic (\idx -> idx - _childBindersNum) $
Map.filterWithKey
(\idx _ -> idx >= _childBindersNum)
(getFreeVarsInfo _childNode ^. infoFreeVars)
Map.unionWith (+) acc
. Map.mapKeysMonotonic (\idx -> idx - _childBindersNum)
. Map.filterWithKey
(\idx _ -> idx >= _childBindersNum)
$ getFreeVarsInfo _childNode ^. infoFreeVars
)
mempty
(children node)
Expand Down
50 changes: 0 additions & 50 deletions src/Juvix/Compiler/Core/Info/ShallowFreeVarsInfo.hs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ letFolding' isFoldable tab =
mapAllNodes
( removeInfo kFreeVarsInfo
. convertNode isFoldable tab
. computeFreeVarsInfo
. computeFreeVarsInfo' 2
)
tab

Expand Down
6 changes: 3 additions & 3 deletions tests/Compilation/positive/test059.juvix
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
-- builtin list
module test059;

import Stdlib.Prelude open hiding {head};
import Stdlib.Prelude open;

mylist : List Nat := [1; 2; 3 + 1];

mylist2 : List (List Nat) := [[10]; [2]; 3 + 1 :: nil];

head : {a : Type} -> a -> List a -> a
head' : {a : Type} -> a -> List a -> a
| a [] := a
| a [x; _] := x
| _ (h :: _) := h;

main : Nat := head 50 mylist + head 50 (head [] mylist2);
main : Nat := head' 50 mylist + head' 50 (head' [] mylist2);

0 comments on commit e0cafd6

Please sign in to comment.