From 03eb5c1228ea9f5997fc19f7075a90cca1d29820 Mon Sep 17 00:00:00 2001 From: Erik Oosting Date: Mon, 18 Dec 2023 18:58:35 +0100 Subject: add free variable searching --- haskell/app/Lift.hs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 haskell/app/Lift.hs (limited to 'haskell/app/Lift.hs') diff --git a/haskell/app/Lift.hs b/haskell/app/Lift.hs new file mode 100644 index 0000000..12f9bc6 --- /dev/null +++ b/haskell/app/Lift.hs @@ -0,0 +1,26 @@ +module Lift where + +import Data.Functor.Foldable +import Data.List +import Types + +converge f a = let a' = f a in if a' == a then a else converge f a' + +-- | R-Algebra to move external variables to arguments +liftArgs :: AExpF (AExp, ([String], AExp)) -> ([String], AExp) +liftArgs = undefined -- paramorphism + +-- | F-Algebras to find free variables +findVars :: CExpF [String] -> [String] +findVars (LetF ident fc rest) = converge (\\ [ident]) $ rest ++ (cata findVarsFC fc) +findVars (IfF cond t e) = t ++ e ++ (cata findVarsAExp cond) +findVars (FCF fc) = cata findVarsFC fc + +findVarsFC :: FuncallF [String] -> [String] +findVarsFC (AtomF aexp) = cata findVarsAExp aexp +findVarsFC (CallF id args) = id : (args >>= cata findVarsAExp) + +findVarsAExp :: AExpF [String] -> [String] +findVarsAExp (IdentF ns) = [ns] +findVarsAExp (LamF args cexp) = converge (\\ args) (fold findVars cexp) +findVarsAExp def = foldMap id def -- cgit 1.4.1-2-gfad0