aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/region.lux
blob: 934c923d06c85d36c0ec298ca94a316364244ab9 (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
(.using
 [library
  [lux "*"
   [abstract
    [functor (.only Functor)]
    [apply (.only Apply)]
    ["[0]" monad (.only Monad do)]]
   [control
    ["[0]" try (.only Try)]]
   [data
    ["[0]" text
     ["%" format (.only format)]]
    [collection
     ["[0]" list ("[1]#[0]" mix)]]]]]
 [//
  ["[0]" exception (.only Exception exception:)]])

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

(type: .public (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: .public [a] (clean_up_error [error Text
                                         output (Try a)])
  (format error
          (case output
            {try.#Success _}
            ""

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

(def: (clean clean_up output)
  (All (_ a) (-> (Try Any) (Try a) (Try a)))
  (case clean_up
    {try.#Success _}
    output

    {try.#Failure error}
    (exception.except ..clean_up_error [error output])))

(def: .public (run! monad computation)
  (All (_ ! a)
    (-> (Monad !) (All (_ r) (Region r ! a))
        (! (Try a))))
  (do [! monad]
    [[cleaners output] (computation [[] (list)])]
    (|> cleaners
        (monad.each ! (function (_ cleaner) (cleaner [])))
        (# ! each (list#mix clean output)))))

(def: .public (acquire! monad cleaner value)
  (All (_ ! a) (-> (Monad !) (-> a (! (Try Any))) a
                   (All (_ r) (Region r ! a))))
  (function (_ [region cleaners])
    (# monad in [{.#Item (function (_ region) (cleaner value))
                         cleaners}
                 {try.#Success value}])))

(implementation: .public (functor super)
  (All (_ !)
    (-> (Functor !)
        (All (_ r) (Functor (Region r !)))))

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

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

(implementation: .public (apply super)
  (All (_ !)
    (-> (Monad !)
        (All (_ r) (Apply (Region r !)))))

  (def: functor
    (..functor (the monad.functor super)))

  (def: (on fa ff)
    (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}
            (in [cleaners {try.#Success (f a)}])
            
            {try.#Failure error}
            (in [cleaners {try.#Failure error}]))
          
          {try.#Failure error}
          (in [cleaners {try.#Failure error}]))))))

(implementation: .public (monad super)
  (All (_ !)
    (-> (Monad !)
        (All (_ r) (Monad (Region r !)))))

  (def: functor
    (..functor (the monad.functor super)))

  (def: (in value)
    (function (_ [region cleaners])
      (# super in [cleaners {try.#Success value}])))

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

          {try.#Failure error}
          (in [cleaners {try.#Failure error}]))))))

(def: .public (failure monad error)
  (All (_ ! a)
    (-> (Monad !) Text
        (All (_ r) (Region r ! a))))
  (function (_ [region cleaners])
    (# monad in [cleaners {try.#Failure error}])))

(def: .public (except monad exception message)
  (All (_ ! e a)
    (-> (Monad !) (Exception e) e
        (All (_ r) (Region r ! a))))
  (failure monad (exception.error exception message)))

(def: .public (lifted monad operation)
  (All (_ ! a)
    (-> (Monad !) (! a)
        (All (_ r) (Region r ! a))))
  (function (_ [region cleaners])
    (# monad each
       (|>> {try.#Success} [cleaners])
       operation)))