aboutsummaryrefslogtreecommitdiff
path: root/source/lux/codata/state.lux
blob: d85ef3dbc751d9c8efb09aae1fb99044453e4366 (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
##  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/.

(;import lux
         (lux/control (functor #as F #refer #all)
                      (monad #as M #refer #all)))

## [Types]
(deftype #export (State s a)
  (-> s (, s a)))

## [Structures]
(defstruct #export State/Functor (Functor State)
  (def (F;map f ma)
    (lambda [state]
      (let [[state' a] (ma state)]
        [state' (f a)]))))

(defstruct #export State/Monad (All [s]
                                 (Monad (State s)))
  (def M;_functor State/Functor)

  (def (M;wrap x)
    (lambda [state]
      [state x]))

  (def (M;join mma)
    (lambda [state]
      (let [[state' ma] (mma state)]
        (ma state')))))