on fb21e21 help sign in
elk/elm/src/Types/Hints.hs
443 lines 19 kB history raw
1module Types.Hints (hints) where
3import Context
4import Control.Arrow (first)
5import Control.Monad (liftM,mapM)
6import Types.Types
7import Types.Substitutions (rescheme)
9ctx str = C (Just str) NoSpan
10prefix pre xs = map (first (\x -> pre ++ "." ++ x)) xs
12-------- Text and Elements --------
14textToText = [ "header", "italic", "bold", "underline"
15 , "overline", "strikeThrough", "monospace" ]
17textAttrs = [ "toText" -: string ==> text
18 , "Text.typeface" -: string ==> text ==> text
19 , "Text.link" -:: string ==> text ==> text
20 , numScheme (\t -> t ==> text ==> text) "Text.height"
21 ] ++ prefix "Text" (hasType (text ==> text) textToText)
23elements =
24 let iee = int ==> element ==> element in
25 [ "plainText" -: string ==> element
26 , "link" -:: string ==> element ==> element
27 , "flow" -: direction ==> listOf element ==> element
28 , "layers" -: listOf element ==> element
29 , "text" -: text ==> element
30 , "image" -: int ==> int ==> string ==> element
31 , "video" -: int ==> int ==> string ==> element
32 , "opacity" -: float ==> element ==> element
33 , "width" -: iee
34 , "height" -: iee
35 , "size" -: int ==> iee
36 , "widthOf" -: element ==> int
37 , "heightOf"-: element ==> int
38 , "sizeOf" -: element ==> pairOf int
39 , "color" -: color ==> element ==> element
40 , "container" -: int ==> int ==> position ==> element ==> element
41 , "spacer" -: int ==> int ==> element
42 , "rightedText" -: text ==> element
43 , "centeredText" -: text ==> element
44 , "justifiedText" -: text ==> element
45 , "asText" -:: a ==> element
46 , "collage" -: int ==> int ==> listOf form ==> element
47 , "fittedImage" -: int ==> int ==> string ==> element
48 ]
50directions = hasType direction ["up","down","left","right","inward","outward"]
51positions =
52 hasType position ["topLeft","midLeft","bottomLeft","midTop","middle"
53 ,"midBottom","topRight","midRight","bottomRight"] ++
54 hasType (location ==> location ==> position)
55 ["topLeftAt","bottomLeftAt","middleAt","topRightAt","bottomRightAt"] ++
56 [ "absolute" -: int ==> location, "relative" -: float ==> location ]
58lineTypes = [ numScheme (\n -> listOf (pairOf n) ==> line) "line"
59 , numScheme (\n -> pairOf n ==> pairOf n ==> line) "segment"
60 , "customLine" -: listOf int ==> color ==> line ==> form
61 ] ++ hasType (color ==> line ==> form) ["solid","dashed","dotted"]
63shapes = [ twoNums (\n m -> listOf (pairOf n) ==> pairOf m ==> shape) "polygon"
64 , "filled" -: color ==> shape ==> form
65 , "outlined" -: color ==> shape ==> form
66 , "textured" -: string ==> shape ==> form
67 , "customOutline" -: listOf int ==> color ==> shape ==> form
68 ] ++ map (twoNums (\n m -> n ==> n ==> pairOf m ==> shape)) [ "ngon"
69 , "rect"
70 , "oval" ]
72collages = [ numScheme (\n -> pairOf n ==> element ==> form) "toForm"
73 , numScheme (\n -> string ==> n ==> n ==> pairOf n ==> form) "sprite"
74 , numScheme (\n -> n ==> n ==> form ==> form) "move"
75 , numScheme (\n -> n ==> form ==> form) "rotate"
76 , numScheme (\n -> n ==> form ==> form) "scale"
77 , numScheme (\n -> pairOf n ==> form ==> bool) "isWithin"
78 ]
80graphicsElement = prefix "Graphics"
81 (concat [elements,directions,positions,lineTypes,shapes,collages])
82graphicsColor = prefix "Color" clrs
83 where clrs = [ numScheme (\n -> n ==> n ==> n ==> color) "rgb"
84 , numScheme (\n -> n ==> n ==> n ==> n ==> color) "rgba"
85 , "complement" -: color ==> color
86 ] ++ hasType color ["red","green","blue","black","white"
87 ,"yellow","cyan","magenta","grey","gray"]
90-------- Foreign --------
92casts =
93 [ "castJSBoolToBool" -: jsBool ==> bool
94 , "castBoolToJSBool" -: bool ==> jsBool
95 , "castJSNumberToInt" -: jsNumber ==> int
96 , "castIntToJSNumber" -: int ==> jsNumber
97 , "castJSElementToElement" -: int ==> int ==> jsElement ==> element
98 , "castElementToJSElement" -: element ==> jsElement
99 , "castJSStringToString" -: jsString ==> string
100 , "castStringToJSString" -: string ==> jsString
101 , "castJSNumberToFloat" -: jsNumber ==> float
102 , "castFloatToJSNumber" -: float ==> jsNumber
103 ]
105castToTuple n = (,) name $ Forall [1..n] [] (jsTuple vs ==> tupleOf vs)
106 where vs = map VarT [1..n]
107 name = "castJSTupleToTuple" ++ show n
108castToJSTuple n = (,) name $ Forall [1..n] [] (tupleOf vs ==> jsTuple vs)
109 where vs = map VarT [1..n]
110 name = "castTupleToJSTuple" ++ show n
112polyCasts =
113 map castToTuple [2..5] ++ map castToJSTuple [2..5] ++
114 [ "castJSArrayToList" -:: jsArray a ==> listOf a
115 , "castListToJSArray" -:: listOf a ==> jsArray a
116 ]
118javascript = prefix "JavaScript" (concat [casts,polyCasts])
120json = prefix "JSON"
121 [ "JsonString" -: string ==> jsonValue
122 , "JsonBool" -: bool ==> jsonValue
123 , "JsonNull" -: jsonValue
124 , "JsonArray" -: listOf jsonValue ==> jsonValue
125 , "JsonObject" -: jsonObject ==> jsonValue
126 , numScheme (\n -> n ==> jsonValue) "JsonNumber"
127 , "toString" -: jsonObject ==> string
128 , "fromString" -: string ==> jsonObject
129 , "lookup" -: string ==> jsonObject ==> maybeOf jsonValue
130 , "findObject" -: string ==> jsonObject ==> jsonObject
131 , "findArray" -: string ==> jsonObject ==> listOf jsonValue
132 , "findString" -: string ==> jsonObject ==> string
133 , "findWithDefault" -:: jsonValue ==> string ==> jsonObject ==> jsonValue
134 , "toPrettyString" -: string ==> jsonObject ==> string
135 , "toPrettyJSString" -: string ==> jsonObject ==> jsString
136 , "toList" -: jsonObject ==> listOf (tupleOf [string,jsonValue])
137 , "fromList" -: listOf (tupleOf [string,jsonValue]) ==> jsonObject
138 , "toJSString" -: jsonObject ==> jsString
139 , "fromJSString" -: jsString ==> jsonObject
140 ]
143-------- Signals --------
145lyft n = sig (n+1) ("lift" ++ show n)
146sig n name = (,) name $ Forall [1..n] [] (fn ts ==> fn (map signalOf ts))
147 where fn = foldr1 (==>)
148 ts = map VarT [1..n]
150signals = prefix "Signal"
151 [ sig 1 "constant"
152 , sig 2 "lift"
153 ] ++ map lyft [2..8] ++ [
154 "<~" -:: (a ==> b) ==> signalOf a ==> signalOf b
155 , "~" -:: signalOf (a ==> b) ==> signalOf a ==> signalOf b
156 , "foldp" -:: (a ==> b ==> b) ==> b ==> signalOf a ==> signalOf b
157 , "foldp1" -:: (a ==> a ==> a) ==> signalOf a ==> signalOf a
158 , "foldp'" -:: (a ==> b ==> b) ==> (a ==> b) ==> signalOf a ==> signalOf b
159 , "count" -:: signalOf a ==> signalOf int
160 , "countIf" -:: (a ==> bool) ==> signalOf a ==> signalOf int
161 , "keepIf" -:: (a ==> bool) ==> a ==> signalOf a ==> signalOf a
162 , "dropIf" -:: (a ==> bool) ==> a ==> signalOf a ==> signalOf a
163 , "keepWhen" -:: signalOf bool ==> a ==> signalOf a ==> signalOf a
164 , "dropWhen" -:: signalOf bool ==> a ==> signalOf a ==> signalOf a
165 , "dropRepeats" -:: signalOf a ==> signalOf a
166 , "sampleOn" -:: signalOf a ==> signalOf b ==> signalOf b
167 , "timestamp" -:: signalOf a ==> signalOf (tupleOf [time,a])
168 , "timeOf" -:: signalOf a ==> signalOf time
169 , "merge" -:: signalOf a ==> signalOf a ==> signalOf a
170 , "merges" -:: listOf (signalOf a) ==> signalOf a
171 , "mergeEither" -:: signalOf a ==> signalOf b ==> signalOf (eitherOf a b)
172 , numScheme (\n -> int ==> signalOf n ==> signalOf float) "average"
173 ]
175http = prefix "HTTP"
176 [ "send" -:: signalOf (request a) ==> signalOf (response string)
177 , "sendGet" -:: signalOf string ==> signalOf (response string)
178 , "get" -: string ==> request string
179 , "post" -: string ==> string ==> request string
180 , "request" -: string ==> string ==> string ==> listOf (pairOf string) ==> request string
181 , "Waiting" -:: response a
182 , "Failure" -:: int ==> string ==> response a
183 , "Success" -:: a ==> response a ]
184 where request t = ADT "Request" [t]
185 response t = ADT "Response" [t]
187concreteSignals =
188 [ "Random.inRange" -: int ==> int ==> signalOf int
189 , "Random.randomize" -:: int ==> int ==> signalOf a ==> signalOf int
190 , "Window.dimensions" -: signalOf point
191 , "Window.width" -: signalOf int
192 , "Window.height" -: signalOf int
193 , "Mouse.position" -: signalOf point
194 , "Mouse.x" -: signalOf int
195 , "Mouse.y" -: signalOf int
196 , "Mouse.isDown" -: signalOf bool
197 , "Mouse.isClicked" -: signalOf bool
198 , "Mouse.clicks" -: signalOf (tupleOf [])
199 , "Input.textField" -: string ==> tupleOf [element, signalOf string]
200 , "Input.password" -: string ==> tupleOf [element, signalOf string]
201 , "Input.textArea" -: int ==> int ==> tupleOf [element, signalOf string]
202 , "Input.checkBox" -: bool ==> tupleOf [element, signalOf bool]
203 , "Input.button" -: string ==> tupleOf [element, signalOf bool]
204 , "Input.stringDropDown" -: listOf string ==> tupleOf [element, signalOf string]
205 , "Input.dropDown" -:: listOf (tupleOf [string,a]) ==> tupleOf [element, signalOf a]
206 ]
208keyboards = prefix "Keyboard"
209 [ "Raw.keysDown" -: signalOf (listOf int)
210 , "Raw.charPressed" -: signalOf (maybeOf int)
211 , "arrows" -: signalOf (recordOf [("x",int),("y",int)])
212 , "wasd" -: signalOf (recordOf [("x",int),("y",int)])
213 , "ctrl" -: signalOf bool
214 , "space" -: signalOf bool
215 , "shift" -: signalOf bool
216 ]
218touches = prefix "Touch"
219 [ "touches" -: signalOf (listOf (recordOf [("x" ,int) ,("y" ,int)
220 ,("x0",int) ,("y0",int)
221 ,("t0",time),("id",int)]))
222 , "taps" -: signalOf (recordOf [("x",int),("y",int)])
223 ]
225times = prefix "Time"
226 [ numScheme (\n -> n ==> signalOf time) "fps"
227 , numScheme (\n -> n ==> signalOf bool ==> signalOf time) "fpsWhen"
228 , "every" -: time ==> signalOf time
229 , "delay" -:: time ==> signalOf a ==> signalOf a
230 , "since" -:: time ==> signalOf a ==> signalOf bool
231 , "hour" -: time
232 , "minute" -: time
233 , "second" -: time
234 , "ms" -: time
235 , "inHours" -: time ==> float
236 , "inMinutes" -: time ==> float
237 , "inSeconds" -: time ==> float
238 , "inMss" -: time ==> float
239 , "toDate" -: time ==> date
240 , "read" -: string ==> maybeOf time
241 ]
243dates =
244 let days = map (-: day) ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
245 months = map (-: month) [ "Jan", "Feb", "Mar", "Apr", "May", "Jun"
246 , "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]
247 in prefix "Date"
248 ([ "read" -: string ==> maybeOf date
249 , "year" -: date ==> int
250 , "month" -: date ==> month
251 , "day" -: date ==> int
252 , "hour" -: date ==> int
253 , "minute" -: date ==> int
254 , "second" -: date ==> int
255 , "dayOfWeek" -: date ==> day
256 , "toTime" -: date ==> time
257 ] ++ days ++ months)
259-------- Math and Binops --------
261binop t = t ==> t ==> t
262scheme1 super t name =
263 (name, Forall [0] [ ctx name $ VarT 0 :<: super
264 ] (t (VarT 0)))
265scheme2 s1 s2 t name =
266 (name, Forall [0,1] [ ctx name $ VarT 0 :<: s1
267 , ctx name $ VarT 1 :<: s2
268 ] (t (VarT 0) (VarT 1)))
269numScheme t name = scheme1 number t name
270twoNums f name = scheme2 number number f name
272math =
273 map (numScheme (\t -> t ==> binop t)) ["clamp"] ++
274 map (numScheme (\t -> binop t)) ["+","-","*","max","min"] ++
275 [ numScheme (\t -> t ==> t) "abs" ] ++
276 hasType (binop float) [ "/", "logBase" ] ++
277 hasType (binop int) ["rem","div","mod"] ++
278 hasType (float ==> float) ["sin","cos","tan","asin","acos","atan","sqrt"] ++
279 hasType float ["pi","e"] ++
280 hasType (int ==> float) ["toFloat","castIntToFloat"] ++
281 hasType (float ==> int) ["round","floor","ceiling","truncate"] ++
282 [ "show" -:: a ==> string
283 , "readInt" -: string ==> maybeOf int
284 , "readFloat" -: string ==> maybeOf float ]
286bools =
287 [ "not" -: bool ==> bool ] ++
288 hasType (binop bool) ["&&","||"] ++
289 map (scheme1 comparable (\t -> t ==> t ==> bool)) ["<",">","<=",">="] ++
290 [ ( "compare"
291 , Forall [0,1] [ ctx "compare" $ VarT 0 :<: comparable ] (VarT 0 ==> VarT 0 ==> VarT 1) )
292 ]
294chars = prefix "Char" (classify ++ convert1 ++ convert2)
295 where classify = hasType (char ==> bool)
296 ["isDigit","isOctDigit","isHexDigit","isUpper","isLower"]
297 convert1 = hasType (char ==> char)
298 ["toUpper","toLower","toLocaleUpper","toLocaleLower"]
299 convert2 = [ "toCode" -: char ==> int, "fromCode" -: int ==> char ]
302-------- Polymorphic Functions --------
304[a,b,c] = map VarT [1,2,3]
306infix 8 -::
307name -:: tipe = (name, Forall [1,2,3] [] tipe)
309funcs =
310 [ "id" -:: a ==> a
311 , "==" -:: a ==> a ==> bool
312 , "/=" -:: a ==> a ==> bool
313 , "flip" -:: (a ==> b ==> c) ==> (b ==> a ==> c)
314 , "." -:: (b ==> c) ==> (a ==> b) ==> (a ==> c)
315 , "$" -:: (a ==> b) ==> a ==> b
316 , ":" -:: a ==> listOf a ==> listOf a
317 , (,) "++" . Forall [0,1] [ ctx "++" $ VarT 0 :<: appendable (VarT 1) ] $ VarT 0 ==> VarT 0 ==> VarT 0
318 , "Cons" -:: a ==> listOf a ==> listOf a
319 , "Nil" -:: listOf a
320 , "Just" -:: a ==> maybeOf a
321 , "Nothing" -:: maybeOf a
322 , "Left" -:: a ==> eitherOf a b
323 , "Right" -:: b ==> eitherOf a b
324 , "curry" -:: (tupleOf [a,b] ==> c) ==> a ==> b ==> c
325 , "uncurry" -:: (a ==> b ==> c) ==> tupleOf [a,b] ==> c
326 ] ++ map tuple [0..8]
328tuple n = ("Tuple" ++ show n, Forall [1..n] [] $ foldr (==>) (tupleOf vs) vs)
329 where vs = map VarT [1..n]
331lists = prefix "List"
332 [ "and" -:: listOf bool ==> bool
333 , "or" -:: listOf bool ==> bool
334 , numScheme (\n -> listOf n ==> listOf n) "sort"
335 , "head" -:: listOf a ==> a
336 , "tail" -:: listOf a ==> listOf a
337 , "length" -:: listOf a ==> int
338 , "filter" -:: (a ==> bool) ==> listOf a ==> listOf a
339 , "foldr1" -:: (a ==> a ==> a) ==> listOf a ==> a
340 , "foldl1" -:: (a ==> a ==> a) ==> listOf a ==> a
341 , "scanl1" -:: (a ==> a ==> a) ==> listOf a ==> a
342 , "all" -:: (a ==> bool) ==> listOf a ==> bool
343 , "any" -:: (a ==> bool) ==> listOf a ==> bool
344 , "reverse" -:: listOf a ==> listOf a
345 , "take" -:: int ==> listOf a ==> listOf a
346 , "drop" -:: int ==> listOf a ==> listOf a
347 , "partition" -:: (a ==> bool) ==> listOf a ==> tupleOf [listOf a,listOf a]
348 , "intersperse" -:: a ==> listOf a ==> listOf a
349 , "zip" -:: listOf a ==>listOf b ==>listOf(tupleOf [a,b])
350 , "map" -:: (a ==> b) ==> listOf a ==> listOf b
351 , "foldr" -:: (a ==> b ==> b) ==> b ==> listOf a ==> b
352 , "foldl" -:: (a ==> b ==> b) ==> b ==> listOf a ==> b
353 , "scanl" -:: (a ==> b ==> b) ==> b ==> listOf a ==> listOf b
354 , (,) "concat" . Forall [0,1] [ ctx "concat" $ VarT 0 :<: appendable (VarT 1) ] $
355 listOf (VarT 0) ==> VarT 0
356 , (,) "concatMap" . Forall [0,1,2] [ ctx "concatMap" $ VarT 0 :<: appendable (VarT 1) ] $
357 (VarT 2 ==> VarT 0) ==> listOf (VarT 2) ==> VarT 0
358 , (,) "intercalate" . Forall [0,1] [ ctx "intercalate" $ VarT 0 :<: appendable (VarT 1) ] $
359 VarT 0 ==> listOf (VarT 0) ==> VarT 0
360 , "zipWith" -:: (a ==> b ==> c) ==> listOf a ==> listOf b ==> listOf c
361 ] ++ map (numScheme (\n -> listOf n ==> n)) [ "sum", "product"
362 , "maximum", "minimum" ]
364maybeFuncs = prefix "Maybe"
365 [ "maybe" -:: b ==> (a ==> b) ==> maybeOf a ==> b
366 , "isJust" -:: maybeOf a ==> bool
367 , "isNothing" -:: maybeOf a ==> bool
368 , "cons" -:: maybeOf a ==> listOf a ==> listOf a
369 , "justs" -:: listOf (maybeOf a) ==> listOf a
370 ]
372eithers = prefix "Either"
373 [ "isLeft" -:: eitherOf a b ==> bool
374 , "isRight" -:: eitherOf a b ==> bool
375 , "either" -:: (a ==> c) ==> (b ==> c) ==> eitherOf a b ==> c
376 , "lefts" -:: listOf (eitherOf a b) ==> listOf a
377 , "rights" -:: listOf (eitherOf a b) ==> listOf b
378 , "partition" -:: listOf (eitherOf a b) ==> tupleOf [listOf a,listOf b]
379 ]
381dictionary =
382 let dict k v = ADT "Dict" [k,v] in
383 prefix "Dict"
384 [ "empty" -:: dict a b
385 , "singleton" -:: a ==> b ==> dict a b
386 , "insert" -:: a ==> b ==> dict a b ==> dict a b
387 , "remove" -:: a ==> dict a b ==> dict a b
388 , "member" -:: a ==> dict a b ==> bool
389 , "lookup" -:: a ==> dict a b ==> maybeOf b
390 , "findWithDefault" -:: b ==> a ==> dict a b ==> b
391 , "intersect" -:: dict a b ==> dict a c ==> dict a b
392 , "union" -:: dict a b ==> dict a b ==> dict a b
393 , "diff" -:: dict a b ==> dict a c ==> dict a b
394 , "map" -:: (b ==> c) ==> dict a b ==> dict a c
395 , "foldl" -:: (a ==> b ==> c ==> c) ==> c ==> dict a b ==> c
396 , "foldr" -:: (a ==> b ==> c ==> c) ==> c ==> dict a b ==> c
397 , "keys" -:: dict a b ==> listOf a
398 , "values" -:: dict a b ==> listOf b
399 , "toList" -:: dict a b ==> listOf (tupleOf [a,b])
400 , "fromList" -:: listOf (tupleOf [a,b]) ==> dict a b
401 ]
403sets =
404 let set v = ADT "Set" [v] in
405 prefix "Set"
406 [ "empty" -:: set a
407 , "singleton" -:: a ==> set a
408 , "insert" -:: a ==> set a ==> set a
409 , "remove" -:: a ==> set a ==> set a
410 , "member" -:: a ==> set a ==> bool
411 , "intersect" -:: set a ==> set a ==> set a
412 , "union" -:: set a ==> set a ==> set a
413 , "diff" -:: set a ==> set a ==> set a
414 , "map" -:: (a ==> b) ==> set a ==> set b
415 , "foldl" -:: (a ==> b ==> b) ==> b ==> set a ==> b
416 , "foldr" -:: (a ==> b ==> b) ==> b ==> set a ==> b
417 , "toList" -:: set a ==> listOf a
418 , "fromList" -:: listOf a ==> set a
419 ]
421automaton =
422 let auto a b = ADT "Automaton" [a,b] in
423 prefix "Automaton"
424 [ "pure" -:: (a ==> b) ==> auto a b
425 , "init" -:: b ==> (a ==> b ==> b) ==> auto a b
426 , "init'" -:: c ==> (a ==> c ==> tupleOf [b,c]) ==> auto a b
427 , ">>>" -:: auto a b ==> auto b c ==> auto a c
428 , "<<<" -:: auto b c ==> auto a b ==> auto a c
429 , "combine" -:: listOf (auto a b) ==> auto a (listOf b)
430 , "run" -:: auto a b ==> signalOf a ==> signalOf b
431 , "step" -:: auto a b ==> a ==> tupleOf [b,auto a b]
432 , "count" -:: auto a int
433 , "draggable" -:: form ==> auto (tupleOf [bool,point]) form
434 ]
436-------- Everything --------
438hints = mapM (\(n,s) -> (,) n `liftM` rescheme s) (concat hs)
439 where hs = [ funcs, lists, signals, math, bools, textAttrs
440 , graphicsElement, graphicsColor, eithers, keyboards, touches
441 , concreteSignals, javascript, json, maybeFuncs
442 , http, dictionary, sets, automaton, times, dates
443 ]