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
@@ -5,187 +5,188 @@ import example.records.inner as inner
5
5
  -- Records: a type with one constructor has fields that code reads by name
6
6
  -- (account.balance) and replaces by name (account with balance = 0).
7
7
 
8
- type Account is
9
- Account
8
+ type Account is a record{
9
+
10
10
  owner :: Text
11
11
  balance :: Int64
12
- holds :: List Int64
13
- end
12
+ holds :: List Int64}
13
+ end type
14
+
15
+ type Ledger is a record{
14
16
 
15
- type Ledger is
16
- Ledger
17
17
  account :: Account
18
- entries :: Int32
19
- end
18
+ entries :: Int32}
19
+ end type
20
20
 
21
- definition deposit (a :: Account) (amount :: Int64 where amount >= 0 && amount <= 1000000) :: Account is
22
- if a.balance >= 0 && a.balance <= 1000000000 then a with balance = prelude.Int64 (a.balance + amount) else a
23
- end
21
+ function deposit (a :: Account) (amount :: Int64 where amount >= 0 && amount <= 1000000) -> Account is
22
+ if a.balance >= 0 && a.balance <= 1000000000 then a with balance = prelude.Int64 (a.balance + amount) else a end if
23
+ end function
24
24
 
25
- definition rename (l :: Ledger) (name :: Text) :: Ledger is
25
+ function rename (l :: Ledger) (name :: Text) -> Ledger is
26
26
  l with account = (l.account with owner = name), entries = 0
27
- end
28
-
29
- law `a deposit adds to the balance` is
30
- definition is
31
- `for all` (owner :: Text) (start :: Int64 where start >= 0 && start <= 1000000000) (amount :: Int64 where amount >= 0 && amount <= 1000000) .
32
- (deposit (Account owner start []) amount).balance = start + amount
33
- end
34
- end
35
-
36
- law `a deposit keeps the owner and holds` is
37
- definition is
38
- `for all` (a :: Account) (amount :: Int64 where amount >= 0 && amount <= 1000000) .
39
- (deposit a amount).owner = a.owner and (deposit a amount).holds = a.holds
40
- end
41
- end
42
-
43
- law `renaming reads back` is
44
- definition is
45
- `for all` (l :: Ledger) (name :: Text) . (rename l name).account.owner = name and (rename l name).entries = 0
46
- end
47
- end
27
+ end function
28
+
29
+ law "|a deposit adds to the balance| is
30
+ definition :
31
+ for all (owner :: Text) (start :: Int64 where start >= 0 && start <= 1000000000) (amount :: Int64 where amount >= 0 && amount <= 1000000) =>
32
+ (deposit (Account record{ owner = owner , balance = start , holds = []}) amount).balance = start + amount end for all
33
+ end definition
34
+ end law
35
+
36
+ law "|a deposit keeps the owner and holds| is
37
+ definition :
38
+ for all (a :: Account) (amount :: Int64 where amount >= 0 && amount <= 1000000) =>
39
+ (deposit a amount).owner = a.owner and (deposit a amount).holds = a.holds end for all
40
+ end definition
41
+ end law
42
+
43
+ law "|renaming reads back| is
44
+ definition :
45
+ for all (l :: Ledger) (name :: Text) => (rename l name).account.owner = name and (rename l name).entries = 0 end for all
46
+ end definition
47
+ end law
48
48
 
49
49
  -- A definition may fail without any ability, and another may attempt it.
50
- type Refusal is | Overdrawn | Unfunded end
50
+ type Refusal is one of | Overdrawn | Unfunded end type
51
51
 
52
- definition withdraw (a :: Account) (amount :: Int64) :: Account fails with Refusal is
52
+ procedure withdraw (a :: Account) (amount :: Int64) -> Account fails with Refusal is
53
53
  if amount < 0 then raise Unfunded else
