aboutsummaryrefslogtreecommitdiff
path: root/state-monad.scm
blob: eaafe30883e260fed844a845c507c161a5024488 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
(read-enable 'curly-infix)

(use-modules (oop goops)
             (oop goops describe)
             (ice-9 match)
             (srfi srfi-1))

(define-class <monoid> ())

(define-generic null)
(define-generic mappend)

(define (<> . args)
  (fold mappend (null (car args)) (reverse args)))

(define-method (mappend (a <pair>)
                        (b <pair>))
  (append a b))
(define-method (mappend (a <pair>) (b <null>)) a)
(define-method (mappend (a <null>) (b <pair>)) b)
(define-method (mappend (a <null>) (b <null>)) '())
(define-method (null (a <pair>)) '())
(define-method (null (a <null>)) '())

(define-macro (null type)
  (symbol-append type '-null))

(define-class <applicative> ())
(define-generic map)

(define-class <monad> (<applicative> <monoid>))
(define-generic bind)
#; (define-generic return)

(define-class <optional> ()
  (slot #:init-value #f
        #:init-keyword #:slot)
  (just  #:init-value #t
        #:init-keyword #:just))

(define (nothing) (make <optional> #:just #f))

(define (just obj) (make <optional>
                     #:just #t
                     #:slot obj))

(define (nothing? this)
  (not (slot-ref this 'just)))

(define (just? this)
  (not (nothing? this)))


(define-method (write (this <optional>) port)
  (if (just? this)
      (format port "[Just ~s]" (slot-ref this 'slot))
      (format port "[Nothing]")))

(define-class <either> ()
  (slot #:init-keyword #:slot)
  (dir #:init-keyword #:dir #:init-value 'left))

(define (left val)
  "Error values"
  (make <either> #:slot val #:dir 'left))

(define (right val)
  "Good values"
  (make <either> #:slot val #:dir 'right))

(define-method (write (this <either>) port)
  (format port "[~a ~s]"
          (slot-ref this 'dir)
          (slot-ref this 'slot)))

(define-method (>>= (this <either>)
                    (proc <procedure>))
  (case (slot-ref this 'dir)
    ((left) this) 
    ((right) (match this (($ <either> slot) (proc slot))))))

(define return-either right)
(define return-optional just)

;; bind :: Monad m => m a -> (a -> m b) -> m b
;; return :: Monad m => a -> m a
;; map :: Functor f => (a -> b) -> f a -> f b

(define-method (>>= (this <optional>)
                    (proc <procedure>))
  (cond ((nothing? this) (nothing))
        ((just? this) 
         (match this
           (($ <optional> slot) (proc slot))))))

(define-method (>>= (this <null>)
                    proc)
  '())

(define-method (>>= (this <pair>)
                    (proc <procedure>))
  (concatenate! (map proc this)))

(define-class <writer> ()
  (value #:init-keyword #:value)
  (monoid #:init-keyword #:monoid))

(define (writer value context)
  (make <writer>
    #:value value
    #:monoid context))

(define-method (>>= (this <writer>)
                    (proc <procedure>))
  (match this (($ <writer> value monoid)
               (match (proc value)
                 (($ <writer> nval ncontext)
                  (writer nval { monoid <> ", " <> ncontext }))))))

(define-method (write (this <writer>) port)
  (match this (($ <writer> value monoid)
               (format port "[Writer ~s, ~s]" value monoid))))

(define-syntax do
  (syntax-rules (<- let =)
    ((_ let var = val rest ...)
     (let ((var val)) (do rest ...)))
    ((_ ptrn <- val rest ...)
     (<- ptrn val rest ...))
    ((_ a) a)
    ((_ token rest ...)
     (begin token (do rest ...)))))

(define-syntax <-
  (lambda (x)
    (syntax-case x (just left right writer)
      ((_ (just var) val rest ...)
       #'(>>= val (lambda (var) rest ...)))
      ((_ (writer var) val rest ...)
       #'(>>= val (lambda (var) rest ...)))
      #; 
      ((_ (left var) val rest ...)      ;
      #'(>>= val (lambda (var) rest ...)))
      #; 
      ((_ (right var) val rest ...)     ;
      #'(>>= val (lambda (var) rest ...))))))

;;; Examples:
;;; These are do in the conext of the optional monad.

(do (just x) <- (just 10)
    x) ; => 10

(do let y = (just 10)
    (just x) <- y
    (+ x 5)) ; => 15

(do (just x) <- (nothing)
    (+ x 5)) ; => [Nothing]

;;;

(do let either = left 10
    (left x) <- either
    x) ; EVALUATION ERROR

;; Int -> Writer Int String
(define (log-number n)
  (writer n (format #f "Got nuber: ~a" n)))

(define (mult-with-log)
  (do (writer a) <- (log-number 3)
      (writer b) <- (log-number 5)
      (* a b)))

(do (writer a) <- (log-number 1)
    a) ; EVALUATION ERROR

(begin { (log-number 1) >>= log-number })
;; => [Writer 1, "Got nuber: 1, Got nuber: 1"]

(do (writer a) <- (log-number 3)
    (writer b) <- (log-number 5)
    (writer (* a b) ""))
;; EVALUATION ERROR
;; => [Writer 3, "Got nuber: 3, base"]

(log-number 3) ; => [Writer 3, "Got nuber: 3"]
(just 1) ; => [Just 1]

(do let y = 5
    (just x) <- (just 10)
    (just (* x y)))
;; => [Just 50]

;;; TODO
;;; '<- and 'let can't be used after '<-