Commit b7f48832 authored by Reg Huijben's avatar Reg Huijben

Got statements working, lost will to live

parent a1d6b4e1
...@@ -54,8 +54,8 @@ instance pure Res where ...@@ -54,8 +54,8 @@ instance pure Res where
int :: Int -> Element int :: Int -> Element
int i = pure i int i = pure i
size :: Set -> Element Size :: Set -> Element
size se = fmap length se Size se = fmap length se
instance + Element where instance + Element where
(+) (Sem a) (Sem b) = Sem \s. case a s of (+) (Sem a) (Sem b) = Sem \s. case a s of
...@@ -110,13 +110,116 @@ instance ==. Set where ...@@ -110,13 +110,116 @@ instance ==. Set where
instance ==. Element where instance ==. Element where
(==.) a b = (==) <$> a <*> b (==.) a b = (==) <$> a <*> b
//:: Expr = New [Int]
// | Int Int
// | Var Ident
// | Size Set
// | (+.) infixl 6 Expr Expr
//class IF b where
// IF :: (Sem Bool) (Sem b) (Sem b) -> (Sem b)
/* instance IF a where
IF (Sem cond) (Sem st1) (Sem st2) = Sem \s. case cond s of
(Res a, s) = case (if a (st1) (st2) s) of
(Res f, s) = (Res f, s)
(Err e,s) = (Err e, s)
(Err e,s) = (Err e, s) */
class If a b where
If :: (Sem Bool) (Sem a) (Sem b) -> (Sem ())
instance If a b where
If (Sem cond) (Sem st1) (Sem st2) = Sem \s. case cond s of
(Res a, s) = if a
(case st1 s of
(Res _, s) = (Res (), s)
(Err e,s) = (Err e, s)
)
(case st2 s of
(Res _, s) = (Res (), s)
(Err e,s) = (Err e, s)
)
(Err e,s) = (Err e, s)
/*Logical /*
For :: Ident Set (Sem a) -> (Sem ())
For i (Sem st) (Sem stmt) = Sem \s. case st s of
(Res [a:aa], s) = case stmt (State ('Map'.put i (I a) s)) of // a is [Int]
(Res _, s) = case (For i (pure aa) (Sem stmt)) s of
(Res _,s)= (Res (),s)
(Err e,s) = (Err e, s)
(Err e,s) = (Err e, s)
(Res [], s) = (Res (),s) // a is [Int]
(Err e,s) = (Err e, s)
*/
iif :: Logical Statement Statement -> Bool /*
iif log st1 st2 = False For :: Ident Set (Sem a) -> (Sem ())
For i (Sem st) (Sem stmt) = Sem \s. case st s of
(Res [a:aa], s) = let (Sem fff, st) = (For i (pure aa) (Sem stmt)) in
case stmt (State ('Map'.put i (I a) st)) of // a is [Int]
(Res _, s) = case fff s of
(Res _,s)= (Res (),s)
(Err e,s) = (Err e, s)
(Err e,s) = (Err e, s)
(Res [], s) = (Res (),s) // a is [Int]
(Err e,s) = (Err e, s)
*/ */
/*
For :: Ident Set (Sem a) -> (Sem ())
For i (Sem st) (Sem stmt) = Sem \s. case st s of // eval the set
(Res [a:aa], s) = case stmt (\s .State ('Map'.put i (I a) s)) s of // eval the statement after the assignment
(Res _, s) = let (Sem fff) = (For i (pure aa) (Sem stmt)) in
case fff s of // eval the rest of the statements
(Res (), s) = (Res (),s)
(Err e,s) = (Err e, s)
(Err e,s) = (Err e, s)
(Res [], s) = (Res (),s) // a is [Int]
(Err e,s) = (Err e, s)
*/
//(=.) infixl 2 :: Ident (Sem a) -> (Sem a)
// This sucks, surely there is a sane way to do this? jfc
For :: Ident Set (Sem a) -> (Sem ())
For i (Sem st) (Sem stmt) = Sem \s. case st s of
(Res [a:aa], State s) = case stmt (State ('Map'.put i (I a) s)) of
(Res _, s) = let (Sem f) = (For i (pure aa) (Sem stmt)) in f s
(Err e,s) = (Err e, s)
(Res [],s) = (Res (), s)
(Err e,s) = (Err e, s)
While :: (Sem Bool) (Sem a) -> (Sem ())
While (Sem bl) (Sem stmt) = Sem \s. case bl s of
(Res True,s) = case stmt s of
(Res _, s) = let (Sem f) = (While (Sem bl) (Sem stmt)) in f s
(Err e,s) = (Err e, s)
(Res False,s) = (Res (), s)
(Err e,s) = (Err e, s)
// Sem st >>= \ x. Sem \s. (Err x, s)
(In) infix 4 :: (Element) (Set) -> (Sem Bool)
(In) (Sem e) (Sem st) = Sem \s. case e s of
(Res a, s) = case st s of
(Res theSet, s) = (Res (elem a theSet), s)
(Err e,s) = (Err e, s)
(Err e,s) = (Err e, s)
/*Logical
*/
class =. a where class =. a where
(=.) infixl 2 :: Ident (Sem a) -> (Sem a) (=.) infixl 2 :: Ident (Sem a) -> (Sem a)
...@@ -134,28 +237,44 @@ instance =. Int where ...@@ -134,28 +237,44 @@ instance =. Int where
(Err e, s) = (Err e, s) (Err e, s) = (Err e, s)
//(=.) i v = Sem \(State s). (pure v, State ('Map'.put i v s))
class Var a where class Var a where
var :: Ident -> (Sem a) Var :: Ident -> (Sem a)
instance Var Int where instance Var Int where
var i = Sem \(State s) . case ('Map'.get i s) of Var i = Sem \(State s) . case ('Map'.get i s) of
(Just a) -> case a of (Just a) -> case a of
(I i) = (pure i, State s) (I i) = (pure i, State s)
(S st) = (Err ("Expected int, found set " +++ (toString st)), State s ) (S st) = (Err ("Expected int, found set " +++ (toString st)), State s )
_ -> (Err ("Could not find variable " +++ i), State s ) _ -> (Err ("Could not find variable " +++ i), State s )
instance Var [Int] where instance Var [Int] where
var i = Sem \(State s) . case ('Map'.get i s) of Var i = Sem \(State s) . case ('Map'.get i s) of
(Just a) -> case a of (Just a) -> case a of
(I i) = (Err ("Expected set, found int " +++ (toString i)), State s ) (I i) = (Err ("Expected set, found int " +++ (toString i)), State s )
(S st) = (pure st, State s) (S st) = (pure st, State s)
_ -> (Err ("Could not find variable " +++ i), State s ) _ -> (Err ("Could not find variable " +++ i), State s )
(<=.) infix 4 :: Element Element -> Sem Bool
(<=.) e1 e2 = (<=) <$> e1 <*> e2
(||.) infixr 2 :: (Sem Bool) (Sem Bool) -> Sem Bool
(||.) e1 e2 = (||) <$> e1 <*> e2
(&&.) infixr 2 :: (Sem Bool) (Sem Bool) -> Sem Bool
(&&.) e1 e2 = (&&) <$> e1 <*> e2
Not :: (Sem Bool) -> (Sem Bool)
Not s = not <$> s
TRUE :: (Sem Bool)
TRUE = pure True
FALSE :: (Sem Bool)
FALSE = pure False
//(==.) infixr 3 :: Set Set -> Sem Bool //(==.) infixr 3 :: Set Set -> Sem Bool
//(==.) a b = (equalSet) <$> a <*> b //(==.) a b = (equalSet) <$> a <*> b
...@@ -221,9 +340,33 @@ zfr = pure [9,2,3] ...@@ -221,9 +340,33 @@ zfr = pure [9,2,3]
//zoefs = (zfl) ==. (zfr) //zoefs = (zfl) ==. (zfr)
zoefs = "A" =. (zfr) zoefs = "A" =. (zfr)
//zoefs = int 8 ==. int 8 //zoefs = int 8 ==. int 8
zoefss = var "A" + var "A" //pure [1,2,3] zoefss = Var "A" + int 8 //var "A" //pure [1,2,3]
New :: [Int] -> Set
New s = pure s
Elem :: Int -> Element
Elem i = int i
hetProgramma = If (Elem 4 In New [1,2,3]) ("A" =. New [6]) ("B" =. Elem 6)
hetProgramma2 = If (New [4] ==. New [1,2,3]) ("A" =. New [6]) ("B" =. Elem 6)
hetProgramma3 = If ((Size (New [4])) ==. Elem 1) ("A" =. New [6]) ("B" =. Elem 6)
hetProgramma4 = Elem 3 In New [7] ||. Elem 8 <=. Elem 7
//Start = equalSet [9,2,3] [9,3,2,1] || equalSet [9,3,2,1] [9,2,3] //Start = equalSet [9,2,3] [9,3,2,1] || equalSet [9,3,2,1] [9,2,3]
//Start = let ((a,b) = evl zoefs emptyState) in evl zoefss b //Start = let ((a,b) = evl zoefs emptyState) in evl zoefss b
Start = evl zoefss (snd (evl zoefs emptyState) ) //Start = evl zoefss (snd (evl zoefs emptyState) )
\ No newline at end of file //Start = evl (IF (pure True) ("A" =. zfr) ("B" =. zfr) ) emptyState
//Start = evl (For "A" (New [1,2,3,8]) (If (Var "A" ==. Elem 3) ("Z" =. Var "A" + Elem 0) (Elem 8))) emptyState
someSt = State ('Map'.put "A" (I 0) 'Map'.newMap)
Start = evl (While (Var "A" <=. Elem 3) ("A" =. (Var "A" + Elem 1))) someSt
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment