catala/compiler/desugared/ast.ml

162 lines
5.9 KiB
OCaml
Raw Normal View History

2020-06-22 17:16:55 +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>
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
http://www.apache.org/licenses/LICENSE-2.0
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
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)
module RuleName : Uid.Id with type info = Uid.MarkedString.info = Uid.Make (Uid.MarkedString) ()
2020-12-14 19:00:42 +03:00
module RuleMap : Map.S with type key = RuleName.t = Map.Make (RuleName)
module RuleSet : Set.S with type elt = RuleName.t = Set.Make (RuleName)
module LabelName : Uid.Id with type info = Uid.MarkedString.info = Uid.Make (Uid.MarkedString) ()
module LabelMap : Map.S with type key = LabelName.t = Map.Make (LabelName)
module LabelSet : Set.S with type elt = LabelName.t = Set.Make (LabelName)
2020-11-23 14:20:38 +03:00
(** Inside a scope, a definition can refer either to a scope def, or a subscope def *)
module ScopeDef = struct
type t =
2020-11-23 18:12:45 +03:00
| Var of Scopelang.Ast.ScopeVar.t
| SubScopeVar of Scopelang.Ast.SubScopeName.t * Scopelang.Ast.ScopeVar.t
2020-11-23 14:20:38 +03:00
(** In this case, the [Uid.Var.t] lives inside the context of the subscope's original
declaration *)
let compare x y =
match (x, y) with
2020-11-23 18:12:45 +03:00
| Var x, Var y | Var x, SubScopeVar (_, y) | SubScopeVar (_, x), Var y ->
Scopelang.Ast.ScopeVar.compare x y
2021-01-11 17:19:05 +03:00
| SubScopeVar (x', x), SubScopeVar (y', y) ->
let cmp = Scopelang.Ast.SubScopeName.compare x' y' in
if cmp = 0 then Scopelang.Ast.ScopeVar.compare x y else cmp
2020-11-23 14:20:38 +03:00
let get_position x =
match x with
| Var x -> Pos.get_position (Scopelang.Ast.ScopeVar.get_info x)
| SubScopeVar (x, _) -> Pos.get_position (Scopelang.Ast.SubScopeName.get_info x)
let format_t fmt x =
2020-11-23 14:20:38 +03:00
match x with
| Var v -> Scopelang.Ast.ScopeVar.format_t fmt v
2020-11-23 14:20:38 +03:00
| SubScopeVar (s, v) ->
Format.fprintf fmt "%a.%a" Scopelang.Ast.SubScopeName.format_t s
Scopelang.Ast.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
| Var v -> Scopelang.Ast.ScopeVar.hash v
2021-01-11 17:19:05 +03:00
| SubScopeVar (w, v) -> Scopelang.Ast.SubScopeName.hash w * Scopelang.Ast.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)
2020-11-23 11:22:47 +03:00
2020-12-14 19:00:42 +03:00
module ScopeDefSet : Set.S with type elt = ScopeDef.t = Set.Make (ScopeDef)
(** {1 AST} *)
2020-06-22 17:16:55 +03:00
2020-11-25 12:10:27 +03:00
type rule = {
rule_id : RuleName.t;
rule_just : Scopelang.Ast.expr Pos.marked Bindlib.box;
rule_cons : Scopelang.Ast.expr Pos.marked Bindlib.box;
rule_parameter : (Scopelang.Ast.Var.t * Scopelang.Ast.typ Pos.marked) option;
rule_exception_to_rules : RuleSet.t Pos.marked;
2020-11-25 12:10:27 +03:00
}
2020-06-22 17:16:55 +03:00
let empty_rule (pos : Pos.t) (have_parameter : Scopelang.Ast.typ Pos.marked option) : rule =
2020-11-25 12:10:27 +03:00
{
rule_just = Bindlib.box (Scopelang.Ast.ELit (Dcalc.Ast.LBool false), pos);
rule_cons = Bindlib.box (Scopelang.Ast.ELit Dcalc.Ast.LEmptyError, pos);
rule_parameter =
2021-03-23 12:59:43 +03:00
(match have_parameter with
| Some typ -> Some (Scopelang.Ast.Var.make ("dummy", pos), typ)
2021-03-23 12:59:43 +03:00
| None -> None);
rule_exception_to_rules = (RuleSet.empty, pos);
rule_id = RuleName.fresh ("empty", pos);
2020-11-25 12:10:27 +03:00
}
2020-08-10 00:01:42 +03:00
let always_false_rule (pos : Pos.t) (have_parameter : Scopelang.Ast.typ Pos.marked option) : rule =
{
rule_just = Bindlib.box (Scopelang.Ast.ELit (Dcalc.Ast.LBool true), pos);
rule_cons = Bindlib.box (Scopelang.Ast.ELit (Dcalc.Ast.LBool false), pos);
rule_parameter =
2021-03-23 12:59:43 +03:00
(match have_parameter with
| Some typ -> Some (Scopelang.Ast.Var.make ("dummy", pos), typ)
2021-03-23 12:59:43 +03:00
| None -> None);
rule_exception_to_rules = (RuleSet.empty, pos);
rule_id = RuleName.fresh ("always_false", pos);
}
type assertion = Scopelang.Ast.expr Pos.marked Bindlib.box
2020-06-22 17:16:55 +03:00
type variation_typ = Increasing | Decreasing
type reference_typ = Decree | Law
type meta_assertion =
| FixedBy of reference_typ Pos.marked
2020-11-23 11:22:47 +03:00
| VariesWith of unit * variation_typ Pos.marked option
2020-06-22 17:16:55 +03:00
type scope_def = {
scope_def_rules : rule RuleMap.t;
scope_def_typ : Scopelang.Ast.typ Pos.marked;
scope_def_is_condition : bool;
scope_def_visibility : Scopelang.Ast.visibility;
scope_def_label_groups : RuleSet.t LabelMap.t;
}
2020-06-22 17:16:55 +03:00
type scope = {
scope_vars : Scopelang.Ast.ScopeVarSet.t;
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;
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
}
type program = {
program_scopes : scope Scopelang.Ast.ScopeMap.t;
program_enums : Scopelang.Ast.enum_ctx;
program_structs : Scopelang.Ast.struct_ctx;
}
2020-11-27 13:37:21 +03:00
let free_variables (def : rule RuleMap.t) : Pos.t ScopeDefMap.t =
let add_locs (acc : Pos.t ScopeDefMap.t) (locs : Scopelang.Ast.LocationSet.t) :
2020-11-27 13:37:21 +03:00
Pos.t ScopeDefMap.t =
Scopelang.Ast.LocationSet.fold
(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
| Scopelang.Ast.ScopeVar v -> ScopeDef.Var (Pos.unmark v)
| Scopelang.Ast.SubScopeVar (_, sub_index, sub_var) ->
2021-03-23 12:59:43 +03:00
ScopeDef.SubScopeVar (Pos.unmark sub_index, Pos.unmark sub_var))
2020-11-27 13:37:21 +03:00
loc_pos acc)
locs acc
in
RuleMap.fold
(fun _ rule acc ->
2020-11-27 18:27:10 +03:00
let locs =
Scopelang.Ast.LocationSet.union
(Scopelang.Ast.locations_used (Bindlib.unbox rule.rule_just))
(Scopelang.Ast.locations_used (Bindlib.unbox rule.rule_cons))
2020-11-27 18:27:10 +03:00
in
add_locs acc locs)
2020-11-27 13:37:21 +03:00
def ScopeDefMap.empty