54
- if amount > a.balance then raise Overdrawn else a with balance = prelude.Int64 (a.balance - amount)
55
- end
56
-
57
- definition settled (a :: Account) (amount :: Int64) :: Either Refusal Int64 is
58
- match prelude.attempt (withdraw a amount) with
59
- | Left refusal -> Left refusal
60
- | Right after -> Right after.balance
61
- end
62
- end
63
-
64
- law `a withdrawal within the balance leaves the rest` is
65
- definition is
66
- `for all` (owner :: Text) (start :: Int64 where start >= 0) (amount :: Int64 where amount >= 0 && amount <= start) .
67
- settled (Account owner start []) amount = Right (prelude.Int64 (start - amount))
68
- end
69
- end
70
-
71
- law `a withdrawal beyond the balance is refused` is
72
- definition is
73
- `for all` (owner :: Text) (start :: Int64 where start >= 0 && start < 1000) (amount :: Int64 where amount >= 1000) .
74
- settled (Account owner start []) amount = Left Overdrawn and settled (Account owner start []) (0 - amount) = Left Unfunded
75
- end
76
- end
54
+ if amount > a.balance then raise Overdrawn else a with balance = prelude.Int64 (a.balance - amount) end if end if
55
+ end procedure
56
+
57
+ function settled (a :: Account) (amount :: Int64) -> Either Refusal Int64 is
58
+ first matching case of prelude.attempt (withdraw a amount) :
59
+ when Left refusal then Left refusal
60
+ when Right after then Right after.balance
61
+ end case
62
+ end function
63
+
64
+ law "|a withdrawal within the balance leaves the rest| is
65
+ definition :
66
+ for all (owner :: Text) (start :: Int64 where start >= 0) (amount :: Int64 where amount >= 0 && amount <= start) =>
67
+ settled (Account record{ owner = owner , balance = start , holds = []}) amount = Right (prelude.Int64 (start - amount)) end for all
68
+ end definition
69
+ end law
70
+
71
+ law "|a withdrawal beyond the balance is refused| is
72
+ definition :
73
+ for all (owner :: Text) (start :: Int64 where start >= 0 && start < 1000) (amount :: Int64 where amount >= 1000) =>
74
+ settled (Account record{ owner = owner , balance = start , holds = []}) amount = Left Overdrawn and settled (Account record{ owner = owner , balance = start , holds = []}) (0 - amount) = Left Unfunded end for all
75
+ end definition
76
+ end law
77
77
 
78
78
  -- A refinement through a definition still guides generation: sample
79
79
  -- names come from the pattern, so the law does not starve its filter.
80
- definition validOwner (name :: Text) :: Bool is
81
- prelude.length name == 9 && name matches regex "acct-[0-9]{4}"
82
- end
80
+ function validOwner (name :: Text) -> Bool is
81
+ prelude.length name = 9 && name matches regex "acct-[0-9]{4}"
82
+ end function
83
83
 
84
- law `a renamed ledger keeps a valid owner` is
85
- definition is
86
- `for all` (l :: Ledger) (name :: Text where validOwner name) . validOwner (rename l name).account.owner
87
- end
88
- end
84
+ law "|a renamed ledger keeps a valid owner| is
85
+ definition :
86
+ for all (l :: Ledger) (name :: Text where validOwner name) => validOwner (rename l name).account.owner end for all
87
+ end definition
88
+ end law
89
89
 
90
90
  -- A literal takes its type from where it is used: 0 is an Int64 here.
91
- definition opening (a :: Maybe Account) :: Int64 is
92
- let start = (match a with | Nothing -> 0 | Just account -> account.balance end) in
93
- if start < 0 then 0 else start
94
- end
91
+ function opening (a :: Maybe Account) -> Int64 is
92
+ let start = (first matching case of a : when Nothing then 0 when Just account then account.balance end case) in
93
+ if start < 0 then 0 else start end if end let
94
+ end function
95
95
 
96
- law `an absent account opens at zero` is
97
- definition is opening Nothing = 0 end
98
- end
96
+ law "|an absent account opens at zero| is
97
+ definition : opening Nothing = 0 end definition
98
+ end law
99
99
 
100
100
  -- Recursion on an integer that moves toward a bound is total.
101
- definition countdown (n :: Natural) :: List Natural is
102
- if n == 0 then [] else Cons n (countdown (n - 1))
103
- end
101
+ function countdown (n :: Whole) -> List Whole is
102
+ if n = 0 then [] else Cons n (countdown (n - 1)) end if
103
+ end function
104
104
 
105
- definition evensBelow (i :: Int32) (n :: Int32) :: List Int32 is
105
+ function evensBelow (i :: Int32) (n :: Int32) -> List Int32 is
106
106
  if i >= n || i < 0 then [] else
