aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/region.lux
blob: ff624741804f6804d872a36657cebe08e79ce903 (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
154
155
156
157
158
(.module:
  [library
   [lux #*
    [abstract
     [functor (#+ Functor)]
     [apply (#+ Apply)]
     ["." monad (#+ Monad do)]]
    [control
     ["." try (#+ Try)]]
    [data
     ["." text
      ["%" format (#+ format)]]
     [collection
      ["." list ("#\." fold)]]]]]
  [//
   ["." exception (#+ Exception exception:)]])

(type: (Cleaner r !)
  (-> r (! (Try Any))))

(type: #export (Region r ! a)
  (-> [r (List (Cleaner r !))]
      (! [(List (Cleaner r !))
          (Try a)])))

(def: separator
  Text
  (format text.new_line
          "-----------------------------------------" text.new_line
          "-----------------------------------------" text.new_line
          "-----------------------------------------" text.new_line
          text.new_line))

(exception: #export [a] (clean_up_error {error Text}
                                        {output (Try a)})
  (format error
          (case output
            (#try.Success _)
            ""

            (#try.Failure error|output)
            (format separator
                    error|output))))

(def: (combine_outcomes clean_up output)
  (All [a] (-> (Try Any) (Try a) (Try a)))
  (case clean_up
    (#try.Success _)
    output

    (#try.Failure error)
    (exception.throw ..clean_up_error [error output])))

(def: #export (run monad computation)
  (All [! a]
    (-> (Monad !) (All [r] (Region r ! a))
        (! (Try a))))
  (do {! monad}
    [[cleaners output] (computation [[] (list)])
     results (monad.map ! (function (_ cleaner) (cleaner []))
                        cleaners)]
    (wrap (list\fold combine_outcomes output results))))

(def: #export (acquire monad cleaner value)
  (All [! a] (-> (Monad !) (-> a (! (Try Any))) a
                 (All [r] (Region r ! a))))
  (function (_ [region cleaners])
    (\ monad wrap [(#.Cons (function (_ region) (cleaner value))
                           cleaners)
                   (#try.Success value)])))

(implementation: #export (functor super)
  (All [!]
    (-> (Functor !)
        (All [r] (Functor (Region r !)))))

  (def: (map f)
    (function (_ fa)
      (function (_ region+cleaners)
        (\ super map
           (function (_ [cleaners' temp])
             [cleaners' (case temp
                          (#try.Success value)
                          (#try.Success (f value))

                          (#try.Failure error)
                          (#try.Failure error))])
           (fa region+cleaners))))))

(implementation: #export (apply super)
  (All [!]
    (-> (Monad !)
        (All [r] (Apply (Region r !)))))

  (def: &functor
    (..functor (get@ #monad.&functor super)))

  (def: (apply ff fa)
    (function (_ [region cleaners])
      (do super
        [[cleaners ef] (ff [region cleaners])
         [cleaners ea] (fa [region cleaners])]
        (case ef
          (#try.Success f)
          (case ea
            (#try.Success a)
            (wrap [cleaners (#try.Success (f a))])
            
            (#try.Failure error)
            (wrap [cleaners (#try.Failure error)]))
          
          (#try.Failure error)
          (wrap [cleaners (#try.Failure error)]))))))

(implementation: #export (monad super)
  (All [!]
    (-> (Monad !)
        (All [r] (Monad (Region r !)))))

  (def: &functor
    (..functor (get@ #monad.&functor super)))

  (def: (wrap value)
    (function (_ [region cleaners])
      (\ super wrap [cleaners (#try.Success value)])))

  (def: (join ffa)
    (function (_ [region cleaners])
      (do super
        [[cleaners efa] (ffa [region cleaners])]
        (case efa
          (#try.Success fa)
          (fa [region cleaners])

          (#try.Failure error)
          (wrap [cleaners (#try.Failure error)]))))))

(def: #export (fail monad error)
  (All [! a]
    (-> (Monad !) Text
        (All [r] (Region r ! a))))
  (function (_ [region cleaners])
    (\ monad wrap [cleaners (#try.Failure error)])))

(def: #export (throw monad exception message)
  (All [! e a]
    (-> (Monad !) (Exception e) e
        (All [r] (Region r ! a))))
  (fail monad (exception.construct exception message)))

(def: #export (lift monad operation)
  (All [! a]
    (-> (Monad !) (! a)
        (All [r] (Region r ! a))))
  (function (_ [region cleaners])
    (do monad
      [output operation]
      (wrap [cleaners (#try.Success output)]))))