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
@@ -0,0 +1,53 @@
1
+ unit example.delivery.urr
2
+
3
+ type Delivery is one of
4
+ | Email of type record{recipient of type Text
5
+ document of type record{number of type Text
6
+ file of type record{fileName of type Text
7
+ byteCount of type Whole}}}
8
+ | Download of type record{document of type record{number of type Text
9
+ file of type record{fileName of type Text
10
+ byteCount of type Whole}}}
11
+ end type
12
+
13
+ function "|email invoice PDF file name|
14
+ (delivery of type Delivery) -> Maybe Text is
15
+ first matching case of delivery:
16
+ when Email record{document = record{file = record{fileName = name}}}
17
+ then Just name
18
+ otherwise Nothing
19
+ end case
20
+ end function
21
+
22
+ function emailPdfInvoice (name of type Text) -> Delivery is
23
+ Email (record{recipient = "accounts@example.com", document =
24
+ record{number = "INV-2026-10", file = record{fileName =
25
+ name,
26
+ byteCount = 1200}}})
27
+ end function
28
+
29
+ law "|emailPdfInvoice result| is
30
+ description:
31
+ "PDF invoices sent by email retain their file names."
32
+ end description
33
+ definition:
34
+ "|email invoice PDF file name| (emailPdfInvoice "invoice.pdf") must be Just
35
+ "invoice.pdf"
36
+ end definition
37
+ end law
38
+
39
+ function downloadPdfInvoice (name of type Text) -> Delivery is
40
+ Download (record{document = record{number =
41
+ "INV-2026-10", file = record{fileName = name, byteCount = 1200}}})
42
+ end function
43
+
44
+ law "|downloadPdfInvoice result| is
45
+ description:
46
+ "Downloads have no email invoice PDF file name."
47
+ end description
48
+ definition:
49
+ "|email invoice PDF file name| (downloadPdfInvoice "invoice.pdf") must be
50
+ Nothing
51
+ end definition
52
+ end law
53
+ end unit
@@ -0,0 +1,73 @@
1
+ unit example.delivery.uru
2
+
3
+ type File is one of
4
+ | Pdf of type record{fileName of type Text
5
+ byteCount of type Whole}
6
+ | PlainText of type record{fileName of type Text}
7
+ end type
8
+
9
+ type Delivery is one of
10
+ | Email of type record{recipient of type Text
11
+ document of type record{number of type Text
12
+ file of type File}}
13
+ | Download of type record{document of type record{number of type Text
14
+ file of type File}}
15
+ end type
16
+
17
+ function "|email invoice PDF file name|
18
+ (delivery of type Delivery) -> Maybe Text is
19
+ first matching case of delivery:
20
+ when Email record{document = record{file = Pdf record{fileName = name}}}
21
+ then Just name
22
+ otherwise Nothing
23
+ end case
24
+ end function
25
+
26
+ function emailPdfInvoice (name of type Text) -> Delivery is
27
+ Email (record{recipient = "accounts@example.com", document =
28
+ record{number = "INV-2026-10", file = (Pdf (record{fileName
29
+ = name, byteCount = 1200}))}})
30
+ end function
31
+
32
+ law "|emailPdfInvoice result| is
33
+ description:
34
+ "PDF invoices sent by email retain their file names."
35
+ end description
36
+ definition:
37
+ "|email invoice PDF file name| (emailPdfInvoice "invoice.pdf") must be Just
38
+ "invoice.pdf"
39
+ end definition
40
+ end law
41
+
42
+ function emailPlainTextInvoice (name of type Text) -> Delivery is
43
+ Email (record{recipient = "accounts@example.com", document =
44
+ record{number = "INV-2026-10", file = (PlainText (record{fileName =
45
+ name}))}})
46
+ end function
47
+
48
+ law "|emailPlainTextInvoice result| is
49
+ description:
50
+ "Plain-text invoices have no PDF file name."
51
+ end description
52
+ definition:
53
+ "|email invoice PDF file name| (emailPlainTextInvoice "invoice.pdf") must be
54
+ Nothing
55
+ end definition
56
+ end law
57
+
58
+ function downloadPdfInvoice (name of type Text) -> Delivery is
59
+ Download (record{document = record{number =
60
+ "INV-2026-10", file = (Pdf (record{fileName = name, byteCount =
61
+ 1200}))}})
62
+ end function
63
+
64
+ law "|downloadPdfInvoice result| is
65
+ description:
66
+ "Downloads have no email invoice PDF file name."
67
+ end description
68
+ definition:
69
+ "|email invoice PDF file name| (downloadPdfInvoice "invoice.pdf") must be
70
+ Nothing
71
+ end definition
72
+ end law
73
+ end unit
@@ -0,0 +1,74 @@
1
+ unit example.delivery.uur
2
+
3
+ type Document is one of
4
+ | Invoice of type record{number of type Text
5
+ file of type record{fileName of type Text
6
+ byteCount of type Whole}}
7
+ | Letter of type record{subject of type Text
8
+ file of type record{fileName of type Text
9
+ byteCount of type Whole}}
10
+ end type
11
+
12
+ type Delivery is one of
13
+ | Email of type record{recipient of type Text
14
+ document of type Document}
15
+ | Download of type record{document of type Document}
16
+ end type
17
+
18
+ function "|email invoice PDF file name|
19
+ (delivery of type Delivery) -> Maybe Text is
20
+ first matching case of delivery:
21
+ when Email record{document = Invoice record{file = record{fileName = name}}}
22
+ then Just name
23
+ otherwise Nothing
24
+ end case
25
+ end function
26
+
27
+ function emailPdfInvoice (name of type Text) -> Delivery is
28
+ Email (record{recipient = "accounts@example.com", document =
29
+ (Invoice (record{number = "INV-2026-10", file = record{fileName = name,
30
+ byteCount = 1200}}))})
31
+ end function
32
+
33
+ law "|emailPdfInvoice result| is
34
+ description:
35
+ "PDF invoices sent by email retain their file names."
36
+ end description
37
+ definition:
38
+ "|email invoice PDF file name| (emailPdfInvoice "invoice.pdf") must be Just
39
+ "invoice.pdf"
40
+ end definition
41
+ end law
42
+
43
+ function emailPdfLetter (name of type Text) -> Delivery is
44
+ Email (record{recipient = "accounts@example.com", document =
45
+ (Letter (record{subject = "Receipt", file = record{fileName = name,
46
+ byteCount = 1200}}))})
47
+ end function
48
+
49
+ law "|emailPdfLetter result| is
50
+ description:
51
+ "Letters have no invoice PDF file name."
52
+ end description
53
+ definition:
54
+ "|email invoice PDF file name| (emailPdfLetter "invoice.pdf") must be
55
+ Nothing
56
+ end definition
57
+ end law
58
+
59
+ function downloadPdfInvoice (name of type Text) -> Delivery is
60
+ Download (record{document = (Invoice (record{number = "INV-2026-10", file =
61
+ record{fileName = name,
62
+ byteCount = 1200}}))})
63
+ end function
64
+
65
+ law "|downloadPdfInvoice result| is
66
+ description:
67
+ "Downloads have no email invoice PDF file name."
68
+ end description
69
+ definition:
70
+ "|email invoice PDF file name| (downloadPdfInvoice "invoice.pdf") must be
71
+ Nothing
72
+ end definition
73
+ end law
74
+ end unit
@@ -0,0 +1,96 @@
1
+ unit example.delivery.uuu
2
+
3
+ type File is one of
4
+ | Pdf of type record{fileName of type Text
5
+ byteCount of type Whole}
6
+ | PlainText of type record{fileName of type Text}
7
+ end type
8
+
9
+ type Document is one of
10
+ | Invoice of type record{number of type Text
11
+ file of type File}
12
+ | Letter of type record{subject of type Text
13
+ file of type File}
14
+ end type
15
+
16
+ type Delivery is one of
17
+ | Email of type record{recipient of type Text
18
+ document of type Document}
19
+ | Download of type record{document of type Document}
20
+ end type
21
+
22
+ function "|email invoice PDF file name|
23
+ (delivery of type Delivery) -> Maybe Text is
24
+ first matching case of delivery:
25
+ when Email record{document = Invoice record{file = Pdf record{fileName =
26
+ name}}}
27
+ then Just name
28
+ otherwise Nothing
29
+ end case
30
+ end function
31
+
32
+ function emailPdfInvoice (name of type Text) -> Delivery is
33
+ Email (record{recipient = "accounts@example.com", document =
34
+ (Invoice (record{number = "INV-2026-10", file = (Pdf
35
+ (record{fileName = name, byteCount = 1200}))}))})
36
+ end function
37
+
38
+ law "|emailPdfInvoice result| is
39
+ description:
40
+ "PDF invoices sent by email retain their file names."
41
+ end description
42
+ definition:
43
+ "|email invoice PDF file name| (emailPdfInvoice "invoice.pdf") must be Just
44
+ "invoice.pdf"
45
+ end definition
46
+ end law
47
+
48
+ function emailPlainTextInvoice (name of type Text) -> Delivery is
49
+ Email (record{recipient = "accounts@example.com", document =
50
+ (Invoice (record{number = "INV-2026-10", file = (PlainText
51
+ (record{fileName = name}))}))})
52
+ end function
53
+
54
+ law "|emailPlainTextInvoice result| is
55
+ description:
56
+ "Plain-text invoices have no PDF file name."
57
+ end description
58
+ definition:
59
+ "|email invoice PDF file name| (emailPlainTextInvoice "invoice.pdf") must be
60
+ Nothing
61
+ end definition
62
+ end law
63
+
64
+ function emailPdfLetter (name of type Text) -> Delivery is
65
+ Email (record{recipient = "accounts@example.com", document =
66
+ (Letter (record{subject = "Receipt", file = (Pdf (record{fileName = name,
67
+ byteCount = 1200}))}))})
68
+ end function
69
+
70
+ law "|emailPdfLetter result| is
71
+ description:
72
+ "Letters have no invoice PDF file name."
73
+ end description
74
+ definition:
75
+ "|email invoice PDF file name| (emailPdfLetter "invoice.pdf") must be
76
+ Nothing
77
+ end definition
78
+ end law
79
+
80
+ function downloadPdfInvoice (name of type Text) -> Delivery is
81
+ Download (record{document = (Invoice (record{number = "INV-2026-10", file =
82
+ (Pdf (record{fileName =
83
+ name,
84
+ byteCount = 1200}))}))})
85
+ end function
86
+
87
+ law "|downloadPdfInvoice result| is
88
+ description:
89
+ "Downloads have no email invoice PDF file name."
90
+ end description
91
+ definition:
92
+ "|email invoice PDF file name| (downloadPdfInvoice "invoice.pdf") must be
93
+ Nothing
94
+ end definition
95
+ end law
96
+ end unit
@@ -0,0 +1,127 @@
1
+ unit example.orders
2
+
3
+ type OrderState is one of
4
+ | Pending
5
+ | Authorized
6
+ | Shipped
7
+ | Cancelled
8
+ end type
9
+
10
+ type OrderAction is one of
11
+ | Authorize
12
+ | Dispatch
13
+ | Cancel
14
+ end type
15
+
16
+ function nextState (state of type OrderState)
17
+ (action of type OrderAction) -> OrderState is
18
+ first matching case of action:
19
+ when Authorize then
20
+ if state = Pending then Authorized else state end if
21
+ when Dispatch then
22
+ if state = Authorized then Shipped else state end if
23
+ when Cancel then
24
+ if state = Shipped then Shipped else Cancelled end if
25
+ end case
26
+ end function
27
+
28
+ ability OrderStore is
29
+ current of type OrderState
30
+ change of type OrderState -> ()
31
+ end ability
32
+
33
+ handler inMemory for OrderStore with state order of type OrderState
34
+ start Pending is
35
+ operation current is order end operation
36
+ operation change updated is ~order := updated; () end operation
37
+ end handler
38
+
39
+ procedure perform (action of type OrderAction) -> OrderState uses OrderStore is
40
+ let updated = nextState current action in
41
+ change updated; current end let
42
+ end procedure
43
+
44
+ procedure process (actions of type List OrderAction) -> OrderState
45
+ uses OrderStore is
46
+ first matching case of actions:
47
+ when Nil then current
48
+ when Cons action remaining then
49
+ perform action; process remaining
50
+ end case
51
+ end procedure
52
+
53
+ function runOrder (actions of type List OrderAction) -> OrderState is
54
+ handle process actions with inMemory end handle
55
+ end function
56
+
57
+ function modelRun (state of type OrderState)
58
+ (actions of type List OrderAction) -> OrderState is
59
+ first matching case of actions:
60
+ when Nil then state
61
+ when Cons action remaining then
62
+ modelRun (nextState state action) remaining
63
+ end case
64
+ end function
65
+
66
+ law "|order transitions follow the business lifecycle| is
67
+ definition:
68
+ for all (state of type OrderState) (action of type OrderAction) =>
69
+ (state = Shipped implies nextState state action must be Shipped)
70
+ and (state = Cancelled implies nextState state action must be Cancelled)
71
+ and (state = Pending and action = Authorize implies
72
+ nextState state action must be Authorized)
73
+ and (state = Pending and action = Dispatch implies
74
+ nextState state action must be Pending)
75
+ and (state = Authorized and action = Authorize implies
76
+ nextState state action must be Authorized)
77
+ and (state = Authorized and action = Dispatch implies
78
+ nextState state action must be Shipped)
79
+ and ((state = Pending or state = Authorized) and action = Cancel implies
80
+ nextState state action must be Cancelled)
81
+ end for all
82
+ end definition
83
+ expect nextState state action = expected
84
+ in table (state, action, expected):
85
+ row Pending, Authorize, Authorized
86
+ row Pending, Dispatch, Pending
87
+ row Pending, Cancel, Cancelled
88
+ row Authorized, Authorize, Authorized
89
+ row Authorized, Dispatch, Shipped
90
+ row Authorized, Cancel, Cancelled
91
+ row Shipped, Authorize, Shipped
92
+ row Shipped, Dispatch, Shipped
93
+ row Shipped, Cancel, Shipped
94
+ row Cancelled, Authorize, Cancelled
95
+ row Cancelled, Dispatch, Cancelled
96
+ row Cancelled, Cancel, Cancelled
97
+ end table
98
+ end law
99
+
100
+ law "|processing retains each completed transition| is
101
+ description:
102
+ "Every action observes the state left by the previous action."
103
+ end description
104
+ definition:
105
+ for all (actions of type List OrderAction) =>
106
+ runOrder actions must be modelRun Pending actions
107
+ end for all
108
+ end definition
109
+ expect runOrder actions = expected
110
+ in table (actions, expected):
111
+ row [], Pending
112
+ row [Dispatch], Pending
113
+ row [Authorize], Authorized
114
+ row [Authorize, Dispatch], Shipped
115
+ row [Cancel, Authorize, Dispatch], Cancelled
116
+ row [Authorize, Cancel, Dispatch], Cancelled
117
+ row [Authorize, Dispatch, Cancel], Shipped
118
+ row [Authorize, Dispatch, Authorize], Shipped
119
+ end table
120
+ end law
121
+
122
+ law "|independent orders start pending| is
123
+ definition:
124
+ runOrder [Authorize] must be Authorized and runOrder [] must be Pending
125
+ end definition
126
+ end law
127
+ end unit
@@ -0,0 +1,93 @@
1
+ unit example.refinedPayloads
2
+
3
+ type Measurement (a of type Type) is a record{
4
+ reading of type record{value of type a}
5
+ }
6
+ end type
7
+
8
+ type Parcel is a record{
9
+ details of type record{
10
+ weight of type (measurement of type Measurement Integer
11
+ where measurement.reading.value > 0)
12
+ }
13
+ }
14
+ end type
15
+
16
+ type DeliveryLabel is a record{
17
+ code of type UInt8
18
+ recipient of type Text
19
+ }
20
+ end type
21
+
22
+ type Delivery (a of type Type) is a record{
23
+ contents of type record{value of type a}
24
+ }
25
+ end type
26
+
27
+ function sampleParcel (unused of type ()) -> Parcel is
28
+ Parcel record{details = record{
29
+ weight = Measurement record{reading = record{value = 1}}
30
+ }}
31
+ end function
32
+
33
+ function weightOf (parcel of type record{
34
+ weight of type (measurement of type Measurement Integer
35
+ where measurement.reading.value > 0)
36
+ }) -> Integer is
37
+ parcel.weight.reading.value
38
+ end function
39
+
40
+ function recipient (delivery of type Delivery DeliveryLabel) -> Text is
41
+ delivery.contents.value.recipient
42
+ end function
43
+
44
+ law sampleWeight is
45
+ description:
46
+ "A sample parcel weighs one gram."
47
+ end description
48
+ definition:
49
+ sampleParcel () must be Parcel record{details = record{
50
+ weight = Measurement record{reading = record{value = 1}}
51
+ }}
52
+ end definition
53
+ end law
54
+
55
+ law positiveWeights is
56
+ description:
57
+ "Every accepted parcel measurement has a positive weight."
58
+ end description
59
+ definition:
60
+ for all (parcel of type record{
61
+ weight of type (measurement of type Measurement Integer
62
+ where measurement.reading.value > 0)
63
+ }) =>
64
+ weightOf parcel > 0
65
+ end for all
66
+ end definition
67
+ expect weightOf parcel = expected
68
+ in table (parcel, expected):
69
+ row record{weight = Measurement record{reading = record{value = 1}}}, 1
70
+ row record{weight = Measurement record{reading = record{value = 3}}}, 3
71
+ end table
72
+ end law
73
+
74
+ law recipientLabels is
75
+ description:
76
+ "Deliveries preserve the recipient written on their label."
77
+ end description
78
+ definition:
79
+ for all (delivery of type Delivery DeliveryLabel) =>
80
+ recipient delivery = recipient delivery
81
+ end for all
82
+ end definition
83
+ expect recipient delivery = expected
84
+ in table (delivery, expected):
85
+ row Delivery record{contents = record{value = DeliveryLabel record{
86
+ recipient = "Asha", code = 7
87
+ }}}, "Asha"
88
+ row Delivery record{contents = record{value = DeliveryLabel record{
89
+ code = 8, recipient = "Bo"
90
+ }}}, "Bo"
91
+ end table
92
+ end law
93
+ end unit
@@ -0,0 +1,52 @@
1
+ unit pictures.records
2
+
3
+ type Orientation is one of
4
+ | Landscape
5
+ | Portrait
6
+ | Square
7
+ end type
8
+
9
+ type Picture is a record{
10
+ fileName of type Text
11
+ dimensions of type record{
12
+ width of type Nat
13
+ height of type Nat
14
+ }
15
+ }
16
+ end type
17
+
18
+ function orientation
19
+ (record{dimensions = record{width = width, height = height}} of type Picture)
20
+ -> Orientation is
21
+ first matching case of width:
22
+ when it > height then Landscape
23
+ when it < height then Portrait
24
+ otherwise Square
25
+ end case
26
+ end function
27
+
28
+ law "|named dimensions determine orientation| is
29
+ description:
30
+ "Dimension labels preserve their meaning in either field order."
31
+ end description
32
+ definition:
33
+ for all (picture of type Picture) =>
34
+ orientation picture must be
35
+ if picture.dimensions.width > picture.dimensions.height then Landscape
36
+ else if picture.dimensions.width < picture.dimensions.height then Portrait
37
+ else Square
38
+ end if
39
+ end if
40
+ end for all
41
+ end definition
42
+ expect orientation picture = expected
43
+ in table (picture, expected):
44
+ row Picture record{fileName = "wide.jpg", dimensions = record{width = 12,
45
+ height = 8}}, Landscape
46
+ row Picture record{fileName = "tall.jpg", dimensions = record{height = 12,
47
+ width = 8}}, Portrait
48
+ row Picture record{fileName = "square.jpg", dimensions = record{width = 10,
49
+ height = 10}}, Square
50
+ end table
51
+ end law
52
+ end unit
@@ -0,0 +1,48 @@
1
+ unit pictures.tuples
2
+
3
+ type Orientation is one of
4
+ | Landscape
5
+ | Portrait
6
+ | Square
7
+ end type
8
+
9
+ type Picture is a record{
10
+ fileName of type Text
11
+ dimensions of type (Nat, Nat)
12
+ }
13
+ end type
14
+
15
+ function orientation
16
+ (record{dimensions = (width, height)} of type Picture) -> Orientation is
17
+ first matching case of width:
18
+ when it > height then Landscape
19
+ when it < height then Portrait
20
+ otherwise Square
21
+ end case
22
+ end function
23
+
24
+ law "|tuple dimensions determine orientation| is
25
+ description:
26
+ "Width comes first and height second in a picture's dimensions."
27
+ end description
28
+ definition:
29
+ for all (picture of type Picture) =>
30
+ orientation picture must be
31
+ first matching case of picture.dimensions:
32
+ when (width, height) then
33
+ if width > height then Landscape
34
+ else if width < height then Portrait
35
+ else Square
36
+ end if
37
+ end if
38
+ end case
39
+ end for all
40
+ end definition
41
+ expect orientation picture = expected
42
+ in table (picture, expected):
43
+ row Picture record{fileName = "wide.jpg", dimensions = (12, 8)}, Landscape
44
+ row Picture record{fileName = "tall.jpg", dimensions = (8, 12)}, Portrait
45
+ row Picture record{fileName = "square.jpg", dimensions = (10, 10)}, Square
46
+ end table
47
+ end law
48
+ end unit
@@ -0,0 +1,57 @@
1
+ unit example.payloads
2
+ type Picture is a record{
3
+ fileName of type Text
4
+ dimensions of type record{width of type Nat height of type Nat}
5
+ } end type
6
+ type Delivery is one of
7
+ | Download of type record{document of type Text}
8
+ | Unavailable
9
+ end type
10
+ type Envelope (a of type Type) is a record{payload of type record{value of type
11
+ a copy of type a}} end type
12
+ type Box (a of type Type) is one of | Box of type record{value of type a}
13
+ end type
14
+ function width (picture of type Picture) -> Nat is picture.dimensions.width
15
+ end function
16
+ function label (delivery of type Delivery) -> Text is
17
+ first matching case of delivery:
18
+ when Download record{document = name} then name
19
+ when Unavailable then "unavailable"
20
+ end case
21
+ end function
22
+ function extract (envelope of type Envelope a) -> a is envelope.payload.value
23
+ end function
24
+ function unpack (envelope of type Envelope a) -> record{value of type a copy
25
+ of type a} is envelope.payload end function
26
+ function boxedPayload (box of type Box a) -> record{value of type a} is
27
+ first matching case of box: when Box value then value end case end function
28
+ law pictureWidth is definition:
29
+ width (Picture record{fileName = "photo", dimensions = record{height = 3,
30
+ width = 2}}) must be 2
31
+ end definition end law
32
+ law downloadLabel is definition: label (Download record{document = "paper"})
33
+ must be "paper" end definition end law
34
+ law wrappedText is definition: extract (Envelope record{payload = record{value =
35
+ "note", copy = "other"}}) must be "note" end definition end law
36
+ law wrappedBoolean is definition: extract (Envelope record{payload =
37
+ record{value = true, copy = false}}) must be true end definition end law
38
+ law wholeRecord is definition: unpack (Envelope record{payload = record{value =
39
+ true, copy = false}}) = record{copy = false, value = true} end definition end law
40
+ law boxedResult is definition: boxedPayload (Box record{value = true}) =
41
+ record{value = true} end definition end law
42
+ law boxedRows is definition: for all (box of type Box Text) => boxedPayload box
43
+ = boxedPayload box end for all end definition
44
+ expect (boxedPayload box).value = expected in table (box, expected):
45
+ row Box record{value = "note"}, "note"
46
+ row Box record{value = "paper"}, "paper"
47
+ end table end law
48
+ law labels is
49
+ definition: for all (delivery of type Delivery) => label delivery = label
50
+ delivery end for all end definition
51
+ expect label delivery = expected
52
+ in table (delivery, expected):
53
+ row Download record{document = "paper"}, "paper"
54
+ row Unavailable, "unavailable"
55
+ end table
56
+ end law
57
+ end unit