107
- (if prelude.rem i 2 == 0 then Cons i (evensBelow (prelude.Int32 (i + 1)) n) else evensBelow (prelude.Int32 (i + 1)) n)
108
- end
109
-
110
- law `a countdown has n items` is
111
- definition is `for all` (n :: Natural where n <= 50) . prelude.length (countdown n) = n end
112
- end
113
-
114
- law `the evens below n are half of the range` is
115
- definition is
116
- `for all` (n :: Int32 where n >= 0 && n <= 40) .
117
- prelude.length (evensBelow 0 n) = prelude.quot (n + 1) 2
118
- end
119
- end
120
-
121
- law `range counts from the first up to the last` is
122
- definition is
123
- `for all` (n :: Int32 where n >= 0 && n <= 40) . prelude.length (prelude.range 0 n) = n
124
- end
125
- example `three` is
107
+ (if prelude.rem i 2 = 0 then Cons i (evensBelow (prelude.Int32 (i + 1)) n) else evensBelow (prelude.Int32 (i + 1)) n end if) end if
108
+ end function
109
+
110
+ law "|a countdown has n items| is
111
+ definition : for all (n :: Whole where n <= 50) => prelude.length (countdown n) = n end for all end definition
112
+ end law
113
+
114
+ law "|the evens below n are half of the range| is
115
+ definition :
116
+ for all (n :: Int32 where n >= 0 && n <= 40) =>
117
+ prelude.length (evensBelow 0 n) = prelude.quot (n + 1) 2 end for all
118
+ end definition
119
+ end law
120
+
121
+ law "|range counts from the first up to the last| is
122
+ definition :
123
+ for all (n :: Int32 where n >= 0 && n <= 40) => prelude.length (prelude.range 0 n) = n end for all
124
+ end definition
125
+ example "|three| :
126
126
  n = 3
127
127
  expect prelude.range 0 n = [0, 1, 2]
128
- end
129
- end
128
+ end example
129
+ end law
130
130
 
131
131
  -- The prover keeps what guards say about calls, lengths and remainders.
132
- definition clip (xs :: List Int32) :: Int32 is
133
- if prelude.length xs <= 100 then prelude.Int32 (prelude.length xs) else 100
134
- end
132
+ function clip (xs :: List Int32) -> Int32 is
133
+ if prelude.length xs <= 100 then prelude.Int32 (prelude.length xs) else 100 end if
134
+ end function
135
135
 
136
- definition owner (p :: Int32) (k :: Int32) :: Int32 is
137
- if k >= 1 && p >= 0 then prelude.Int32 (prelude.rem p k) else 0
138
- end
136
+ function owner (p :: Int32) (k :: Int32) -> Int32 is
137
+ if k >= 1 && p >= 0 then prelude.Int32 (prelude.rem p k) else 0 end if
138
+ end function
139
139
 
140
- law `a clipped length is at most 100` is
141
- definition is `for all` (xs :: List Int32) . (clip xs <= 100 && clip xs >= 0) = true end
142
- end
140
+ law "|a clipped length is at most 100| is
141
+ definition : for all (xs :: List Int32) => (clip xs <= 100 && clip xs >= 0) = true end for all end definition
142
+ end law
143
143
 
144
- law `an owner is one of the k` is
145
- definition is
146
- `for all` (p :: Int32 where p >= 0) (k :: Int32 where k >= 1 && k <= 64) . (owner p k >= 0 && owner p k < k) = true
147
- end
148
- end
144
+ law "|an owner is one of the k| is
145
+ definition :
146
+ for all (p :: Int32 where p >= 0) (k :: Int32 where k >= 1 && k <= 64) => (owner p k >= 0 && owner p k < k) = true end for all
147
+ end definition
148
+ end law
149
149
 
150
150
  -- Arithmetic into a bounded type, by name.
