1
1
mirror of https://github.com/thma/LtuPatternFactory.git synced 2024-12-04 12:43:14 +03:00

working on interpreter

This commit is contained in:
thma 2018-12-28 22:06:36 +01:00
parent 029b8072eb
commit e9ccdc0d14
2 changed files with 67 additions and 32 deletions

67
src/Interpreter.hs Normal file
View File

@ -0,0 +1,67 @@
{-# LANGUAGE FlexibleContexts #-}
module Interpreter where
import Control.Monad.Reader
import Control.Monad.State
data Exp a =
Var String
| Op (Operator a) (Exp a) (Exp a)
| Let String (Exp a) (Exp a)
| Val a
type Operator a = a -> a -> a
type Env a = [(String, a)]
-- using a Reader Monad to thread the environment. The Environment can be accessed by ask.
--eval :: Real a => Exp a -> Env a -> a
eval (Var x) = asks (fetch x)
eval (Val i) = return i
eval (Op op e1 e2) = liftM2 op (eval e1) (eval e2)
eval (Let x e1 e2) = asks (eval e1) >>= \v -> local ((x,v):) (eval e2)
{--
env <- ask
v <- eval e1
local ((x,v):) (eval e2)
--}
{--
--eval5 :: Num a => Exp a -> Env a -> a
eval5 :: (MonadState (Env a) m, Num a) => Exp a -> m a
eval5 (Var x) =
gets (fetch x)
eval5 (Def k v) = do
env <- get
put ((k,v):env)
return v
eval5 (Val i) = return i
eval5 (Add p q) = liftM2 (+) (eval5 p) (eval5 q)
eval5 (Mul p q) = liftM2 (*) (eval5 p) (eval5 q)
--}
-- simple environment lookup
fetch :: String -> Env a -> a
fetch x [] = error $ "variable " ++ x ++ " is not defined"
fetch x ((y,v):ys)
| x == y = v
| otherwise = fetch x ys
interpreterDemo :: IO ()
interpreterDemo = do
putStrLn "Interpreter -> Reader Monad + ADTs + pattern matching"
let exp = Let "x"
(Let "y"
(Op (+) (Val 5) (Val 6))
(Op (/) (Var "y") (Val 5)))
(Op (*) (Val 3) (Var "x"))
env = [("pi", pi)]
print $ eval exp env
--print $ runReader (eval exp) env
--print $ eval5 exp env
--print $ eval5 exp (put env :: State (Env Double) ())
putStrLn ""

View File

@ -1,12 +1,9 @@
{-# LANGUAGE FlexibleContexts #-}
module Singleton where
import IdiomBrackets
import Control.Monad.Reader
import Control.Monad.State
data Exp a =
Var String
| Def String a
| Val a
| Add (Exp a) (Exp a)
| Mul (Exp a) (Exp a) deriving (Show)
@ -58,26 +55,6 @@ eval3 (Val i) = iI i Ii
eval3 (Add p q) = iI (+) (eval3 p) (eval3 q) Ii
eval3 (Mul p q) = iI (*) (eval3 p) (eval3 q) Ii
-- using a Reader Monad to thread the environment. The Environment can be accessed by ask.
eval4 :: Num a => Exp a -> Env a -> a
eval4 (Var x) = ask >>= return $ fetch x
eval4 (Val i) = return i
eval4 (Add p q) = liftM2 (+) (eval4 p) (eval4 q)
eval4 (Mul p q) = liftM2 (*) (eval4 p) (eval4 q)
--eval5 :: Num a => Exp a -> Env a -> a
eval5 :: (MonadState (Env a) m, Num a) => Exp a -> m a
eval5 (Var x) = do
env <- get
return $ fetch x env
eval5 (Def k v) = do
env <- get
put ((k,v):env)
return v
eval5 (Val i) = return i
eval5 (Add p q) = liftM2 (+) (eval5 p) (eval5 q)
eval5 (Mul p q) = liftM2 (*) (eval5 p) (eval5 q)
-- simple environment lookup
fetch :: String -> Env a -> a
fetch x [] = error $ "variable " ++ x ++ " is not defined"
@ -85,9 +62,6 @@ fetch x ((y,v):ys)
| x == y = v
| otherwise = fetch x ys
exp1 = Mul (Add (Def "pi" pi) (Val 1))
(Mul (Val 2) (Var "pi"))
singletonDemo :: IO ()
singletonDemo = do
putStrLn "Singleton -> Applicative Functor (and let in general)"
@ -99,10 +73,4 @@ singletonDemo = do
print $ eval2 exp env
print $ eval3 exp env
print $ eval4 exp env
let exp1 = Mul (Add (Def "pi" 4) (Val 1))
(Mul (Val 2) (Var "pi"))
-- print $ eval5 exp (state env)
putStrLn ""