lawspec 0.25.0 → 0.27.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.
Files changed (156) hide show
  1. package/CHANGELOG.md +109 -1
  2. package/NORMALIZATION-LICENSES.txt +296 -0
  3. package/README.md +13 -4
  4. package/api.mjs +0 -8
  5. package/bin/lawspec.mjs +114 -79
  6. package/build.json +99 -117
  7. package/core.wasm +2 -2
  8. package/core_jsffi.js +5 -0
  9. package/doctor.mjs +14 -4
  10. package/examples/native-payments/clojure/lawspec.json +1 -1
  11. package/examples/native-payments/csharp/lawspec.json +1 -1
  12. package/examples/native-payments/elixir/lawspec.json +1 -1
  13. package/examples/native-payments/erlang/lawspec.json +1 -1
  14. package/examples/native-payments/fsharp/lawspec.json +1 -1
  15. package/examples/native-payments/gleam/lawspec.json +1 -1
  16. package/examples/native-payments/go/lawspec.json +1 -1
  17. package/examples/native-payments/groovy/lawspec.json +1 -1
  18. package/examples/native-payments/haskell/lawspec.json +1 -1
  19. package/examples/native-payments/java/lawspec.json +1 -1
  20. package/examples/native-payments/javascript/lawspec.json +1 -1
  21. package/examples/native-payments/kotlin/lawspec.json +1 -1
  22. package/examples/native-payments/prolog/lawspec.json +20 -0
  23. package/examples/native-payments/prolog/src/domain/payments.pl +23 -0
  24. package/examples/native-payments/prolog/tests/domain/payment_generators.pl +17 -0
  25. package/examples/native-payments/python/lawspec.json +1 -1
  26. package/examples/native-payments/rust/lawspec.json +1 -1
  27. package/examples/native-payments/scala/lawspec.json +1 -1
  28. package/examples/native-payments/swift/lawspec.json +1 -1
  29. package/examples/native-payments/typescript/lawspec.json +1 -1
  30. package/examples/packages/orders/lawspec.json +1 -1
  31. package/examples/packages/orders/orders.lawspec +27 -25
  32. package/examples/packages/shop-domain/src/domain.lawspec +15 -14
  33. package/examples/packages/shop-tax-1/src/api.lawspec +1 -0
  34. package/examples/packages/shop-tax-1/src/rates.lawspec +1 -0
  35. package/examples/packages/shop-tax-2/src/api.lawspec +1 -0
  36. package/examples/packages/shop-tax-2/src/rates.lawspec +1 -0
  37. package/examples/rc1/authorization-snapshots.lawspec +24 -23
  38. package/examples/rc1/banner-filenames.lawspec +7 -6
  39. package/examples/rc1/bit-codec.lawspec +12 -11
  40. package/examples/rc1/bounded-witness-search.lawspec +52 -45
  41. package/examples/rc1/conflicting-requirements.lawspec +8 -7
  42. package/examples/rc1/constructive-witnesses.lawspec +40 -33
  43. package/examples/rc1/documentary-review.lawspec +9 -8
  44. package/examples/rc1/empty-domains.lawspec +16 -15
  45. package/examples/rc1/finite-integer-domains.lawspec +24 -23
  46. package/examples/rc1/law-metadata.lawspec +6 -5
  47. package/examples/rc1/native-payments.lawspec +17 -16
  48. package/examples/rc1/nesting/rrr.lawspec +4 -3
  49. package/examples/rc1/nesting/rru.lawspec +7 -6
  50. package/examples/rc1/nesting/rur.lawspec +7 -6
  51. package/examples/rc1/nesting/ruu.lawspec +10 -9
  52. package/examples/rc1/nesting/urr.lawspec +7 -6
  53. package/examples/rc1/nesting/uru.lawspec +10 -9
  54. package/examples/rc1/nesting/uur.lawspec +10 -9
  55. package/examples/rc1/nesting/uuu.lawspec +13 -12
  56. package/examples/rc1/order-workflow.lawspec +18 -17
  57. package/examples/rc1/parcel-measurements.lawspec +14 -13
  58. package/examples/rc1/picture-records.lawspec +6 -5
  59. package/examples/rc1/picture-tuples.lawspec +6 -5
  60. package/examples/rc1/record-payloads.lawspec +12 -11
  61. package/examples/rc1/recursive-records.lawspec +11 -10
  62. package/examples/rc1/refund-conformance.lawspec +7 -6
  63. package/examples/rc1/refund-policy.lawspec +15 -14
  64. package/examples/rc1/requirement-composition.lawspec +26 -25
  65. package/examples/rc1/structural-records.lawspec +13 -12
  66. package/examples/rc1-authoring.lawspec +6 -5
  67. package/examples/specs/abilities.lawspec +67 -65
  68. package/examples/specs/ability_imports.lawspec +58 -57
  69. package/examples/specs/actors.lawspec +23 -22
  70. package/examples/specs/algebra.lawspec +142 -141
  71. package/examples/specs/approvals.lawspec +19 -18
  72. package/examples/specs/async_bindings.lawspec +10 -9
  73. package/examples/specs/async_fetch.lawspec +12 -11
  74. package/examples/specs/atoi_codec.lawspec +16 -15
  75. package/examples/specs/boolean_flags.lawspec +10 -9
  76. package/examples/specs/builtins.lawspec +73 -72
  77. package/examples/specs/canonical_url.lawspec +14 -13
  78. package/examples/specs/collections.lawspec +153 -154
  79. package/examples/specs/composition.lawspec +70 -66
  80. package/examples/specs/concurrent_collections.lawspec +8 -7
  81. package/examples/specs/consistency.lawspec +4 -3
  82. package/examples/specs/contracts.lawspec +80 -79
  83. package/examples/specs/crypto.lawspec +26 -25
  84. package/examples/specs/ctc.lawspec +148 -0
  85. package/examples/specs/currying.lawspec +32 -31
  86. package/examples/specs/data_types.lawspec +50 -49
  87. package/examples/specs/distribution.lawspec +57 -56
  88. package/examples/specs/domain_modeling.lawspec +68 -47
  89. package/examples/specs/durations.lawspec +39 -38
  90. package/examples/specs/equivalent.lawspec +25 -24
  91. package/examples/specs/failures.lawspec +64 -63
  92. package/examples/specs/finite_data.lawspec +32 -31
  93. package/examples/specs/flow_stack.lawspec +64 -63
  94. package/examples/specs/gadt_expressions.lawspec +50 -49
  95. package/examples/specs/generation.lawspec +115 -114
  96. package/examples/specs/handler_bindings.lawspec +42 -40
  97. package/examples/specs/handles.lawspec +2 -1
  98. package/examples/specs/harness.lawspec +45 -43
  99. package/examples/specs/harness_testing.lawspec +18 -13
  100. package/examples/specs/indexed_arithmetic.lawspec +401 -126
  101. package/examples/specs/indexed_families.lawspec +182 -66
  102. package/examples/specs/keywords.lawspec +22 -21
  103. package/examples/specs/limits.lawspec +7 -6
  104. package/examples/specs/list_contracts.lawspec +121 -120
  105. package/examples/specs/list_refinements.lawspec +43 -42
  106. package/examples/specs/lists.lawspec +45 -44
  107. package/examples/specs/matchers.lawspec +79 -78
  108. package/examples/specs/matching.lawspec +43 -42
  109. package/examples/specs/mixed_inputs.lawspec +25 -24
  110. package/examples/specs/models.lawspec +44 -43
  111. package/examples/specs/parse_port.lawspec +28 -27
  112. package/examples/specs/payments.lawspec +55 -54
  113. package/examples/specs/railway.lawspec +59 -58
  114. package/examples/specs/records.lawspec +149 -148
  115. package/examples/specs/records_inner.lawspec +5 -4
  116. package/examples/specs/recursive_refinements.lawspec +30 -29
  117. package/examples/specs/refined_definitions.lawspec +24 -23
  118. package/examples/specs/refinements.lawspec +58 -57
  119. package/examples/specs/resilience.lawspec +161 -160
  120. package/examples/specs/resources.lawspec +61 -60
  121. package/examples/specs/scalar_adapters.lawspec +60 -59
  122. package/examples/specs/scalar_catalog.lawspec +165 -164
  123. package/examples/specs/scalars.lawspec +78 -77
  124. package/examples/specs/scheduling_order.lawspec +8 -7
  125. package/examples/specs/scheduling_overlap.lawspec +5 -4
  126. package/examples/specs/sequences.lawspec +66 -65
  127. package/examples/specs/sessions.lawspec +13 -12
  128. package/examples/specs/slug.lawspec +16 -15
  129. package/examples/specs/sum_refinements.lawspec +42 -41
  130. package/examples/specs/tables.lawspec +29 -28
  131. package/examples/specs/total_functions.lawspec +71 -70
  132. package/examples/specs/workflows.lawspec +101 -100
  133. package/examples-command.mjs +7 -7
  134. package/explain.mjs +6 -4
  135. package/expression-presentation.mjs +8 -8
  136. package/files.mjs +1 -1
  137. package/index.d.ts +10 -528
  138. package/launcher.mjs +9 -2
  139. package/lsp.mjs +10 -6
  140. package/native-evidence.mjs +25 -12
  141. package/native-examples.mjs +3 -3
  142. package/package.json +2 -1
  143. package/starter-rc1.lawspec +6 -5
  144. package/starter.lawspec +16 -15
  145. package/templates.mjs +88 -5
  146. package/test-command.mjs +40 -4
  147. package/migrate.mjs +0 -135
  148. package/migration-bindings.mjs +0 -86
  149. package/migration-cases.mjs +0 -45
  150. package/migration-collections.mjs +0 -54
  151. package/migration-correspondence.mjs +0 -275
  152. package/migration-generation.mjs +0 -99
  153. package/migration-laws.mjs +0 -102
  154. package/migration-records.mjs +0 -166
  155. package/migration-source.mjs +0 -247
  156. package/migration-write.mjs +0 -156
