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,43 +5,44 @@ referenceRender :: Int32 -> Text
5
5
  clamp :: Int32 -> Int32
6
6
  referenceClamp :: Int32 -> Int32
7
7
 
8
- law `decimal renderers agree` is
9
- definition is
10
- `equivalent` render referenceRender
11
- end
12
- description is
8
+ law "|decimal renderers agree| is
9
+ definition :
10
+ apply law "|equivalent| render referenceRender
11
+ end definition
12
+ description :
13
13
  "{render} agrees with {referenceRender} on decimal formatting"
14
- end
15
- rationale is
14
+ end description
15
+ rationale :
16
16
  "changing the formatting implementation must preserve its result"
17
- end
18
- example `both renderers produce "-42"` is
17
+ end rationale
18
+ example "|both renderers produce "-42"| :
19
19
  x = -42
20
20
  expect render x = "-42"
21
21
  expect referenceRender x = "-42"
22
- end
23
- example `both renderers produce "0"` is
22
+ end example
23
+ example "|both renderers produce "0"| :
24
24
  x = 0
25
25
  expect render x = "0"
26
26
  expect referenceRender x = "0"
27
- end
28
- end
27
+ end example
28
+ end law
29
29
 
30
- law `nonnegative clamps agree` is
31
- definition is
32
- `equivalent` clamp referenceClamp
33
- end
34
- description is
30
+ law "|nonnegative clamps agree| is
31
+ definition :
32
+ apply law "|equivalent| clamp referenceClamp
33
+ end definition
34
+ description :
35
35
  "{clamp} agrees with {referenceClamp} when clamping negative inputs to zero"
36
- end
37
- example `both clamps return 0 for -2147483648` is
36
+ end description
37
+ example "|both clamps return 0 for -2147483648| :
38
38
  x = -2147483648
39
39
  expect clamp x = 0
40
40
  expect referenceClamp x = 0
41
- end
42
- example `both clamps return 2147483647 for 2147483647` is
41
+ end example
42
+ example "|both clamps return 2147483647 for 2147483647| :
43
43
  x = 2147483647
44
44
  expect clamp x = 2147483647
45
45
  expect referenceClamp x = 2147483647
46
- end
47
- end
46
+ end example
47
+ end law
48
+ end unit
@@ -5,92 +5,93 @@ unit example.failures
5
5
  -- pattern, where _ matches any field, and may also check the failure's
6
6
  -- message.
7
7
 
8
- type Decision is
8
+ type Decision is one of
9
9
  | Approve
10
- | Decline reason :: Text
10
+ | Decline of type Text
11
11
  | Block
12
- end
12
+ end type
13
13
 
14
- type PaymentError is
15
- | Declined message :: Text
14
+ type PaymentError is one of
15
+ | Declined of type record{ message :: Text}
16
16
  | Blocked
17
- | TooLarge limit :: Int32
18
- end
17
+ | TooLarge of type Int32
18
+ end type
19
19
 
20
20
  -- The payment provider decides; its native handler is written by hand.
21
21
  ability Gateway is
22
22
  decide :: Int32 -> Decision
23
- end
23
+ end ability
24
24
 
25
- definition charge (cents :: Int32) :: Int32 uses Gateway fails with PaymentError is
25
+ procedure charge (cents :: Int32) -> Int32 uses Gateway fails with PaymentError is
26
26
  if cents > 100000 then raise (TooLarge 100000) else
27
- match decide cents with
28
- | Approve -> cents
29
- | Decline reason -> raise (Declined reason)
30
- | Block -> raise Blocked
31
- end
32
- end
27
+ first matching case of decide cents :
28
+ when Approve then cents
29
+ when Decline reason then raise (Declined record{ message = reason})
30
+ when Block then raise Blocked
31
+ end case end if
32
+ end procedure
33
33
 
