Add row polymorphism macros to Michelson
With Babylon, we have entrypoints: the contract parameter is a tagged union of types, where each element represents an individual entrypoint.
This idea could be extended to product types:
- Construction behaves the same
- A destruction macro is added, equivalent to some combination of
CAR
's andCDR
's:
:: 'a : 'A -> 'b : 'A
iff field_type 'label 'a :: 'b
GET_FIELD 'label / a : S => b : S
- An update macro is added:
:: 'b : 'a : 'A -> 'b : 'A
iff field_type 'label 'a :: 'b
SET_FIELD 'label / b : a : S => b : S
-
DIP_FIELD
? -
contract t
then would represent any contract with an entrypointt
with at most the given product elements.- E.g.
contract (pair (nat %counter) (bool %verbose))
may refer to a contract with parameter type(nat %counter)
- E.g.
What are the benefits?
- Code/lambdas accessing product elements may be composed without manually adapting them to different products
- Code to access some deeply-nested element of a product may be determined from the choice of product element
- Entrypoints become more flexible, allowing callers to provide at least the given arguments
- Callbacks become more flexible: e.g. if a contract accepts a callback of parameter type
(pair (nat %account_balance) (bool %is_admin))
, we can provide a callback of type(nat %account_balance)
and ignore the(bool %is_admin)
field - High-level code that compiles to Michelson could benefit
- In theory,
GET_FIELD
may be compiled to accessing a struct of all fields, which is more efficient than traversing a deeply-nested composition ofpair
s.