Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcz committed Aug 2, 2022
1 parent 293ce7e commit d9e09be
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion juvix-stdlib
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Juvix.Core.Types.InfoTable where
module Juvix.Core.Data.InfoTable where

import Juvix.Core.Language
import Juvix.Core.Language.Type
Expand Down
2 changes: 1 addition & 1 deletion src/Juvix/Core/Evaluator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import GHC.Show
import Juvix.Core.Extra
import Juvix.Core.Language
import Juvix.Core.Language.Info qualified as Info
import Juvix.Core.Types.InfoTable
import Juvix.Core.Data.InfoTable

newtype EvalError = EvalError String

Expand Down
4 changes: 4 additions & 0 deletions src/Juvix/Core/Extra.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Data.HashSet qualified as HashSet
import Juvix.Core.Extra.Base
import Juvix.Core.Extra.Recursors
import Juvix.Core.Language
import Juvix.Core.Language.Info qualified as Info

-- `isClosed` may short-circuit evaluation due to the use of `&&`, so it's not
-- entirely reducible to `getFreeVars` in terms of computation time.
Expand Down Expand Up @@ -124,3 +125,6 @@ etaExpandConstrs argsNum = umap go
| argsNum constrTag > length constrArgs ->
etaExpand (argsNum constrTag - length constrArgs) n
_ -> n

removeInfo :: IsInfo i => Key i -> Node -> Node
removeInfo k = umap (modifyInfo (Info.delete k))
32 changes: 19 additions & 13 deletions src/Juvix/Core/Extra/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,42 @@ getArgs = snd . unfoldType
{------------------------------------------------------------------------}
{- functions on Node -}

mkApp :: Node -> [(Info, Node)] -> Node
mkApp = foldl' (\acc (i, n) -> App i acc n)
mkApp' :: Node -> [(Info, Node)] -> Node
mkApp' = foldl' (\acc (i, n) -> App i acc n)

mkApp' :: Node -> [Node] -> Node
mkApp' = foldl' (App Info.empty)
mkApp :: Node -> [Node] -> Node
mkApp = foldl' (App Info.empty)

unfoldApp :: Node -> (Node, [(Info, Node)])
unfoldApp = go []
unfoldApp' :: Node -> (Node, [(Info, Node)])
unfoldApp' = go []
where
go :: [(Info, Node)] -> Node -> (Node, [(Info, Node)])
go acc n = case n of
App i l r -> go ((i, r) : acc) l
_ -> (n, acc)

mkLambdas :: [Info] -> Node -> Node
mkLambdas is n = foldr Lambda n is
unfoldApp :: Node -> (Node, [Node])
unfoldApp = second (map snd) . unfoldApp'

mkLambdas' :: Int -> Node -> Node
mkLambdas' k = mkLambdas (replicate k Info.empty)
mkLambdas' :: [Info] -> Node -> Node
mkLambdas' is n = foldr Lambda n is

unfoldLambdas :: Node -> ([Info], Node)
unfoldLambdas = go []
mkLambdas :: Int -> Node -> Node
mkLambdas k = mkLambdas' (replicate k Info.empty)

unfoldLambdas' :: Node -> ([Info], Node)
unfoldLambdas' = go []
where
go :: [Info] -> Node -> ([Info], Node)
go acc n = case n of
Lambda i b -> go (i : acc) b
_ -> (acc, n)

unfoldLambdas :: Node -> (Int, Node)
unfoldLambdas = first length . unfoldLambdas'

etaExpand :: Int -> Node -> Node
etaExpand k n = mkLambdas' k (mkApp' n (map (Var Info.empty) [0 .. k - 1]))
etaExpand k n = mkLambdas k (mkApp n (map (Var Info.empty) [0 .. k - 1]))

-- `NodeDetails` is a convenience datatype which provides the most commonly needed
-- information about a node in a generic fashion.
Expand Down

0 comments on commit d9e09be

Please sign in to comment.