lawspec 0.24.0 → 0.26.0
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 +109 -1
- package/README.md +34 -26
- package/UNICODE-LICENSE.txt +39 -0
- package/api.mjs +6 -23
- package/bin/lawspec.mjs +148 -65
- package/build.json +245 -177
- package/builtin-data-names.mjs +73 -0
- package/core.wasm +2 -2
- package/core_jsffi.js +5 -0
- package/examples/native-payments/clojure/lawspec.json +13 -2
- package/examples/native-payments/csharp/lawspec.json +13 -2
- package/examples/native-payments/elixir/lawspec.json +14 -3
- package/examples/native-payments/erlang/lawspec.json +14 -3
- package/examples/native-payments/fsharp/lawspec.json +14 -3
- package/examples/native-payments/gleam/lawspec.json +14 -3
- package/examples/native-payments/go/lawspec.json +14 -3
- package/examples/native-payments/groovy/lawspec.json +14 -3
- package/examples/native-payments/haskell/lawspec.json +14 -3
- package/examples/native-payments/java/lawspec.json +14 -3
- package/examples/native-payments/javascript/lawspec.json +14 -3
- package/examples/native-payments/kotlin/lawspec.json +14 -3
- package/examples/native-payments/python/lawspec.json +14 -3
- package/examples/native-payments/rust/lawspec.json +14 -3
- package/examples/native-payments/scala/lawspec.json +14 -3
- package/examples/native-payments/swift/lawspec.json +14 -3
- package/examples/native-payments/typescript/lawspec.json +14 -3
- package/examples/packages/orders/lawspec.json +25 -5
- package/examples/packages/orders/orders.lawspec +57 -53
- package/examples/packages/shop-domain/src/domain.lawspec +36 -33
- package/examples/packages/shop-tax-1/src/api.lawspec +1 -0
- package/examples/packages/shop-tax-1/src/rates.lawspec +5 -4
- package/examples/packages/shop-tax-2/src/api.lawspec +1 -0
- package/examples/packages/shop-tax-2/src/rates.lawspec +6 -4
- package/examples/rc1/authorization-snapshots.lawspec +113 -0
- package/examples/rc1/banner-filenames.lawspec +64 -0
- package/examples/rc1/bit-codec.lawspec +57 -0
- package/examples/rc1/bounded-witness-search.lawspec +151 -0
- package/examples/rc1/conflicting-requirements.lawspec +31 -0
- package/examples/rc1/constructive-witnesses.lawspec +120 -0
- package/examples/rc1/documentary-review.lawspec +43 -0
- package/examples/rc1/empty-domains.lawspec +47 -0
- package/examples/rc1/finite-integer-domains.lawspec +76 -0
- package/examples/rc1/law-metadata.lawspec +30 -0
- package/examples/rc1/native-payments.lawspec +108 -0
- package/examples/rc1/nesting/rrr.lawspec +34 -0
- package/examples/rc1/nesting/rru.lawspec +57 -0
- package/examples/rc1/nesting/rur.lawspec +58 -0
- package/examples/rc1/nesting/ruu.lawspec +78 -0
- package/examples/rc1/nesting/urr.lawspec +53 -0
- package/examples/rc1/nesting/uru.lawspec +73 -0
- package/examples/rc1/nesting/uur.lawspec +74 -0
- package/examples/rc1/nesting/uuu.lawspec +96 -0
- package/examples/rc1/order-workflow.lawspec +127 -0
- package/examples/rc1/parcel-measurements.lawspec +93 -0
- package/examples/rc1/picture-records.lawspec +52 -0
- package/examples/rc1/picture-tuples.lawspec +48 -0
- package/examples/rc1/record-payloads.lawspec +57 -0
- package/examples/rc1/recursive-records.lawspec +65 -0
- package/examples/rc1/refund-conformance.lawspec +45 -0
- package/examples/rc1/refund-policy.lawspec +89 -0
- package/examples/rc1/requirement-composition.lawspec +90 -0
- package/examples/rc1/structural-records.lawspec +54 -0
- package/examples/rc1-authoring.lawspec +57 -0
- package/examples/specs/abilities.lawspec +67 -65
- package/examples/specs/ability_imports.lawspec +58 -57
- package/examples/specs/actors.lawspec +23 -22
- package/examples/specs/algebra.lawspec +142 -141
- package/examples/specs/approvals.lawspec +16 -15
- package/examples/specs/async_bindings.lawspec +10 -9
- package/examples/specs/async_fetch.lawspec +12 -11
- package/examples/specs/atoi_codec.lawspec +16 -15
- package/examples/specs/boolean_flags.lawspec +10 -9
- package/examples/specs/builtins.lawspec +73 -72
- package/examples/specs/canonical_url.lawspec +14 -13
- package/examples/specs/collections.lawspec +153 -154
- package/examples/specs/composition.lawspec +70 -66
- package/examples/specs/concurrent_collections.lawspec +8 -7
- package/examples/specs/consistency.lawspec +4 -3
- package/examples/specs/contracts.lawspec +80 -79
- package/examples/specs/crypto.lawspec +26 -25
- package/examples/specs/ctc.lawspec +148 -0
- package/examples/specs/currying.lawspec +32 -31
- package/examples/specs/data_types.lawspec +50 -49
- package/examples/specs/distribution.lawspec +57 -56
- package/examples/specs/domain_modeling.lawspec +48 -47
- package/examples/specs/durations.lawspec +39 -38
- package/examples/specs/equivalent.lawspec +25 -24
- package/examples/specs/failures.lawspec +64 -63
- package/examples/specs/finite_data.lawspec +32 -31
- package/examples/specs/flow_stack.lawspec +64 -63
- package/examples/specs/gadt_expressions.lawspec +50 -49
- package/examples/specs/generation.lawspec +115 -114
- package/examples/specs/handler_bindings.lawspec +42 -40
- package/examples/specs/handles.lawspec +2 -1
- package/examples/specs/harness.lawspec +45 -43
- package/examples/specs/harness_testing.lawspec +18 -13
- package/examples/specs/indexed_arithmetic.lawspec +401 -126
- package/examples/specs/indexed_families.lawspec +182 -66
- package/examples/specs/keywords.lawspec +22 -21
- package/examples/specs/limits.lawspec +6 -5
- package/examples/specs/list_contracts.lawspec +121 -120
- package/examples/specs/list_refinements.lawspec +43 -42
- package/examples/specs/lists.lawspec +45 -44
- package/examples/specs/matchers.lawspec +79 -78
- package/examples/specs/matching.lawspec +43 -42
- package/examples/specs/mixed_inputs.lawspec +25 -24
- package/examples/specs/models.lawspec +44 -43
- package/examples/specs/parse_port.lawspec +28 -27
- package/examples/specs/payments.lawspec +55 -54
- package/examples/specs/railway.lawspec +59 -58
- package/examples/specs/records.lawspec +149 -148
- package/examples/specs/records_inner.lawspec +5 -4
- package/examples/specs/recursive_refinements.lawspec +30 -29
- package/examples/specs/refined_definitions.lawspec +24 -23
- package/examples/specs/refinements.lawspec +58 -57
- package/examples/specs/resilience.lawspec +159 -158
- package/examples/specs/resources.lawspec +61 -60
- package/examples/specs/scalar_adapters.lawspec +60 -59
- package/examples/specs/scalar_catalog.lawspec +165 -164
- package/examples/specs/scalars.lawspec +78 -77
- package/examples/specs/scheduling_order.lawspec +8 -7
- package/examples/specs/scheduling_overlap.lawspec +5 -4
- package/examples/specs/sequences.lawspec +66 -65
- package/examples/specs/sessions.lawspec +13 -12
- package/examples/specs/slug.lawspec +16 -15
- package/examples/specs/sum_refinements.lawspec +42 -41
- package/examples/specs/tables.lawspec +29 -28
- package/examples/specs/total_functions.lawspec +71 -70
- package/examples/specs/workflows.lawspec +101 -100
- package/examples-command.mjs +8 -4
- package/explain.mjs +114 -0
- package/expression-presentation.mjs +333 -0
- package/files.mjs +16 -0
- package/index.d.ts +406 -11
- package/launcher.mjs +9 -2
- package/lsp.mjs +235 -0
- package/native-evidence.mjs +187 -0
- package/native-examples.mjs +8 -1
- package/node-events-reporter.mjs +23 -0
- package/package.json +8 -1
- package/positional-fields.mjs +13 -0
- package/proof.d.ts +35 -0
- package/proof.mjs +174 -0
- package/starter-rc1.lawspec +26 -0
- package/starter.lawspec +16 -15
- package/templates.mjs +6 -4
- package/test-command.mjs +107 -35
- package/typed-presentation.mjs +165 -0
|
@@ -6,9 +6,9 @@ unit example.collections
|
|
|
6
6
|
-- build them from lists.
|
|
7
7
|
|
|
8
8
|
-- The distinct values of a list.
|
|
9
|
-
dedupe :: List Int32 ->
|
|
9
|
+
dedupe :: List Int32 -> SortedSet Int32
|
|
10
10
|
-- How many times each word occurs.
|
|
11
|
-
wordCounts :: List Text -> KeyVal Text
|
|
11
|
+
wordCounts :: List Text -> KeyVal Text Integer
|
|
12
12
|
-- Enqueue every value, first to last.
|
|
13
13
|
fifo :: List Int8 -> Queue Int8
|
|
14
14
|
-- Push every value, first to last: the last is on top.
|
|
@@ -16,175 +16,174 @@ lifo :: List Int8 -> Stack Int8
|
|
|
16
16
|
-- Move the front item to the back.
|
|
17
17
|
rotate :: Deque Int8 -> Deque Int8
|
|
18
18
|
-- The distinct lists among some lists: structured elements.
|
|
19
|
-
distinctRows :: List (List Int8) ->
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
law
|
|
39
|
-
definition
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
law
|
|
43
|
-
definition
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
law
|
|
47
|
-
definition
|
|
48
|
-
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
law
|
|
53
|
-
definition
|
|
54
|
-
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
law
|
|
59
|
-
definition
|
|
60
|
-
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
law
|
|
65
|
-
definition
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
law
|
|
69
|
-
definition
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
law
|
|
73
|
-
definition
|
|
74
|
-
|
|
75
|
-
end
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
law
|
|
79
|
-
definition
|
|
80
|
-
|
|
81
|
-
end
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
law
|
|
85
|
-
definition
|
|
86
|
-
|
|
87
|
-
end
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
law
|
|
91
|
-
definition
|
|
92
|
-
|
|
93
|
-
prelude.
|
|
94
|
-
end
|
|
95
|
-
end
|
|
96
|
-
|
|
97
|
-
law
|
|
98
|
-
definition
|
|
99
|
-
|
|
100
|
-
prelude.lookup k (prelude.put k w (prelude.put k v m)) = Just w
|
|
101
|
-
end
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
law
|
|
105
|
-
definition
|
|
106
|
-
|
|
107
|
-
end
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
law
|
|
111
|
-
definition
|
|
112
|
-
|
|
113
|
-
end
|
|
114
|
-
end
|
|
115
|
-
|
|
116
|
-
law
|
|
117
|
-
definition
|
|
118
|
-
|
|
119
|
-
end
|
|
120
|
-
end
|
|
121
|
-
|
|
122
|
-
law
|
|
123
|
-
definition
|
|
124
|
-
|
|
125
|
-
end
|
|
126
|
-
end
|
|
19
|
+
distinctRows :: List (List Int8) -> SortedSet (List Int8)
|
|
20
|
+
|
|
21
|
+
function countWords (words :: List Text) (counts :: KeyVal Text Integer) -> KeyVal Text Integer is
|
|
22
|
+
first matching case of words :
|
|
23
|
+
when Nil then counts
|
|
24
|
+
when Cons w rest then first matching case of prelude.lookup w counts :
|
|
25
|
+
when Nothing then countWords rest (prelude.put w 1 counts)
|
|
26
|
+
when Just n then countWords rest (prelude.put w ((n + 1) :: Integer) counts)
|
|
27
|
+
end case
|
|
28
|
+
end case
|
|
29
|
+
end function
|
|
30
|
+
|
|
31
|
+
function rotated (d :: Deque Int8) -> Deque Int8 is
|
|
32
|
+
first matching case of prelude.peekFront d :
|
|
33
|
+
when Nothing then d
|
|
34
|
+
when Just x then prelude.pushBack x (prelude.popFront d)
|
|
35
|
+
end case
|
|
36
|
+
end function
|
|
37
|
+
|
|
38
|
+
law "|dedupe agrees with setOf| is
|
|
39
|
+
definition : for all (xs :: List Int32) => dedupe xs = prelude.sortedSetOf xs end for all end definition
|
|
40
|
+
end law
|
|
41
|
+
|
|
42
|
+
law "|distinct rows agree with setOf| is
|
|
43
|
+
definition : for all (rows :: List (List Int8)) => distinctRows rows = prelude.sortedSetOf rows end for all end definition
|
|
44
|
+
end law
|
|
45
|
+
|
|
46
|
+
law "|word counts agree with the definition| is
|
|
47
|
+
definition :
|
|
48
|
+
for all (words :: List Text) => wordCounts words = countWords words (prelude.keyValOf []) end for all
|
|
49
|
+
end definition
|
|
50
|
+
end law
|
|
51
|
+
|
|
52
|
+
law "|the queue's front is the first value| is
|
|
53
|
+
definition :
|
|
54
|
+
for all (x :: Int8) (xs :: List Int8) => prelude.front (fifo (Cons x xs)) = Just x end for all
|
|
55
|
+
end definition
|
|
56
|
+
end law
|
|
57
|
+
|
|
58
|
+
law "|the stack's top is the last value| is
|
|
59
|
+
definition :
|
|
60
|
+
for all (x :: Int8) (xs :: List Int8) => prelude.peek (lifo [x]) = Just x end for all
|
|
61
|
+
end definition
|
|
62
|
+
end law
|
|
63
|
+
|
|
64
|
+
law "|fifo keeps the order| is
|
|
65
|
+
definition : for all (xs :: List Int8) => prelude.toList (fifo xs) = xs end for all end definition
|
|
66
|
+
end law
|
|
67
|
+
|
|
68
|
+
law "|rotate agrees with the definition| is
|
|
69
|
+
definition : for all (d :: Deque Int8) => rotate d = rotated d end for all end definition
|
|
70
|
+
end law
|
|
71
|
+
|
|
72
|
+
law "|union is commutative| is
|
|
73
|
+
definition :
|
|
74
|
+
for all (s :: SortedSet Int32) (t :: SortedSet Int32) => prelude.sortedSetUnion s t = prelude.sortedSetUnion t s end for all
|
|
75
|
+
end definition
|
|
76
|
+
end law
|
|
77
|
+
|
|
78
|
+
law "|a removed value is no member| is
|
|
79
|
+
definition :
|
|
80
|
+
for all (x :: Int32) (s :: SortedSet Int32) => prelude.sortedSetMember x (prelude.sortedSetRemove x s) = false end for all
|
|
81
|
+
end definition
|
|
82
|
+
end law
|
|
83
|
+
|
|
84
|
+
law "|an inserted value is a member| is
|
|
85
|
+
definition :
|
|
86
|
+
for all (x :: Int32) (s :: SortedSet Int32) => prelude.sortedSetMember x (prelude.sortedSetInsert x s) = true end for all
|
|
87
|
+
end definition
|
|
88
|
+
end law
|
|
89
|
+
|
|
90
|
+
law "|intersection and difference split a set| is
|
|
91
|
+
definition :
|
|
92
|
+
for all (s :: SortedSet Int32) (t :: SortedSet Int32) =>
|
|
93
|
+
prelude.sortedSetUnion (prelude.sortedSetIntersection s t) (prelude.sortedSetDifference s t) = s end for all
|
|
94
|
+
end definition
|
|
95
|
+
end law
|
|
96
|
+
|
|
97
|
+
law "|the latest put wins| is
|
|
98
|
+
definition :
|
|
99
|
+
for all (k :: Text) (v :: Integer) (w :: Integer) (m :: KeyVal Text Integer) =>
|
|
100
|
+
prelude.lookup k (prelude.put k w (prelude.put k v m)) = Just w end for all
|
|
101
|
+
end definition
|
|
102
|
+
end law
|
|
103
|
+
|
|
104
|
+
law "|a deleted key is absent| is
|
|
105
|
+
definition :
|
|
106
|
+
for all (k :: Text) (m :: KeyVal Text Integer) => prelude.lookup k (prelude.delete k m) = Nothing end for all
|
|
107
|
+
end definition
|
|
108
|
+
end law
|
|
109
|
+
|
|
110
|
+
law "|keys are distinct| is
|
|
111
|
+
definition :
|
|
112
|
+
for all (m :: KeyVal Text Integer) => prelude.size (prelude.sortedSetOf (prelude.keys m)) = prelude.size m end for all
|
|
113
|
+
end definition
|
|
114
|
+
end law
|
|
115
|
+
|
|
116
|
+
law "|popping the back leaves the rest| is
|
|
117
|
+
definition :
|
|
118
|
+
for all (x :: Int8) (d :: Deque Int8) => prelude.popBack (prelude.pushBack x d) = d end for all
|
|
119
|
+
end definition
|
|
120
|
+
end law
|
|
121
|
+
|
|
122
|
+
law "|the empty stack stays empty| is
|
|
123
|
+
definition :
|
|
124
|
+
for all (x :: Int8) => prelude.isEmpty (prelude.pop (prelude.stackOf [x])) = true end for all
|
|
125
|
+
end definition
|
|
126
|
+
end law
|
|
127
127
|
|
|
128
128
|
-- Size-indexed stacks and queues: a pop, top, dequeue or front needs a
|
|
129
129
|
-- non-empty one (SizedStack (n + 1) a), so it can never fail.
|
|
130
|
-
law
|
|
131
|
-
definition
|
|
132
|
-
|
|
133
|
-
end
|
|
134
|
-
end
|
|
135
|
-
|
|
136
|
-
law
|
|
137
|
-
definition
|
|
138
|
-
|
|
139
|
-
end
|
|
140
|
-
end
|
|
141
|
-
|
|
142
|
-
law
|
|
143
|
-
definition
|
|
144
|
-
|
|
145
|
-
end
|
|
146
|
-
end
|
|
130
|
+
law "|the top is what was pushed| is
|
|
131
|
+
definition :
|
|
132
|
+
for all (x :: Int8) (s :: SizedStack 2 Int8) => prelude.sizedTop (prelude.sizedPush x s) = x end for all
|
|
133
|
+
end definition
|
|
134
|
+
end law
|
|
135
|
+
|
|
136
|
+
law "|popping undoes a push| is
|
|
137
|
+
definition :
|
|
138
|
+
for all (s :: SizedStack 3 Int8) => prelude.sizedStackItems (prelude.sizedPop (prelude.sizedPush 7 s)) = prelude.sizedStackItems s end for all
|
|
139
|
+
end definition
|
|
140
|
+
end law
|
|
141
|
+
|
|
142
|
+
law "|a sized queue keeps arrival order| is
|
|
143
|
+
definition :
|
|
144
|
+
for all (x :: Int8) (y :: Int8) => prelude.sizedFront (prelude.sizedEnqueue y (prelude.sizedEnqueue x SizedQueueEmpty)) = x end for all
|
|
145
|
+
end definition
|
|
146
|
+
end law
|
|
147
147
|
|
|
148
148
|
-- Text keys and items order by code point on every target. U+1F600 follows
|
|
149
149
|
-- U+FF21 by code point, though in UTF-16 its first surrogate (U+D83D)
|
|
150
150
|
-- precedes U+FF21, so a target ordering text by UTF-16 unit fails here.
|
|
151
151
|
|
|
152
152
|
-- The items of a set of words, in the set's order.
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
end
|
|
157
|
-
end
|
|
153
|
+
function orderedItems (s :: SortedSet Text) -> List Text is
|
|
154
|
+
prelude.toList s
|
|
155
|
+
end function
|
|
158
156
|
|
|
159
157
|
-- The distinct words, in the order of the keys that count them.
|
|
160
|
-
|
|
158
|
+
function countedKeys (words :: List Text) -> List Text is
|
|
161
159
|
prelude.keys (countWords words (prelude.keyValOf []))
|
|
162
|
-
end
|
|
160
|
+
end function
|
|
163
161
|
|
|
164
|
-
law
|
|
165
|
-
definition
|
|
166
|
-
|
|
167
|
-
end
|
|
168
|
-
example
|
|
162
|
+
law "|text keys and items order by code point| is
|
|
163
|
+
definition :
|
|
164
|
+
for all (words :: List Text) => countedKeys words = orderedItems (prelude.sortedSetOf words) end for all
|
|
165
|
+
end definition
|
|
166
|
+
example "|a fullwidth letter precedes an emoji| :
|
|
169
167
|
words = ["😀", "A", "😀"]
|
|
170
168
|
expect countedKeys words = ["A", "😀"]
|
|
171
|
-
expect orderedItems (prelude.
|
|
172
|
-
end
|
|
173
|
-
end
|
|
169
|
+
expect orderedItems (prelude.sortedSetOf words) = ["A", "😀"]
|
|
170
|
+
end example
|
|
171
|
+
end law
|
|
174
172
|
|
|
175
173
|
-- How one set of words orders against another.
|
|
176
|
-
|
|
174
|
+
function compareSets (s :: SortedSet Text) (t :: SortedSet Text) -> Ordering is
|
|
177
175
|
prelude.compare s t
|
|
178
|
-
end
|
|
179
|
-
|
|
180
|
-
law
|
|
181
|
-
definition
|
|
182
|
-
|
|
183
|
-
compareSets (prelude.
|
|
184
|
-
end
|
|
185
|
-
example
|
|
176
|
+
end function
|
|
177
|
+
|
|
178
|
+
law "|sets of text order as their items do| is
|
|
179
|
+
definition :
|
|
180
|
+
for all (xs :: List Text) (ys :: List Text) =>
|
|
181
|
+
compareSets (prelude.sortedSetOf xs) (prelude.sortedSetOf ys) = prelude.compare (orderedItems (prelude.sortedSetOf xs)) (orderedItems (prelude.sortedSetOf ys)) end for all
|
|
182
|
+
end definition
|
|
183
|
+
example "|a set led by a fullwidth letter precedes one led by a later letter| :
|
|
186
184
|
xs = ["😀", "A"]
|
|
187
185
|
ys = ["B"]
|
|
188
|
-
expect compareSets (prelude.
|
|
189
|
-
end
|
|
190
|
-
end
|
|
186
|
+
expect compareSets (prelude.sortedSetOf xs) (prelude.sortedSetOf ys) = Less
|
|
187
|
+
end example
|
|
188
|
+
end law
|
|
189
|
+
end unit
|
|
@@ -5,13 +5,13 @@ unit example.composition
|
|
|
5
5
|
-- the spec handlers together. The native handlers share a Store: each case
|
|
6
6
|
-- opens one, and every native handler of the case is made with it.
|
|
7
7
|
|
|
8
|
-
type Problem is | UnknownLog | EmptyRecord end
|
|
8
|
+
type Problem is one of | UnknownLog | EmptyRecord end type
|
|
9
|
+
|
|
10
|
+
type Log is a record{
|
|
9
11
|
|
|
10
|
-
type Log is
|
|
11
|
-
Log
|
|
12
12
|
records :: List Text
|
|
13
|
-
appends :: Int64
|
|
14
|
-
end
|
|
13
|
+
appends :: Int64}
|
|
14
|
+
end type
|
|
15
15
|
|
|
16
16
|
handle Store
|
|
17
17
|
|
|
@@ -19,92 +19,96 @@ openStore :: Unit -> Store
|
|
|
19
19
|
closeStore :: Store -> Unit
|
|
20
20
|
|
|
21
21
|
resource Store is
|
|
22
|
-
acquire is openStore unitValue end
|
|
23
|
-
release store is closeStore store end
|
|
24
|
-
end
|
|
22
|
+
acquire is openStore unitValue end acquire
|
|
23
|
+
release store is closeStore store end release
|
|
24
|
+
end resource
|
|
25
25
|
|
|
26
26
|
ability Logs with Store is
|
|
27
27
|
create :: Text -> Unit
|
|
28
28
|
append :: Text -> Text -> Int64 fails with Problem
|
|
29
29
|
laws
|
|
30
|
-
law
|
|
31
|
-
definition
|
|
32
|
-
end
|
|
33
|
-
law
|
|
34
|
-
definition
|
|
35
|
-
end
|
|
36
|
-
end
|
|
30
|
+
law "|a new log takes its first record at offset zero| is
|
|
31
|
+
definition : for all (t :: Text) (r :: Text where r != "") => (create t; append t r) = 0 end for all end definition
|
|
32
|
+
end law
|
|
33
|
+
law "|an unknown log refuses records| is
|
|
34
|
+
definition : for all (t :: Text) (r :: Text) => append t r fails with UnknownLog end for all end definition
|
|
35
|
+
end law
|
|
36
|
+
end laws
|
|
37
|
+
end ability
|
|
37
38
|
|
|
38
39
|
ability Reader with Store is
|
|
39
40
|
size :: Text -> Maybe Int64
|
|
40
41
|
laws
|
|
41
|
-
law
|
|
42
|
-
definition
|
|
43
|
-
end
|
|
44
|
-
end
|
|
42
|
+
law "|an unknown log has no size| is
|
|
43
|
+
definition : for all (t :: Text) => size t = Nothing end for all end definition
|
|
44
|
+
end law
|
|
45
|
+
end laws
|
|
46
|
+
end ability
|
|
45
47
|
|
|
46
48
|
-- A client over Logs: it keeps the offset of its last write.
|
|
47
49
|
ability Writer with Store is
|
|
48
50
|
write :: Text -> Text -> Bool
|
|
49
51
|
lastOffset :: Maybe Int64
|
|
50
52
|
laws
|
|
51
|
-
law
|
|
52
|
-
definition
|
|
53
|
-
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
end
|
|
53
|
+
law "|a write to a new log is read back| is
|
|
54
|
+
definition :
|
|
55
|
+
for all (t :: Text) (r :: Text where r != "") => (create t; write t r; size t) = Just 1 end for all
|
|
56
|
+
end definition
|
|
57
|
+
end law
|
|
58
|
+
end laws
|
|
59
|
+
end ability
|
|
57
60
|
|
|
58
|
-
|
|
61
|
+
function empty (u :: Unit) -> KeyVal Text Log is prelude.keyValOf [] end function
|
|
59
62
|
|
|
60
63
|
-- One state for both abilities. It starts from a computed value, and the
|
|
61
64
|
-- clause for append updates it in one branch only.
|
|
62
65
|
handler store for Logs, Reader with state logs :: KeyVal Text Log start empty unitValue is
|
|
63
|
-
create t is ~logs := prelude.put t (Log [] 0) logs; unitValue end
|
|
64
|
-
append t r is
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if r
|
|
66
|
+
operation create t is ~logs := prelude.put t (Log record{ records = [] , appends = 0}) logs; unitValue end operation
|
|
67
|
+
operation append t r is
|
|
68
|
+
first matching case of prelude.lookup t logs :
|
|
69
|
+
when Nothing then raise UnknownLog
|
|
70
|
+
when Just log then
|
|
71
|
+
if r = "" then raise EmptyRecord else
|
|
69
72
|
(~logs := prelude.put t (log with records = Cons r log.records, appends = prelude.saturating (log.appends + 1)) logs;
|
|
70
|
-
log.appends)
|
|
71
|
-
end
|
|
72
|
-
end
|
|
73
|
-
size t is
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
end
|
|
73
|
+
log.appends) end if
|
|
74
|
+
end case
|
|
75
|
+
end operation
|
|
76
|
+
operation size t is
|
|
77
|
+
first matching case of prelude.lookup t logs :
|
|
78
|
+
when Nothing then Nothing
|
|
79
|
+
when Just log then Just log.appends
|
|
80
|
+
end case
|
|
81
|
+
end operation
|
|
82
|
+
end handler
|
|
80
83
|
|
|
81
84
|
-- A clause may be a definition that gives Pair result state: `op by f`.
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
end
|
|
87
|
-
end
|
|
85
|
+
procedure writeTo (t :: Text) (r :: Text) (last :: Maybe Int64) -> Pair Bool (Maybe Int64) uses Logs is
|
|
86
|
+
first matching case of prelude.attempt (append t r) :
|
|
87
|
+
when Left problem then Pair false last
|
|
88
|
+
when Right offset then Pair true (Just offset)
|
|
89
|
+
end case
|
|
90
|
+
end procedure
|
|
88
91
|
|
|
89
|
-
|
|
92
|
+
function lastOf (last :: Maybe Int64) -> Pair (Maybe Int64) (Maybe Int64) is Pair last last end function
|
|
90
93
|
|
|
91
94
|
handler logWriter for Writer with state last :: Maybe Int64 start Nothing is
|
|
92
|
-
write by writeTo
|
|
93
|
-
lastOffset by lastOf
|
|
94
|
-
end
|
|
95
|
+
operation write by writeTo end operation
|
|
96
|
+
operation lastOffset by lastOf end operation
|
|
97
|
+
end handler
|
|
95
98
|
|
|
96
99
|
-- The reference composition: every ability answered by spec handlers.
|
|
97
|
-
handlers reference is store, logWriter end
|
|
98
|
-
|
|
99
|
-
law
|
|
100
|
-
definition
|
|
101
|
-
|
|
102
|
-
(create t; append t a; append t b; size t) = Just 2
|
|
103
|
-
end
|
|
104
|
-
end
|
|
105
|
-
|
|
106
|
-
law
|
|
107
|
-
definition
|
|
108
|
-
|
|
109
|
-
end
|
|
110
|
-
end
|
|
100
|
+
handlers reference is store, logWriter end handlers
|
|
101
|
+
|
|
102
|
+
law "|sizes count records| is
|
|
103
|
+
definition :
|
|
104
|
+
for all (t :: Text) (a :: Text where a != "") (b :: Text where b != "") =>
|
|
105
|
+
(create t; append t a; append t b; size t) = Just 2 end for all
|
|
106
|
+
end definition
|
|
107
|
+
end law
|
|
108
|
+
|
|
109
|
+
law "|a writer remembers where it wrote| is
|
|
110
|
+
definition :
|
|
111
|
+
for all (t :: Text) (r :: Text where r != "") => (create t; write t r; lastOffset) = Just 0 end for all
|
|
112
|
+
end definition
|
|
113
|
+
end law
|
|
114
|
+
end unit
|
|
@@ -5,7 +5,7 @@ unit example.concurrent
|
|
|
5
5
|
-- runs the commands in sequence and at the same time, and every history
|
|
6
6
|
-- must be linearizable. Sets and maps are checked key by key.
|
|
7
7
|
|
|
8
|
-
type WorkQueue is
|
|
8
|
+
type WorkQueue is a record{ id :: Int32} end type
|
|
9
9
|
|
|
10
10
|
newQueue :: Unit -> WorkQueue
|
|
11
11
|
async offer :: WorkQueue -> Int32 -> Unit
|
|
@@ -17,23 +17,23 @@ model queue :: shared WorkQueue behaves like Queue Int32 is
|
|
|
17
17
|
offer as offer
|
|
18
18
|
poll as poll
|
|
19
19
|
queueSize as size
|
|
20
|
-
end
|
|
20
|
+
end model
|
|
21
21
|
|
|
22
|
-
type Tags is
|
|
22
|
+
type Tags is a record{ id :: Int32} end type
|
|
23
23
|
|
|
24
24
|
newTags :: Unit -> Tags
|
|
25
25
|
async tag :: Tags -> Int32 -> Bool
|
|
26
26
|
async untag :: Tags -> Int32 -> Bool
|
|
27
27
|
async tagged :: Tags -> Int32 -> Bool
|
|
28
28
|
|
|
29
|
-
model tags :: shared Tags behaves like
|
|
29
|
+
model tags :: shared Tags behaves like SortedSet Int32 is
|
|
30
30
|
start newTags
|
|
31
31
|
tag as add
|
|
32
32
|
untag as remove
|
|
33
33
|
tagged as contains
|
|
34
|
-
end
|
|
34
|
+
end model
|
|
35
35
|
|
|
36
|
-
type Cache is
|
|
36
|
+
type Cache is a record{ id :: Int32} end type
|
|
37
37
|
|
|
38
38
|
newCache :: Unit -> Cache
|
|
39
39
|
async store :: Cache -> Int8 -> Int64 -> Maybe Int64
|
|
@@ -45,4 +45,5 @@ model cache :: shared Cache behaves like KeyVal Int8 Int64 is
|
|
|
45
45
|
store as put
|
|
46
46
|
fetch as get
|
|
47
47
|
evict as remove
|
|
48
|
-
end
|
|
48
|
+
end model
|
|
49
|
+
end unit
|
|
@@ -5,17 +5,18 @@ unit example.consistency
|
|
|
5
5
|
-- that replica has seen, so callers see stale totals, and the total is the
|
|
6
6
|
-- sum of the replicas. Its histories are not linearizable, but once every
|
|
7
7
|
-- hit is done the total must count them all: it is eventually consistent.
|
|
8
|
-
type Views is
|
|
8
|
+
type Views is a record{ id :: Int32} end type
|
|
9
9
|
|
|
10
10
|
newViews :: Unit -> Views
|
|
11
11
|
hit :: Views -> Int64
|
|
12
12
|
total :: Views -> Int64
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
function modelHit (n :: Int64 where n >= 0 && n <= 1000000) -> Pair Int64 Int64 is Pair (n + 1) (n + 1) end function
|
|
15
15
|
|
|
16
16
|
model views :: shared Views by Int64 is
|
|
17
17
|
consistency eventual
|
|
18
18
|
start newViews ~ 0
|
|
19
19
|
hit ~ modelHit
|
|
20
20
|
abstract total
|
|
21
|
-
end
|
|
21
|
+
end model
|
|
22
|
+
end unit
|