Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce validators for building custom contracts #1970

Merged
merged 8 commits into from
Jun 24, 2024

Conversation

yannham
Copy link
Member

@yannham yannham commented Jun 19, 2024

Depends on #1964. Closes #1957.

Motivation

There's currently two constructors for custom contracts: from_predicate, which takes a boolean predicate and turn it into a contract, and custom, which takes a general partial identity.

The former is nice because it's simple to write (and will support boolean operations). However, it can't customize error messages, which is important for developer experience.

The latter is the most general, but it's not very natural to write, as we need to call blame and use label helpers to attach error data, and because we need to return the original value instead of just "ok" or "not ok".

This PR introduces a third form, in the middle, called validators. Validators leverage the enum variants of Nickel to cover the case of eager contracts (like predicates) but with all the customization power of custom. Basically, instead of returning a boolean, a validator returns [| 'Ok, 'Error <ErrorData> |] where ErrorData mimics the structure of a label, but as a normal Nickel record. Because it's eager, it's trivially convertible to a predicate, so validators will be usable with boolean operators.

The hope is that many concrete cases are actually validator, which are simpler to write than general custom contracts, and have much better properties (they can be negated, or-ed, etc.).

Content

This PR:

  • Adds a new custom contract variant in the AST, Validator
  • Adds the corresponding primop to build them, %contract/from_validator%, as well as the corresponding stdlib wrapper std.contract.from_validator
  • Update the examples and the stdlib to use validators whenever possible, instead of general custom contracts
  • Update the contracts section of the manual to introduce validators

@github-actions github-actions bot temporarily deployed to pull request June 19, 2024 17:48 Inactive
@yannham yannham changed the title Feat/contract from validator Introduce validators for building custom contracts Jun 19, 2024
@yannham yannham requested review from jneem and vkleen June 19, 2024 17:53
@yannham yannham marked this pull request as ready for review June 19, 2024 17:54
@github-actions github-actions bot temporarily deployed to pull request June 20, 2024 09:29 Inactive
core/src/term/mod.rs Show resolved Hide resolved
core/src/term/mod.rs Outdated Show resolved Hide resolved
core/src/typecheck/operation.rs Outdated Show resolved Hide resolved
core/stdlib/std.ncl Outdated Show resolved Hide resolved
core/stdlib/std.ncl Outdated Show resolved Hide resolved
@github-actions github-actions bot temporarily deployed to pull request June 24, 2024 10:10 Inactive
Base automatically changed from task/contract-from-custom to master June 24, 2024 10:28
Copy link

dpulls bot commented Jun 24, 2024

🎉 All dependencies have been resolved !

@yannham yannham force-pushed the feat/contract-from-validator branch from 7c96286 to 3e0476e Compare June 24, 2024 12:37
@yannham yannham force-pushed the feat/contract-from-validator branch from 3e0476e to f348ded Compare June 24, 2024 12:38
@github-actions github-actions bot temporarily deployed to pull request June 24, 2024 12:41 Inactive
@yannham yannham added this pull request to the merge queue Jun 24, 2024
Merged via the queue into master with commit 9969d9a Jun 24, 2024
5 checks passed
@yannham yannham deleted the feat/contract-from-validator branch June 24, 2024 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Variant of std.contract.from_predicate to specify an error message
2 participants