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.
Files changed (148) hide show
  1. package/CHANGELOG.md +109 -1
  2. package/README.md +34 -26
  3. package/UNICODE-LICENSE.txt +39 -0
  4. package/api.mjs +6 -23
  5. package/bin/lawspec.mjs +148 -65
  6. package/build.json +245 -177
  7. package/builtin-data-names.mjs +73 -0
  8. package/core.wasm +2 -2
  9. package/core_jsffi.js +5 -0
  10. package/examples/native-payments/clojure/lawspec.json +13 -2
  11. package/examples/native-payments/csharp/lawspec.json +13 -2
  12. package/examples/native-payments/elixir/lawspec.json +14 -3
  13. package/examples/native-payments/erlang/lawspec.json +14 -3
  14. package/examples/native-payments/fsharp/lawspec.json +14 -3
  15. package/examples/native-payments/gleam/lawspec.json +14 -3
  16. package/examples/native-payments/go/lawspec.json +14 -3
  17. package/examples/native-payments/groovy/lawspec.json +14 -3
  18. package/examples/native-payments/haskell/lawspec.json +14 -3
  19. package/examples/native-payments/java/lawspec.json +14 -3
  20. package/examples/native-payments/javascript/lawspec.json +14 -3
  21. package/examples/native-payments/kotlin/lawspec.json +14 -3
  22. package/examples/native-payments/python/lawspec.json +14 -3
  23. package/examples/native-payments/rust/lawspec.json +14 -3
  24. package/examples/native-payments/scala/lawspec.json +14 -3
  25. package/examples/native-payments/swift/lawspec.json +14 -3
  26. package/examples/native-payments/typescript/lawspec.json +14 -3
  27. package/examples/packages/orders/lawspec.json +25 -5
  28. package/examples/packages/orders/orders.lawspec +57 -53
  29. package/examples/packages/shop-domain/src/domain.lawspec +36 -33
  30. package/examples/packages/shop-tax-1/src/api.lawspec +1 -0
  31. package/examples/packages/shop-tax-1/src/rates.lawspec +5 -4
  32. package/examples/packages/shop-tax-2/src/api.lawspec +1 -0
  33. package/examples/packages/shop-tax-2/src/rates.lawspec +6 -4
  34. package/examples/rc1/authorization-snapshots.lawspec +113 -0
  35. package/examples/rc1/banner-filenames.lawspec +64 -0
  36. package/examples/rc1/bit-codec.lawspec +57 -0
  37. package/examples/rc1/bounded-witness-search.lawspec +151 -0
  38. package/examples/rc1/conflicting-requirements.lawspec +31 -0
  39. package/examples/rc1/constructive-witnesses.lawspec +120 -0
  40. package/examples/rc1/documentary-review.lawspec +43 -0
  41. package/examples/rc1/empty-domains.lawspec +47 -0
  42. package/examples/rc1/finite-integer-domains.lawspec +76 -0
  43. package/examples/rc1/law-metadata.lawspec +30 -0
  44. package/examples/rc1/native-payments.lawspec +108 -0
  45. package/examples/rc1/nesting/rrr.lawspec +34 -0
  46. package/examples/rc1/nesting/rru.lawspec +57 -0
  47. package/examples/rc1/nesting/rur.lawspec +58 -0
  48. package/examples/rc1/nesting/ruu.lawspec +78 -0
  49. package/examples/rc1/nesting/urr.lawspec +53 -0
  50. package/examples/rc1/nesting/uru.lawspec +73 -0
  51. package/examples/rc1/nesting/uur.lawspec +74 -0
  52. package/examples/rc1/nesting/uuu.lawspec +96 -0
  53. package/examples/rc1/order-workflow.lawspec +127 -0
  54. package/examples/rc1/parcel-measurements.lawspec +93 -0
  55. package/examples/rc1/picture-records.lawspec +52 -0
  56. package/examples/rc1/picture-tuples.lawspec +48 -0
  57. package/examples/rc1/record-payloads.lawspec +57 -0
  58. package/examples/rc1/recursive-records.lawspec +65 -0
  59. package/examples/rc1/refund-conformance.lawspec +45 -0
  60. package/examples/rc1/refund-policy.lawspec +89 -0
  61. package/examples/rc1/requirement-composition.lawspec +90 -0
  62. package/examples/rc1/structural-records.lawspec +54 -0
  63. package/examples/rc1-authoring.lawspec +57 -0
  64. package/examples/specs/abilities.lawspec +67 -65
  65. package/examples/specs/ability_imports.lawspec +58 -57
  66. package/examples/specs/actors.lawspec +23 -22
  67. package/examples/specs/algebra.lawspec +142 -141
  68. package/examples/specs/approvals.lawspec +16 -15
  69. package/examples/specs/async_bindings.lawspec +10 -9
  70. package/examples/specs/async_fetch.lawspec +12 -11
  71. package/examples/specs/atoi_codec.lawspec +16 -15
  72. package/examples/specs/boolean_flags.lawspec +10 -9
  73. package/examples/specs/builtins.lawspec +73 -72
  74. package/examples/specs/canonical_url.lawspec +14 -13
  75. package/examples/specs/collections.lawspec +153 -154
  76. package/examples/specs/composition.lawspec +70 -66
  77. package/examples/specs/concurrent_collections.lawspec +8 -7
  78. package/examples/specs/consistency.lawspec +4 -3
  79. package/examples/specs/contracts.lawspec +80 -79
  80. package/examples/specs/crypto.lawspec +26 -25
  81. package/examples/specs/ctc.lawspec +148 -0
  82. package/examples/specs/currying.lawspec +32 -31
  83. package/examples/specs/data_types.lawspec +50 -49
  84. package/examples/specs/distribution.lawspec +57 -56
  85. package/examples/specs/domain_modeling.lawspec +48 -47
  86. package/examples/specs/durations.lawspec +39 -38
  87. package/examples/specs/equivalent.lawspec +25 -24
  88. package/examples/specs/failures.lawspec +64 -63
  89. package/examples/specs/finite_data.lawspec +32 -31
  90. package/examples/specs/flow_stack.lawspec +64 -63
  91. package/examples/specs/gadt_expressions.lawspec +50 -49
  92. package/examples/specs/generation.lawspec +115 -114
  93. package/examples/specs/handler_bindings.lawspec +42 -40
  94. package/examples/specs/handles.lawspec +2 -1
  95. package/examples/specs/harness.lawspec +45 -43
  96. package/examples/specs/harness_testing.lawspec +18 -13
  97. package/examples/specs/indexed_arithmetic.lawspec +401 -126
  98. package/examples/specs/indexed_families.lawspec +182 -66
  99. package/examples/specs/keywords.lawspec +22 -21
  100. package/examples/specs/limits.lawspec +6 -5
  101. package/examples/specs/list_contracts.lawspec +121 -120
  102. package/examples/specs/list_refinements.lawspec +43 -42
  103. package/examples/specs/lists.lawspec +45 -44
  104. package/examples/specs/matchers.lawspec +79 -78
  105. package/examples/specs/matching.lawspec +43 -42
  106. package/examples/specs/mixed_inputs.lawspec +25 -24
  107. package/examples/specs/models.lawspec +44 -43
  108. package/examples/specs/parse_port.lawspec +28 -27
  109. package/examples/specs/payments.lawspec +55 -54
  110. package/examples/specs/railway.lawspec +59 -58
  111. package/examples/specs/records.lawspec +149 -148
  112. package/examples/specs/records_inner.lawspec +5 -4
  113. package/examples/specs/recursive_refinements.lawspec +30 -29
  114. package/examples/specs/refined_definitions.lawspec +24 -23
  115. package/examples/specs/refinements.lawspec +58 -57
  116. package/examples/specs/resilience.lawspec +159 -158
  117. package/examples/specs/resources.lawspec +61 -60
  118. package/examples/specs/scalar_adapters.lawspec +60 -59
  119. package/examples/specs/scalar_catalog.lawspec +165 -164
  120. package/examples/specs/scalars.lawspec +78 -77
  121. package/examples/specs/scheduling_order.lawspec +8 -7
  122. package/examples/specs/scheduling_overlap.lawspec +5 -4
  123. package/examples/specs/sequences.lawspec +66 -65
  124. package/examples/specs/sessions.lawspec +13 -12
  125. package/examples/specs/slug.lawspec +16 -15
  126. package/examples/specs/sum_refinements.lawspec +42 -41
  127. package/examples/specs/tables.lawspec +29 -28
  128. package/examples/specs/total_functions.lawspec +71 -70
  129. package/examples/specs/workflows.lawspec +101 -100
  130. package/examples-command.mjs +8 -4
  131. package/explain.mjs +114 -0
  132. package/expression-presentation.mjs +333 -0
  133. package/files.mjs +16 -0
  134. package/index.d.ts +406 -11
  135. package/launcher.mjs +9 -2
  136. package/lsp.mjs +235 -0
  137. package/native-evidence.mjs +187 -0
  138. package/native-examples.mjs +8 -1
  139. package/node-events-reporter.mjs +23 -0
  140. package/package.json +8 -1
  141. package/positional-fields.mjs +13 -0
  142. package/proof.d.ts +35 -0
  143. package/proof.mjs +174 -0
  144. package/starter-rc1.lawspec +26 -0
  145. package/starter.lawspec +16 -15
  146. package/templates.mjs +6 -4
  147. package/test-command.mjs +107 -35
  148. package/typed-presentation.mjs +165 -0