34
- law `large charges fail as too large` is
35
- definition is
36
- `for all` (cents :: Int32 where cents > 100000) . charge cents fails with TooLarge 100000
37
- end
38
- end
34
+ law "|large charges fail as too large| is
35
+ definition :
36
+ for all (cents :: Int32 where cents > 100000) => charge cents fails with TooLarge 100000 end for all
37
+ end definition
38
+ end law
39
39
 
40
- law `odd amounts are declined, and say why` is
41
- definition is
42
- `for all` (cents :: Int32 where cents >= 0 && cents <= 100000 && prelude.rem cents 2 == 1) .
43
- charge cents fails with Declined _ message contains "odd"
44
- end
45
- end
40
+ law "|odd amounts are declined, and say why| is
41
+ definition :
42
+ for all (cents :: Int32 where cents >= 0 && cents <= 100000 && prelude.rem cents 2 = 1) =>
43
+ charge cents fails with Declined record{ message = _} message contains "odd" end for all
44
+ end definition
45
+ end law
46
46
 
47
- law `negative amounts are blocked` is
48
- definition is
49
- `for all` (cents :: Int32 where cents < 0) . charge cents fails with Blocked
50
- end
51
- example `minus one` is
47
+ law "|negative amounts are blocked| is
48
+ definition :
49
+ for all (cents :: Int32 where cents < 0) => charge cents fails with Blocked end for all
50
+ end definition
51
+ example "|minus one| :
52
52
  cents = -1
53
53
  expect charge cents fails with Blocked
54
- expect !(charge cents fails with Declined _)
55
- end
56
- end
54
+ expect !(charge cents fails with Declined record{ message = _})
55
+ end example
56
+ end law
57
57
 
58
- law `even amounts go through` is
59
- definition is
60
- `for all` (cents :: Int32 where cents >= 0 && cents <= 100000 && prelude.rem cents 2 == 0) .
61
- prelude.attempt (charge cents) = Right cents
62
- end
63
- end
58
+ law "|even amounts go through| is
59
+ definition :
60
+ for all (cents :: Int32 where cents >= 0 && cents <= 100000 && prelude.rem cents 2 = 0) =>
61
+ prelude.attempt (charge cents) = Right cents end for all
62
+ end definition
63
+ end law
64
64
 
65
65
  -- Native adapters fail too: their native code raises the runtime's Fail
66
66
  -- with a PaymentError, synchronously or from an async adapter.
67
67
  refund :: Int32 -> Int32 fails with PaymentError
68
68
  async settle :: Int32 -> Int32 fails with PaymentError
69
69
 
70
- law `refunds over the limit are too large` is
71
- definition is
72
- `for all` (cents :: Int32 where cents > 5000) . refund cents fails with TooLarge 5000
73
- end
74
- end
70
+ law "|refunds over the limit are too large| is
71
+ definition :
72
+ for all (cents :: Int32 where cents > 5000) => refund cents fails with TooLarge 5000 end for all
73
+ end definition
74
+ end law
75
75
 
76
- law `refunds within the limit go through` is
77
- definition is
78
- `for all` (cents :: Int32 where cents >= 0 && cents <= 5000) . prelude.attempt (refund cents) = Right cents
79
- end
80
- end
76
+ law "|refunds within the limit go through| is
77
+ definition :
78
+ for all (cents :: Int32 where cents >= 0 && cents <= 5000) => prelude.attempt (refund cents) = Right cents end for all
79
+ end definition
80
+ end law
81
81
 
82
- law `settling a negative amount is blocked` is
83
- definition is
84
- `for all` (cents :: Int32 where cents < 0) . settle cents fails with Blocked
85
- end
86
- end
82
+ law "|settling a negative amount is blocked| is
83
+ definition :
84
+ for all (cents :: Int32 where cents < 0) => settle cents fails with Blocked end for all
85
+ end definition
86
+ end law
87
87
 
88
- law `settling nothing is declined, and says why` is
89
- definition is settle 0 fails with Declined _ message contains "nothing" end
90
- end
88
+ law "|settling nothing is declined, and says why| is
89
+ definition : settle 0 fails with Declined record{ message = _} message contains "nothing" end definition
90
+ end law
91
91
 
