first order simple patterns
[clean-tests.git] / datatype / Interpreter.hs
1 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE MultiParamTypeClasses #-}
4 module Interpreter where
5
6 import Language
7 import Control.Monad
8
9 newtype Interpreter a = I {runInterpreter :: Maybe a}
10 deriving (Functor, Applicative, Monad)
11
12 instance Expression Interpreter where
13 lit = pure
14 (+.) = liftM2 (+)
15 (-.) = liftM2 (-)
16 (/.) = liftM2 (/)
17 (*.) = liftM2 (*)
18 neg = fmap negate
19 (&.) = liftM2 (&&)
20 (|.) = liftM2 (||)
21 not = fmap Prelude.not
22 (==.) = liftM2 (==)
23 (/=.) = liftM2 (/=)
24 (<.) = liftM2 (<)
25 (>.) = liftM2 (>)
26 (<=.) = liftM2 (<=)
27 (>=.) = liftM2 (>=)
28 if' p t e = p >>= \b->if b then t else e
29
30 instance Function a Interpreter where
31 fun def = Main $ let g :- m = def g in unmain m