@@ -6,38 +6,38 @@ unit example.models
6
6
 
7
7
  -- A linear model: the stack's size is in its type, so a run never pops an
8
8
  -- empty stack.
9
- type Stack (n :: Natural) is
9
+ type Stack (n :: Whole) is one of
10
10
  | Empty where n = 0
11
- | Push top :: Int8 rest :: Stack m where n = m + 1
12
- end
11
+ | Push of type record{ top :: Int8 rest :: Stack m} where n = m + 1
12
+ end type
13
13
 
14
14
  empty :: Unit -> Stack 0
15
15
  push :: (x :: Int8) -> Stack n / Stack (n + 1) -> Unit
16
16
  pop :: Stack (n + 1) / Stack n -> Int8
17
17
  peek :: Stack (n + 1) / Stack (n + 1) -> Int8
18
18
 
19
- definition modelPush (x :: Int8) (xs :: List Int8) :: List Int8 is Cons x xs end
20
-
21
- definition modelPop (xs :: List Int8) :: Pair Int8 (List Int8) is
22
- match xs with
23
- | Nil -> Pair 0 Nil
24
- | Cons x rest -> Pair x rest
25
- end
26
- end
27
-
28
- definition modelPeek (xs :: List Int8) :: Pair Int8 (List Int8) is
29
- match xs with
30
- | Nil -> Pair 0 Nil
31
- | Cons x rest -> Pair x xs
32
- end
33
- end
34
-
35
- definition toList (s :: Stack n) :: List Int8 is
36
- match s with
37
- | Empty -> Nil
38
- | Push top rest -> Cons top (toList rest)
39
- end
40
- end
19
+ function modelPush (x :: Int8) (xs :: List Int8) -> List Int8 is Cons x xs end function
20
+
21
+ function modelPop (xs :: List Int8) -> Pair Int8 (List Int8) is
22
+ first matching case of xs :
23
+ when Nil then Pair 0 Nil
24
+ when Cons x rest then Pair x rest
25
+ end case
26
+ end function
27
+
28
+ function modelPeek (xs :: List Int8) -> Pair Int8 (List Int8) is
29
+ first matching case of xs :
30
+ when Nil then Pair 0 Nil
31
+ when Cons x rest then Pair x xs
32
+ end case
33
+ end function
34
+
35
+ function toList (s :: Stack n) -> List Int8 is
36
+ first matching case of s :
37
+ when Empty then Nil
38
+ when Push record{ top = top , rest = rest} then Cons top (toList rest)
39
+ end case
40
+ end function
41
41
 