151
- definition nextEpoch (e :: Int16) :: Int16 is prelude.wrapping (e + 1) end
152
- definition topUp (level :: UInt8) :: UInt8 is prelude.saturating (level + 10) end
153
- definition total (a :: Int32) (b :: Int32) :: Maybe Int32 is prelude.bounded (a + b) end
154
-
155
- law `epochs wrap around` is
156
- definition is
157
- `for all` (e :: Int16) . nextEpoch e = (if e == Int16.max then Int16.min else prelude.Int16 (e + 1))
158
- end
159
- end
160
-
161
- law `a top up stops at the top` is
162
- definition is
163
- `for all` (level :: UInt8) . topUp level = (if level >= 245 then 255 else prelude.UInt8 (level + 10))
164
- end
165
- end
166
-
167
- law `a total that overflows is Nothing` is
168
- definition is
169
- `for all` (a :: Int32) (b :: Int32) .
170
- total a b = (if a + b > Int32.max || a + b < Int32.min then Nothing else Just (prelude.Int32 (a + b)))
171
- end
172
- end
151
+ function nextEpoch (e :: Int16) -> Int16 is prelude.wrapping (e + 1) end function
152
+ function topUp (level :: UInt8) -> UInt8 is prelude.saturating (level + 10) end function
153
+ function total (a :: Int32) (b :: Int32) -> Maybe Int32 is prelude.bounded (a + b) end function
154
+
155
+ law "|epochs wrap around| is
156
+ definition :
157
+ for all (e :: Int16) => nextEpoch e = (if e = Int16.max then Int16.min else prelude.Int16 (e + 1) end if) end for all
158
+ end definition
159
+ end law
160
+
161
+ law "|a top up stops at the top| is
162
+ definition :
163
+ for all (level :: UInt8) => topUp level = (if level >= 245 then 255 else prelude.UInt8 (level + 10) end if) end for all
164
+ end definition
165
+ end law
166
+
167
+ law "|a total that overflows is Nothing| is
168
+ definition :
169
+ for all (a :: Int32) (b :: Int32) =>
170
+ total a b = (if a + b > Int32.max || a + b < Int32.min then Nothing else Just (prelude.Int32 (a + b)) end if) end for all
171
+ end definition
172
+ end law
173
173
 
174
174
  -- A refinement shapes generation: texts a regex must match are drawn from
175
175
  -- the regex, through the definition that says so, not filtered from any
176
176
  -- text.
177
- definition validTopic (name :: Text) :: Bool is
177
+ function validTopic (name :: Text) -> Bool is
178
178
  let n = prelude.length name in
179
- n >= 1 && n <= 249 && name != "." && name != ".." && name matches regex "[a-zA-Z0-9._-]+"
180
- end
181
-
182
- law `a valid topic name keeps to its characters` is
183
- definition is
184
- `for all` (t :: Text where validTopic t) (n :: Int32 where n >= 1 && n <= 64) .
185
- (prelude.length t >= 1 && prelude.length t <= 249 && n <= 64) = true
186
- end
187
- end
188
-
189
- law `a unit may sit above its package's other units` is
190
- definition is `for all` (x :: Int32) . inner.doubled x = x + x end
191
- end
179
+ n >= 1 && n <= 249 && name != "." && name != ".." && name matches regex "[a-zA-Z0-9._-]+" end let
180
+ end function
181
+
182
+ law "|a valid topic name keeps to its characters| is
183
+ definition :
184
+ for all (t :: Text where validTopic t) (n :: Int32 where n >= 1 && n <= 64) =>
185
+ (prelude.length t >= 1 && prelude.length t <= 249 && n <= 64) = true end for all
186
+ end definition
187
+ end law
188
+
189
+ law "|a unit may sit above its package's other units| is
190
+ definition : for all (x :: Int32) => inner.doubled x = x + x end for all end definition
191
+ end law
192
+ end unit
@@ -3,8 +3,9 @@ unit example.records.inner
3
3
  -- A unit inside another unit's name: on Python, example.records is then its
4
4
  -- package's __init__.py, and this unit is example/records/inner.py.
5
5
 
6
- definition doubled (x :: Int32) :: Integer is x * 2 end
6
+ function doubled (x :: Int32) -> Integer is x * 2 end function
7
7
 
8
- law `doubling is even` is
9
- definition is `for all` (x :: Int32) . prelude.rem (doubled x) 2 = 0 end
10
- end
8
+ law "|doubling is even| is
9
+ definition : for all (x :: Int32) => prelude.rem (doubled x) 2 = 0 end for all end definition
10
+ end law
11
+ end unit
@@ -2,40 +2,41 @@ unit example.recursive_refinements
2
2
 
3
3
  -- Refining a type argument follows its stored occurrences through recursion.
4
4
  -- The fixed Int8 field remains independent of the refined argument.