92
- law `settling a positive amount goes through` is
93
- definition is
94
- `for all` (cents :: Int32 where cents > 0) . prelude.attempt (settle cents) = Right cents
95
- end
96
- end
92
+ law "|settling a positive amount goes through| is
93
+ definition :
94
+ for all (cents :: Int32 where cents > 0) => prelude.attempt (settle cents) = Right cents end for all
95
+ end definition
96
+ end law
97
+ end unit
@@ -1,37 +1,38 @@
1
1
  unit example.finite_data
2
2
 
3
- type Choice is Yes | No end
3
+ type Choice is one of Yes | No end type
4
4
 
5
- type Pair (a :: Type) (b :: Type) is
6
- Pair first :: a second :: b
7
- end
5
+ type Pair (a :: Type) (b :: Type) is a record{
6
+ first :: a second :: b}
7
+ end type
8
8
 
9
- law `sum constructors remain distinct` is
10
- definition is
11
- `for all` (choice :: Choice) .
12
- (match choice with
13
- | Yes -> Yes
14
- | No -> No
15
- end) = choice
16
- end
17
- example `yes` is choice = Yes expect choice = Yes end
18
- end
9
+ law "|sum constructors remain distinct| is
10
+ definition :
11
+ for all (choice :: Choice) =>
12
+ (first matching case of choice :
13
+ when Yes then Yes
14
+ when No then No
15
+ end case) = choice end for all
16
+ end definition
17
+ example "|yes| : choice = Yes expect choice = Yes end example
18
+ end law
19
19
 
20
- law `products preserve both fields` is
21
- definition is
22
- `for all` (pair :: Pair Bool Bool) .
23
- (match pair with
24
- | Pair first second -> Pair first second
25
- end) = pair
26
- end
27
- example `different fields` is
28
- pair = Pair true false
29
- expect pair = Pair true false
30
- end
31
- end
20
+ law "|products preserve both fields| is
21
+ definition :
22
+ for all (pair :: Pair Bool Bool) =>
23
+ (first matching case of pair :
24
+ when Pair record{ first = first , second = second} then Pair record{ first = first , second = second}
25
+ end case) = pair end for all
26
+ end definition
27
+ example "|different fields| :
28
+ pair = Pair record{ first = true , second = false}
29
+ expect pair = Pair record{ first = true , second = false}
30
+ end example
31
+ end law
32
32
 
33
- law `nested optional products keep presence states` is
34
- definition is
35
- `for all` (value :: Maybe (Pair Choice (Nullable Bool))) . value = value
36
- end
37
- end
33
+ law "|nested optional products keep presence states| is
34
+ definition :
35
+ for all (value :: Maybe (Pair Choice (Nullable Bool))) => value = value end for all
36
+ end definition
37
+ end law
38
+ end unit
@@ -1,9 +1,9 @@
1
1
  unit example.flow
2
2
 
3
- type Stack (n :: Natural) is
3
+ type Stack (n :: Whole) is one of
4
4
  | Empty where n = 0
5
- | Push top :: Int8 rest :: Stack m where n = m + 1
6
- end
5
+ | Push of type record{ top :: Int8 rest :: Stack m} where n = m + 1
6
+ end type
7
7
 
8
8
  -- A / A' is a flow parameter: the call takes the stack from A to A'. Each
9
9
  -- adapter returns its result with the stack it leaves, as a generated
@@ -13,81 +13,82 @@ pop :: Stack (n + 1) / Stack n -> Int8
13
13
  peek :: Stack (n + 1) / Stack (n + 1) -> Int8
14
14
 
15
15
  -- A reference definition updates its flow parameter with ~s := e.
16
- definition pushed (x :: Int8) (s :: Stack n / Stack (n + 1)) :: Unit is
17
- ~s := Push x s
18
- end
16
+ function pushed (x :: Int8) (s :: Stack n / Stack (n + 1)) -> Unit is
17
+ ~s := Push record{ top = x , rest = s}
18
+ end function
19
19
 