42
42
  model stack :: Stack n by List Int8 is
43
43
  start empty by Nil
@@ -45,29 +45,29 @@ model stack :: Stack n by List Int8 is
45
45
  pop by modelPop
46
46
  peek by modelPeek
47
47
  abstract toList
48
- end
48
+ end model
49
49
 
50
50
  -- A shared model: commands take one counter, whose type never changes, and
51
51
  -- may run at the same time, so each must be atomic. LawSpec also runs them
52
52
  -- in parallel and checks that every history is linearizable.
53
- type Counter is Counter id :: Int32 end
53
+ type Counter is a record{ id :: Int32} end type
54
54
 
55
55
  newCounter :: Unit -> Counter
56
56
  async increment :: Counter -> Int64
57
57
  async decrement :: Counter -> Int64
58
58
  async read :: Counter -> Int64
59
59
 
60
- definition modelIncrement (n :: Int64 where n >= 0 && n <= 1000000) :: Pair Int64 Int64 is Pair (n + 1) (n + 1) end
61
- definition modelDecrement (n :: Int64 where n >= 0 && n <= 1000000) :: Pair Int64 Int64 is Pair (n - 1) (n - 1) end
62
- definition modelRead (n :: Int64) :: Pair Int64 Int64 is Pair n n end
63
- definition positive (n :: Int64) :: Bool is n > 0 end
60
+ function modelIncrement (n :: Int64 where n >= 0 && n <= 1000000) -> Pair Int64 Int64 is Pair (n + 1) (n + 1) end function
61
+ function modelDecrement (n :: Int64 where n >= 0 && n <= 1000000) -> Pair Int64 Int64 is Pair (n - 1) (n - 1) end function
62
+ function modelRead (n :: Int64) -> Pair Int64 Int64 is Pair n n end function
63
+ function positive (n :: Int64) -> Bool is n > 0 end function
64
64
 
