aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/continuation.lux
blob: 5aa7f47a38a54f16d84da319e105c6be1ddf4898 (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
(.require
 [library
  [lux (.except)
   [abstract
    [functor (.only Functor)]
    [apply (.only Apply)]
    [monad (.only Monad do)]]
   [control
    ["[0]" function]
    [parser
     ["<[0]>" code]]]
   [macro (.only with_symbols)
    [syntax (.only 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)))

(def .public pending
  (syntax (_ [expr <code>.any])
    (with_symbols [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)))

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

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

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

(def .public monad
  (All (_ o) (Monad (All (_ i) (Cont i o))))
  (implementation
   (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))))))