@@ -10,112 +10,113 @@ import lawspec.concurrent
10
10
 
11
11
  -- Time. A deadline is now plus a wait; instants move by durations and are
12
12
  -- a duration apart.
13
- definition deadline (wait :: Duration where wait <= 1h) :: Instant uses Clock is now + wait end
13
+ procedure deadline (wait :: Duration where wait <= 1h) -> Instant uses Clock is now + wait end procedure
14
14
 
15
15
  -- Under the virtual clock, time moves only when told to.
16
- law `a deadline is as far off as the wait` using virtual clock is
17
- definition is
18
- `for all` (wait :: Duration where wait <= 1h) . (let d = deadline wait in d - now) = wait
19
- end
20
- end
21
-
22
- law `advancing moves the virtual clock` using virtual clock is
23
- definition is
24
- `for all` (d :: Duration where d <= 1h) . (let a = now in advance d; let b = now in b - a) = d
25
- end
26
- end
16
+ law "|a deadline is as far off as the wait| using virtual clock is
17
+ definition :
18
+ for all (wait :: Duration where wait <= 1h) => (let d = deadline wait in d - now end let) = wait end for all
19
+ end definition
20
+ end law
21
+
22
+ law "|advancing moves the virtual clock| using virtual clock is
23
+ definition :
24
+ for all (d :: Duration where d <= 1h) => (let a = now in advance d; let b = now in b - a end let end let) = d end for all
25
+ end definition
26
+ end law
27
27
 
