merge
[mTask.git] / mTask.dcl
1 definition module mTask
2
3 /*
4 Pieter Koopman pieter@cs.ru.nl
5 Final version for TFP2016
6
7 -2: assignment =. suited for digital and analog input and output
8 -3: ad hoc tasks
9
10 todo:
11 move task-loop ti setup()
12 adhoc tasks
13 task combinators
14 imporove setp: >>*.
15 */
16
17 import Generics.gCons
18 import Generics.gdynamic
19
20 import iTasks
21
22 import iTasks._Framework.Generic
23 from iTasks._Framework.Task import :: Task
24 import StdClass
25 import GenEq, StdMisc, StdArray
26
27 import mTaskCode, mTaskSimulation, mTaskInterpret
28 import mTaskSerial, mTaskLCD
29
30 // =================== mTask ===================
31
32
33 // ----- dsl definition ----- //
34
35 :: DigitalPin
36 = D0 | D1 | D2 | D3 | D4 | D5 |D6 | D7 | D8 | D9 | D10 | D11 | D12 | D13
37 :: AnalogPin = A0 | A1 | A2 | A3 | A4 | A5
38 :: UserLED = LED1 | LED2 | LED3
39 :: PinMode = INPUT | OUTPUT | INPUT_PULLUP
40 :: Pin = Digital DigitalPin | Analog AnalogPin
41
42 class pin p | type, == p where
43 pin :: p -> Pin
44 instance pin DigitalPin
45 instance pin AnalogPin
46
47 :: Upd = Upd
48 :: Expr = Expr
49 :: Stmt = Stmt
50 :: MTask = MTask Int // String
51
52 class isExpr a :: a -> Int
53 instance isExpr Upd
54 instance isExpr Expr
55
56 class isStmt a :: a -> Int
57 instance isStmt Upd
58 instance isStmt Expr
59 instance isStmt Stmt
60
61 instance == MTask
62
63 :: Main a = {main :: a}
64
65 unMain :: (Main x) -> x
66
67 class arith v where
68 lit :: t -> v t Expr | toCode t & toByteCode t
69 (+.) infixl 6 :: (v t p) (v t q) -> v t Expr | type, +, zero t & isExpr p & isExpr q //& toExpr2 p & toExpr2 q
70 (-.) infixl 6 :: (v t p) (v t q) -> v t Expr | type, -, zero t & isExpr p & isExpr q //& toExpr2 p & toExpr2 q
71 (*.) infixl 7 :: (v t p) (v t q) -> v t Expr | type, *, zero, one t & isExpr p & isExpr q //& toExpr2 p & toExpr2 q
72 (/.) infixl 7 :: (v t p) (v t q) -> v t Expr | type, / t & isExpr p & isExpr q //& toExpr2 p & toExpr2 q
73 class boolExpr v where
74 (&.) infixr 3 :: (v Bool p) (v Bool q) -> v Bool Expr | isExpr p & isExpr q //& toExpr2 p & toExpr2 q
75 (|.) infixr 2 :: (v Bool p) (v Bool q) -> v Bool Expr | isExpr p & isExpr q //& toExpr2 p & toExpr2 q
76 Not :: (v Bool p) -> v Bool Expr | isExpr p //& toExpr2 p
77 (==.) infix 4 :: (v a p) (v a q) -> v Bool Expr | ==, toCode a & isExpr p & isExpr q //& toExpr2 p & toExpr2 q
78 (!=.) infix 4 :: (v a p) (v a q) -> v Bool Expr | ==, toCode a & isExpr p & isExpr q //& toExpr2 p & toExpr2 q
79 (<.) infix 4 :: (v a p) (v a q) -> v Bool Expr | <, Ord, toCode a & isExpr p & isExpr q //& toExpr2 p & toExpr2 q
80 (>.) infix 4 :: (v a p) (v a q) -> v Bool Expr | <, Ord, toCode a & isExpr p & isExpr q //& toExpr2 p & toExpr2 q
81 (<=.) infix 4 :: (v a p) (v a q) -> v Bool Expr | <, Ord, toCode a & isExpr p & isExpr q //& toExpr2 p & toExpr2 q
82 (>=.) infix 4 :: (v a p) (v a q) -> v Bool Expr | <, Ord, toCode a & isExpr p & isExpr q //& toExpr2 p & toExpr2 q
83 // using functional dependencies
84 class If v q r ~s where
85 If :: (v Bool p) (v t q) (v t r) -> v t s | isExpr p & type t
86 class IF v where
87 IF :: (v Bool p) (v t q) (v s r) -> v () Stmt | isExpr p
88 (?) infix 1 :: (v Bool p) (v t q) -> v () Stmt | isExpr p
89 class var2 v where
90 var2 :: t ((v t Upd)->(Main (v c s))) -> (Main (v c s)) | type, toCode t
91 con2 :: t ((v t Expr) ->(Main (v c s))) -> (Main (v c s)) | type t
92 class sds v where
93 sds :: ((v t Upd)->In t (Main (v c s))) -> (Main (v c s)) | type, toByteCode, toCode t
94 con :: ((v t Expr) ->In t (Main (v c s))) -> (Main (v c s)) | type t
95 pub :: (v t Upd) -> v t Expr | type t
96 class seq v where
97 (>>=.) infixr 0 :: (v t p) ((v t Expr) -> (v u q)) -> (v u Stmt) | type t & type u
98 (:.) infixr 0 :: (v t p) (v u q) -> v u Stmt | type t & type u
99 class step` v where
100 (>>*.) infixl 1 :: (v t p) ((v t Expr) -> [Step v u]) -> v u Stmt | type t & type u
101 :: Step v t = E.p.q: Cond (v Bool q) (v t p) | E.p: Ever (v t p)
102 class assign v where
103 (=.) infixr 2 :: (v t Upd) (v t p) -> v t Expr | type t & isExpr p
104 class fun v t where
105 fun :: ((t->v s Expr)->In (t->v s p) (Main (v u q))) -> Main (v u q) | type s
106 class mtask v a where
107 task :: (((v delay r) a->v MTask Expr)->In (a->v u p) (Main (v t q))) -> Main (v t q) | type t & type u & isExpr r & long v delay
108 class lag v where
109 lag :: (v delay r) (v t p) -> v MTask Expr | type t & long v delay
110 class setDelay v where
111 setDelay :: (v Long p) (v MTask Expr) -> (v MTask Expr) | isExpr p
112 class mtasks v a b where
113 tasks :: (((v delay r1) a->v MTask Expr, (v delay r2) b->v MTask Expr)->In (a->v t p, b->v u p) (Main (v s q))) -> Main (v s q) | type s & isExpr r1 & isExpr r2 & long v delay
114 class output v where
115 output :: (v t p) -> v () Expr | type t & isExpr p
116 class noOp v where noOp :: v t p
117
118 class pinMode v where
119 pinmode :: p PinMode -> v () Expr | pin p
120 class digitalIO v where
121 digitalRead :: p -> v Bool Expr | pin, readPinD p
122 digitalWrite :: p (v Bool q) -> v Bool Expr | pin, writePinD p
123 class analogIO v where
124 analogRead :: AnalogPin -> v Int Expr
125 analogWrite :: AnalogPin (v Int p) -> v Int Expr
126 class dIO v where
127 dIO :: p -> v Bool Upd | pin, readPinD p
128 class aIO v where
129 aIO :: AnalogPin -> v Int Upd
130 class time v where
131 delay :: (v Long p) -> (v Long Expr)
132 millis :: (v Long Expr)
133
134 class userLed v where
135 ledOn :: UserLED -> (v () Stmt)
136 ledOff :: UserLED -> (v () Stmt)
137
138 class pio p t where pio :: p -> v t Upd | aIO v & dIO v
139 instance pio AnalogPin Int
140 instance pio AnalogPin Bool
141 instance pio DigitalPin Bool
142
143 int :: (v Int p) -> (v Int p)
144 bool :: (v Bool p) -> (v Bool p)
145 char :: (v Char p) -> (v Char p)
146
147 class type t | showType, dyn, toCode, ==, type2string, varName t
148 class type2string t :: t -> String
149 instance type2string Int
150 instance type2string Long
151 instance type2string Real
152 instance type2string Bool
153 instance type2string Char
154 instance type2string MTask
155 instance type2string DigitalPin
156 instance type2string AnalogPin
157 instance type2string String
158 instance type2string ()
159 class varName a :: a -> String
160 instance varName Int
161 instance varName Long
162 instance varName Bool
163 instance varName Char
164 instance varName Real
165 instance varName x
166
167 class dsl t | arith, boolExpr, sds, assign, seq t
168
169 :: SV t = SV String
170
171 class showType2 t :: SV t
172 instance showType2 ()
173 instance showType2 Int
174 instance showType2 Char
175 instance showType2 Bool
176 instance showType2 a
177
178 class showType t | showType2 /*, type*/ t :: (Code t p)
179 instance showType ()
180 instance showType Int
181 instance showType Long
182 instance showType Char
183 instance showType Bool
184 instance showType a
185
186 class typeSelector t | showType2, type t :: (Code t p)
187 instance typeSelector Int
188 instance typeSelector Char
189 instance typeSelector Bool
190 instance typeSelector a
191
192 :: In a b = In infix 0 a b
193
194 read` :: Int (ReadWrite a) State` -> (a,State`) | dyn a
195
196 // ----- long ----- //
197
198 :: Long = L Int // 32 bit on Arduino
199 instance + Long
200 instance - Long
201 instance * Long
202 instance / Long
203 instance == Long
204 instance one Long
205 instance zero Long
206
207 class long v t :: (v t p) -> v Long Expr | isExpr p
208 instance long Code Int
209 instance long Code Long
210 instance long Eval Int
211 instance long Eval Long
212
213 // ----- tools ----- //
214
215 derive consName DigitalPin, AnalogPin, PinMode
216
217 instance == DigitalPin
218 instance == AnalogPin
219
220 derive consIndex DigitalPin, AnalogPin
221
222 tab =: toString (repeatn tabSize ' ')
223 tabSize :== 2
224
225 instance toString ()
226
227 a0 :== pio A0
228 a1 :== pio A1
229 a2 :== pio A2
230 a3 :== pio A3
231 a4 :== pio A4
232 a5 :== pio A5
233
234 d0 :== pio D0
235 d1 :== pio D1
236 d2 :== pio D2
237 d3 :== pio D3
238 d4 :== pio D4
239 d5 :== pio D5
240 d6 :== pio D6
241 d7 :== pio D7
242 d8 :== pio D8
243 d9 :== pio D9
244 d10 :== pio D10
245 d11 :== pio D11
246 d12 :== pio D12
247 d13 :== pio D13