20
20
  -- ~s passes s to a flow parameter and rebinds s to the stack the call
21
21
  -- leaves; e1; e2 sequences, left to right. pop needs Stack (m + 1): after
22
22
  -- push, s is a Stack (n + 1), so the pop below type-checks.
23
- law `pop returns what push pushed` is
24
- definition is
25
- `for all` (x :: Int8) (s :: Stack n) . (push x ~s; pop ~s) = x
26
- end
27
- end
23
+ law "|pop returns what push pushed| is
24
+ definition :
25
+ for all (x :: Int8) (s :: Stack n) => (push x ~s; pop ~s) = x end for all
26
+ end definition
27
+ end law
28
28
 
29
- law `push grows the stack` is
30
- definition is
31
- `for all` (x :: Int8) (s :: Stack n) . (push x ~s; nOfStack s) = n + 1
32
- end
33
- end
29
+ law "|push grows the stack| is
30
+ definition :
31
+ for all (x :: Int8) (s :: Stack n) => (push x ~s; nOfStack s) = n + 1 end for all
32
+ end definition
33
+ end law
34
34
 
35
- law `pops come back in reverse` is
36
- definition is
37
- `for all` (x :: Int8) (y :: Int8) (s :: Stack n) .
38
- (push x ~s; push y ~s; pop ~s; pop ~s) = x
39
- end
40
- end
35
+ law "|pops come back in reverse| is
36
+ definition :
37
+ for all (x :: Int8) (y :: Int8) (s :: Stack n) =>
38
+ (push x ~s; push y ~s; pop ~s; pop ~s) = x end for all
39
+ end definition
40
+ end law
41
41
 
42
42
  -- The quantifier's index k + 1 lets peek and pop take s.
43
- law `peek and pop agree` is
44
- definition is
45
- `for all` (s :: Stack (k + 1)) . peek ~s = pop ~s
46
- end
47
- end
43
+ law "|peek and pop agree| is
44
+ definition :
45
+ for all (s :: Stack (k + 1)) => peek ~s = pop ~s end for all
46
+ end definition
47
+ end law
48
48
 
49
- law `pop shrinks the stack` is
50
- definition is
51
- `for all` (s :: Stack (k + 1)) . (pop ~s; nOfStack s) = k
52
- end
53
- end
49
+ law "|pop shrinks the stack| is
50
+ definition :
51
+ for all (s :: Stack (k + 1)) => (pop ~s; nOfStack s) = k end for all
52
+ end definition
53
+ end law
54
54
 
55
- law `push agrees with the reference` is
56
- definition is
57
- `for all` (x :: Int8) (s :: Stack n) . (pushed x ~s; pop ~s) = x
58
- end
59
- end
55
+ law "|push agrees with the reference| is
56
+ definition :
57
+ for all (x :: Int8) (s :: Stack n) => (pushed x ~s; pop ~s) = x end for all
58
+ end definition
59
+ end law
60
60
 
61
61
  -- A function may take several flow parameters: pushBoth takes two stacks and
62
62
  -- leaves each one higher. A call passes ~a ~b, rebinding each.
63
- definition pushBoth (x :: Int8) (a :: Stack n / Stack (n + 1)) (b :: Stack m / Stack (m + 1)) :: Unit is
64
- ~a := Push x a; ~b := Push x b
65
- end
63
+ function pushBoth (x :: Int8) (a :: Stack n / Stack (n + 1)) (b :: Stack m / Stack (m + 1)) -> Unit is
64
+ ~a := Push record{ top = x , rest = a}; ~b := Push record{ top = x , rest = b}
65
+ end function
66
66
 
67
- law `both stacks get the value` is
68
- definition is
69
- `for all` (x :: Int8) (a :: Stack n) (b :: Stack m) . (pushBoth x ~a ~b; pop ~a; pop ~b) = x
70
- end
71
- end
67
+ law "|both stacks get the value| is
68
+ definition :
69
+ for all (x :: Int8) (a :: Stack n) (b :: Stack m) => (pushBoth x ~a ~b; pop ~a; pop ~b) = x end for all
70
+ end definition
71
+ end law
72
72
 