5
- type Tree (a :: Type) is
6
- Leaf value :: a fixed :: Int8
7
- Fork left :: Tree a right :: Tree a
8
- end
5
+ type Tree (a :: Type) is one of
6
+ Leaf of type record{ value :: a fixed :: Int8}
7
+ Fork of type record{ left :: Tree a right :: Tree a}
8
+ end type
9
9
 
10
- refinement Positive is (value :: Int8 where value > 0) end
10
+ refinement Positive is (value :: Int8 where value > 0) end refinement
11
11
 
12
12
  -- The payload guarantee is available when proving recursive definitions.
13
- definition positiveSum (tree :: Tree Positive) :: (total :: BigInt where total > 0) is
14
- match tree with
15
- | Leaf value fixed -> value + 0
16
- | Fork left right -> positiveSum left + positiveSum right
17
- end
18
- end
13
+ function positiveSum (tree :: Tree Positive) -> (total :: Integer where total > 0) is
14
+ first matching case of tree :
15
+ when Leaf record{ value = value , fixed = fixed} then value + 0
16
+ when Fork record{ left = left , right = right} then positiveSum left + positiveSum right
17
+ end case
18
+ end function
19
19
 
20
- law `positive leaves have a positive total` is
21
- definition is
22
- `for all` (tree :: Tree Positive) . positiveSum tree > 0
23
- end
24
- example `fixed fields may be negative` is
25
- tree = Fork (Leaf 1 -128) (Fork (Leaf 2 0) (Leaf 3 -1))
20
+ law "|positive leaves have a positive total| is
21
+ definition :
22
+ for all (tree :: Tree Positive) => positiveSum tree > 0 end for all
23
+ end definition
24
+ example "|fixed fields may be negative| :
25
+ tree = Fork record{ left = (Leaf record{ value = 1 , fixed = -128}) , right = (Fork record{ left = (Leaf record{ value = 2 , fixed = 0}) , right = (Leaf record{ value = 3 , fixed = -1})})}
26
26
  expect positiveSum tree = 6
27
- end
28
- end
27
+ end example
28
+ end law
29
29
 
30
30
  -- Dependent predicates retain the enclosing input, even below several forks.
31
- law `every stored argument exceeds the threshold` is
32
- definition is
33
- `for all` (threshold :: Int8 where threshold < 127)
34
- (tree :: Tree (value :: Int8 where value > threshold)) . tree = tree
35
- end
36
- example `nested threshold` is
31
+ law "|every stored argument exceeds the threshold| is
32
+ definition :
33
+ for all (threshold :: Int8 where threshold < 127)
34
+ (tree :: Tree (value :: Int8 where value > threshold)) => tree = tree end for all
35
+ end definition
36
+ example "|nested threshold| :
37
37
  threshold = 4
38
- tree = Fork (Leaf 5 -128) (Leaf 6 0)
39
- expect tree = Fork (Leaf 5 -128) (Leaf 6 0)
40
- end
41
- end
38
+ tree = Fork record{ left = (Leaf record{ value = 5 , fixed = -128}) , right = (Leaf record{ value = 6 , fixed = 0})}
39
+ expect tree = Fork record{ left = (Leaf record{ value = 5 , fixed = -128}) , right = (Leaf record{ value = 6 , fixed = 0})}
40
+ end example
41
+ end law
42
+ end unit
@@ -2,43 +2,44 @@ unit example.refined_definitions
2
2
 
3
3
  refinement Nonzero (T :: Type) requires Integer T is
4
4
  (value :: T where value != 0)
5
- end
5
+ end refinement
6
6
 
7
7
  -- The compiler proves division is defined under the input contract.
8
8
  -- Native callers are checked before the body runs.
9
- definition reciprocal (x :: Nonzero a) :: Rational
9
+ function reciprocal (x :: Nonzero a) -> Rational
10
10
  requires Integer a
11
11
  is
12
12
  1 / x
13
- end
13
+ end function
14
14
 
15
15
  -- Arithmetic promotes before this explicit, proved narrowing conversion.
16
- definition increment (x :: Int8 where x < 127)
17
- :: (result :: Int8 where result > x)
16
+ function increment (x :: Int8 where x < 127)
17
+ -> (result :: Int8 where result > x)
18
18
  is
19
19
  prelude.Int8 (x + 1)
20
- end
20
+ end function
21
21
 
22
22
  -- Later argument refinements can depend on earlier arguments.
