mirror of
https://github.com/CatalaLang/catala.git
synced 2024-11-08 07:51:43 +03:00
231 lines
6.2 KiB
OCaml
231 lines
6.2 KiB
OCaml
(* This file is part of the Catala compiler, a specification language for tax
|
|
and social benefits computation rules. Copyright (C) 2020 Inria, contributor:
|
|
Denis Merigoux <denis.merigoux@inria.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. *)
|
|
|
|
open Utils
|
|
|
|
(** Abstract syntax tree for the lambda calculus *)
|
|
|
|
(** {1 Abstract syntax tree} *)
|
|
|
|
(** The expressions use the {{:https://lepigre.fr/ocaml-bindlib/} Bindlib}
|
|
library, based on higher-order abstract syntax*)
|
|
|
|
type lit =
|
|
| LBool of bool
|
|
| LInt of Runtime.integer
|
|
| LRat of Runtime.decimal
|
|
| LMoney of Runtime.money
|
|
| LUnit
|
|
| LDate of Runtime.date
|
|
| LDuration of Runtime.duration
|
|
|
|
type except = ConflictError | EmptyError | NoValueProvided | Crash
|
|
type 'm mark = 'm Dcalc.Ast.mark
|
|
|
|
type 'm marked_expr = ('m expr, 'm) Dcalc.Ast.marked
|
|
|
|
and 'm expr =
|
|
| EVar of 'm expr Bindlib.var
|
|
| ETuple of 'm marked_expr list * Dcalc.Ast.StructName.t option
|
|
(** The [MarkedString.info] is the former struct field name*)
|
|
| ETupleAccess of
|
|
'm marked_expr
|
|
* int
|
|
* Dcalc.Ast.StructName.t option
|
|
* Dcalc.Ast.typ Marked.pos list
|
|
(** The [MarkedString.info] is the former struct field name *)
|
|
| EInj of
|
|
'm marked_expr
|
|
* int
|
|
* Dcalc.Ast.EnumName.t
|
|
* Dcalc.Ast.typ Marked.pos list
|
|
(** The [MarkedString.info] is the former enum case name *)
|
|
| EMatch of 'm marked_expr * 'm marked_expr list * Dcalc.Ast.EnumName.t
|
|
(** The [MarkedString.info] is the former enum case name *)
|
|
| EArray of 'm marked_expr list
|
|
| ELit of lit
|
|
| EAbs of
|
|
('m expr, 'm marked_expr) Bindlib.mbinder * Dcalc.Ast.typ Marked.pos list
|
|
| EApp of 'm marked_expr * 'm marked_expr list
|
|
| EAssert of 'm marked_expr
|
|
| EOp of Dcalc.Ast.operator
|
|
| EIfThenElse of 'm marked_expr * 'm marked_expr * 'm marked_expr
|
|
| ERaise of except
|
|
| ECatch of 'm marked_expr * except * 'm marked_expr
|
|
|
|
type 'm program = {
|
|
decl_ctx : Dcalc.Ast.decl_ctx;
|
|
scopes : ('m expr, 'm) Dcalc.Ast.scopes;
|
|
}
|
|
|
|
(** {1 Variable helpers} *)
|
|
|
|
type 'm var = 'm expr Bindlib.var
|
|
type 'm vars = 'm expr Bindlib.mvar
|
|
|
|
module Var : sig
|
|
type t
|
|
|
|
val t : 'm expr Bindlib.var -> t
|
|
val get : t -> 'm expr Bindlib.var
|
|
val compare : t -> t -> int
|
|
end
|
|
|
|
module VarMap : Map.S with type key = Var.t
|
|
module VarSet : Set.S with type elt = Var.t
|
|
|
|
val new_var : string -> 'm var
|
|
|
|
type 'm binder = ('m expr, 'm marked_expr) Bindlib.binder
|
|
|
|
(** {1 Boxed constructors} *)
|
|
|
|
val evar : 'm expr Bindlib.var -> 'm mark -> 'm marked_expr Bindlib.box
|
|
|
|
val etuple :
|
|
'm marked_expr Bindlib.box list ->
|
|
Dcalc.Ast.StructName.t option ->
|
|
'm mark ->
|
|
'm marked_expr Bindlib.box
|
|
|
|
val etupleaccess :
|
|
'm marked_expr Bindlib.box ->
|
|
int ->
|
|
Dcalc.Ast.StructName.t option ->
|
|
Dcalc.Ast.typ Marked.pos list ->
|
|
'm mark ->
|
|
'm marked_expr Bindlib.box
|
|
|
|
val einj :
|
|
'm marked_expr Bindlib.box ->
|
|
int ->
|
|
Dcalc.Ast.EnumName.t ->
|
|
Dcalc.Ast.typ Marked.pos list ->
|
|
'm mark ->
|
|
'm marked_expr Bindlib.box
|
|
|
|
val ematch :
|
|
'm marked_expr Bindlib.box ->
|
|
'm marked_expr Bindlib.box list ->
|
|
Dcalc.Ast.EnumName.t ->
|
|
'm mark ->
|
|
'm marked_expr Bindlib.box
|
|
|
|
val earray :
|
|
'm marked_expr Bindlib.box list -> 'm mark -> 'm marked_expr Bindlib.box
|
|
|
|
val elit : lit -> 'm mark -> 'm marked_expr Bindlib.box
|
|
|
|
val eabs :
|
|
('m expr, 'm marked_expr) Bindlib.mbinder Bindlib.box ->
|
|
Dcalc.Ast.typ Marked.pos list ->
|
|
'm mark ->
|
|
'm marked_expr Bindlib.box
|
|
|
|
val eapp :
|
|
'm marked_expr Bindlib.box ->
|
|
'm marked_expr Bindlib.box list ->
|
|
'm mark ->
|
|
'm marked_expr Bindlib.box
|
|
|
|
val eassert :
|
|
'm marked_expr Bindlib.box -> 'm mark -> 'm marked_expr Bindlib.box
|
|
|
|
val eop : Dcalc.Ast.operator -> 'm mark -> 'm marked_expr Bindlib.box
|
|
|
|
val eifthenelse :
|
|
'm marked_expr Bindlib.box ->
|
|
'm marked_expr Bindlib.box ->
|
|
'm marked_expr Bindlib.box ->
|
|
'm mark ->
|
|
'm marked_expr Bindlib.box
|
|
|
|
val ecatch :
|
|
'm marked_expr Bindlib.box ->
|
|
except ->
|
|
'm marked_expr Bindlib.box ->
|
|
'm mark ->
|
|
'm marked_expr Bindlib.box
|
|
|
|
val eraise : except -> 'm mark -> 'm marked_expr Bindlib.box
|
|
|
|
(** {1 Language terms construction}*)
|
|
|
|
val make_var : ('m var, 'm) Dcalc.Ast.marked -> 'm marked_expr Bindlib.box
|
|
|
|
val make_abs :
|
|
'm vars ->
|
|
'm marked_expr Bindlib.box ->
|
|
Dcalc.Ast.typ Marked.pos list ->
|
|
'm mark ->
|
|
'm marked_expr Bindlib.box
|
|
|
|
val make_app :
|
|
'm marked_expr Bindlib.box ->
|
|
'm marked_expr Bindlib.box list ->
|
|
'm mark ->
|
|
'm marked_expr Bindlib.box
|
|
|
|
val make_let_in :
|
|
'm var ->
|
|
Dcalc.Ast.typ Marked.pos ->
|
|
'm marked_expr Bindlib.box ->
|
|
'm marked_expr Bindlib.box ->
|
|
Pos.t ->
|
|
'm marked_expr Bindlib.box
|
|
|
|
val make_multiple_let_in :
|
|
'm vars ->
|
|
Dcalc.Ast.typ Marked.pos list ->
|
|
'm marked_expr Bindlib.box list ->
|
|
'm marked_expr Bindlib.box ->
|
|
Pos.t ->
|
|
'm marked_expr Bindlib.box
|
|
|
|
val option_enum : Dcalc.Ast.EnumName.t
|
|
val none_constr : Dcalc.Ast.EnumConstructor.t
|
|
val some_constr : Dcalc.Ast.EnumConstructor.t
|
|
|
|
val option_enum_config :
|
|
(Dcalc.Ast.EnumConstructor.t * Dcalc.Ast.typ Marked.pos) list
|
|
|
|
val make_none : 'm mark -> 'm marked_expr Bindlib.box
|
|
val make_some : 'm marked_expr Bindlib.box -> 'm marked_expr Bindlib.box
|
|
|
|
val make_matchopt_with_abs_arms :
|
|
'm marked_expr Bindlib.box ->
|
|
'm marked_expr Bindlib.box ->
|
|
'm marked_expr Bindlib.box ->
|
|
'm marked_expr Bindlib.box
|
|
|
|
val make_matchopt :
|
|
'm mark ->
|
|
'm var ->
|
|
Dcalc.Ast.typ Marked.pos ->
|
|
'm marked_expr Bindlib.box ->
|
|
'm marked_expr Bindlib.box ->
|
|
'm marked_expr Bindlib.box ->
|
|
'm marked_expr Bindlib.box
|
|
(** [e' = make_matchopt'' pos v e e_none e_some] Builds the term corresponding
|
|
to [match e with | None -> fun () -> e_none |Some -> fun v -> e_some]. *)
|
|
|
|
val box_expr : 'm marked_expr -> 'm marked_expr Bindlib.box
|
|
|
|
(** {1 Special symbols} *)
|
|
|
|
val handle_default : Var.t
|
|
val handle_default_opt : Var.t
|