65
65
  model counter :: shared Counter by Int64 is
66
66
  start newCounter ~ 0
67
67
  increment ~ modelIncrement
68
68
  decrement ~ modelDecrement when positive
69
69
  read ~ modelRead
70
- end
70
+ end model
71
71
 
72
72
  -- A protocol lists what one end of a channel sends and receives; the other
73
73
  -- end does the reverse. Scenarios run a shared model's commands from
@@ -76,11 +76,11 @@ end
76
76
  -- each run must agree with the model.
77
77
  protocol Report is
78
78
  send Int64
79
- end
79
+ end protocol
80
80
 
81
81
  protocol Request is
82
82
  send Report
83
- end
83
+ end protocol
84
84
 
85
85
  scenario `a count is passed on` in counter is
86
86
  channel report :: Report
@@ -90,8 +90,8 @@ scenario `a count is passed on` in counter is
90
90
  with
91
91
  receive report m
92
92
  expect m = 1
93
- end
94
- end
93
+ end par
94
+ end scenario
95
95
 
96
96
  -- The asker hands its end of the answer channel to a worker, which replies
97
97
  -- to the listener directly.
@@ -107,25 +107,25 @@ scenario `a reply is delegated` in counter is
107
107
  with
108
108
  receive answer m
109
109
  expect m = 1
110
- end
111
- end
110
+ end par
111
+ end scenario
112
112
 
113
113
  scenario `two counts at once` in counter is
114
114
  par
115
115
  a <- increment
116
116
  with
117
117
  b <- increment
118
- end
118
+ end par
119
119
  total <- read
120
120
  expect total = 2
121
- end
121
+ end scenario
122
122
 
123
123
  -- Two channels between the same two processes close a cycle. That is
124
124
  -- accepted when no process waits for another in a cycle: here the first
125
125
  -- process sends before it waits, so the echo can always answer.
126
126
  protocol Echo is
127
127
  receive Int64
128
- end
128
+ end protocol
129
129
 
130
130
  scenario `a count is echoed back` in counter is
131
131
  channel out :: Report
@@ -138,5 +138,6 @@ scenario `a count is echoed back` in counter is
138
138
  with
139
139
  receive out k
140
140
  send back k
141
- end
142
- end
141
+ end par
142
+ end scenario
143
+ end unit
@@ -4,63 +4,64 @@ validPort :: Int32 -> Bool
4
4
  render :: Int32 -> Text
5
5
  parse :: Text -> Int32
6
6
 
7
- law `valid ports round trip` is
8
- definition is
9
- `for all` (x :: Int32) .
7
+ law "|valid ports round trip| is
8
+ definition :
9
+ for all (x :: Int32) =>
10
10
  validPort x implies
11
- parse (render x) = x
12
- end
11
+ parse (render x) = x end for all
12
+ end definition
13
13
 
14
- description is
14
+ description :
15
15
  "For ports in 1 through 65535, {parse} recovers the value written by {render}"
16
- end
16
+ end description
17
17
 
18
- example `ordinary port` is
18
+ example "|ordinary port| :
19
19
  x = 443
20
20
  expect validPort x = true
21
21
  expect render x = "443"
22
22
  expect parse (render x) = 443
23
- end
23
+ end example
24
24
 
25
- example `lowest port is accepted and round trips` is
25
+ example "|lowest port is accepted and round trips| :
26
26
  x = 1
27
27
  expect validPort x = true
28
28
  expect render x = "1"
