2022-03-08 17:03:14 +03:00
|
|
|
(* This file is part of the Catala compiler, a specification language for tax
|
|
|
|
and social benefits computation rules. Copyright (C) 2020 Inria, contributor:
|
|
|
|
Nicolas Chataing <nicolas.chataing@ens.fr>
|
2020-06-22 17:16:55 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
the License at
|
2020-06-22 17:16:55 +03:00
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
License for the specific language governing permissions and limitations under
|
2020-06-22 17:16:55 +03:00
|
|
|
the License. *)
|
|
|
|
|
2020-12-14 19:00:42 +03:00
|
|
|
(** Abstract syntax tree of the desugared representation *)
|
|
|
|
|
2021-01-21 23:33:04 +03:00
|
|
|
open Utils
|
2020-11-23 14:20:38 +03:00
|
|
|
|
2020-12-14 19:00:42 +03:00
|
|
|
(** {1 Names, Maps and Keys} *)
|
|
|
|
|
|
|
|
module IdentMap : Map.S with type key = String.t = Map.Make (String)
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
module RuleName : Uid.Id with type info = Uid.MarkedString.info =
|
|
|
|
Uid.Make (Uid.MarkedString) ()
|
2020-11-25 16:35:26 +03:00
|
|
|
|
2020-12-14 19:00:42 +03:00
|
|
|
module RuleMap : Map.S with type key = RuleName.t = Map.Make (RuleName)
|
2020-12-18 17:59:15 +03:00
|
|
|
module RuleSet : Set.S with type elt = RuleName.t = Set.Make (RuleName)
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
module LabelName : Uid.Id with type info = Uid.MarkedString.info =
|
|
|
|
Uid.Make (Uid.MarkedString) ()
|
2022-01-04 20:19:15 +03:00
|
|
|
|
|
|
|
module LabelMap : Map.S with type key = LabelName.t = Map.Make (LabelName)
|
|
|
|
module LabelSet : Set.S with type elt = LabelName.t = Set.Make (LabelName)
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
module StateName : Uid.Id with type info = Uid.MarkedString.info =
|
|
|
|
Uid.Make (Uid.MarkedString) ()
|
2022-02-28 17:40:19 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
module ScopeVar : Uid.Id with type info = Uid.MarkedString.info =
|
|
|
|
Uid.Make (Uid.MarkedString) ()
|
2022-02-28 19:19:06 +03:00
|
|
|
|
|
|
|
module ScopeVarSet : Set.S with type elt = ScopeVar.t = Set.Make (ScopeVar)
|
|
|
|
module ScopeVarMap : Map.S with type key = ScopeVar.t = Map.Make (ScopeVar)
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(** Inside a scope, a definition can refer either to a scope def, or a subscope
|
|
|
|
def *)
|
2020-11-23 14:20:38 +03:00
|
|
|
module ScopeDef = struct
|
|
|
|
type t =
|
2022-02-28 19:19:06 +03:00
|
|
|
| Var of ScopeVar.t * StateName.t option
|
|
|
|
| SubScopeVar of Scopelang.Ast.SubScopeName.t * ScopeVar.t
|
2022-03-08 17:03:14 +03:00
|
|
|
(** In this case, the [ScopeVar.t] lives inside the context of the
|
|
|
|
subscope's original declaration *)
|
2020-11-23 14:20:38 +03:00
|
|
|
|
|
|
|
let compare x y =
|
2022-05-04 16:48:03 +03:00
|
|
|
match x, y with
|
2022-02-28 19:19:06 +03:00
|
|
|
| Var (x, None), Var (y, None)
|
|
|
|
| Var (x, Some _), Var (y, None)
|
|
|
|
| Var (x, None), Var (y, Some _)
|
|
|
|
| Var (x, _), SubScopeVar (_, y)
|
|
|
|
| SubScopeVar (_, x), Var (y, _) ->
|
2022-05-04 18:40:55 +03:00
|
|
|
ScopeVar.compare x y
|
2022-02-28 19:19:06 +03:00
|
|
|
| Var (x, Some sx), Var (y, Some sy) ->
|
2022-05-04 18:40:55 +03:00
|
|
|
let cmp = ScopeVar.compare x y in
|
|
|
|
if cmp = 0 then StateName.compare sx sy else cmp
|
2021-01-11 17:19:05 +03:00
|
|
|
| SubScopeVar (x', x), SubScopeVar (y', y) ->
|
2022-05-04 18:40:55 +03:00
|
|
|
let cmp = Scopelang.Ast.SubScopeName.compare x' y' in
|
|
|
|
if cmp = 0 then ScopeVar.compare x y else cmp
|
2020-11-23 14:20:38 +03:00
|
|
|
|
2020-12-18 17:59:15 +03:00
|
|
|
let get_position x =
|
|
|
|
match x with
|
2022-05-30 12:20:48 +03:00
|
|
|
| Var (x, None) -> Marked.get_mark (ScopeVar.get_info x)
|
|
|
|
| Var (_, Some sx) -> Marked.get_mark (StateName.get_info sx)
|
2022-03-08 17:03:14 +03:00
|
|
|
| SubScopeVar (x, _) ->
|
2022-05-30 12:20:48 +03:00
|
|
|
Marked.get_mark (Scopelang.Ast.SubScopeName.get_info x)
|
2020-12-18 17:59:15 +03:00
|
|
|
|
2020-11-25 13:53:56 +03:00
|
|
|
let format_t fmt x =
|
2020-11-23 14:20:38 +03:00
|
|
|
match x with
|
2022-02-28 19:19:06 +03:00
|
|
|
| Var (v, None) -> ScopeVar.format_t fmt v
|
2022-03-08 17:03:14 +03:00
|
|
|
| Var (v, Some sv) ->
|
2022-05-04 18:40:55 +03:00
|
|
|
Format.fprintf fmt "%a.%a" ScopeVar.format_t v StateName.format_t sv
|
2020-11-23 14:20:38 +03:00
|
|
|
| SubScopeVar (s, v) ->
|
2022-05-04 18:40:55 +03:00
|
|
|
Format.fprintf fmt "%a.%a" Scopelang.Ast.SubScopeName.format_t s
|
|
|
|
ScopeVar.format_t v
|
2020-11-23 14:20:38 +03:00
|
|
|
|
2020-11-23 18:12:45 +03:00
|
|
|
let hash x =
|
|
|
|
match x with
|
2022-02-28 19:19:06 +03:00
|
|
|
| Var (v, None) -> ScopeVar.hash v
|
|
|
|
| Var (v, Some sv) -> Int.logxor (ScopeVar.hash v) (StateName.hash sv)
|
2022-03-08 17:03:14 +03:00
|
|
|
| SubScopeVar (w, v) ->
|
2022-05-04 18:40:55 +03:00
|
|
|
Int.logxor (Scopelang.Ast.SubScopeName.hash w) (ScopeVar.hash v)
|
2020-11-23 14:20:38 +03:00
|
|
|
end
|
|
|
|
|
2020-12-14 19:00:42 +03:00
|
|
|
module ScopeDefMap : Map.S with type key = ScopeDef.t = Map.Make (ScopeDef)
|
|
|
|
module ScopeDefSet : Set.S with type elt = ScopeDef.t = Set.Make (ScopeDef)
|
|
|
|
|
|
|
|
(** {1 AST} *)
|
2020-06-22 17:16:55 +03:00
|
|
|
|
2022-02-28 19:19:06 +03:00
|
|
|
type location =
|
2022-05-30 12:20:48 +03:00
|
|
|
| ScopeVar of ScopeVar.t Marked.pos * StateName.t option
|
2022-02-28 19:19:06 +03:00
|
|
|
| SubScopeVar of
|
2022-03-08 17:03:14 +03:00
|
|
|
Scopelang.Ast.ScopeName.t
|
2022-05-30 12:20:48 +03:00
|
|
|
* Scopelang.Ast.SubScopeName.t Marked.pos
|
|
|
|
* ScopeVar.t Marked.pos
|
2022-02-28 19:19:06 +03:00
|
|
|
|
2022-05-30 12:20:48 +03:00
|
|
|
module LocationSet : Set.S with type elt = location Marked.pos =
|
2022-03-08 17:03:14 +03:00
|
|
|
Set.Make (struct
|
2022-05-30 12:20:48 +03:00
|
|
|
type t = location Marked.pos
|
2022-02-28 19:19:06 +03:00
|
|
|
|
|
|
|
let compare x y =
|
2022-05-30 12:20:48 +03:00
|
|
|
match Marked.unmark x, Marked.unmark y with
|
2022-02-28 19:19:06 +03:00
|
|
|
| ScopeVar (vx, None), ScopeVar (vy, None)
|
|
|
|
| ScopeVar (vx, Some _), ScopeVar (vy, None)
|
|
|
|
| ScopeVar (vx, None), ScopeVar (vy, Some _) ->
|
2022-05-30 12:20:48 +03:00
|
|
|
ScopeVar.compare (Marked.unmark vx) (Marked.unmark vy)
|
2022-02-28 19:19:06 +03:00
|
|
|
| ScopeVar ((x, _), Some sx), ScopeVar ((y, _), Some sy) ->
|
2022-05-04 18:40:55 +03:00
|
|
|
let cmp = ScopeVar.compare x y in
|
|
|
|
if cmp = 0 then StateName.compare sx sy else cmp
|
2022-03-08 17:03:14 +03:00
|
|
|
| ( SubScopeVar (_, (xsubindex, _), (xsubvar, _)),
|
|
|
|
SubScopeVar (_, (ysubindex, _), (ysubvar, _)) ) ->
|
2022-05-04 18:40:55 +03:00
|
|
|
let c = Scopelang.Ast.SubScopeName.compare xsubindex ysubindex in
|
|
|
|
if c = 0 then ScopeVar.compare xsubvar ysubvar else c
|
2022-02-28 19:19:06 +03:00
|
|
|
| ScopeVar _, SubScopeVar _ -> -1
|
|
|
|
| SubScopeVar _, ScopeVar _ -> 1
|
|
|
|
end)
|
|
|
|
|
2022-06-03 17:40:03 +03:00
|
|
|
type marked_expr = expr Marked.pos
|
2022-03-08 17:03:14 +03:00
|
|
|
(** The expressions use the {{:https://lepigre.fr/ocaml-bindlib/} Bindlib}
|
|
|
|
library, based on higher-order abstract syntax*)
|
2022-06-03 17:40:03 +03:00
|
|
|
|
|
|
|
and expr =
|
2022-02-28 19:19:06 +03:00
|
|
|
| ELocation of location
|
2022-06-03 17:40:03 +03:00
|
|
|
| EVar of expr Bindlib.var
|
2022-03-08 17:03:14 +03:00
|
|
|
| EStruct of
|
2022-06-03 17:40:03 +03:00
|
|
|
Scopelang.Ast.StructName.t * marked_expr Scopelang.Ast.StructFieldMap.t
|
2022-03-08 17:03:14 +03:00
|
|
|
| EStructAccess of
|
2022-06-03 17:40:03 +03:00
|
|
|
marked_expr * Scopelang.Ast.StructFieldName.t * Scopelang.Ast.StructName.t
|
2022-03-08 17:03:14 +03:00
|
|
|
| EEnumInj of
|
2022-06-03 17:40:03 +03:00
|
|
|
marked_expr * Scopelang.Ast.EnumConstructor.t * Scopelang.Ast.EnumName.t
|
2022-02-28 19:19:06 +03:00
|
|
|
| EMatch of
|
2022-06-03 17:40:03 +03:00
|
|
|
marked_expr
|
2022-02-28 19:19:06 +03:00
|
|
|
* Scopelang.Ast.EnumName.t
|
2022-06-03 17:40:03 +03:00
|
|
|
* marked_expr Scopelang.Ast.EnumConstructorMap.t
|
2022-02-28 19:19:06 +03:00
|
|
|
| ELit of Dcalc.Ast.lit
|
2022-03-08 17:03:14 +03:00
|
|
|
| EAbs of
|
2022-06-03 17:40:03 +03:00
|
|
|
(expr, marked_expr) Bindlib.mbinder * Scopelang.Ast.typ Marked.pos list
|
|
|
|
| EApp of marked_expr * marked_expr list
|
2022-02-28 19:19:06 +03:00
|
|
|
| EOp of Dcalc.Ast.operator
|
2022-06-03 17:40:03 +03:00
|
|
|
| EDefault of marked_expr list * marked_expr * marked_expr
|
|
|
|
| EIfThenElse of marked_expr * marked_expr * marked_expr
|
|
|
|
| EArray of marked_expr list
|
|
|
|
| ErrorOnEmpty of marked_expr
|
2022-02-28 19:19:06 +03:00
|
|
|
|
2022-05-25 15:41:04 +03:00
|
|
|
module Expr = struct
|
|
|
|
type t = expr
|
|
|
|
|
|
|
|
(** Syntactic comparison, up to locations and alpha-renaming *)
|
|
|
|
let rec compare e1 e2 =
|
|
|
|
let rec list_compare cmp l1 l2 =
|
|
|
|
(* List.compare is available from OCaml 4.12 on *)
|
|
|
|
match l1, l2 with
|
|
|
|
| [], [] -> 0
|
|
|
|
| [], _ :: _ -> -1
|
|
|
|
| _ :: _, [] -> 1
|
|
|
|
| a1 :: l1, a2 :: l2 ->
|
|
|
|
let c = cmp a1 a2 in
|
|
|
|
if c <> 0 then c else list_compare cmp l1 l2
|
|
|
|
in
|
|
|
|
match e1, e2 with
|
|
|
|
| ELocation _, ELocation _ -> 0
|
2022-06-03 17:40:03 +03:00
|
|
|
| EVar v1, EVar v2 -> Bindlib.compare_vars v1 v2
|
2022-05-25 15:41:04 +03:00
|
|
|
| EStruct (name1, field_map1), EStruct (name2, field_map2) -> (
|
|
|
|
match Scopelang.Ast.StructName.compare name1 name2 with
|
|
|
|
| 0 ->
|
2022-05-30 12:20:48 +03:00
|
|
|
Scopelang.Ast.StructFieldMap.compare (Marked.compare compare) field_map1
|
|
|
|
field_map2
|
2022-05-25 15:41:04 +03:00
|
|
|
| n -> n)
|
|
|
|
| ( EStructAccess ((e1, _), field_name1, struct_name1),
|
|
|
|
EStructAccess ((e2, _), field_name2, struct_name2) ) -> (
|
|
|
|
match compare e1 e2 with
|
|
|
|
| 0 -> (
|
|
|
|
match Scopelang.Ast.StructFieldName.compare field_name1 field_name2 with
|
|
|
|
| 0 -> Scopelang.Ast.StructName.compare struct_name1 struct_name2
|
|
|
|
| n -> n)
|
|
|
|
| n -> n)
|
|
|
|
| EEnumInj ((e1, _), cstr1, name1), EEnumInj ((e2, _), cstr2, name2) -> (
|
|
|
|
match compare e1 e2 with
|
|
|
|
| 0 -> (
|
|
|
|
match Scopelang.Ast.EnumName.compare name1 name2 with
|
|
|
|
| 0 -> Scopelang.Ast.EnumConstructor.compare cstr1 cstr2
|
|
|
|
| n -> n)
|
|
|
|
| n -> n)
|
|
|
|
| EMatch ((e1, _), name1, emap1), EMatch ((e2, _), name2, emap2) -> (
|
|
|
|
match compare e1 e2 with
|
|
|
|
| 0 -> (
|
|
|
|
match Scopelang.Ast.EnumName.compare name1 name2 with
|
|
|
|
| 0 ->
|
2022-05-30 12:20:48 +03:00
|
|
|
Scopelang.Ast.EnumConstructorMap.compare (Marked.compare compare)
|
2022-05-25 15:41:04 +03:00
|
|
|
emap1 emap2
|
|
|
|
| n -> n)
|
|
|
|
| n -> n)
|
|
|
|
| ELit l1, ELit l2 -> Stdlib.compare l1 l2
|
2022-06-03 17:40:03 +03:00
|
|
|
| EAbs (binder1, typs1), EAbs (binder2, typs2) -> (
|
2022-05-25 15:41:04 +03:00
|
|
|
match
|
2022-05-30 12:20:48 +03:00
|
|
|
list_compare (Marked.compare Scopelang.Ast.Typ.compare) typs1 typs2
|
2022-05-25 15:41:04 +03:00
|
|
|
with
|
|
|
|
| 0 ->
|
|
|
|
let _, (e1, _), (e2, _) = Bindlib.unmbind2 binder1 binder2 in
|
|
|
|
compare e1 e2
|
|
|
|
| n -> n)
|
|
|
|
| EApp ((f1, _), args1), EApp ((f2, _), args2) -> (
|
|
|
|
match compare f1 f2 with
|
|
|
|
| 0 -> list_compare (fun (x1, _) (x2, _) -> compare x1 x2) args1 args2
|
|
|
|
| n -> n)
|
|
|
|
| EOp op1, EOp op2 -> Stdlib.compare op1 op2
|
|
|
|
| ( EDefault (exs1, (just1, _), (cons1, _)),
|
|
|
|
EDefault (exs2, (just2, _), (cons2, _)) ) -> (
|
|
|
|
match compare just1 just2 with
|
|
|
|
| 0 -> (
|
|
|
|
match compare cons1 cons2 with
|
2022-05-30 12:20:48 +03:00
|
|
|
| 0 -> list_compare (Marked.compare compare) exs1 exs2
|
2022-05-25 15:41:04 +03:00
|
|
|
| n -> n)
|
|
|
|
| n -> n)
|
|
|
|
| ( EIfThenElse ((i1, _), (t1, _), (e1, _)),
|
|
|
|
EIfThenElse ((i2, _), (t2, _), (e2, _)) ) -> (
|
|
|
|
match compare i1 i2 with
|
|
|
|
| 0 -> ( match compare t1 t2 with 0 -> compare e1 e2 | n -> n)
|
|
|
|
| n -> n)
|
|
|
|
| EArray a1, EArray a2 ->
|
|
|
|
list_compare (fun (e1, _) (e2, _) -> compare e1 e2) a1 a2
|
|
|
|
| ErrorOnEmpty (e1, _), ErrorOnEmpty (e2, _) -> compare e1 e2
|
|
|
|
| ELocation _, _ -> -1
|
|
|
|
| _, ELocation _ -> 1
|
|
|
|
| EVar _, _ -> -1
|
|
|
|
| _, EVar _ -> 1
|
|
|
|
| EStruct _, _ -> -1
|
|
|
|
| _, EStruct _ -> 1
|
|
|
|
| EStructAccess _, _ -> -1
|
|
|
|
| _, EStructAccess _ -> 1
|
|
|
|
| EEnumInj _, _ -> -1
|
|
|
|
| _, EEnumInj _ -> 1
|
|
|
|
| EMatch _, _ -> -1
|
|
|
|
| _, EMatch _ -> 1
|
|
|
|
| ELit _, _ -> -1
|
|
|
|
| _, ELit _ -> 1
|
|
|
|
| EAbs _, _ -> -1
|
|
|
|
| _, EAbs _ -> 1
|
|
|
|
| EApp _, _ -> -1
|
|
|
|
| _, EApp _ -> 1
|
|
|
|
| EOp _, _ -> -1
|
|
|
|
| _, EOp _ -> 1
|
|
|
|
| EDefault _, _ -> -1
|
|
|
|
| _, EDefault _ -> 1
|
|
|
|
| EIfThenElse _, _ -> -1
|
|
|
|
| _, EIfThenElse _ -> 1
|
|
|
|
| EArray _, _ -> -1
|
|
|
|
| _, EArray _ -> 1
|
|
|
|
end
|
|
|
|
|
|
|
|
module ExprMap = Map.Make (Expr)
|
|
|
|
|
2022-02-28 20:34:32 +03:00
|
|
|
module Var = struct
|
|
|
|
type t = expr Bindlib.var
|
|
|
|
|
2022-06-03 17:40:03 +03:00
|
|
|
let make (s : string) : t =
|
|
|
|
Bindlib.new_var (fun (x : expr Bindlib.var) : expr -> EVar x) s
|
2022-02-28 20:34:32 +03:00
|
|
|
|
|
|
|
let compare x y = Bindlib.compare_vars x y
|
|
|
|
end
|
|
|
|
|
|
|
|
type vars = expr Bindlib.mvar
|
|
|
|
|
2020-11-25 12:10:27 +03:00
|
|
|
type rule = {
|
2022-01-04 20:19:15 +03:00
|
|
|
rule_id : RuleName.t;
|
2022-05-30 12:20:48 +03:00
|
|
|
rule_just : expr Marked.pos Bindlib.box;
|
|
|
|
rule_cons : expr Marked.pos Bindlib.box;
|
|
|
|
rule_parameter : (Var.t * Scopelang.Ast.typ Marked.pos) option;
|
|
|
|
rule_exception_to_rules : RuleSet.t Marked.pos;
|
2020-11-25 12:10:27 +03:00
|
|
|
}
|
2020-06-22 17:16:55 +03:00
|
|
|
|
2022-05-25 15:41:04 +03:00
|
|
|
module Rule = struct
|
|
|
|
type t = rule
|
|
|
|
|
|
|
|
(** Structural equality (otherwise, you should just compare the [rule_id]
|
|
|
|
fields) *)
|
|
|
|
let compare r1 r2 =
|
|
|
|
match r1.rule_parameter, r2.rule_parameter with
|
|
|
|
| None, None -> (
|
|
|
|
let j1, _ = Bindlib.unbox r1.rule_just in
|
|
|
|
let j2, _ = Bindlib.unbox r2.rule_just in
|
|
|
|
match Expr.compare j1 j2 with
|
|
|
|
| 0 ->
|
|
|
|
let c1, _ = Bindlib.unbox r1.rule_cons in
|
|
|
|
let c2, _ = Bindlib.unbox r2.rule_cons in
|
|
|
|
Expr.compare c1 c2
|
|
|
|
| n -> n)
|
|
|
|
| Some (v1, (t1, _)), Some (v2, (t2, _)) -> (
|
|
|
|
match Scopelang.Ast.Typ.compare t1 t2 with
|
|
|
|
| 0 -> (
|
|
|
|
let open Bindlib in
|
|
|
|
let b1 = unbox (bind_var v1 r1.rule_just) in
|
|
|
|
let b2 = unbox (bind_var v2 r2.rule_just) in
|
|
|
|
let _, (j1, _), (j2, _) = unbind2 b1 b2 in
|
|
|
|
match Expr.compare j1 j2 with
|
|
|
|
| 0 ->
|
|
|
|
let b1 = unbox (bind_var v1 r1.rule_cons) in
|
|
|
|
let b2 = unbox (bind_var v2 r2.rule_cons) in
|
|
|
|
let _, (c1, _), (c2, _) = unbind2 b1 b2 in
|
|
|
|
Expr.compare c1 c2
|
|
|
|
| n -> n)
|
|
|
|
| n -> n)
|
|
|
|
| None, Some _ -> -1
|
|
|
|
| Some _, None -> 1
|
|
|
|
end
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
let empty_rule
|
2022-05-04 19:40:28 +03:00
|
|
|
(pos : Pos.t)
|
2022-05-30 12:20:48 +03:00
|
|
|
(have_parameter : Scopelang.Ast.typ Marked.pos option) : rule =
|
2020-11-25 12:10:27 +03:00
|
|
|
{
|
2022-02-28 19:19:06 +03:00
|
|
|
rule_just = Bindlib.box (ELit (Dcalc.Ast.LBool false), pos);
|
|
|
|
rule_cons = Bindlib.box (ELit Dcalc.Ast.LEmptyError, pos);
|
2022-01-04 20:19:15 +03:00
|
|
|
rule_parameter =
|
2022-03-08 17:03:14 +03:00
|
|
|
(match have_parameter with
|
2022-06-03 17:40:03 +03:00
|
|
|
| Some typ -> Some (Var.make "dummy", typ)
|
2022-03-08 17:03:14 +03:00
|
|
|
| None -> None);
|
2022-05-04 16:48:03 +03:00
|
|
|
rule_exception_to_rules = RuleSet.empty, pos;
|
2022-01-04 20:19:15 +03:00
|
|
|
rule_id = RuleName.fresh ("empty", pos);
|
2020-11-25 12:10:27 +03:00
|
|
|
}
|
2020-08-10 00:01:42 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
let always_false_rule
|
2022-05-04 19:40:28 +03:00
|
|
|
(pos : Pos.t)
|
2022-05-30 12:20:48 +03:00
|
|
|
(have_parameter : Scopelang.Ast.typ Marked.pos option) : rule =
|
2020-12-31 02:28:26 +03:00
|
|
|
{
|
2022-02-28 19:19:06 +03:00
|
|
|
rule_just = Bindlib.box (ELit (Dcalc.Ast.LBool true), pos);
|
|
|
|
rule_cons = Bindlib.box (ELit (Dcalc.Ast.LBool false), pos);
|
2022-01-04 20:19:15 +03:00
|
|
|
rule_parameter =
|
2022-03-08 17:03:14 +03:00
|
|
|
(match have_parameter with
|
2022-06-03 17:40:03 +03:00
|
|
|
| Some typ -> Some (Var.make "dummy", typ)
|
2022-03-08 17:03:14 +03:00
|
|
|
| None -> None);
|
2022-05-04 16:48:03 +03:00
|
|
|
rule_exception_to_rules = RuleSet.empty, pos;
|
2022-01-04 20:19:15 +03:00
|
|
|
rule_id = RuleName.fresh ("always_false", pos);
|
2020-12-31 02:28:26 +03:00
|
|
|
}
|
|
|
|
|
2022-05-30 12:20:48 +03:00
|
|
|
type assertion = expr Marked.pos Bindlib.box
|
2020-06-22 17:16:55 +03:00
|
|
|
type variation_typ = Increasing | Decreasing
|
|
|
|
type reference_typ = Decree | Law
|
|
|
|
|
|
|
|
type meta_assertion =
|
2022-05-30 12:20:48 +03:00
|
|
|
| FixedBy of reference_typ Marked.pos
|
|
|
|
| VariesWith of unit * variation_typ Marked.pos option
|
2020-06-22 17:16:55 +03:00
|
|
|
|
2022-01-04 20:19:15 +03:00
|
|
|
type scope_def = {
|
|
|
|
scope_def_rules : rule RuleMap.t;
|
2022-05-30 12:20:48 +03:00
|
|
|
scope_def_typ : Scopelang.Ast.typ Marked.pos;
|
2022-01-04 20:19:15 +03:00
|
|
|
scope_def_is_condition : bool;
|
2022-02-07 12:30:36 +03:00
|
|
|
scope_def_io : Scopelang.Ast.io;
|
2022-01-04 20:19:15 +03:00
|
|
|
scope_def_label_groups : RuleSet.t LabelMap.t;
|
|
|
|
}
|
|
|
|
|
2022-02-28 19:19:06 +03:00
|
|
|
type var_or_states = WholeVar | States of StateName.t list
|
|
|
|
|
2020-06-22 17:16:55 +03:00
|
|
|
type scope = {
|
2022-02-28 19:19:06 +03:00
|
|
|
scope_vars : var_or_states ScopeVarMap.t;
|
2020-11-25 13:53:56 +03:00
|
|
|
scope_sub_scopes : Scopelang.Ast.ScopeName.t Scopelang.Ast.SubScopeMap.t;
|
2020-11-23 14:20:38 +03:00
|
|
|
scope_uid : Scopelang.Ast.ScopeName.t;
|
2022-01-04 20:19:15 +03:00
|
|
|
scope_defs : scope_def ScopeDefMap.t;
|
2020-06-22 17:16:55 +03:00
|
|
|
scope_assertions : assertion list;
|
2020-09-13 01:33:56 +03:00
|
|
|
scope_meta_assertions : meta_assertion list;
|
2020-06-22 17:16:55 +03:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:40:17 +03:00
|
|
|
type program = {
|
|
|
|
program_scopes : scope Scopelang.Ast.ScopeMap.t;
|
|
|
|
program_enums : Scopelang.Ast.enum_ctx;
|
|
|
|
program_structs : Scopelang.Ast.struct_ctx;
|
|
|
|
}
|
2020-11-25 13:53:56 +03:00
|
|
|
|
2022-05-30 12:20:48 +03:00
|
|
|
let rec locations_used (e : expr Marked.pos) : LocationSet.t =
|
|
|
|
match Marked.unmark e with
|
|
|
|
| ELocation l -> LocationSet.singleton (l, Marked.get_mark e)
|
2022-02-28 19:19:06 +03:00
|
|
|
| EVar _ | ELit _ | EOp _ -> LocationSet.empty
|
2022-06-03 17:40:03 +03:00
|
|
|
| EAbs (binder, _) ->
|
2022-05-04 18:40:55 +03:00
|
|
|
let _, body = Bindlib.unmbind binder in
|
|
|
|
locations_used body
|
2022-02-28 19:19:06 +03:00
|
|
|
| EStruct (_, es) ->
|
2022-05-04 18:40:55 +03:00
|
|
|
Scopelang.Ast.StructFieldMap.fold
|
|
|
|
(fun _ e' acc -> LocationSet.union acc (locations_used e'))
|
|
|
|
es LocationSet.empty
|
2022-02-28 19:19:06 +03:00
|
|
|
| EStructAccess (e1, _, _) -> locations_used e1
|
|
|
|
| EEnumInj (e1, _, _) -> locations_used e1
|
|
|
|
| EMatch (e1, _, es) ->
|
2022-05-04 18:40:55 +03:00
|
|
|
Scopelang.Ast.EnumConstructorMap.fold
|
|
|
|
(fun _ e' acc -> LocationSet.union acc (locations_used e'))
|
|
|
|
es (locations_used e1)
|
2022-02-28 19:19:06 +03:00
|
|
|
| EApp (e1, args) ->
|
2022-05-04 18:40:55 +03:00
|
|
|
List.fold_left
|
|
|
|
(fun acc arg -> LocationSet.union (locations_used arg) acc)
|
|
|
|
(locations_used e1) args
|
2022-02-28 19:19:06 +03:00
|
|
|
| EIfThenElse (e1, e2, e3) ->
|
2022-05-04 18:40:55 +03:00
|
|
|
LocationSet.union (locations_used e1)
|
|
|
|
(LocationSet.union (locations_used e2) (locations_used e3))
|
2022-02-28 19:19:06 +03:00
|
|
|
| EDefault (excepts, just, cons) ->
|
2022-05-04 18:40:55 +03:00
|
|
|
List.fold_left
|
|
|
|
(fun acc except -> LocationSet.union (locations_used except) acc)
|
|
|
|
(LocationSet.union (locations_used just) (locations_used cons))
|
|
|
|
excepts
|
2022-02-28 19:19:06 +03:00
|
|
|
| EArray es ->
|
2022-05-04 18:40:55 +03:00
|
|
|
List.fold_left
|
|
|
|
(fun acc e' -> LocationSet.union acc (locations_used e'))
|
|
|
|
LocationSet.empty es
|
2022-02-28 19:19:06 +03:00
|
|
|
| ErrorOnEmpty e' -> locations_used e'
|
|
|
|
|
2020-11-27 13:37:21 +03:00
|
|
|
let free_variables (def : rule RuleMap.t) : Pos.t ScopeDefMap.t =
|
2022-03-08 17:03:14 +03:00
|
|
|
let add_locs (acc : Pos.t ScopeDefMap.t) (locs : LocationSet.t) :
|
|
|
|
Pos.t ScopeDefMap.t =
|
2022-02-28 19:19:06 +03:00
|
|
|
LocationSet.fold
|
2020-12-03 23:02:28 +03:00
|
|
|
(fun (loc, loc_pos) acc ->
|
2020-11-27 13:37:21 +03:00
|
|
|
ScopeDefMap.add
|
2021-03-23 12:59:43 +03:00
|
|
|
(match loc with
|
2022-05-30 12:20:48 +03:00
|
|
|
| ScopeVar (v, st) -> ScopeDef.Var (Marked.unmark v, st)
|
2022-02-28 19:19:06 +03:00
|
|
|
| SubScopeVar (_, sub_index, sub_var) ->
|
2022-05-30 12:20:48 +03:00
|
|
|
ScopeDef.SubScopeVar (Marked.unmark sub_index, Marked.unmark sub_var))
|
2020-11-27 13:37:21 +03:00
|
|
|
loc_pos acc)
|
2020-12-03 23:02:28 +03:00
|
|
|
locs acc
|
2020-11-25 13:53:56 +03:00
|
|
|
in
|
2020-11-25 16:35:26 +03:00
|
|
|
RuleMap.fold
|
|
|
|
(fun _ rule acc ->
|
2020-11-27 18:27:10 +03:00
|
|
|
let locs =
|
2022-02-28 19:19:06 +03:00
|
|
|
LocationSet.union
|
|
|
|
(locations_used (Bindlib.unbox rule.rule_just))
|
|
|
|
(locations_used (Bindlib.unbox rule.rule_cons))
|
2020-11-27 18:27:10 +03:00
|
|
|
in
|
2020-11-25 13:53:56 +03:00
|
|
|
add_locs acc locs)
|
2020-11-27 13:37:21 +03:00
|
|
|
def ScopeDefMap.empty
|
2022-02-28 20:34:32 +03:00
|
|
|
|
2022-05-30 12:20:48 +03:00
|
|
|
let make_var ((x, pos) : Var.t Marked.pos) : expr Marked.pos Bindlib.box =
|
2022-05-04 16:48:03 +03:00
|
|
|
Bindlib.box_apply (fun v -> v, pos) (Bindlib.box_var x)
|
2022-02-28 20:34:32 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
let make_abs
|
|
|
|
(xs : vars)
|
2022-05-30 12:20:48 +03:00
|
|
|
(e : expr Marked.pos Bindlib.box)
|
|
|
|
(taus : Scopelang.Ast.typ Marked.pos list)
|
|
|
|
(pos : Pos.t) : expr Marked.pos Bindlib.box =
|
2022-06-03 17:40:03 +03:00
|
|
|
Bindlib.box_apply (fun b -> EAbs (b, taus), pos) (Bindlib.bind_mvar xs e)
|
2022-03-08 17:03:14 +03:00
|
|
|
|
|
|
|
let make_app
|
2022-05-30 12:20:48 +03:00
|
|
|
(e : expr Marked.pos Bindlib.box)
|
|
|
|
(u : expr Marked.pos Bindlib.box list)
|
|
|
|
(pos : Pos.t) : expr Marked.pos Bindlib.box =
|
2022-05-04 16:48:03 +03:00
|
|
|
Bindlib.box_apply2 (fun e u -> EApp (e, u), pos) e (Bindlib.box_list u)
|
2022-02-28 20:34:32 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
let make_let_in
|
|
|
|
(x : Var.t)
|
2022-05-30 12:20:48 +03:00
|
|
|
(tau : Scopelang.Ast.typ Marked.pos)
|
|
|
|
(e1 : expr Marked.pos Bindlib.box)
|
|
|
|
(e2 : expr Marked.pos Bindlib.box) : expr Marked.pos Bindlib.box =
|
2022-02-28 20:34:32 +03:00
|
|
|
Bindlib.box_apply2
|
2022-05-30 12:20:48 +03:00
|
|
|
(fun e u -> EApp (e, u), Marked.get_mark (Bindlib.unbox e2))
|
2022-06-03 17:40:03 +03:00
|
|
|
(make_abs (Array.of_list [x]) e2 [tau] (Marked.get_mark (Bindlib.unbox e2)))
|
2022-05-04 16:48:03 +03:00
|
|
|
(Bindlib.box_list [e1])
|
2022-02-28 20:34:32 +03:00
|
|
|
|
|
|
|
module VarMap = Map.Make (Var)
|