quasiquoting for patterns
[clean-tests.git] / datatype / Interpreter.hs
diff --git a/datatype/Interpreter.hs b/datatype/Interpreter.hs
new file mode 100644 (file)
index 0000000..cf41d9a
--- /dev/null
@@ -0,0 +1,34 @@
+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
+{-# LANGUAGE FlexibleInstances #-}
+{-# LANGUAGE MultiParamTypeClasses #-}
+module Interpreter where
+
+import Language
+import Control.Monad
+
+newtype Interpreter a = I {runInterpreter :: Maybe a}
+  deriving (Functor, Applicative, Monad)
+
+instance Expression Interpreter where
+    lit = pure
+    (+.) = liftM2 (+)
+    (-.) = liftM2 (-)
+    (/.) = liftM2 (/)
+    (*.) = liftM2 (*)
+    (^.) = liftM2 (^)
+    neg = fmap negate
+    (&.) = liftM2 (&&)
+    (|.) = liftM2 (||)
+    not = fmap Prelude.not
+    (==.) = liftM2 (==)
+    (/=.) = liftM2 (/=)
+    (<.) = liftM2 (<)
+    (>.) = liftM2 (>)
+    (<=.) = liftM2 (<=)
+    (>=.) = liftM2 (>=)
+    if' p t e = p >>= \b->if b then t else e
+
+instance Function a Interpreter where
+    fun def = Main $
+        let g :- m = def g
+        in unmain m