26 lines
917 B
Plaintext
26 lines
917 B
Plaintext
!import "prelude" !Local
|
|
|
|
-- Custom contract combinators built directly on matchResult.
|
|
myAndC = (c1 c2 value rest :
|
|
matchResult
|
|
(msg _ : contractErr msg rest)
|
|
(v _ : c2 v rest)
|
|
(c1 value rest))
|
|
|
|
-- Plain predicates lifted into contracts with a diagnostic message.
|
|
natural? = guardC "natural" isNat?
|
|
nonZero? = guardC "non-zero" (n : and? (isNat? n) (not? (isZero_? n)))
|
|
|
|
-- Phantom annotations let point-free definitions carry their own contracts.
|
|
-- The base library now uses the same syntax, so head/tail/div etc. are
|
|
-- guarded by default.
|
|
myHead @(nonEmptyListOf anyC) =@anyC head
|
|
myTail @(listOf anyC) =@(listOf anyC) tail
|
|
myDiv @natural? @(myAndC natural? nonZero?) =@natural? div
|
|
|
|
-- The `check` helper applies a contract to any value and returns the
|
|
-- checked value (or the diagnostic message on failure).
|
|
checkedSuccessor = check natural? (add 1 2)
|
|
|
|
main = pair checkedSuccessor (myDiv 10 2)
|