From 5471af45fd04169eb184371dcd8f791e507eab6f Mon Sep 17 00:00:00 2001 From: Alasdair Armstrong Date: Wed, 24 Oct 2018 17:25:47 +0100 Subject: Add constraint synonyms Currently not enabled by default, the flag -Xconstraint_synonyms enables them For generating constraints in ASL parser, we want to be able to give names to the constraints that we attach to certain variables. It's slightly awkward right now when constraints get long complicated because the entire constraint always has to be typed out in full whenever it appears, and there's no way to abstract away from that. This adds constraint synonyms, which work much like type synonyms except for constraints, e.g. constraint Size('n) = 'n in {1, 2, 4, 8} | 128 <= 'n <= 256 these constraints can then be used instead of the full constraint, e.g. val f : forall 'n, where Size('n). int('n) -> unit Unfortunatly we need to have a keyword to 'call' the constraint synonym otherwise the grammer stops being LR(1). This could be resolved by parsing all constraints into Parse_ast.atyp and then de-sugaring them into constraints, which is what happens for n-expressions already, but that would require quite a bit of work on the parser. To avoid this forcing changes to any other parts of Sail, the intended invariant is that all constraints appearing anywhere in a type-checked AST have no constraint synonyms, so they don't have to worry about matching on NC_app, or calling Env.expand_typquant_synonyms (which isn't even exported for this reason). --- language/sail.ott | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'language/sail.ott') diff --git a/language/sail.ott b/language/sail.ott index 2edffcbe..59d51d68 100644 --- a/language/sail.ott +++ b/language/sail.ott @@ -293,6 +293,7 @@ n_constraint :: 'NC_' ::= | kid 'IN' { num1 , ... , numn } :: :: set | n_constraint \/ n_constraint' :: :: or | n_constraint /\ n_constraint' :: :: and + | id ( nexp1 , ... , nexpn ) :: :: app | true :: :: true | false :: :: false @@ -1012,29 +1013,30 @@ prec :: '' ::= def :: 'DEF_' ::= {{ com top-level definition }} {{ auxparam 'a }} - | kind_def :: :: kind + | kind_def :: :: kind {{ com definition of named kind identifiers }} - | type_def :: :: type + | type_def :: :: type {{ com type definition }} - | fundef :: :: fundef + | fundef :: :: fundef {{ com function definition }} - | mapdef :: :: mapdef + | mapdef :: :: mapdef {{ com mapping definition }} - | letbind :: :: val + | letbind :: :: val {{ com value definition }} - | val_spec :: :: spec + | val_spec :: :: spec {{ com top-level type constraint }} - | fix prec num id :: :: fixity + | fix prec num id :: :: fixity {{ com fixity declaration }} - | overload id [ id1 ; ... ; idn ] :: :: overload + | overload id [ id1 ; ... ; idn ] :: :: overload {{ com operator overload specification }} - | default_spec :: :: default + | default_spec :: :: default {{ com default kind and type assumptions }} - | scattered_def :: :: scattered + | scattered_def :: :: scattered {{ com scattered function and type definition }} - | dec_spec :: :: reg_dec + | dec_spec :: :: reg_dec {{ com register declaration }} - | fundef1 .. fundefn :: I :: internal_mutrec + | constraint id ( kid1 , ... , kidn ) = n_constraint :: :: constraint + | fundef1 .. fundefn :: I :: internal_mutrec {{ com internal representation of mutually recursive functions }} defs :: '' ::= -- cgit v1.2.3