.
[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 newtype Interpreter a = I {runInterpreter :: Maybe a}
11 deriving (Functor, Applicative, Monad, MonadFail)
12
13 instance Expression Interpreter where
14 lit = pure
15 (+.) = liftM2 (+)
16 (-.) = liftM2 (-)
17 (/.) = liftM2 (/)
18 (*.) = liftM2 (*)
19 neg = fmap negate
20 (&.) = liftM2 (&&)
21 (|.) = liftM2 (||)
22 not = fmap Prelude.not
23 (==.) = liftM2 (==)
24 (/=.) = liftM2 (/=)
25 (<.) = liftM2 (<)
26 (>.) = liftM2 (>)
27 (<=.) = liftM2 (<=)
28 (>=.) = liftM2 (>=)
29 if' p t e = p >>= \b->if b then t else e
30 bottom = fail
31
32 instance Function a Interpreter where
33 fun def = Main $ let g :- m = def g in unmain m
34
35 instance DSL Interpreter