1
1
mirror of https://github.com/tweag/nickel.git synced 2024-10-07 00:30:47 +03:00
nickel/examples/simple-contracts
Mathieu Boespflug b0e0311267 Switch to snake_case by convention for values
This renames all builtins and standard library functions to conform to
a snake case convention, as per decision recorded in #493. This also
adapts examples and tests to this change. All functions have been
renamed the way you'd expect, except the following:

* `fromPred` -> `from_predicate`
* `typeOf` -> `typeof` (like in C)
* `fieldsOf` -> `fields` (the `_of` is redundant for a function)
* `valuesOf` -> `values` (the `_of` is redundant for a function)

Closes #493
2021-12-28 23:40:49 +02:00
..
README.md Add blameWith and fromPred, update examples accordingly 2021-11-23 15:31:35 +01:00
simple-contract-bool.ncl Switch to snake_case by convention for values 2021-12-28 23:40:49 +02:00
simple-contract-div.ncl Switch to snake_case by convention for values 2021-12-28 23:40:49 +02:00

Simple contracts

This directory contain two examples of simple contrtacts.

The first one, simple-contract-bool.ncl, defines simple contracts on booleans and illustrates their use as pre-conditions and post-conditions of a function.

The second one, simple-contract-div.ncl, defines simple contracts on numbers and illustrates the application of several contracts to one value. It is expected to fail, to demonstrate basic error-reporting for contracts.

For illustrative purpose, those contracts are written as plain custom contracts: functions Lbl -> Dyn -> Dyn. However, they are just boolean predicates here. In this case, you should rather write them more succintly as predicates Dyn -> Bool and use contracts.fromPred to obtain the corresponding contracts.

Run

$ nickel -f simple-contract-bool.ncl
$ nickel -f simple-contract-div.ncl

Playground

You can try to break the one of the contracts of simple-contract-bool.ncl to see them fail. Conversely, you can provide a valid value in simple-contract-div.ncl to see the execution succeed.