23
- definition inverseGap (x :: Int8) (y :: Int8 where y > x) :: Rational is
23
+ function inverseGap (x :: Int8) (y :: Int8 where y > x) -> Rational is
24
24
  1 / (y - x)
25
- end
25
+ end function
26
26
 
27
- law `exact reciprocal` is
28
- definition is `for all` (x :: Nonzero Int8) . reciprocal x = 1 / x end
29
- example `one half` is x = 2 expect reciprocal x = rational(1, 2) end
30
- end
27
+ law "|exact reciprocal| is
28
+ definition : for all (x :: Nonzero Int8) => reciprocal x = 1 / x end for all end definition
29
+ example "|one half| : x = 2 expect reciprocal x = rational(1, 2) end example
30
+ end law
31
31
 
32
- law `checked increment` is
33
- definition is `for all` (x :: Int8 where x < 127) . increment x = x + 1 end
34
- example `largest admitted input` is x = 126 expect increment x = 127 end
35
- end
32
+ law "|checked increment| is
33
+ definition : for all (x :: Int8 where x < 127) => increment x = x + 1 end for all end definition
34
+ example "|largest admitted input| : x = 126 expect increment x = 127 end example
35
+ end law
36
36
 
37
- law `dependent gap` is
38
- definition is `for all` (x :: Int8) (y :: Int8 where y > x) .
39
- inverseGap x y = 1 / (y - x)
40
- end
41
- example `difference exceeds Int8` is
37
+ law "|dependent gap| is
38
+ definition : for all (x :: Int8) (y :: Int8 where y > x) =>
39
+ inverseGap x y = 1 / (y - x) end for all
40
+ end definition
41
+ example "|difference exceeds Int8| :
42
42
  x = -128 y = 127 expect inverseGap x y = rational(1, 255)
43
- end
44
- end
43
+ end example
44
+ end law
45
+ end unit
@@ -3,84 +3,85 @@ unit example.refinements
3
3
  refinement Between (T :: Type) (minimum :: T) (maximum :: T)
4
4
  requires Ordered T is
5
5
  (value :: T where minimum <= value && value <= maximum)
6
- end
6
+ end refinement
7
7
 
8
8
  refinement Positive (T :: Type) requires Integer T is
9
9
  (value :: T where value > 0)
10
- end
10
+ end refinement
11
11
 
12
12
  refinement AdditionOverflows (T :: Type) (left :: T)
13
13
  requires Integer T Bounded T is
14
14
  (right :: T where left + right > T.max)
15
- end
15
+ end refinement
16
16
 
17
17
  add :: (x :: Int8)
18
18
  -> (y :: AdditionOverflows Int8 x)
19
- -> (result :: Integer where result == x + y)
20
- successor :: (x :: Int8) -> (result :: Integer where result == x + 1)
19
+ -> (result :: Integer where result = x + y)
20
+ successor :: (x :: Int8) -> (result :: Integer where result = x + 1)
21
21
  count :: (text :: Text where prelude.length text > 0)
22
- -> (result :: Integer where result == prelude.length text)
23
- preserve :: (x :: UInt64) -> (result :: Integer where result == x)
24
- positive :: (x :: Positive Int8) -> (result :: Positive Int8 where result == x)
22
+ -> (result :: Integer where result = prelude.length text)
23
+ preserve :: (x :: UInt64) -> (result :: Integer where result = x)
24
+ positive :: (x :: Positive Int8) -> (result :: Positive Int8 where result = x)
25
25
 
26
- law `maximum input` is
27
- definition is `for all` (x :: Int8) . successor x = x + 1 end
28
- example `beyond Int8` is x = 127 expect successor x = 128 end
29
- end
26
+ law "|maximum input| is
27
+ definition : for all (x :: Int8) => successor x = x + 1 end for all end definition
28
+ example "|beyond Int8| : x = 127 expect successor x = 128 end example
29
+ end law
30
30
 
31
- law `overflow pairs` is
32
- definition is
33
- `for all` (x :: Int8) (y :: AdditionOverflows Int8 x) .
34
- add x y = x + y
35
- end
36
- example `smallest overflowing sum` is x = 127 y = 1 expect add x y = 128 end
37
- example `largest sum` is x = 127 y = 127 expect add x y = 254 end
38
- end
31
+ law "|overflow pairs| is
32
+ definition :
33
+ for all (x :: Int8) (y :: AdditionOverflows Int8 x) =>
34
+ add x y = x + y end for all
35
+ end definition
36
+ example "|smallest overflowing sum| : x = 127 y = 1 expect add x y = 128 end example
37
+ example "|largest sum| : x = 127 y = 127 expect add x y = 254 end example
38
+ end law
39
39
 