28
28
  -- Time in laws: temporal propositions check over the clock, so under the
29
29
  -- virtual clock they are deterministic.
30
- law `a deadline eventually passes` using virtual clock is
31
- definition is
32
- `for all` (wait :: Duration where wait <= 1s) . (let d = deadline wait in eventually within 2 s, now >= d) = true
33
- end
34
- end
30
+ law "|a deadline eventually passes| using virtual clock is
31
+ definition :
32
+ for all (wait :: Duration where wait <= 1s) => (let d = deadline wait in eventually within 2 s, now >= d end let) = true end for all
33
+ end definition
34
+ end law
35
35
 
36
- law `nothing is late before its deadline` using virtual clock is
37
- definition is (let d = deadline 3s in always within 2 s, now < d) = true end
38
- end
36
+ law "|nothing is late before its deadline| using virtual clock is
37
+ definition : (let d = deadline 3s in always within 2 s, now < d end let) = true end definition
38
+ end law
39
39
 
40
40
  -- A performance budget, measured on the real clock.
41
- law `a deadline is quick to make` is
42
- definition is `for all` (wait :: Duration where wait <= 1h) . deadline wait takes at most 500 ms end
43
- example `an hour` is
41
+ law "|a deadline is quick to make| is
42
+ definition : for all (wait :: Duration where wait <= 1h) => deadline wait takes at most 500 ms end for all end definition
43
+ example "|an hour| :
44
44
  wait = 1h
