aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/control/comonad.lux
blob: 00d807ef1acc7bf63d70f1346ef803e678c2d098 (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
##  Copyright (c) Eduardo Julian. All rights reserved.
##  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
##  If a copy of the MPL was not distributed with this file,
##  You can obtain one at http://mozilla.org/MPL/2.0/.

(;module:
  lux
  ["F" ../functor]
  [lux/data/coll/list #* "" Fold<List>])

## [Signatures]
(sig: #export (CoMonad w)
  {#;doc "CoMonads are the opposite/complement to monads.

          CoMonadic structures are often infinite in size and built upon lazily-evaluated functions."}
  (: (F;Functor w)
     functor)
  (: (All [a]
       (-> (w a) a))
     unwrap)
  (: (All [a]
       (-> (w a) (w (w a))))
     split))

## [Syntax]
(macro: #export (be tokens state)
  {#;doc (doc "A co-monadic parallel to the \"do\" macro."
              (let [square (lambda [n] (i.* n n))]
                (be CoMonad<Stream>
                  [inputs (iterate i.inc 2)]
                  (square (head inputs)))))}
  (case tokens
    (#;Cons comonad (#;Cons [_ (#;TupleS bindings)] (#;Cons body #;Nil)))
    (let [g!@ (: AST [["" -1 -1] (#;SymbolS ["" "@"])])
          g!map (: AST [["" -1 -1] (#;SymbolS ["" " map "])])
          g!split (: AST [["" -1 -1] (#;SymbolS ["" " split "])])
          body' (fold (: (-> [AST AST] AST AST)
                         (lambda [binding body']
                           (let [[var value] binding]
                             (case var
                               [_ (#;TagS ["" "let"])]
                               (` (let (~ value) (~ body')))

                               _
                               (` (|> (~ value) (~ g!split) ((~ g!map) (lambda [(~ var)] (~ body')))))
                               ))))
                      body
                      (reverse (as-pairs bindings)))]
      (#;Right [state (#;Cons (` (;_lux_case (~ comonad)
                                             (~ g!@)
                                             (;_lux_case (~ g!@)
                                                         {#functor {#F;map (~ g!map)} #unwrap (~' unwrap) #split (~ g!split)}
                                                         (~ body'))))
                              #;Nil)]))

    _
    (#;Left "Wrong syntax for be")))