aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/state.lux
blob: 778162d61a82f58864ac8b45fbd383037cb252f8 (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
(.module:
  [lux #*
   ["_" test (#+ Test)]
   [abstract
    [monad (#+ do)]
    {[0 #test]
     [/
      ["$." functor (#+ Injection Comparison)]
      ["$." apply]
      ["$." monad]]}]
   [control
    [pipe (#+ let>)]
    ["." io]]
   [data
    ["." product]
    [text
     ["%" format (#+ format)]]]
   [math
    ["r" random]]]
  {1
   ["." / (#+ State)]})

(def: (with-conditions [state output] computation)
  (-> [Nat Nat] (State Nat Nat) Bit)
  (|> computation
      (/.run state)
      product.right
      (n/= output)))

(def: basics
  (do r.monad
    [state r.nat
     value r.nat]
    ($_ _.and
        (_.test "Can get the state as a value."
                (with-conditions [state state]
                  /.get))
        (_.test "Can replace the state."
                (with-conditions [state value]
                  (do /.monad
                    [_ (/.put value)]
                    /.get)))
        (_.test "Can update the state."
                (with-conditions [state (n/* value state)]
                  (do /.monad
                    [_ (/.update (n/* value))]
                    /.get)))
        (_.test "Can use the state."
                (with-conditions [state (inc state)]
                  (/.use inc)))
        (_.test "Can use a temporary (local) state."
                (with-conditions [state (n/* value state)]
                  (/.local (n/* value)
                           /.get)))
        )))

(def: (injection value)
  (All [s] (Injection (State s)))
  (function (_ state) [state value]))

(def: (comparison init)
  (All [s] (-> s (Comparison (State s))))
  (function (_ == left right)
    (== (product.right (/.run init left))
        (product.right (/.run init right)))))

(def: structures
  Test
  (do r.monad
    [state r.nat
     value r.nat]
    ($_ _.and
        ($functor.spec ..injection (..comparison state) /.functor)
        ($apply.spec ..injection (..comparison state) /.apply)
        ($monad.spec ..injection (..comparison state) /.monad)
        )))

(def: loops
  Test
  (do r.monad
    [limit (|> r.nat (:: @ map (n/% 10)))
     #let [condition (do /.monad
                       [state /.get]
                       (wrap (n/< limit state)))]]
    ($_ _.and
        (_.test "'while' will only execute if the condition is #1."
                (|> (/.while condition (/.update inc))
                    (/.run 0)
                    (let> [state' output']
                          (n/= limit state'))))
        (_.test "'do-while' will execute at least once."
                (|> (/.do-while condition (/.update inc))
                    (/.run 0)
                    (let> [state' output']
                          (or (n/= limit state')
                              (and (n/= 0 limit)
                                   (n/= 1 state'))))))
        )))

(def: monad-transformer
  Test
  (do r.monad
    [state r.nat
     left r.nat
     right r.nat]
    (let [(^open "io;.") io.monad]
      (_.test "Can add state functionality to any monad."
              (|> (: (/.State' io.IO Nat Nat)
                     (do (/.with io.monad)
                       [a (/.lift io.monad (io;wrap left))
                        b (wrap right)]
                       (wrap (n/+ a b))))
                  (/.run' state)
                  io.run
                  (let> [state' output']
                        (and (n/= state state')
                             (n/= (n/+ left right) output')))))
      )))

(def: #export test
  Test
  (<| (_.context (%.name (name-of /.State)))
      ($_ _.and
          (<| (_.context "Basics.")
              ..basics)
          (<| (_.context "Structures.")
              ..structures)
          (<| (_.context "Loops.")
              ..loops)
          (<| (_.context "Monad transformer.")
              ..monad-transformer))))