45
45
  expect deadline wait takes at most 500 ms
46
- end
47
- end
46
+ end example
47
+ end law
48
48
 
49
49
  -- A native adapter gets its handlers as arguments: here the real clock.
50
50
  elapsed :: Int32 -> Duration uses Clock
51
51
 
52
- law `elapsed time is never negative` is
53
- definition is `for all` (n :: Int32 where n >= 0 && n <= 3) . elapsed n >= 0ms end
54
- end
52
+ law "|elapsed time is never negative| is
53
+ definition : for all (n :: Int32 where n >= 0 && n <= 3) => elapsed n >= 0ms end for all end definition
54
+ end law
55
55
 
56
56
  -- Random draws are reproducible: the same seed gives the same draws, on
57
57
  -- every target.
58
- definition roll (sides :: Int64 where sides >= 1) :: Integer uses Random is randomBelow sides + 1 end
59
-
60
- law `a seed fixes the rolls` is
61
- definition is
62
- `for all` (sides :: Int64 where sides >= 1) .
63
- handle roll sides with seededRandom end = handle roll sides with seededRandom end
64
- end
65
- example `the first roll from seed 0` is
58
+ procedure roll (sides :: Int64 where sides >= 1) -> Integer uses Random is randomBelow sides + 1 end procedure
59
+
60
+ law "|a seed fixes the rolls| is
61
+ definition :
62
+ for all (sides :: Int64 where sides >= 1) =>
63
+ handle roll sides with seededRandom end handle = handle roll sides with seededRandom end handle end for all
64
+ end definition
65
+ example "|the first roll from seed 0| :
66
66
  sides = 6
67
- expect handle roll sides with seededRandom end = 6
68
- end
69
- example `the first roll from seed 42` is
67
+ expect handle roll sides with seededRandom end handle = 6
68
+ end example
69
+ example "|the first roll from seed 42| :
70
70
  sides = 1000
71
- expect handle roll sides with seededRandom 42 end = 78
72
- end
73
- end
71
+ expect handle roll sides with seededRandom 42 end handle = 78
72
+ end example
73
+ end law
74
74
 
75
- law `rolls are on the die` using seeded random 42 is
76
- definition is
77
- `for all` (sides :: Int64 where sides >= 1) . (let r = roll sides in r >= 1 && r <= sides) = true
78
- end
79
- end
75
+ law "|rolls are on the die| using seeded random 42 is
76
+ definition :
77
+ for all (sides :: Int64 where sides >= 1) => (let r = roll sides in r >= 1 && r <= sides end let) = true end for all
78
+ end definition
79
+ end law
80
80
 
81
81
  -- Secure draws come from SecureRandom, which a seeded source cannot answer.
82
82
  token :: Int32 -> Bytes uses SecureRandom
83
83
 
84
- law `a token has the length asked` is
85
- definition is `for all` (n :: Int32 where n >= 0 && n <= 64) . prelude.length (token n) = n end
86
- end
84
+ law "|a token has the length asked| is
85
+ definition : for all (n :: Int32 where n >= 0 && n <= 64) => prelude.length (token n) = n end for all end definition
86
+ end law
87
87
 
88
88
  -- Files, the environment and ports.
89
- definition remember (note :: Bytes) :: Maybe Bytes uses FileSystem is
90
- writeBytes ".lawspec-note" note; let read = readBytes ".lawspec-note" in removePath ".lawspec-note"; read
91
- end
89
+ procedure remember (note :: Bytes) -> Maybe Bytes uses FileSystem is
90
+ writeBytes ".lawspec-note" note; let read = readBytes ".lawspec-note" in removePath ".lawspec-note"; read end let
91
+ end procedure
92
92
 
93
- law `a remembered note reads back` is
94
- definition is `for all` (note :: Bytes) . remember note = Just note end
95
- end
93
+ law "|a remembered note reads back| is
94
+ definition : for all (note :: Bytes) => remember note = Just note end for all end definition
95
+ end law
96
96
 
97
- law `an empty environment has no variables` using emptyEnvironment is
98
- definition is `for all` (name :: Text) . environmentVariable name = Nothing end
99
- end
97
+ law "|an empty environment has no variables| using emptyEnvironment is
98
+ definition : for all (name :: Text) => environmentVariable name = Nothing end for all end definition
99
+ end law
100
100
 