29
29
  expect parse (render x) = 1
30
- end
30
+ end example
31
31
 
32
- example `highest port is accepted and round trips` is
32
+ example "|highest port is accepted and round trips| :
33
33
  x = 65535
34
34
  expect validPort x = true
35
35
  expect render x = "65535"
36
36
  expect parse (render x) = 65535
37
- end
37
+ end example
38
38
 
39
- example `zero is rejected; rendering and parsing are skipped` is
39
+ example "|zero is rejected; rendering and parsing are skipped| :
40
40
  x = 0
41
41
  expect validPort x = false
42
- end
42
+ end example
43
43
 
44
- example `negative port is rejected; rendering and parsing are skipped` is
44
+ example "|negative port is rejected; rendering and parsing are skipped| :
45
45
  x = -1
46
46
  expect validPort x = false
47
- end
47
+ end example
48
48
 
49
- example `65536 is rejected; rendering and parsing are skipped` is
49
+ example "|65536 is rejected; rendering and parsing are skipped| :
50
50
  x = 65536
51
51
  expect validPort x = false
52
- end
53
- end
52
+ end example
53
+ end law
54
54
 
55
- law `the same port contract via the prelude` is
56
- definition is
57
- `left inverse when` validPort parse render
58
- end
55
+ law "|the same port contract via the prelude| is
56
+ definition :
57
+ apply law "|left inverse when| validPort parse render
58
+ end definition
59
59
 
60
- example `port 8080 satisfies the inherited condition and round trips` is
60
+ example "|port 8080 satisfies the inherited condition and round trips| :
61
61
  x = 8080
62
62
  expect validPort x = true
63
63
  expect render x = "8080"
64
64
  expect parse (render x) = 8080
65
- end
66
- end
65
+ end example
66
+ end law
67
+ end unit
@@ -3,16 +3,16 @@ unit example.payments
3
3
  -- The executable acceptance model for native domain bindings in 0.10.
4
4
  -- Applications may call Money "Price", amount "major", and currency "unit";
5
5
  -- those representation choices must not alter these laws.
6
- type Currency is USD | EUR | GBP end
6
+ type Currency is one of USD | EUR | GBP end type
7
7
 
8
- type Money is
9
- Money amount :: Decimal currency :: Currency
10
- end
8
+ type Money is a record{
9
+ amount :: Decimal currency :: Currency}
10
+ end type
11
11
 
12
- type Payment is
13
- Paid value :: Money
14
- Declined reason :: Text
15
- end
12
+ type Payment is one of
13
+ Paid of type Money
14
+ Declined of type Text
15
+ end type
16
16
 
17
17
  -- These are adapter functions supplied by the application.
18
18
  addFee :: Money -> Money
@@ -20,57 +20,58 @@ roundTrip :: Payment -> Payment
20
20
  archive :: List (Maybe Payment) -> List (Maybe Payment)
21
21
 
22
22
  -- The model is exact, independent of host decimal rounding settings.
23
- definition feeModel (money :: Money) :: Money is
24
- match money with
25
- | Money amount currency -> Money (amount + 0.2) currency
26
- end
27
- end
23
+ function feeModel (money :: Money) -> Money is
24
+ first matching case of money :
25
+ when Money record{ amount = amount , currency = currency} then Money record{ amount = (amount + 0.2) , currency = currency}
26
+ end case
27
+ end function
28
28
 
29
- law `fees preserve currency and exact decimal value` is
30
- definition is
31
- `for all` (money :: Money) . addFee money = feeModel money
32
- end
33
- example `decimal tenths` is
34
- money = Money 0.1 USD
35
- expect addFee money = Money 0.3 USD
36
- end
37
- example `small amount is not rounded away` is
38
- money = Money 0.000000000000000000000000000001 EUR
39
- expect addFee money = Money 0.200000000000000000000000000001 EUR
40
- end
41
- example `beyond binary floating precision` is
42
- money = Money 9007199254740993.1 GBP
43
- expect addFee money = Money 9007199254740993.3 GBP
44
- end
45
- end
29
+ law "|fees preserve currency and exact decimal value| is
30
+ definition :
31
+ for all (money :: Money) => addFee money = feeModel money end for all
32
+ end definition
33
+ example "|decimal tenths| :
34
+ money = Money record{ amount = 0.1 , currency = USD}
35
+ expect addFee money = Money record{ amount = 0.3 , currency = USD}
36
+ end example
37
+ example "|small amount is not rounded away| :
38
+ money = Money record{ amount = 0.000000000000000000000000000001 , currency = EUR}
39
+ expect addFee money = Money record{ amount = 0.200000000000000000000000000001 , currency = EUR}
40
+ end example
41
+ example "|beyond binary floating precision| :
42
+ money = Money record{ amount = 9007199254740993.1 , currency = GBP}
43
+ expect addFee money = Money record{ amount = 9007199254740993.3 , currency = GBP}
44
+ end example
45
+ end law
46
46
 
