blob: 205958b26b02c3d7ff05f116e8c99bcb4aeac32d (
plain)
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
|
(.module:
[library
[lux #*
[data
[collection
["." list ("#\." fold)]]]
[math
[number
["n" nat]]]
[meta
["." location]]]]
[//
[functor (#+ Functor)]])
(interface: #export (CoMonad w)
{#.doc (doc "CoMonads are the opposite/complement to monads."
"CoMonadic structures are often infinite in size and built upon lazily-evaluated functions.")}
(: (Functor w)
&functor)
(: (All [a]
(-> (w a) a))
out)
(: (All [a]
(-> (w a) (w (w a))))
split))
(macro: #export (be tokens state)
{#.doc (doc "A co-monadic parallel to the 'do' macro."
(let [square (function (_ n) (* n n))]
(be comonad
[inputs (iterate inc +2)]
(square (head inputs)))))}
(case (: (Maybe [(Maybe Text) Code (List Code) Code])
(case tokens
(^ (list [_ (#.Record (list [[_ (#.Identifier ["" name])] comonad]))] [_ (#.Tuple bindings)] body))
(#.Some [(#.Some name) comonad bindings body])
(^ (list comonad [_ (#.Tuple bindings)] body))
(#.Some [#.None comonad bindings body])
_
#.None))
(#.Some [?name comonad bindings body])
(if (|> bindings list.size (n.% 2) (n.= 0))
(let [[module short] (name_of ..be)
gensym (: (-> Text Code)
(|>> ($_ "lux text concat" module " " short " ") [""] #.Identifier [location.dummy]))
g!_ (gensym "_")
g!map (gensym "map")
g!split (gensym "split")
body' (list\fold (: (-> [Code Code] Code Code)
(function (_ binding body')
(let [[var value] binding]
(case var
[_ (#.Tag ["" "let"])]
(` (let (~ value) (~ body')))
_
(` (|> (~ value) (~ g!split) ((~ g!map) (function ((~ g!_) (~ var)) (~ body')))))
))))
body
(list.reverse (list.as_pairs bindings)))]
(#.Right [state (list (case ?name
(#.Some name)
(let [name [location.dummy (#.Identifier ["" name])]]
(` ({(~ name)
({[(~ g!map) (~' out) (~ g!split)]
(~ body')}
(~ name))}
(~ comonad))))
#.None
(` ({[(~ g!map) (~' out) (~ g!split)]
(~ body')}
(~ comonad)))))]))
(#.Left "'be' bindings must have an even number of parts."))
#.None
(#.Left "Wrong syntax for 'be'")))
|