From 3af27545ef9260bd48b89ac244195349a9672724 Mon Sep 17 00:00:00 2001 From: Yann Hamdaoui Date: Wed, 19 Jun 2024 12:08:39 +0200 Subject: [PATCH 1/8] Add validator as a form of custom contract --- core/src/pretty.rs | 32 ++++++++++++-------- core/src/term/mod.rs | 50 ++++++++++++++++++------------- core/src/transform/free_vars.rs | 1 + core/src/typecheck/mk_uniftype.rs | 2 +- core/src/typecheck/mod.rs | 48 +++++++++++++++++++++++++---- 5 files changed, 93 insertions(+), 40 deletions(-) diff --git a/core/src/pretty.rs b/core/src/pretty.rs index 9c4cad7a6f..2bb5175e05 100644 --- a/core/src/pretty.rs +++ b/core/src/pretty.rs @@ -142,7 +142,7 @@ fn needs_parens_in_type_pos(typ: &Type) -> bool { term.as_ref(), Term::Fun(..) | Term::FunPattern(..) - | Term::CustomContract(CustomContract::Predicate(..)) + | Term::CustomContract(_) | Term::Let(..) | Term::LetPattern(..) | Term::Op1(UnaryOp::IfThenElse, _) @@ -821,22 +821,28 @@ where Str(v) => allocator.escaped_string(v).double_quotes(), StrChunks(chunks) => allocator.chunks(chunks, StringRenderStyle::Multiline), Fun(id, body) => allocator.function(allocator.as_string(id), body), - CustomContract(ContractNode::PartialIdentity(ctr)) => docs![ - allocator, - "%contract/custom%", - docs![allocator, allocator.line(), ctr.pretty(allocator).parens()] + // Format this as the primop application ` `. + CustomContract(contract_node) => { + let (constructor, contract) = match contract_node { + ContractNode::Predicate(p) => ("%contract/from_predicate%", p), + ContractNode::Validator(v) => ("%contract/from_validator%", v), + ContractNode::PartialIdentity(pid) => ("%contract/custom%", pid), + }; + + docs![ + allocator, + constructor, + docs![ + allocator, + allocator.line(), + contract.pretty(allocator).parens() + ] .nest(2) .group() - ], + ] + } FunPattern(pat, body) => allocator.function(allocator.pat_with_parens(pat), body), // Format this as the application `std.contract.from_predicate `. - CustomContract(ContractNode::Predicate(pred)) => docs![ - allocator, - "%contract/from_predicate%", - docs![allocator, allocator.line(), pred.pretty(allocator).parens()] - .nest(2) - .group() - ], Lbl(_lbl) => allocator.text("%