47
- law `payment variants and payloads survive native conversion` is
48
- definition is
49
- `for all` (payment :: Payment) . roundTrip payment = payment
50
- end
51
- example `successful payment` is
52
- payment = Paid (Money 12.34 EUR)
53
- expect roundTrip payment = Paid (Money 12.34 EUR)
54
- end
55
- example `declined payment` is
47
+ law "|payment variants and payloads survive native conversion| is
48
+ definition :
49
+ for all (payment :: Payment) => roundTrip payment = payment end for all
50
+ end definition
51
+ example "|successful payment| :
52
+ payment = Paid (Money record{ amount = 12.34 , currency = EUR})
53
+ expect roundTrip payment = Paid (Money record{ amount = 12.34 , currency = EUR})
54
+ end example
55
+ example "|declined payment| :
56
56
  payment = Declined "card declined"
57
57
  expect roundTrip payment = Declined "card declined"
58
- end
59
- end
58
+ end example
59
+ end law
60
60
 
61
- law `archives preserve order duplicates and absence` is
62
- definition is
63
- `for all` (payments :: List (Maybe Payment)) . archive payments = payments
64
- end
65
- example `empty archive` is
61
+ law "|archives preserve order duplicates and absence| is
62
+ definition :
63
+ for all (payments :: List (Maybe Payment)) => archive payments = payments end for all
64
+ end definition
65
+ example "|empty archive| :
66
66
  payments = []
67
67
  expect archive payments = []
68
- end
69
- example `missing is different from a declined payment` is
70
- payments = [Nothing, Just (Declined ""), Just (Paid (Money 0.1 USD)),
71
- Just (Paid (Money 0.1 USD))]
68
+ end example
69
+ example "|missing is different from a declined payment| :
70
+ payments = [Nothing, Just (Declined ""), Just (Paid (Money record{ amount = 0.1 , currency = USD})),
71
+ Just (Paid (Money record{ amount = 0.1 , currency = USD}))]
72
72
  expect archive payments = [Nothing, Just (Declined ""),
73
- Just (Paid (Money 0.1 USD)),
74
- Just (Paid (Money 0.1 USD))]
75
- end
76
- end
73
+ Just (Paid (Money record{ amount = 0.1 , currency = USD})),
74
+ Just (Paid (Money record{ amount = 0.1 , currency = USD}))]
75
+ end example
76
+ end law
77
+ end unit
@@ -3,76 +3,77 @@ unit example.railway
3
3
  -- Railway combinators on Either. Each symbol has a prelude name, and both are
4
4
  -- rewritten into matches, so they work in laws and checked definitions alike.
5
5
 
6
- type Problem is | TooSmall | TooLarge | Odd end
6
+ type Problem is one of | TooSmall | TooLarge | Odd end type
7
7
 
8
- definition positive (x :: BigInt) :: Either Problem BigInt is
8
+ function positive (x :: Integer) -> Either Problem Integer is
9
9
  prelude.select (x > 0) (Right x) (Left TooSmall)
10
- end
10
+ end function
11
11
 
12
- definition small (x :: BigInt) :: Either Problem BigInt is
12
+ function small (x :: Integer) -> Either Problem Integer is
13
13
  prelude.select (x < 100) (Right x) (Left TooLarge)
14
- end
14
+ end function
15
15
 
16
- definition even (x :: BigInt) :: Bool is prelude.rem x 2 == 0 end
16
+ function even (x :: Integer) -> Bool is prelude.rem x 2 = 0 end function
17
17
 
18
- definition twice (x :: BigInt) :: BigInt is x + x end
18
+ function twice (x :: Integer) -> Integer is x + x end function
19
19
 
20
- definition retry (p :: Problem) :: Either Problem BigInt is Right 1 end
20
+ function retry (p :: Problem) -> Either Problem Integer is Right 1 end function
21
21
 