40
- law `bounded input` is
41
- definition is `for all` (x :: Between Int8 1 3) . positive x = x end
42
- example `middle` is x = 2 expect positive x = 2 end
43
- end
40
+ law "|bounded input| is
41
+ definition : for all (x :: Between Int8 1 3) => positive x = x end for all end definition
42
+ example "|middle| : x = 2 expect positive x = 2 end example
43
+ end law
44
44
 
45
- law `large integer` is
46
- definition is `for all` (x :: UInt64) . preserve x = x end
47
- example `maximum UInt64` is x = 18446744073709551615 expect preserve x = 18446744073709551615 end
48
- end
45
+ law "|large integer| is
46
+ definition : for all (x :: UInt64) => preserve x = x end for all end definition
47
+ example "|maximum UInt64| : x = 18446744073709551615 expect preserve x = 18446744073709551615 end example
48
+ end law
49
49
 
50
- abstractEcho :: (x :: Integer) -> (result :: Integer where result == x)
50
+ abstractEcho :: (x :: Integer) -> (result :: Integer where result = x)
51
51
 
52
- law `nested presence` is
53
- definition is `for all` (x :: Optional (Nullable (Positive Int8))) . x = x end
54
- end
52
+ law "|nested presence| is
53
+ definition : for all (x :: Optional (Nullable (Positive Int8))) => x = x end for all end definition
54
+ end law
55
55
 
56
- law `finite positive floats` is
57
- definition is
58
- `for all` (x :: Float32 where prelude.isFinite x && x > 0) .
59
- prelude.isFinite x
60
- end
61
- example `positive` is x = 0.5 expect x = 0.5 end
62
- end
56
+ law "|finite positive floats| is
57
+ definition :
58
+ for all (x :: Float32 where prelude.isFinite x && x > 0) =>
59
+ prelude.isFinite x end for all
60
+ end definition
61
+ example "|positive| : x = 0.5 expect x = 0.5 end example
62
+ end law
63
63
 
64
- law `raw byte length` is
65
- definition is `for all` (x :: Bytes where prelude.length x == 3) . prelude.length x = 3 end
66
- example `arbitrary octets` is x = bytes([0,128,255]) expect prelude.length x = 3 end
67
- end
64
+ law "|raw byte length| is
65
+ definition : for all (x :: Bytes where prelude.length x = 3) => prelude.length x = 3 end for all end definition
66
+ example "|arbitrary octets| : x = bytes([0,128,255]) expect prelude.length x = 3 end example
67
+ end law
68
68
 
69
- law `guarded predicate arithmetic` is
70
- definition is `for all` (x :: Int8) (y :: Int8 where x != 0 && y > 10 / x) . y > 10 / x end
71
- end
69
+ law "|guarded predicate arithmetic| is
70
+ definition : for all (x :: Int8) (y :: Int8 where x != 0 && y > 10 / x) => y > 10 / x end for all end definition
71
+ end law
72
72
 
73
- law `machine bound` is
74
- definition is `for all` (x :: IntSize where x == IntSize.max) . x = IntSize.max end
75
- end
73
+ law "|machine bound| is
74
+ definition : for all (x :: IntSize where x = IntSize.max) => x = IntSize.max end for all end definition
75
+ end law
76
76
 
77
- law `symbol fixture refinement` is
78
- definition is
79
- `for all` (x :: Symbol where x == symbol("selected", "fixture")) .
80
- x = symbol("selected", "fixture")
81
- end
82
- example `selected identity` is
77
+ law "|symbol fixture refinement| is
78
+ definition :
79
+ for all (x :: Symbol where x = symbol("selected", "fixture")) =>
80
+ x = symbol("selected", "fixture") end for all
81
+ end definition
82
+ example "|selected identity| :
83
83
  x = symbol("selected", "fixture")
84
84
  expect x = symbol("selected", "fixture")
85
- end
86
- end
85
+ end example
86
+ end law
87
+ end unit