73
73
  -- A flow call may sit in a branch of an if or a match, when every branch
74
74
  -- leaves the stack at the same type: here each branch pushes once.
75
- law `a push in each branch` is
76
- definition is
77
- `for all` (x :: Int8) (s :: Stack n) .
78
- ((if x > 0 then push x ~s else push 0 ~s); pop ~s) = (if x > 0 then x else 0)
79
- end
80
- end
75
+ law "|a push in each branch| is
76
+ definition :
77
+ for all (x :: Int8) (s :: Stack n) =>
78
+ ((if x > 0 then push x ~s else push 0 ~s end if); pop ~s) = (if x > 0 then x else 0 end if) end for all
79
+ end definition
80
+ end law
81
81
 
82
- law `a push chosen by a match` is
83
- definition is
84
- `for all` (m :: Maybe Int8) (s :: Stack n) .
85
- ((match m with
86
- | Just v -> push v ~s
87
- | Nothing -> push 0 ~s
88
- end); pop ~s) = (match m with
89
- | Just v -> v
90
- | Nothing -> prelude.Int8 0
91
- end)
92
- end
93
- end
82
+ law "|a push chosen by a match| is
83
+ definition :
84
+ for all (m :: Maybe Int8) (s :: Stack n) =>
85
+ ((first matching case of m :
86
+ when Just v then push v ~s
87
+ when Nothing then push 0 ~s
88
+ end case); pop ~s) = (first matching case of m :
89
+ when Just v then v
90
+ when Nothing then prelude.Int8 0
91
+ end case) end for all
92
+ end definition
93
+ end law
94
+ end unit
@@ -1,76 +1,77 @@
1
1
  unit example.gadt
2
2
 
3
- type Pair (a :: Type) (b :: Type) is Pair first :: a second :: b end
3
+ type Pair (a :: Type) (b :: Type) is a record{ first :: a second :: b} end type
4
4
 
5
5
  -- An expression indexed by the type of its value. Each constructor's `where`
6
6
  -- fixes that type, so a value of Expr Bool is never a number literal. The
7
7
  -- types b and c of Both are existential: Expr (Pair b c) determines them.
8
- type Expr (a :: Type) is
9
- | Number value :: BigInt where a = BigInt
10
- | Truth value :: Bool where a = Bool
11
- | Plus left :: Expr BigInt right :: Expr BigInt where a = BigInt
12
- | Same left :: Expr BigInt right :: Expr BigInt where a = Bool
13
- | Negate operand :: Expr Bool where a = Bool
14
- | Both first :: Expr b second :: Expr c where a = Pair b c
15
- end
8
+ type Expr (a :: Type) is one of
9
+ | Number of type Integer where a = Integer
10
+ | Truth of type Bool where a = Bool
11
+ | Plus of type record{ left :: Expr Integer right :: Expr Integer} where a = Integer
12
+ | Same of type record{ left :: Expr Integer right :: Expr Integer} where a = Bool
13
+ | Negate of type Expr Bool where a = Bool
14
+ | Both of type record{ first :: Expr b second :: Expr c} where a = Pair b c
15
+ end type
16
16
 
17
17
  -- Matching Number refines a to BigInt in that branch, so v is a value of a.
18
18
  -- eval calls itself at other types (Expr BigInt inside Expr Bool): each type
19
19
  -- it is used at becomes a checked instance, and unreachable branches vanish.
