Merge pull request #3 from CatalaLang/ir

Rework of the desugared ast and lambda reprentation
This commit is contained in:
Nicolas Chataing 2020-05-26 11:30:35 +02:00 committed by GitHub
commit 9fe51225f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 99 additions and 66 deletions

View File

@ -71,13 +71,17 @@ type constructor = Struct of Struct.t | Enum of Enum.t
(* Type *)
type primitive_typ = Integer | Decimal | Boolean | Money | Text | Date | Named of constructor
type primitive_typ =
| Integer
| Decimal
| Boolean
| Money
| Text
| Date
| Named of constructor
| Unit
type base_typ_data = {
typ_data_collection : Pos.t option;
typ_data_optional : Pos.t option;
typ_data_base : primitive_typ Pos.marked;
}
type base_typ_data = TVec of base_typ_data | TOption of base_typ_data | TPrim of primitive_typ
type base_typ = Condition | Data of base_typ_data
@ -88,7 +92,10 @@ type typ = Base of base_typ | Func of func_typ
(* Expressions *)
(* The [bool] argument is true if the match case introduces a pattern *)
type match_case_pattern = constructor Pos.marked list * bool
type match_case_pattern =
| PEnum of EnumCase.t Pos.marked * match_case_pattern
| PVar of Pos.t
| PWild
type binop = And | Or | Add | Sub | Mult | Div | Lt | Lte | Gt | Gte | Eq | Neq
@ -98,67 +105,64 @@ type builtin_expression = Cardinal | Now
type aggregate_func = AggregateSum | AggregateCount
type litteral_date = {
litteral_date_day : int Pos.marked;
litteral_date_month : int Pos.marked;
litteral_date_year : int Pos.marked;
type literal_date = {
literal_date_day : int Pos.marked;
literal_date_month : int Pos.marked;
literal_date_year : int Pos.marked;
}
type litteral_number = Int of int | Dec of int * int
type literal_number = Int of int | Dec of int * int
type litteral_unit = Percent | Euro | Year | Month | Day
type literal_unit = Percent | Euro | Year | Month | Day
type collection_op = Exists | Forall | Aggregate of aggregate_func
type litteral =
| Number of litteral_number Pos.marked * litteral_unit Pos.marked option
| Date of litteral_date
type literal =
| Number of literal_number Pos.marked * literal_unit Pos.marked option
| Date of literal_date
type match_case = {
match_case_pattern : match_case_pattern Pos.marked;
match_case_expr : expression Pos.marked;
match_case_expr : expression;
}
and match_cases = match_case Pos.marked list
and expression =
| MatchWith of expression Pos.marked * match_cases Pos.marked
| IfThenElse of expression Pos.marked * expression Pos.marked * expression Pos.marked
| Binop of binop Pos.marked * expression Pos.marked * expression Pos.marked
| Unop of unop Pos.marked * expression Pos.marked
| CollectionOp of collection_op Pos.marked * Var.t * expression Pos.marked * expression Pos.marked
| MemCollection of expression Pos.marked * expression Pos.marked
| TestMatchCase of expression Pos.marked * constructor Pos.marked
| FunCall of expression Pos.marked * expression Pos.marked
| Builtin of builtin_expression
| Literal of litteral
| Inject of constructor Pos.marked * expression Pos.marked option
| Project of expression Pos.marked * constructor Pos.marked
and expression = expression' Pos.marked
and expression' =
| MatchWith of expression * match_cases Pos.marked
| IfThenElse of expression * expression * expression
| Binop of binop Pos.marked
| Unop of unop Pos.marked
| CollectionOp of collection_op Pos.marked * Var.t * expression * expression
| MemCollection of expression * expression
| TestMatchCase of expression * EnumCase.t Pos.marked
| FunCall of expression * expression
| Builtin of builtin_expression Pos.marked
| Literal of literal
| Inject of constructor Pos.marked * expression option
| Project of expression * constructor Pos.marked
| BindingParameter of int (* The integer is the De Bruijn index *)
| Var of Var.t Pos.marked
(* Struct declaration *)
type struct_decl_field = {
struct_decl_field_name : StructField.t Pos.marked;
struct_decl_field_typ : typ Pos.marked;
}
(* Wrappers *)
type struct_decl = { struct_decl_fields : struct_decl_field Pos.marked list }
type 'a with_type = { value : 'a Pos.marked; typ : typ Pos.marked }
(* Struct declaration *)
type struct_decl_field = StructField.t with_type
type struct_decl = struct_decl_field Pos.marked list
(* Enum declaration *)
type enum_decl_case = {
enum_decl_case_name : EnumCase.t Pos.marked;
enum_decl_case_typ : typ Pos.marked option;
}
type enum_decl_case = EnumCase.t with_type
type enum_decl = { enum_decl_cases : enum_decl_case Pos.marked list }
type enum_decl = enum_decl_case Pos.marked list
(* Scopes *)
type scope_context_item = {
scope_context_item_name : ScopeParam.t Pos.marked;
scope_context_item_typ : typ Pos.marked;
}
type scope_context_item = ScopeParam.t with_type
type scope_include_join = {
parent_scope_name : Scope.t Pos.marked;
@ -171,22 +175,21 @@ type scope_include = {
scope_include_joins : scope_include_join Pos.marked list;
}
type binder = string Pos.marked
type rule = {
rule_parameter : Pos.t option;
rule_condition : expression Pos.marked option;
rule_parameter : binder option;
rule_condition : expression option;
rule_consequence : bool;
}
type definition = {
definition_parameter : Pos.t option;
definition_condition : expression Pos.marked option;
definition_expr : expression Pos.marked;
definition_parameter : binder option;
definition_condition : expression option;
definition_expr : expression;
}
type assertion = {
assertion_condition : expression Pos.marked option;
assertion_content : expression Pos.marked;
}
type assertion = expression
type variation_typ = Increasing | Decreasing
@ -194,7 +197,7 @@ type reference_typ = Decree | Law
type meta_assertion =
| FixedBy of reference_typ Pos.marked
| VariesWith of expression Pos.marked * variation_typ Pos.marked option
| VariesWith of expression * variation_typ Pos.marked option
type scope = {
scope_var_map : qident VarMap.t;
@ -209,9 +212,13 @@ type scope = {
module EnumMap = Map.Make (Enum)
module ScopeMap = Map.Make (Scope)
module StructMap = Map.Make (Struct)
module StructFieldMap = Map.Make (StructField)
module EnumCaseMap = Map.Make (EnumCase)
type prgm = {
enums : enum_decl EnumMap.t;
scopes : scope ScopeMap.t;
structs : struct_decl StructMap.t;
struct_fields : Struct.t StructFieldMap.t;
enum_cases : Enum.t EnumCaseMap.t;
}

View File

@ -12,20 +12,20 @@
or implied. See the License for the specific language governing permissions and limitations under
the License. *)
type primitive_typ = TInteger | TDecimal | TBoolean | TMoney | TText | TDate
type primitive_typ = TInteger | TDecimal | TBoolean | TMoney | TText | TDate | Unit
type typ_bound = int
type styp =
| TPrimitive of primitive_typ
| TBound of typ_bound
| TFun of styp * styp
| TSum of styp list
| TArrow of styp * styp
| TSum of Ir.Enum.t
| TVec of styp
type typ = TTyp of styp | TPoly of typ
type const = Ir.litteral
type const = Ir.literal
type arith_binop = Add | Sub | Mult | Div | Lt | Lte | Gt | Gte | Eq | Neq
@ -39,16 +39,16 @@ type binding = Ir.Var.t * typ
type enum_case = Ir.EnumCase.t
type term = untyped_term Pos.marked * typ option
type term = untyped_term Pos.marked * typ
and untyped_term =
| EConst of const
| EOp of op
| EBuiltin of builtin
| EIfThenElse
| EExists
| EForall
| EVar of binding
| EIfThenElse of term * term * term
| EExists of binding * term * term
| EForall of binding * term * term
| EVar of int
| EFun of binding list * term
| EApp of term * term list
| EInj of enum_case * term
@ -56,6 +56,32 @@ and untyped_term =
| EPolyIntro of term
| EPolyApp of term * typ
type program_with_default_logic = term list Ir.VarMap.t
(* Wrappers *)
type program_without_default_logic = term Ir.VarMap.t
type 'expr field = {
field_parameters : Ir.scope_context_item Pos.marked list;
field_rules : 'expr Ir.VarMap.t;
field_defs : 'expr Ir.VarMap.t;
field_assertion : term list;
}
type 'expr program = {
enums : Ir.enum_decl Ir.EnumMap.t;
structs : Ir.struct_decl Ir.StructMap.t;
fields : 'expr field Ir.ScopeMap.t;
}
type program_with_normal_logic = term program
module IntMap = Map.Make (Int)
type precondition = term
type consequence = term
type default_term = {
defaults : (precondition * consequence) IntMap.t;
ordering : (int * int) list;
}
type program_with_default_logic = default_term program