summary refs log tree commit diff
diff options
context:
space:
mode:
authorErik Oosting2024-02-01 03:40:54 +0100
committerCrazazy2024-04-25 05:56:19 -0400
commit00f123d6c5df592b3660fe0e6fb2e80b216b2ca3 (patch)
treee3065e4c6d8483aa8b94fadb134cd81ef5af728c
parent5ded005cb6dab79c96e84e67ba6f4f7eddda17d6 (diff)
added example function HEAD master
-rw-r--r--haskell/app/Lift.hs45
1 files changed, 35 insertions, 10 deletions
diff --git a/haskell/app/Lift.hs b/haskell/app/Lift.hs
index 64fa0f6..b503665 100644
--- a/haskell/app/Lift.hs
+++ b/haskell/app/Lift.hs
@@ -21,6 +21,22 @@ factorial =
         )
     )
 
+three :: CExp
+three =
+    ( Let
+        "n"
+        (Atom (Number 1))
+        ( Let
+            "n"
+            (Atom (AAdd (Ident "n") (Number 1)))
+            ( Let
+                "n"
+                (Atom (AAdd (Ident "n") (Number 1)))
+                (FC (Atom (Ident "n")))
+            )
+        )
+    )
+
 liftArgs :: AExp -> AExp
 liftArgs lam@(Lam args body) = Lam (args ++ cata findVarsAExp lam) body
 liftArgs rest = rest
@@ -97,7 +113,7 @@ subVarsCExp (env, queue, LetF name fc rest) =
     let
         (newName, oldNames) =
             foldr
-                (\m (n, ns) -> if n == m then (n ++ "_", n:ns) else (n, ns)) -- repeatedly replace variables in the function call
+                (\m (n, ns) -> if n == m then (n ++ "_", n : ns) else (n, ns)) -- repeatedly replace variables in the function call
                 (name, [])
                 queue -- make sure to do oldest first!
         newFC = foldr replaceVarsFC fc $ oldNames \\ [newName]
@@ -110,15 +126,24 @@ subVarsCExp (env, queue, IfF cond thenPart elsePart) =
         (env, queue, elsePart)
 subVarsCExp (env, queue, FCF fc) = FCF $ foldr replaceVarsFC fc queue
 
-
+unsafeCotransverse ::
+    (Corecursive t, Recursive a, Functor f) =>
+    (f (Base a a) -> Base t (f a)) ->
+    f a ->
+    t
 unsafeCotransverse n = ana (n . fmap project)
 
-subVarsCExp' (env, LetF name fc body) 
-            | name `elem` env =
-              let
-                newName = name ++ "_"
-                newFC = replaceVarsFC newName fc
-                newBody = cata replaceVarsCExp body name
-              in LetF newName newFC (newName : env, newBody)
-            | otherwise = LetF name fc (name : env, body)
+subVarsCExp' :: ([String], CExpF CExp) -> CExpF ([String], CExp)
+subVarsCExp' (env, LetF name fc body)
+    | name `elem` env =
+        let
+            newName = name ++ "_"
+            newFC = replaceVarsFC newName fc
+            newBody = cata replaceVarsCExp body name
+         in
+            LetF newName newFC (newName : env, newBody)
+    | otherwise = LetF name fc (name : env, body)
 subVarsCExp' (env, rest) = fmap (env,) rest
+
+substitute :: CExp -> CExp
+substitute = unsafeCotransverse subVarsCExp' . ([],)