20
- definition eval (e :: Expr a) :: a is
21
- match e with
22
- | Number v -> v
23
- | Truth b -> b
24
- | Plus l r -> eval l + eval r
25
- | Same l r -> eval l == eval r
26
- | Negate x -> !(eval x)
27
- | Both l r -> Pair (eval l) (eval r)
28
- end
29
- end
20
+ function eval (e :: Expr a) -> a is
21
+ first matching case of e :
22
+ when Number v then v
23
+ when Truth b then b
24
+ when Plus record{ left = l , right = r} then eval l + eval r
25
+ when Same record{ left = l , right = r} then eval l = eval r
26
+ when Negate x then !(eval x)
27
+ when Both record{ first = l , second = r} then Pair record{ first = (eval l) , second = (eval r)}
28
+ end case
29
+ end function
30
30
 
31
- evalNumber :: Expr BigInt -> BigInt
31
+ evalNumber :: Expr Integer -> Integer
32
32
  evalTruth :: Expr Bool -> Bool
33
- evalPair :: Expr (Pair BigInt Bool) -> Pair BigInt Bool
33
+ evalPair :: Expr (Pair Integer Bool) -> Pair Integer Bool
34
34
  -- Constant folding: a single number with the same value.
35
- fold :: Expr BigInt -> Expr BigInt
35
+ fold :: Expr Integer -> Expr Integer
36
36
 
37
37
  -- A shown value's type is existential and only the value says what it is:
38
38
  -- each value carries its type, as a witness, to native code.
39
- type Shown is
40
- | Shown value :: b
41
- end
39
+ type Shown is one of
40
+ | Shown of type b
41
+ end type
42
42
 
43
43
  -- The name of the type a shown value holds.
44
44
  describe :: Shown -> Text
45
45
 
46
- law `numbers agree with eval` is
47
- definition is `for all` (e :: Expr BigInt) . evalNumber e = eval e end
48
- end
46
+ law "|numbers agree with eval| is
47
+ definition : for all (e :: Expr Integer) => evalNumber e = eval e end for all end definition
48
+ end law
49
49
 
50
- law `truths agree with eval` is
51
- definition is `for all` (e :: Expr Bool) . evalTruth e = eval e end
52
- end
50
+ law "|truths agree with eval| is
51
+ definition : for all (e :: Expr Bool) => evalTruth e = eval e end for all end definition
52
+ end law
53
53
 
54
- law `pairs agree with eval` is
55
- definition is `for all` (e :: Expr (Pair BigInt Bool)) . evalPair e = eval e end
56
- end
54
+ law "|pairs agree with eval| is
55
+ definition : for all (e :: Expr (Pair Integer Bool)) => evalPair e = eval e end for all end definition
56
+ end law
57
57
 
58
- law `folding keeps the value` is
59
- definition is `for all` (e :: Expr BigInt) . eval (fold e) = eval e end
60
- end
58
+ law "|folding keeps the value| is
59
+ definition : for all (e :: Expr Integer) => eval (fold e) = eval e end for all end definition
60
+ end law
61
61
 
62
- law `a shown number names its type` is
63
- definition is `for all` (x :: Int32) . describe (Shown x) = "Int32" end
64
- end
62
+ law "|a shown number names its type| is
63
+ definition : for all (x :: Int32) => describe (Shown x) = "Int32" end for all end definition
64
+ end law
65
65
 
66
- law `a shown flag names its type` is
67
- definition is `for all` (b :: Bool) . describe (Shown b) = "Bool" end
68
- end
66
+ law "|a shown flag names its type| is
67
+ definition : for all (b :: Bool) => describe (Shown b) = "Bool" end for all end definition
68
+ end law
69
69
 
70
- law `every shown value is described` is
71
- definition is `for all` (s :: Shown) . prelude.length (describe s) > 0 end
72
- end
70
+ law "|every shown value is described| is
71
+ definition : for all (s :: Shown) => prelude.length (describe s) > 0 end for all end definition
72
+ end law
73
73
 
74
- law `two plus three` is
75
- definition is `for all` (x :: BigInt) . eval (Plus (Number 2) (Number 3)) = 5 end
76
- end
74
+ law "|two plus three| is
75
+ definition : for all (x :: Integer) => eval (Plus record{ left = (Number 2) , right = (Number 3)}) = 5 end for all end definition
76
+ end law
77
+ end unit