aboutsummaryrefslogtreecommitdiff
path: root/stdlib/test/test/lux/type/object/protocol.lux
blob: d6596950b6f9fc578662187ac3f704ebdb1051c3 (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
(.module:
  lux
  (lux (data text/format)
       (type (object protocol))))

(type: Counter (Object (Method Unit Nat)))

(def: (count [tick return] state)
  (Class Nat (Method Unit Nat))
  (let [state' (n/inc state)]
    [(return state') state']))

(def: counter
  (-> Nat Counter)
  (object count))

(def: _test0
  [Nat Counter]
  ((counter +0) (message [])))

(protocol: Protocol0
  (method0 [Bool Nat Text] Bool)
  (method1 [Nat Text Bool] Nat)
  (method2 [Text Bool Nat] Text))

(type: Object0 (Object Protocol0))

(def: object0
  Object0
  (loop [num-calls +0]
    (function (_ message)
      [(case message
         (#method0 [arg0 arg1 arg2] output)
         (output (n/= +0 (n/% +2 num-calls)))
         
         (#method1 [arg0 arg1 arg2] output)
         (output num-calls)
         
         (#method2 [arg0 arg1 arg2] output)
         (output (%n num-calls)))
       (recur (n/inc num-calls))])))

(def: _test1
  [Nat Object0]
  (object0 (method1 [+0 "0" false])))

(protocol: (Read a)
  (read [] a))

(def: (readM [tick return] state)
  (All [s] (Class s (Method Unit s)))
  [(return state) state])

(protocol: (Add n)
  (+ n Unit)
  (- n Unit))

(protocol: (Mul n)
  (* n Unit)
  (/ n Unit))

(do-template [<name> <op>]
  [(def: (<name> [diff return] state)
     (Class Nat (Method Nat Unit))
     [(return []) (<op> diff state)])]

  [+M n/+]
  [-M n/-]
  [*M n/*]
  [/M n//]
  )

(def: addM
  (Class Nat (Add Nat))
  (alt +M -M))

(def: mulM
  (Class Nat (Mul Nat))
  (alt *M /M))

(type: (Number n)
  ($_ Alt
      (Read n)
      (Add n)
      (Mul n)))

## TODO: Fix when new-luxc is the official compiler.
## (protocol: (Number n)
##   (^read (Read n))
##   (^add (Add n))
##   (^mul (Mul n)))

(def: numberM
  (Class Nat (Number Nat))
  ($_ alt
      readM
      addM
      mulM))

(type: NatO (Object (Number Nat)))

(def: numberO
  NatO
  (object numberM +1))

(def: _test2
  [Nat NatO]
  (numberO (+0 (read []))))

(def: _test3
  [Unit NatO]
  (numberO (+1 (+0 (+ +123)))))

(def: _test4
  [Unit NatO]
  (numberO (+1 (+1 (* +123)))))

## TODO: Fix when new-luxc is the official compiler.
## (def: _test2
##   [Nat NatO]
##   (numberO (^read (read []))))

## (def: _test3
##   [Unit NatO]
##   (numberO (^add (+ +123))))

## (def: _test4
##   [Unit NatO]
##   (numberO (^mul (* +123))))