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 +++++++++++++++++++++ haskell/app/Main.hs | 4 ++++ haskell/app/Types.hs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 haskell/app/Lift.hs create mode 100644 haskell/app/Types.hs (limited to 'haskell/app') 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 diff --git a/haskell/app/Main.hs b/haskell/app/Main.hs index 65ae4a0..4b26cb2 100644 --- a/haskell/app/Main.hs +++ b/haskell/app/Main.hs @@ -1,4 +1,8 @@ module Main where +import LLVM.Codegen + +test = ppllvm + main :: IO () main = putStrLn "Hello, Haskell!" diff --git a/haskell/app/Types.hs b/haskell/app/Types.hs new file mode 100644 index 0000000..8c4790f --- /dev/null +++ b/haskell/app/Types.hs @@ -0,0 +1,64 @@ +{-# LANGUAGE TemplateHaskell #-} +{-# LANGUAGE TypeFamilies #-} + +module Types where + +import Data.Functor.Foldable.TH +import GHC.Generics + +data AExp + = LitTrue + | LitFalse + | Ident String + | Number Integer + | LitStr String + | AAdd AExp AExp + | ASub AExp AExp + | AMul AExp AExp + | ADiv AExp AExp + | AGt AExp AExp + | ALt AExp AExp + | AEq AExp AExp + | ABsl AExp AExp + | ABsr AExp AExp + | AAnd AExp AExp + | AOr AExp AExp + | AXor AExp AExp + | Lam [String] CExp + deriving (Show, Read, Generic) + +data GlobalAExp + = GlobalLitTrue + | GlobalLitFalse + | GlobalIdent String + | GlobalNumber Integer + | GlobalLitStr String + | GlobalAAdd GlobalAExp GlobalAExp + | GlobalASub GlobalAExp GlobalAExp + | GlobalAMul GlobalAExp GlobalAExp + | GlobalADiv GlobalAExp GlobalAExp + | GlobalAGt GlobalAExp GlobalAExp + | GlobalALt GlobalAExp GlobalAExp + | GlobalAEq GlobalAExp GlobalAExp + | GlobalABsl GlobalAExp GlobalAExp + | GlobalABsr GlobalAExp GlobalAExp + | GlobalAAnd GlobalAExp GlobalAExp + | GlobalAOr GlobalAExp GlobalAExp + | GlobalAXor GlobalAExp GlobalAExp + | Funcref String + deriving (Show, Read, Generic) +data Funcall + = Call String [AExp] + | Atom AExp + deriving (Show, Read, Generic) + +data CExp + = Let String Funcall CExp + | If AExp CExp CExp + | FC Funcall + deriving (Show, Read, Generic) + +makeBaseFunctor ''CExp +makeBaseFunctor ''AExp +makeBaseFunctor ''GlobalAExp +makeBaseFunctor ''Funcall -- cgit 1.4.1-2-gfad0