aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/continuation.lux
blob: 3be6645ff4e337d9c8e5205ea4d623742f92b578 (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
(.module:
  [library
   [lux "*"
    [abstract
     [functor {"+" Functor}]
     [apply {"+" Apply}]
     [monad {"+" Monad do}]]
    [control
     ["[0]" function]
     [parser
      ["<[0]>" code]]]
    [macro {"+" with_identifiers}
     [syntax {"+" syntax:}]
     ["[0]" code]]]])

(type: .public (Cont i o)
  (-> (-> i o) o))

(def: .public (continued next cont)
  (All (_ i o) (-> (-> i o) (Cont i o) o))
  (cont next))

(def: .public result
  (All (_ a) (-> (Cont a a) a))
  (..continued function.identity))

(def: .public (with_current f)
  (All (_ a b z)
    (-> (-> (-> a (Cont b z))
            (Cont a z))
        (Cont a z)))
  (function (_ k)
    (f (function (_ a) (function (_ _) (k a)))
       k)))

(syntax: .public (pending [expr <code>.any])
  (with_identifiers [g!_ g!k]
    (in (list (` (.function ((~ g!_) (~ g!k)) ((~ g!k) (~ expr))))))))

(def: .public (reset scope)
  (All (_ i o) (-> (Cont i i) (Cont i o)))
  (function (_ k)
    (k (result scope))))

(def: .public (shift f)
  (All (_ a)
    (-> (-> (-> a (Cont a a))
            (Cont a a))
        (Cont a a)))
  (function (_ oc)
    (f (function (_ a) (function (_ ic) (ic (oc a))))
       function.identity)))

(implementation: .public functor
  (All (_ o) (Functor (All (_ i) (Cont i o))))
  
  (def: (each f fv)
    (function (_ k)
      (fv (function.composite k f)))))

(implementation: .public apply
  (All (_ o) (Apply (All (_ i) (Cont i o))))
  
  (def: &functor ..functor)

  (def: (on fv ff)
    (function (_ k)
      (|> (k (f v))
          (function (_ v)) fv
          (function (_ f)) ff))))

(implementation: .public monad
  (All (_ o) (Monad (All (_ i) (Cont i o))))
  
  (def: &functor ..functor)

  (def: (in value)
    (function (_ k) (k value)))

  (def: (conjoint ffa)
    (function (_ k)
      (ffa (continued k)))))

(def: .public (portal init)
  (All (_ i o z)
    (-> i
        (Cont [(-> i (Cont o z))
               i]
              z)))
  (with_current
    (function (_ k)
      (do ..monad
        [.let [nexus (function (nexus val)
                       (k [nexus val]))]
         _ (k [nexus init])]
        (in (undefined))))))