101
101
  listening :: Int32 -> Bool uses Ports
102
102
 
103
- law `a free port can be listened on` is
104
- definition is `for all` (n :: Int32 where n >= 0 && n <= 2) . listening n = true end
105
- end
103
+ law "|a free port can be listened on| is
104
+ definition : for all (n :: Int32 where n >= 0 && n <= 2) => listening n = true end for all end definition
105
+ end law
106
106
 
107
107
  -- Logs. A law that inspects what was logged records the log.
108
108
  charge :: Int32 -> Bool uses Log
109
109
 
110
- law `a charge is logged once` using recording Log is
111
- definition is
112
- `for all` (cents :: Int32 where cents >= 0 && cents <= 1000) .
113
- (if charge cents then calls of logMessage == 1 else calls of logMessage == 0) = true
114
- end
115
- end
110
+ law "|a charge is logged once| using recording Log is
111
+ definition :
112
+ for all (cents :: Int32 where cents >= 0 && cents <= 1000) =>
113
+ (if charge cents then calls of logMessage = 1 else calls of logMessage = 0 end if) = true end for all
114
+ end definition
115
+ end law
116
116
 
117
- definition traced (n :: Int32) :: Int32 uses Trace, Async is traceEvent "traced"; pause; n end
117
+ procedure traced (n :: Int32) -> Int32 uses Trace, Async is traceEvent "traced"; pause; n end procedure
118
118
 
119
- law `tracing keeps the value` is
120
- definition is `for all` (n :: Int32) . traced n = n end
121
- end
119
+ law "|tracing keeps the value| is
120
+ definition : for all (n :: Int32) => traced n = n end for all end definition
121
+ end law
122
+ end unit
@@ -2,23 +2,24 @@ unit example.canonical_url
2
2
 
3
3
  canonicalize :: Text -> Text
4
4
 
5
- law `canonicalization reaches a fixed point` is
6
- definition is
7
- `idempotent` canonicalize
8
- end
9
- description is
5
+ law "|canonicalization reaches a fixed point| is
6
+ definition :
7
+ apply law "|idempotent| canonicalize
8
+ end definition
9
+ description :
10
10
  "{canonicalize} removes trailing slashes until the result is stable"
11
- end
12
- example `all trailing slashes are removed in one pass` is
11
+ end description
12
+ example "|all trailing slashes are removed in one pass| :
13
13
  x = "https://example.com/path///"
14
14
  expect canonicalize x = "https://example.com/path"
15
- end
16
- example `a URL without trailing slashes stays unchanged` is
15
+ end example
16
+ example "|a URL without trailing slashes stays unchanged| :
17
17
  x = "https://example.com/path"
18
18
  expect canonicalize x = "https://example.com/path"
19
- end
20
- example `empty input remains empty` is
19
+ end example
20
+ example "|empty input remains empty| :
21
21
  x = ""
22
22
  expect canonicalize x = ""
23
- end
24
- end
23
+ end example
24
+ end law
25
+ end unit
@@ -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 -> Set Int32
9
+ dedupe :: List Int32 -> SortedSet Int32
10
10
  -- How many times each word occurs.