22
22
  -- A checked pipeline written with symbols: positive, then small, then doubled.
23
- definition checked (x :: BigInt) :: Either Problem BigInt is
23
+ function checked (x :: Integer) -> Either Problem Integer is
24
24
  twice <$> (positive x >>= small)
25
- end
25
+ end function
26
26
 
27
27
  -- The same pipeline in English.
28
- definition checkedInWords (x :: BigInt) :: Either Problem BigInt is
28
+ function checkedInWords (x :: Integer) -> Either Problem Integer is
29
29
  prelude.map twice (prelude.bind (positive x) small)
30
- end
31
-
32
- law `symbols and English agree` is
33
- definition is `for all` (x :: BigInt) . checked x = checkedInWords x end
34
- end
35
-
36
- law `binding associates` is
37
- definition is
38
- `for all` (x :: BigInt) . ((positive x >>= small) >>= positive) = (positive x >>= (small >=> positive))
39
- end
40
- end
41
-
42
- law `a failure short-circuits` is
43
- definition is `for all` (x :: BigInt) . x <= 0 implies checked x = Left TooSmall end
44
- end
45
-
46
- law `recovery replaces a failure` is
47
- definition is `for all` (x :: BigInt) . (positive x <|> retry) = prelude.select (x > 0) (Right x) (Right 1) end
48
- end
49
-
50
- law `ensure keeps even values` is
51
- definition is
52
- `for all` (x :: BigInt) .
53
- prelude.ensure even Odd (positive x) = prelude.select (x > 0) (prelude.select (even x) (Right x) (Left Odd)) (Left TooSmall)
54
- end
55
- end
56
-
57
- law `both pairs successes` is
58
- definition is
59
- `for all` (x :: BigInt) . prelude.isRight (positive x <*> small x) = (x > 0 && x < 100)
60
- end
61
- end
30
+ end function
31
+
32
+ law "|symbols and English agree| is
33
+ definition : for all (x :: Integer) => checked x = checkedInWords x end for all end definition
34
+ end law
35
+
36
+ law "|binding associates| is
37
+ definition :
38
+ for all (x :: Integer) => ((positive x >>= small) >>= positive) = (positive x >>= (small >=> positive)) end for all
39
+ end definition
40
+ end law
41
+
42
+ law "|a failure short-circuits| is
43
+ definition : for all (x :: Integer) => x <= 0 implies checked x = Left TooSmall end for all end definition
44
+ end law
45
+
46
+ law "|recovery replaces a failure| is
47
+ definition : for all (x :: Integer) => (positive x <|> retry) = prelude.select (x > 0) (Right x) (Right 1) end for all end definition
48
+ end law
49
+
50
+ law "|ensure keeps even values| is
51
+ definition :
52
+ for all (x :: Integer) =>
53
+ prelude.ensure even Odd (positive x) = prelude.select (x > 0) (prelude.select (even x) (Right x) (Left Odd)) (Left TooSmall) end for all
54
+ end definition
55
+ end law
56
+
57
+ law "|both pairs successes| is
58
+ definition :
59
+ for all (x :: Integer) => prelude.isRight (positive x <*> small x) = (x > 0 && x < 100) end for all
60
+ end definition
61
+ end law
62
62
 
63
63
  -- The prelude's railway laws, instantiated for these definitions.
64
- law `positive is a left identity case` is
65
- definition is `bind has a left identity` positive end
66
- end
67
-
68
- law `positive, small and positive associate` is
69
- definition is `bind is associative` positive small positive end
70
- end
71
-
72
- law `mapping twice fuses` is
73
- definition is `map fuses` positive twice twice end
74
- end
75
-
76
- law `retry keeps successes` is
77
- definition is `recovery keeps successes` positive retry end
78
- end
64
+ law "|positive is a left identity case| is
65
+ definition : apply law "|bind has a left identity| positive end definition
66
+ end law
67
+
68
+ law "|positive, small and positive associate| is
69
+ definition : apply law "|bind is associative| positive small positive end definition
70
+ end law
71
+
72
+ law "|mapping twice fuses| is
73
+ definition : apply law "|map fuses| positive twice twice end definition
74
+ end law
75
+
76
+ law "|retry keeps successes| is
77
+ definition : apply law "|recovery keeps successes| positive retry end definition
78
+ end law
79
+ end unit