-
Notifications
You must be signed in to change notification settings - Fork 0
/
HaskSyntaxUntyped.hs
142 lines (90 loc) · 2.67 KB
/
HaskSyntaxUntyped.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
{-# LANGUAGE FlexibleInstances, OverloadedStrings #-} -- , MultiParamTypeClasses, FunctionalDependencies, UndecidableInstances, FlexibleContexts, GeneralizedNewtypeDeriving, NoMonomorphismRestriction give up on types -}
module HaskSyntaxUntyped where
import Expr
import qualified Expr as E
import EvalM
import Data.List
import Numbers
import Data.String
import Data.Unique
import System.IO.Unsafe
default (Int, RealNum)
infixl 1 =:
class Assignable s where
(=:) :: String -> E -> s
instance Assignable Declare where
x =: y = let (nm:vars) = splitBySpaces x in
Let (PatVar nm UnspecifiedT) $ addLams (reverse vars) y
instance Assignable (String, E) where -- LetE
x =: y = (x, y)
instance Assignable (Pat, E) where -- LetE
x =: y = (PatVar x UnspecifiedT, y)
instance Assignable (E, E) where -- LetE
x =: y = (Var x, y)
fstV (PairV x y) = x
sndV (PairV x y) = y
unsafeUniqueInt = unsafePerformIO (hashUnique `fmap` newUnique)
addLams [] e = e
addLams (v:vs) e = addLams vs $ Lam v UnspecifiedT e
splitBySpaces :: String -> [String]
splitBySpaces = filter (not. null) . splitBySpaces'
splitBySpaces' [] = []
splitBySpaces' (' ':s) = splitBySpaces s
splitBySpaces' s = let (hd, tl) = span (/=' ') s
in hd : splitBySpaces tl
instance IsString E where
fromString s = Var s
infixl 1 ~>
x ~> y = (x, y)
true, false :: E
true = Const . BoolV $ True
false = Const . BoolV $ False
lam :: String -> E -> E
lam vn bod = let vars = splitBySpaces vn in
addLams (reverse vars) bod
lam' :: (E->E) -> E
lam' bodf = bodf $ (Var ("unique_var_"++show unsafeUniqueInt))
sig = Sig
delay = SigDelay
val = SigVal
list :: [E] -> E
list [] = Nil
list (x:xs) = Cons x (list xs)
--pair x y = Pair x y
foo = If true "x" "y"
bar :: E
bar = (lam "x y z" "x") $> 1
baz :: Declare
baz = "f x" =: "x"
infixl 1 *>
infixl 1 <*
x *> y = SinkConnect x y
x <* y = ReadSource x y
dbl :: RealNum -> E
dbl = Const . NumV . NReal
int :: Int -> E
int = Const . NumV . NInt
cdbl :: RealNum -> V
cdbl = NumV . NReal
cint :: Int -> V
cint = NumV . NInt
class ToVal a where
toVal :: a-> V
instance ToVal Int where
toVal = NumV . NInt
instance ToVal Double where
toVal = NumV . NReal
instance ToVal [Char] where
toVal = StringV
instance ToVal a => ToVal [a] where
toVal xs = ListV $ map toVal xs
instance ToVal () where
toVal _ = Unit
instance (ToVal a, ToVal b) => ToVal (a,b) where
toVal (x,y) = PairV (toVal x) (toVal y)
anyNumT = NumT Nothing
realT = NumT (Just RealT)
intT = NumT (Just IntT)
(.->.) = LamT
eventT t = PairT (anyNumT) (t)
eventsT t = ListT $ eventT t