11
- wordCounts :: List Text -> KeyVal Text BigInt
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) -> Set (List Int8)
20
-
21
- definition countWords (words :: List Text) (counts :: KeyVal Text BigInt) :: KeyVal Text BigInt is
22
- match words with
23
- | Nil -> counts
24
- | Cons w rest -> match prelude.lookup w counts with
25
- | Nothing -> countWords rest (prelude.put w 1 counts)
26
- | Just n -> countWords rest (prelude.put w ((n + 1) :: BigInt) counts)
27
- end
28
- end
29
- end
30
-
31
- definition rotated (d :: Deque Int8) :: Deque Int8 is
32
- match prelude.peekFront d with
33
- | Nothing -> d
34
- | Just x -> prelude.pushBack x (prelude.popFront d)
35
- end
36
- end
37
-
38
- law `dedupe agrees with setOf` is
39
- definition is `for all` (xs :: List Int32) . dedupe xs = prelude.setOf xs end
40
- end
41
-
42
- law `distinct rows agree with setOf` is
43
- definition is `for all` (rows :: List (List Int8)) . distinctRows rows = prelude.setOf rows end
44
- end
45
-
46
- law `word counts agree with the definition` is
47
- definition is
48
- `for all` (words :: List Text) . wordCounts words = countWords words (prelude.keyValOf [])
49
- end
50
- end
51
-
52
- law `the queue's front is the first value` is
53
- definition is
54
- `for all` (x :: Int8) (xs :: List Int8) . prelude.front (fifo (Cons x xs)) = Just x
55
- end
56
- end
57
-
58
- law `the stack's top is the last value` is
59
- definition is
60
- `for all` (x :: Int8) (xs :: List Int8) . prelude.peek (lifo [x]) = Just x
61
- end
62
- end
63
-
64
- law `fifo keeps the order` is
65
- definition is `for all` (xs :: List Int8) . prelude.toList (fifo xs) = xs end
66
- end
67
-
68
- law `rotate agrees with the definition` is
69
- definition is `for all` (d :: Deque Int8) . rotate d = rotated d end
70
- end
71
-
72
- law `union is commutative` is
73
- definition is
74
- `for all` (s :: Set Int32) (t :: Set Int32) . prelude.union s t = prelude.union t s
75
- end
76
- end
77
-
78
- law `a removed value is no member` is
79
- definition is
80
- `for all` (x :: Int32) (s :: Set Int32) . prelude.member x (prelude.remove x s) = false
81
- end
82
- end
83
-
84
- law `an inserted value is a member` is
85
- definition is
86
- `for all` (x :: Int32) (s :: Set Int32) . prelude.member x (prelude.insert x s) = true
87
- end
88
- end
89
-
90
- law `intersection and difference split a set` is
91
- definition is
92
- `for all` (s :: Set Int32) (t :: Set Int32) .
93
- prelude.union (prelude.intersection s t) (prelude.difference s t) = s
94
- end
95
- end
96
-
97
- law `the latest put wins` is
98
- definition is
99
- `for all` (k :: Text) (v :: BigInt) (w :: BigInt) (m :: KeyVal Text BigInt) .
100
- prelude.lookup k (prelude.put k w (prelude.put k v m)) = Just w
101
- end
102
- end
103
-
104
- law `a deleted key is absent` is
105
- definition is
106
- `for all` (k :: Text) (m :: KeyVal Text BigInt) . prelude.lookup k (prelude.delete k m) = Nothing
107
- end
108
- end
109
-
110
- law `keys are distinct` is
111
- definition is
112
- `for all` (m :: KeyVal Text BigInt) . prelude.size (prelude.setOf (prelude.keys m)) = prelude.size m
113
- end
114
- end
115
-
116
- law `popping the back leaves the rest` is
117
- definition is
118
- `for all` (x :: Int8) (d :: Deque Int8) . prelude.popBack (prelude.pushBack x d) = d
119
- end
120
- end
121
-
122
- law `the empty stack stays empty` is
123
- definition is
124
- `for all` (x :: Int8) . prelude.isEmpty (prelude.pop (prelude.stackOf [x])) = true
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 `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
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
- definition orderedItems (s :: Set Text) :: List Text is
154
- match s with
155
- | SetItems xs -> xs
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
- definition countedKeys (words :: List Text) :: List Text is
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 `text keys and items order by code point` is
165
- definition is
166
- `for all` (words :: List Text) . countedKeys words = orderedItems (prelude.setOf words)
167
- end
168
- example `a fullwidth letter precedes an emoji` is
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.setOf words) = ["A", "😀"]
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
- definition compareSets (s :: Set Text) (t :: Set Text) :: Ordering is
174
+ function compareSets (s :: SortedSet Text) (t :: SortedSet Text) -> Ordering is
177
175
  prelude.compare s t
178
- end
179
-
180
- law `sets of text order as their items do` is
181
- definition is
182
- `for all` (xs :: List Text) (ys :: List Text) .
183
- compareSets (prelude.setOf xs) (prelude.setOf ys) = prelude.compare (orderedItems (prelude.setOf xs)) (orderedItems (prelude.setOf ys))
184
- end
185
- example `a set led by a fullwidth letter precedes one led by a later letter` is
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.setOf xs) (prelude.setOf ys) = Less
189
- end
190
- end
186
+ expect compareSets (prelude.sortedSetOf xs) (prelude.sortedSetOf ys) = Less
187
+ end example
188
+ end law
189
+ end unit