blob: c7dc9f1d965ed32e1e883a5c3dbd3f52f6b8e5ae (
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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
(.module:
[library
[lux #*
[abstract
[apply (#+ Apply)]
[equivalence (#+ Equivalence)]
["." functor (#+ Functor)]
["." monad (#+ Monad do)]]
[meta
["." location]]]])
(type: .public (Try a)
{#.doc (example "A computation that can fail with an error message.")}
(#Failure Text)
(#Success a))
(implementation: .public functor
(Functor Try)
(def: (map f ma)
(case ma
(#Failure msg)
(#Failure msg)
(#Success datum)
(#Success (f datum)))))
(implementation: .public apply
(Apply Try)
(def: &functor ..functor)
(def: (apply ff fa)
(case ff
(#Success f)
(case fa
(#Success a)
(#Success (f a))
(#Failure msg)
(#Failure msg))
(#Failure msg)
(#Failure msg))))
(implementation: .public monad
(Monad Try)
(def: &functor ..functor)
(def: (in a)
(#Success a))
(def: (join mma)
(case mma
(#Failure msg)
(#Failure msg)
(#Success ma)
ma)))
(implementation: .public (with monad)
{#.doc (example "Enhances a monad with error-handling functionality.")}
... TODO: Replace (All [a] (! (Try a))) with (functor.Then ! Try)
(All [!] (-> (Monad !) (Monad (All [a] (! (Try a))))))
(def: &functor
(functor.composite (get@ #monad.&functor monad) ..functor))
(def: in
(|>> (\ ..monad in)
(\ monad in)))
(def: (join MeMea)
(do monad
[eMea MeMea]
(case eMea
(#Failure try)
(in (#Failure try))
(#Success Mea)
Mea))))
(def: .public (lifted monad)
{#.doc (example "Wraps a monadic value with error-handling machinery.")}
(All [! a] (-> (Monad !) (-> (! a) (! (Try a)))))
(\ monad map (\ ..monad in)))
(implementation: .public (equivalence (^open "_\."))
(All [a] (-> (Equivalence a) (Equivalence (Try a))))
(def: (= reference sample)
(case [reference sample]
[(#Success reference) (#Success sample)]
(_\= reference sample)
[(#Failure reference) (#Failure sample)]
("lux text =" reference sample)
_
false
)))
(def: .public (trusted try)
{#.doc (example "Assumes a Try value succeeded, and yields its value."
"If it didn't, raises the error as a runtime error."
"WARNING: Use with caution.")}
(All [a] (-> (Try a) a))
(case try
(#Success value)
value
(#Failure message)
(panic! message)))
(def: .public (maybe try)
(All [a] (-> (Try a) (Maybe a)))
(case try
(#Success value)
(#.Some value)
(#Failure message)
#.None))
(def: .public (of_maybe maybe)
(All [a] (-> (Maybe a) (Try a)))
(case maybe
(#.Some value)
(#Success value)
#.None
(#Failure (`` (("lux in-module" (~~ (static .prelude_module)) .name\encode)
(name_of ..of_maybe))))))
(macro: .public (else tokens compiler)
{#.doc (example "Allows you to provide a default value that will be used"
"if a (Try x) value turns out to be #Failure."
"Note: the expression for the default value will not be computed if the base computation succeeds."
(= "bar"
(else "foo" (#..Success "bar")))
(= "foo"
(else "foo" (#..Failure "KABOOM!"))))}
(case tokens
(^ (list else try))
(#Success [compiler (list (` (case (~ try)
(#..Success (~' g!temp))
(~' g!temp)
(#..Failure (~ [location.dummy (#.Identifier ["" ""])]))
(~ else))))])
_
(#Failure "Wrong syntax for else")))
|