lawspec 0.19.1 → 0.20.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +712 -698
- package/README.md +4 -4
- package/bin/lawspec.mjs +5 -2
- package/build.json +168 -168
- package/core.wasm +0 -0
- package/examples/packages/orders/lawspec.json +2 -2
- package/examples/packages/orders/orders.lawspec +25 -0
- package/examples/packages/shop-domain/lawspec-package.json +3 -1
- package/examples/packages/shop-domain/src/domain.lawspec +8 -0
- package/examples/packages/shop-tax-1/lawspec-package.json +5 -0
- package/examples/packages/shop-tax-1/src/api.lawspec +6 -0
- package/examples/packages/shop-tax-1/src/rates.lawspec +8 -0
- package/examples/packages/shop-tax-2/lawspec-package.json +5 -0
- package/examples/packages/shop-tax-2/src/api.lawspec +6 -0
- package/examples/packages/shop-tax-2/src/rates.lawspec +9 -0
- package/examples/specs/approvals.lawspec +35 -0
- package/examples/specs/async_bindings.lawspec +39 -0
- package/examples/specs/collections.lawspec +20 -0
- package/examples/specs/distribution.lawspec +24 -2
- package/examples/specs/flow_stack.lawspec +34 -0
- package/examples/specs/generation.lawspec +22 -0
- package/examples/specs/models.lawspec +21 -0
- package/examples/specs/workflows.lawspec +30 -0
- package/package.json +2 -1
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
unit example.warehouse
|
|
2
|
+
|
|
3
|
+
-- Asynchronous adapters bound to native code. Each target's bindings name a
|
|
4
|
+
-- native function or method returning the target's asynchronous type (a
|
|
5
|
+
-- coroutine, Promise, CompletableFuture, suspend function, Go task, Rust
|
|
6
|
+
-- future or IO action); the generated bridge awaits it and converts the
|
|
7
|
+
-- result.
|
|
8
|
+
|
|
9
|
+
-- A price, looked up by a native asynchronous function.
|
|
10
|
+
async priceOf :: Text -> Int32
|
|
11
|
+
-- The same price, computed by a native synchronous function.
|
|
12
|
+
quoteOf :: Text -> Int32
|
|
13
|
+
|
|
14
|
+
law `a looked-up price is the quoted price` is
|
|
15
|
+
definition is `for all` (sku :: Text) . priceOf sku = quoteOf sku end
|
|
16
|
+
example `a free sample` is
|
|
17
|
+
sku = "free"
|
|
18
|
+
expect priceOf sku = 0
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
-- A shelf is a native object; its methods are asynchronous.
|
|
23
|
+
handle Shelf
|
|
24
|
+
|
|
25
|
+
newShelf :: Unit -> Shelf
|
|
26
|
+
async restock :: Shelf -> (amount :: Int32 where amount >= 0 && amount <= 100) -> Unit
|
|
27
|
+
async count :: Shelf -> Int64
|
|
28
|
+
|
|
29
|
+
definition modelRestock (amount :: Int32 where amount >= 0 && amount <= 100) (total :: Int64 where total >= 0 && total <= 1000000) :: Int64 is
|
|
30
|
+
total + amount
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
definition modelCount (total :: Int64) :: Pair Int64 Int64 is Pair total total end
|
|
34
|
+
|
|
35
|
+
model shelf :: shared Shelf by Int64 is
|
|
36
|
+
start newShelf ~ 0
|
|
37
|
+
restock ~ modelRestock
|
|
38
|
+
count ~ modelCount
|
|
39
|
+
end
|
|
@@ -124,3 +124,23 @@ law `the empty stack stays empty` is
|
|
|
124
124
|
`for all` (x :: Int8) . prelude.isEmpty (prelude.pop (prelude.stackOf [x])) = true
|
|
125
125
|
end
|
|
126
126
|
end
|
|
127
|
+
|
|
128
|
+
-- Size-indexed stacks and queues: a pop, top, dequeue or front needs a
|
|
129
|
+
-- non-empty one (SizedStack (n + 1) a), so it can never fail.
|
|
130
|
+
law `the top is what was pushed` is
|
|
131
|
+
definition is
|
|
132
|
+
`for all` (x :: Int8) (s :: SizedStack 2 Int8) . prelude.sizedTop (prelude.sizedPush x s) = x
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
law `popping undoes a push` is
|
|
137
|
+
definition is
|
|
138
|
+
`for all` (s :: SizedStack 3 Int8) . prelude.sizedStackItems (prelude.sizedPop (prelude.sizedPush 7 s)) = prelude.sizedStackItems s
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
law `a sized queue keeps arrival order` is
|
|
143
|
+
definition is
|
|
144
|
+
`for all` (x :: Int8) (y :: Int8) . prelude.sizedFront (prelude.sizedEnqueue y (prelude.sizedEnqueue x SizedQueueEmpty)) = x
|
|
145
|
+
end
|
|
146
|
+
end
|
|
@@ -97,8 +97,9 @@ law `messages reach a mailbox on another node` is
|
|
|
97
97
|
definition is `for all` (x :: Int32) . remoteLedger x = 2 * x end
|
|
98
98
|
end
|
|
99
99
|
|
|
100
|
-
-- A channel end sent to another node
|
|
101
|
-
-- conversation, so the end works on its new
|
|
100
|
+
-- A channel end sent to another node. A local end stays where it is and
|
|
101
|
+
-- the sending node relays its conversation, so the end works on its new
|
|
102
|
+
-- node as it did on the old one.
|
|
102
103
|
protocol Handoff is
|
|
103
104
|
send Doubling
|
|
104
105
|
end
|
|
@@ -108,3 +109,24 @@ async remoteHandoff :: Int32 -> Int64
|
|
|
108
109
|
law `a channel end handed to another node` is
|
|
109
110
|
definition is `for all` (x :: Int32) . remoteHandoff x = 2 * x end
|
|
110
111
|
end
|
|
112
|
+
|
|
113
|
+
-- An end that is already between nodes moves: its state goes to the new
|
|
114
|
+
-- node, with any values that reached it, and its peer is told the new
|
|
115
|
+
-- address. Here an end whose peer is on node C goes from A to B and on to D
|
|
116
|
+
-- over a network that loses, duplicates and delays frames, while C is
|
|
117
|
+
-- already sending to it; then A and B close, and the conversation between C
|
|
118
|
+
-- and D still completes.
|
|
119
|
+
protocol Answering is
|
|
120
|
+
receive Int32
|
|
121
|
+
send Int64
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
protocol Passing is
|
|
125
|
+
send Answering
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
async remoteHandoffOnward :: Int32 -> Int64
|
|
129
|
+
|
|
130
|
+
law `a channel end moved on twice` is
|
|
131
|
+
definition is `for all` (x :: Int32) . remoteHandoffOnward x = 2 * x end
|
|
132
|
+
end
|
|
@@ -57,3 +57,37 @@ law `push agrees with the reference` is
|
|
|
57
57
|
`for all` (x :: Int8) (s :: Stack n) . (pushed x ~s; pop ~s) = x
|
|
58
58
|
end
|
|
59
59
|
end
|
|
60
|
+
|
|
61
|
+
-- A function may take several flow parameters: pushBoth takes two stacks and
|
|
62
|
+
-- leaves each one higher. A call passes ~a ~b, rebinding each.
|
|
63
|
+
definition pushBoth (x :: Int8) (a :: Stack n / Stack (n + 1)) (b :: Stack m / Stack (m + 1)) :: Unit is
|
|
64
|
+
~a := Push x a; ~b := Push x b
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
law `both stacks get the value` is
|
|
68
|
+
definition is
|
|
69
|
+
`for all` (x :: Int8) (a :: Stack n) (b :: Stack m) . (pushBoth x ~a ~b; pop ~a; pop ~b) = x
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
-- A flow call may sit in a branch of an if or a match, when every branch
|
|
74
|
+
-- leaves the stack at the same type: here each branch pushes once.
|
|
75
|
+
law `a push in each branch` is
|
|
76
|
+
definition is
|
|
77
|
+
`for all` (x :: Int8) (s :: Stack n) .
|
|
78
|
+
((if x > 0 then push x ~s else push 0 ~s); pop ~s) = (if x > 0 then x else 0)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
law `a push chosen by a match` is
|
|
83
|
+
definition is
|
|
84
|
+
`for all` (m :: Maybe Int8) (s :: Stack n) .
|
|
85
|
+
((match m with
|
|
86
|
+
| Just v -> push v ~s
|
|
87
|
+
| Nothing -> push 0 ~s
|
|
88
|
+
end); pop ~s) = (match m with
|
|
89
|
+
| Just v -> v
|
|
90
|
+
| Nothing -> prelude.Int8 0
|
|
91
|
+
end)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
@@ -140,3 +140,25 @@ law `refined amounts are drawn from their range` is
|
|
|
140
140
|
`for all` (p :: Purse) (amount :: Int32 where amount >= 1 && amount <= 1000) . spend p amount >= 1
|
|
141
141
|
end
|
|
142
142
|
end
|
|
143
|
+
|
|
144
|
+
-- if c then a else b evaluates only the branch c selects, and each branch is
|
|
145
|
+
-- checked knowing which way c went: the division is safe because n > 0.
|
|
146
|
+
definition share (total :: Int32 where total >= 0 && total <= 1000) (n :: Int32 where n >= 0 && n <= 10) :: Int32 is
|
|
147
|
+
if n > 0 then prelude.quot total n else 0
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
law `a share is at most the total` is
|
|
151
|
+
definition is
|
|
152
|
+
`for all` (t :: Int32 where t >= 0 && t <= 1000) (n :: Int32 where n >= 0 && n <= 10) . share t n <= t
|
|
153
|
+
end
|
|
154
|
+
example `nobody to share with` is
|
|
155
|
+
t = 7
|
|
156
|
+
n = 0
|
|
157
|
+
expect share t n = 0
|
|
158
|
+
end
|
|
159
|
+
example `three people` is
|
|
160
|
+
t = 7
|
|
161
|
+
n = 3
|
|
162
|
+
expect share t n = 2
|
|
163
|
+
end
|
|
164
|
+
end
|
|
@@ -119,3 +119,24 @@ scenario `two counts at once` in counter is
|
|
|
119
119
|
total <- read
|
|
120
120
|
expect total = 2
|
|
121
121
|
end
|
|
122
|
+
|
|
123
|
+
-- Two channels between the same two processes close a cycle. That is
|
|
124
|
+
-- accepted when no process waits for another in a cycle: here the first
|
|
125
|
+
-- process sends before it waits, so the echo can always answer.
|
|
126
|
+
protocol Echo is
|
|
127
|
+
receive Int64
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
scenario `a count is echoed back` in counter is
|
|
131
|
+
channel out :: Report
|
|
132
|
+
channel back :: Echo
|
|
133
|
+
par
|
|
134
|
+
n <- increment
|
|
135
|
+
send out n
|
|
136
|
+
receive back m
|
|
137
|
+
expect m = 1
|
|
138
|
+
with
|
|
139
|
+
receive out k
|
|
140
|
+
send back k
|
|
141
|
+
end
|
|
142
|
+
end
|
|
@@ -143,3 +143,33 @@ law `vetting a named child reports its age` is
|
|
|
143
143
|
vet (Signup "ada" (prelude.Int32 age)) = Left (VetFailures [VetCheckAgeFailed "too young"])
|
|
144
144
|
end
|
|
145
145
|
end
|
|
146
|
+
|
|
147
|
+
-- In a workflow with asynchronous steps, an all group runs them at the same
|
|
148
|
+
-- time. Order -1's checks are slow: the stock check takes 400ms and the
|
|
149
|
+
-- credit check 250ms, so run in turn they would take 650ms. Every order with
|
|
150
|
+
-- a negative number fails both checks.
|
|
151
|
+
type Order is Order number :: Int64 end
|
|
152
|
+
|
|
153
|
+
async checkStock :: Order -> Either Text Order
|
|
154
|
+
async checkCredit :: Order -> Either Text Order
|
|
155
|
+
|
|
156
|
+
definition firstOrder (a :: Order) (b :: Order) :: Order is a end
|
|
157
|
+
|
|
158
|
+
workflow approve :: Order -> Either _ Order is
|
|
159
|
+
all accumulate
|
|
160
|
+
then checkStock
|
|
161
|
+
then checkCredit
|
|
162
|
+
end
|
|
163
|
+
combine firstOrder
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
-- The failures are in the order the steps are declared, not the order they
|
|
167
|
+
-- finish in.
|
|
168
|
+
law `approval reports failures in declaration order` is
|
|
169
|
+
definition is
|
|
170
|
+
`for all` (n :: Int64) .
|
|
171
|
+
approve (Order n) = prelude.select (n < 0)
|
|
172
|
+
(Left (ApproveFailures [ApproveCheckStockFailed "no stock", ApproveCheckCreditFailed "no credit"]))
|
|
173
|
+
(Right (Order n))
|
|
174
|
+
end
|
|
175
|
+
end
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lawspec",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.1",
|
|
4
4
|
"description": "State the law once. Check it everywhere.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
"examples"
|
|
36
36
|
],
|
|
37
37
|
"scripts": {
|
|
38
|
+
"prepack": "cp ../README.md ../CHANGELOG.md .",
|
|
38
39
|
"test": "node --test test/*.test.mjs"
|
|
39
40
|
}
|
|
40
41
|
}
|