2022-01-07 20:36:56 +03:00
|
|
|
(* This file is part of the Catala compiler, a specification language for tax
|
2022-01-10 12:59:30 +03:00
|
|
|
and social benefits computation rules. Copyright (C) 2022 Inria,
|
2022-01-07 20:36:56 +03:00
|
|
|
contributors: Alain Delaët <alain.delaet--tixeuil@inria.fr>, 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. *)
|
2022-11-21 12:46:17 +03:00
|
|
|
open Catala_utils
|
2023-04-21 12:56:07 +03:00
|
|
|
open Definitions
|
2022-01-07 20:36:56 +03:00
|
|
|
|
2023-04-21 12:56:07 +03:00
|
|
|
type ('a, 'b, 'm) optimizations_ctx = {
|
|
|
|
var_values :
|
2023-05-17 17:15:00 +03:00
|
|
|
( (('a, 'b) dcalc_lcalc, 'm) gexpr,
|
|
|
|
(('a, 'b) dcalc_lcalc, 'm) gexpr )
|
2023-04-21 12:56:07 +03:00
|
|
|
Var.Map.t;
|
2022-01-31 16:30:42 +03:00
|
|
|
decl_ctx : decl_ctx;
|
|
|
|
}
|
2022-01-07 20:36:56 +03:00
|
|
|
|
2023-04-21 13:32:09 +03:00
|
|
|
let all_match_cases_are_id_fun cases n =
|
2023-07-12 12:48:46 +03:00
|
|
|
EnumConstructor.Map.for_all
|
|
|
|
(fun i case ->
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove case with
|
2023-04-21 13:32:09 +03:00
|
|
|
| EAbs { binder; _ } -> (
|
|
|
|
let var, body = Bindlib.unmbind binder in
|
|
|
|
(* because of invariant [invariant_match], the arity is always one. *)
|
|
|
|
let[@warning "-8"] [| var |] = var in
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove body with
|
2023-04-21 13:32:09 +03:00
|
|
|
| EInj { cons = i'; name = n'; e = EVar x, _ } ->
|
|
|
|
EnumConstructor.equal i i'
|
|
|
|
&& EnumName.equal n n'
|
|
|
|
&& Bindlib.eq_vars x var
|
|
|
|
| EInj { cons = i'; name = n'; e = ELit LUnit, _ } ->
|
|
|
|
(* since unit is the only value of type unit. We don't need to check
|
|
|
|
the equality. *)
|
|
|
|
EnumConstructor.equal i i' && EnumName.equal n n'
|
|
|
|
| _ -> false)
|
|
|
|
| _ ->
|
|
|
|
(* because of invariant [invariant_match], there is always some EAbs in
|
|
|
|
each cases. *)
|
|
|
|
assert false)
|
2023-07-12 12:48:46 +03:00
|
|
|
cases
|
2023-04-21 13:32:09 +03:00
|
|
|
|
|
|
|
let all_match_cases_map_to_same_constructor cases n =
|
2023-07-12 12:48:46 +03:00
|
|
|
EnumConstructor.Map.for_all
|
|
|
|
(fun i case ->
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove case with
|
2023-04-21 13:32:09 +03:00
|
|
|
| EAbs { binder; _ } -> (
|
|
|
|
let _, body = Bindlib.unmbind binder in
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove body with
|
2023-04-21 13:32:09 +03:00
|
|
|
| EInj { cons = i'; name = n'; _ } ->
|
|
|
|
EnumConstructor.equal i i' && EnumName.equal n n'
|
|
|
|
| _ -> false)
|
|
|
|
| _ -> assert false)
|
2023-07-12 12:48:46 +03:00
|
|
|
cases
|
2023-04-21 13:32:09 +03:00
|
|
|
|
2023-06-14 12:39:27 +03:00
|
|
|
let binder_vars_used_at_most_once
|
|
|
|
(binder :
|
|
|
|
( (('a, 'b) dcalc_lcalc, ('a, 'b) dcalc_lcalc, 'm) base_gexpr,
|
|
|
|
(('a, 'b) dcalc_lcalc, 'm) gexpr )
|
|
|
|
Bindlib.mbinder) : bool =
|
|
|
|
(* fast path: variables not used at all *)
|
|
|
|
(not (Array.exists Fun.id (Bindlib.mbinder_occurs binder)))
|
|
|
|
||
|
|
|
|
let vars, body = Bindlib.unmbind binder in
|
|
|
|
let rec vars_count (e : (('a, 'b) dcalc_lcalc, 'm) gexpr) : int array =
|
|
|
|
match e with
|
|
|
|
| EVar v, _ ->
|
|
|
|
Array.map
|
|
|
|
(fun i -> if Bindlib.eq_vars v (Array.get vars i) then 1 else 0)
|
|
|
|
(Array.make (Array.length vars) 0)
|
|
|
|
| e ->
|
|
|
|
Expr.shallow_fold
|
|
|
|
(fun e' acc -> Array.map2 (fun x y -> x + y) (vars_count e') acc)
|
|
|
|
e
|
|
|
|
(Array.make (Array.length vars) 0)
|
|
|
|
in
|
|
|
|
not (Array.exists (fun c -> c > 1) (vars_count body))
|
|
|
|
|
2023-04-21 12:56:07 +03:00
|
|
|
let rec optimize_expr :
|
|
|
|
type a b.
|
|
|
|
(a, b, 'm) optimizations_ctx ->
|
2023-05-17 17:15:00 +03:00
|
|
|
((a, b) dcalc_lcalc, 'm) gexpr ->
|
|
|
|
((a, b) dcalc_lcalc, 'm) boxed_gexpr =
|
2023-04-21 12:56:07 +03:00
|
|
|
fun ctx e ->
|
2022-11-17 19:13:35 +03:00
|
|
|
(* We proceed bottom-up, first apply on the subterms *)
|
2023-04-21 12:56:07 +03:00
|
|
|
let e = Expr.map ~f:(optimize_expr ctx) e in
|
2023-05-17 16:44:57 +03:00
|
|
|
let mark = Mark.get e in
|
2022-11-17 19:13:35 +03:00
|
|
|
(* Then reduce the parent node *)
|
2023-05-17 17:15:00 +03:00
|
|
|
let reduce (e : ((a, b) dcalc_lcalc, 'm) gexpr) =
|
2022-11-17 19:13:35 +03:00
|
|
|
(* Todo: improve the handling of eapp(log,elit) cases here, it obfuscates
|
|
|
|
the matches and the log calls are not preserved, which would be a good
|
|
|
|
property *)
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove e with
|
2022-11-17 19:13:35 +03:00
|
|
|
| EApp
|
|
|
|
{
|
|
|
|
f =
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
( EOp { op = Not; _ }, _
|
|
|
|
| ( EApp
|
|
|
|
{
|
|
|
|
f = EOp { op = Log _; _ }, _;
|
|
|
|
args = [(EOp { op = Not; _ }, _)];
|
|
|
|
},
|
2022-11-17 19:13:35 +03:00
|
|
|
_ ) ) as op;
|
|
|
|
args = [e1];
|
|
|
|
} -> (
|
|
|
|
(* reduction of logical not *)
|
|
|
|
match e1 with
|
|
|
|
| ELit (LBool false), _ -> ELit (LBool true)
|
|
|
|
| ELit (LBool true), _ -> ELit (LBool false)
|
|
|
|
| e1 -> EApp { f = op; args = [e1] })
|
|
|
|
| EApp
|
|
|
|
{
|
|
|
|
f =
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
( EOp { op = Or; _ }, _
|
|
|
|
| ( EApp
|
|
|
|
{
|
|
|
|
f = EOp { op = Log _; _ }, _;
|
|
|
|
args = [(EOp { op = Or; _ }, _)];
|
|
|
|
},
|
2022-11-17 19:13:35 +03:00
|
|
|
_ ) ) as op;
|
|
|
|
args = [e1; e2];
|
|
|
|
} -> (
|
|
|
|
(* reduction of logical or *)
|
|
|
|
match e1, e2 with
|
|
|
|
| (ELit (LBool false), _), new_e | new_e, (ELit (LBool false), _) ->
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.remove new_e
|
2022-11-17 19:13:35 +03:00
|
|
|
| (ELit (LBool true), _), _ | _, (ELit (LBool true), _) ->
|
|
|
|
ELit (LBool true)
|
|
|
|
| _ -> EApp { f = op; args = [e1; e2] })
|
|
|
|
| EApp
|
|
|
|
{
|
|
|
|
f =
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
( EOp { op = And; _ }, _
|
|
|
|
| ( EApp
|
|
|
|
{
|
|
|
|
f = EOp { op = Log _; _ }, _;
|
|
|
|
args = [(EOp { op = And; _ }, _)];
|
|
|
|
},
|
2022-11-17 19:13:35 +03:00
|
|
|
_ ) ) as op;
|
|
|
|
args = [e1; e2];
|
|
|
|
} -> (
|
|
|
|
(* reduction of logical and *)
|
|
|
|
match e1, e2 with
|
|
|
|
| (ELit (LBool true), _), new_e | new_e, (ELit (LBool true), _) ->
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.remove new_e
|
2022-11-17 19:13:35 +03:00
|
|
|
| (ELit (LBool false), _), _ | _, (ELit (LBool false), _) ->
|
|
|
|
ELit (LBool false)
|
|
|
|
| _ -> EApp { f = op; args = [e1; e2] })
|
2023-04-21 13:32:09 +03:00
|
|
|
| EMatch { e = EInj { e = e'; cons; name = n' }, _; cases; name = n }
|
|
|
|
(* iota-reduction *)
|
|
|
|
when EnumName.equal n n' -> (
|
|
|
|
(* match E x with | E y -> e1 = e1[y |-> x]*)
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove @@ EnumConstructor.Map.find cons cases with
|
2023-04-21 13:32:09 +03:00
|
|
|
(* holds because of invariant_match_inversion *)
|
|
|
|
| EAbs { binder; _ } ->
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.remove
|
2023-04-21 13:32:09 +03:00
|
|
|
(Bindlib.msubst binder ([e'] |> List.map fst |> Array.of_list))
|
|
|
|
| _ -> assert false)
|
|
|
|
| EMatch { e = e'; cases; name = n } when all_match_cases_are_id_fun cases n
|
|
|
|
->
|
|
|
|
(* iota-reduction when the match is equivalent to an identity function *)
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.remove e'
|
2023-04-21 13:32:09 +03:00
|
|
|
| EMatch
|
|
|
|
{
|
|
|
|
e = EMatch { e = arg; cases = cases1; name = n1 }, _;
|
|
|
|
cases = cases2;
|
|
|
|
name = n2;
|
|
|
|
}
|
|
|
|
when false
|
|
|
|
(* TODO: this case is buggy because of the box/unbox manipulation, it
|
|
|
|
should be fixed before removing this [false] value*)
|
2023-08-10 17:52:39 +03:00
|
|
|
&& EnumName.equal n1 n2
|
2023-04-21 13:32:09 +03:00
|
|
|
&& all_match_cases_map_to_same_constructor cases1 n1 ->
|
|
|
|
(* iota-reduction when the matched expression is itself a match of the
|
|
|
|
same enum mapping all constructors to themselves *)
|
|
|
|
let cases =
|
2023-07-12 12:48:46 +03:00
|
|
|
EnumConstructor.Map.merge
|
|
|
|
(fun _i o1 o2 ->
|
2023-04-21 13:32:09 +03:00
|
|
|
match o1, o2 with
|
|
|
|
| Some b1, Some e2 -> (
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove b1, Mark.remove e2 with
|
2023-04-21 13:32:09 +03:00
|
|
|
| EAbs { binder = b1; _ }, EAbs { binder = b2; tys } -> (
|
|
|
|
let v1, e1 = Bindlib.unmbind b1 in
|
|
|
|
let[@warning "-8"] [| v1 |] = v1 in
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove e1 with
|
2023-04-21 13:32:09 +03:00
|
|
|
| EInj { e = e1; _ } ->
|
|
|
|
Some
|
|
|
|
(Expr.unbox
|
|
|
|
(Expr.make_abs [| v1 |]
|
|
|
|
(Expr.box
|
|
|
|
(Bindlib.msubst b2
|
|
|
|
([e1] |> List.map fst |> Array.of_list)))
|
|
|
|
tys (Expr.pos e2)))
|
|
|
|
| _ -> assert false)
|
|
|
|
| _ -> assert false)
|
|
|
|
| _ -> assert false)
|
2023-07-12 12:48:46 +03:00
|
|
|
cases1 cases2
|
2023-04-21 13:32:09 +03:00
|
|
|
in
|
|
|
|
EMatch { e = arg; cases; name = n1 }
|
2023-06-14 12:18:39 +03:00
|
|
|
| EApp { f = EAbs { binder; _ }, _; args }
|
2023-06-14 12:39:27 +03:00
|
|
|
when binder_vars_used_at_most_once binder ->
|
|
|
|
(* beta reduction when variables not used. *)
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.remove (Bindlib.msubst binder (List.map fst args |> Array.of_list))
|
2023-02-28 11:06:33 +03:00
|
|
|
| EStructAccess { name; field; e = EStruct { name = name1; fields }, _ }
|
2023-08-10 17:52:39 +03:00
|
|
|
when StructName.equal name name1 ->
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.remove (StructField.Map.find field fields)
|
2022-11-17 19:13:35 +03:00
|
|
|
| EDefault { excepts; just; cons } -> (
|
2023-04-19 19:26:50 +03:00
|
|
|
(* TODO: mechanically prove each of these optimizations correct *)
|
2022-11-17 19:13:35 +03:00
|
|
|
let excepts =
|
2023-05-17 16:44:57 +03:00
|
|
|
List.filter (fun except -> Mark.remove except <> EEmptyError) excepts
|
2022-11-17 19:13:35 +03:00
|
|
|
(* we can discard the exceptions that are always empty error *)
|
|
|
|
in
|
|
|
|
let value_except_count =
|
|
|
|
List.fold_left
|
|
|
|
(fun nb except -> if Expr.is_value except then nb + 1 else nb)
|
|
|
|
0 excepts
|
|
|
|
in
|
|
|
|
if value_except_count > 1 then
|
|
|
|
(* at this point we know a conflict error will be triggered so we just
|
|
|
|
feed the expression to the interpreter that will print the beautiful
|
|
|
|
right error message *)
|
2023-09-09 23:02:39 +03:00
|
|
|
let (_ : _ gexpr) = Interpreter.evaluate_expr ctx.decl_ctx e in
|
2023-04-19 19:26:50 +03:00
|
|
|
assert false
|
2022-11-17 19:13:35 +03:00
|
|
|
else
|
|
|
|
match excepts, just with
|
|
|
|
| [except], _ when Expr.is_value except ->
|
2022-01-09 21:16:34 +03:00
|
|
|
(* if there is only one exception and it is a non-empty value it is
|
|
|
|
always chosen *)
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.remove except
|
2022-01-09 21:16:34 +03:00
|
|
|
| ( [],
|
|
|
|
( ( ELit (LBool true)
|
2022-11-17 19:13:35 +03:00
|
|
|
| EApp
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
{
|
|
|
|
f = EOp { op = Log _; _ }, _;
|
|
|
|
args = [(ELit (LBool true), _)];
|
|
|
|
} ),
|
2022-11-17 19:13:35 +03:00
|
|
|
_ ) ) ->
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.remove cons
|
2022-05-04 18:40:55 +03:00
|
|
|
| ( [],
|
2022-01-09 21:16:34 +03:00
|
|
|
( ( ELit (LBool false)
|
2022-11-17 19:13:35 +03:00
|
|
|
| EApp
|
|
|
|
{
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
f = EOp { op = Log _; _ }, _;
|
2022-11-17 19:13:35 +03:00
|
|
|
args = [(ELit (LBool false), _)];
|
|
|
|
} ),
|
|
|
|
_ ) ) ->
|
2023-04-07 11:51:21 +03:00
|
|
|
EEmptyError
|
2022-11-17 19:13:35 +03:00
|
|
|
| excepts, just -> EDefault { excepts; just; cons })
|
|
|
|
| EIfThenElse
|
|
|
|
{
|
|
|
|
cond =
|
|
|
|
( ELit (LBool true), _
|
|
|
|
| ( EApp
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
{
|
|
|
|
f = EOp { op = Log _; _ }, _;
|
|
|
|
args = [(ELit (LBool true), _)];
|
|
|
|
},
|
2022-11-17 19:13:35 +03:00
|
|
|
_ ) );
|
|
|
|
etrue;
|
|
|
|
_;
|
|
|
|
} ->
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.remove etrue
|
2022-11-17 19:13:35 +03:00
|
|
|
| EIfThenElse
|
|
|
|
{
|
|
|
|
cond =
|
|
|
|
( ( ELit (LBool false)
|
|
|
|
| EApp
|
|
|
|
{
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
f = EOp { op = Log _; _ }, _;
|
2022-11-17 19:13:35 +03:00
|
|
|
args = [(ELit (LBool false), _)];
|
|
|
|
} ),
|
|
|
|
_ );
|
|
|
|
efalse;
|
|
|
|
_;
|
|
|
|
} ->
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.remove efalse
|
2022-11-17 19:13:35 +03:00
|
|
|
| EIfThenElse
|
|
|
|
{
|
|
|
|
cond;
|
|
|
|
etrue =
|
|
|
|
( ( ELit (LBool btrue)
|
|
|
|
| EApp
|
|
|
|
{
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
f = EOp { op = Log _; _ }, _;
|
2022-11-17 19:13:35 +03:00
|
|
|
args = [(ELit (LBool btrue), _)];
|
|
|
|
} ),
|
|
|
|
_ );
|
|
|
|
efalse =
|
|
|
|
( ( ELit (LBool bfalse)
|
|
|
|
| EApp
|
|
|
|
{
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
f = EOp { op = Log _; _ }, _;
|
2022-11-17 19:13:35 +03:00
|
|
|
args = [(ELit (LBool bfalse), _)];
|
|
|
|
} ),
|
|
|
|
_ );
|
|
|
|
} ->
|
2023-05-17 16:44:57 +03:00
|
|
|
if btrue && not bfalse then Mark.remove cond
|
2022-11-17 19:13:35 +03:00
|
|
|
else if (not btrue) && bfalse then
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
EApp
|
|
|
|
{
|
|
|
|
f = EOp { op = Not; tys = [TLit TBool, Expr.mark_pos mark] }, mark;
|
|
|
|
args = [cond];
|
|
|
|
}
|
2022-11-17 19:13:35 +03:00
|
|
|
(* note: this last call eliminates the condition & might skip log calls
|
|
|
|
as well *)
|
|
|
|
else (* btrue = bfalse *) ELit (LBool btrue)
|
2023-04-21 13:32:09 +03:00
|
|
|
| EApp { f = EOp { op = Op.Fold; _ }, _; args = [_f; init; (EArray [], _)] }
|
|
|
|
->
|
|
|
|
(*reduces a fold with an empty list *)
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.remove init
|
2023-04-21 13:32:09 +03:00
|
|
|
| EApp
|
|
|
|
{ f = EOp { op = Op.Fold; _ }, _; args = [f; init; (EArray [e'], _)] }
|
|
|
|
->
|
|
|
|
(* reduces a fold with one element *)
|
|
|
|
EApp { f; args = [init; e'] }
|
|
|
|
| ECatch { body; exn; handler } -> (
|
|
|
|
(* peephole exception catching reductions *)
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove body, Mark.remove handler with
|
2023-04-21 13:32:09 +03:00
|
|
|
| ERaise exn', ERaise exn'' when exn' = exn && exn = exn'' -> ERaise exn
|
2023-05-17 16:44:57 +03:00
|
|
|
| ERaise exn', _ when exn' = exn -> Mark.remove handler
|
|
|
|
| _, ERaise exn' when exn' = exn -> Mark.remove body
|
2023-04-21 13:32:09 +03:00
|
|
|
| _ -> ECatch { body; exn; handler })
|
2022-11-17 19:13:35 +03:00
|
|
|
| e -> e
|
|
|
|
in
|
|
|
|
Expr.Box.app1 e reduce mark
|
2022-01-07 20:36:56 +03:00
|
|
|
|
2023-04-19 19:26:50 +03:00
|
|
|
let optimize_expr :
|
|
|
|
'm.
|
|
|
|
decl_ctx ->
|
|
|
|
(('a, 'b) dcalc_lcalc, 'm) gexpr ->
|
|
|
|
(('a, 'b) dcalc_lcalc, 'm) boxed_gexpr =
|
|
|
|
fun (decl_ctx : decl_ctx) (e : (('a, 'b) dcalc_lcalc, 'm) gexpr) ->
|
2023-04-21 12:56:07 +03:00
|
|
|
optimize_expr { var_values = Var.Map.empty; decl_ctx } e
|
2022-01-07 20:36:56 +03:00
|
|
|
|
2022-09-27 17:27:26 +03:00
|
|
|
let optimize_program (p : 'm program) : 'm program =
|
2022-04-02 15:51:11 +03:00
|
|
|
Bindlib.unbox
|
2023-04-21 12:56:07 +03:00
|
|
|
(Program.map_exprs ~f:(optimize_expr p.decl_ctx) ~varf:(fun v -> v) p)
|
2023-04-21 13:32:09 +03:00
|
|
|
|
|
|
|
let test_iota_reduction_1 () =
|
|
|
|
let x = Var.make "x" in
|
2023-08-30 18:49:29 +03:00
|
|
|
let enumT = EnumName.fresh [] ("t", Pos.no_pos) in
|
2023-04-21 13:32:09 +03:00
|
|
|
let consA = EnumConstructor.fresh ("A", Pos.no_pos) in
|
|
|
|
let consB = EnumConstructor.fresh ("B", Pos.no_pos) in
|
|
|
|
let consC = EnumConstructor.fresh ("C", Pos.no_pos) in
|
|
|
|
let consD = EnumConstructor.fresh ("D", Pos.no_pos) in
|
|
|
|
let nomark = Untyped { pos = Pos.no_pos } in
|
2023-08-10 17:52:39 +03:00
|
|
|
let injA = Expr.einj ~e:(Expr.evar x nomark) ~cons:consA ~name:enumT nomark in
|
|
|
|
let injC = Expr.einj ~e:(Expr.evar x nomark) ~cons:consC ~name:enumT nomark in
|
|
|
|
let injD = Expr.einj ~e:(Expr.evar x nomark) ~cons:consD ~name:enumT nomark in
|
2023-04-21 13:32:09 +03:00
|
|
|
let cases : ('a, 't) boxed_gexpr EnumConstructor.Map.t =
|
2023-07-12 12:48:46 +03:00
|
|
|
EnumConstructor.Map.of_list
|
|
|
|
[
|
|
|
|
consA, Expr.eabs (Expr.bind [| x |] injC) [TAny, Pos.no_pos] nomark;
|
|
|
|
consB, Expr.eabs (Expr.bind [| x |] injD) [TAny, Pos.no_pos] nomark;
|
|
|
|
]
|
2023-04-21 13:32:09 +03:00
|
|
|
in
|
2023-08-10 17:52:39 +03:00
|
|
|
let matchA = Expr.ematch ~e:injA ~name:enumT ~cases nomark in
|
2023-04-21 13:32:09 +03:00
|
|
|
Alcotest.(check string)
|
|
|
|
"same string"
|
|
|
|
"before=match (A x)\n\
|
|
|
|
\ with\n\
|
|
|
|
\ | A → (λ (x: any) → C x)\n\
|
|
|
|
\ | B → (λ (x: any) → D x)\n\
|
|
|
|
after=C\n\
|
|
|
|
x"
|
2023-05-02 17:33:23 +03:00
|
|
|
(Format.asprintf "before=%a\nafter=%a" Expr.format (Expr.unbox matchA)
|
|
|
|
Expr.format
|
2023-04-19 19:26:50 +03:00
|
|
|
(Expr.unbox (optimize_expr Program.empty_ctx (Expr.unbox matchA))))
|
2023-04-21 13:32:09 +03:00
|
|
|
|
|
|
|
let cases_of_list l : ('a, 't) boxed_gexpr EnumConstructor.Map.t =
|
2023-07-12 12:48:46 +03:00
|
|
|
EnumConstructor.Map.of_list
|
2023-04-21 13:32:09 +03:00
|
|
|
@@ ListLabels.map l ~f:(fun (cons, f) ->
|
|
|
|
let var = Var.make "x" in
|
|
|
|
( cons,
|
|
|
|
Expr.eabs
|
|
|
|
(Expr.bind [| var |] (f var))
|
|
|
|
[TAny, Pos.no_pos]
|
|
|
|
(Untyped { pos = Pos.no_pos }) ))
|
|
|
|
|
|
|
|
let test_iota_reduction_2 () =
|
2023-08-30 18:49:29 +03:00
|
|
|
let enumT = EnumName.fresh [] ("t", Pos.no_pos) in
|
2023-06-07 19:10:50 +03:00
|
|
|
let consA = EnumConstructor.fresh ("A", Pos.no_pos) in
|
|
|
|
let consB = EnumConstructor.fresh ("B", Pos.no_pos) in
|
|
|
|
let consC = EnumConstructor.fresh ("C", Pos.no_pos) in
|
|
|
|
let consD = EnumConstructor.fresh ("D", Pos.no_pos) in
|
2023-04-21 13:32:09 +03:00
|
|
|
|
2023-06-07 19:10:50 +03:00
|
|
|
let nomark = Untyped { pos = Pos.no_pos } in
|
2023-04-21 13:32:09 +03:00
|
|
|
|
2023-06-07 19:10:50 +03:00
|
|
|
let num n = Expr.elit (LInt (Runtime.integer_of_int n)) nomark in
|
2023-04-21 13:32:09 +03:00
|
|
|
|
2023-08-10 17:52:39 +03:00
|
|
|
let injAe e = Expr.einj ~e ~cons:consA ~name:enumT nomark in
|
|
|
|
let injBe e = Expr.einj ~e ~cons:consB ~name:enumT nomark in
|
|
|
|
let injCe e = Expr.einj ~e ~cons:consC ~name:enumT nomark in
|
|
|
|
let injDe e = Expr.einj ~e ~cons:consD ~name:enumT nomark in
|
2023-04-21 13:32:09 +03:00
|
|
|
|
2023-06-07 19:10:50 +03:00
|
|
|
(* let injA x = injAe (Expr.evar x nomark) in *)
|
|
|
|
let injB x = injBe (Expr.evar x nomark) in
|
|
|
|
let injC x = injCe (Expr.evar x nomark) in
|
|
|
|
let injD x = injDe (Expr.evar x nomark) in
|
2023-04-21 13:32:09 +03:00
|
|
|
|
2023-06-07 19:10:50 +03:00
|
|
|
let matchA =
|
|
|
|
Expr.ematch
|
2023-08-10 17:52:39 +03:00
|
|
|
~e:
|
|
|
|
(Expr.ematch ~e:(num 1) ~name:enumT
|
|
|
|
~cases:
|
|
|
|
(cases_of_list
|
2023-06-07 19:10:50 +03:00
|
|
|
[
|
|
|
|
(consB, fun x -> injBe (injB x));
|
|
|
|
(consA, fun _x -> injAe (num 20));
|
|
|
|
])
|
|
|
|
nomark)
|
2023-08-10 17:52:39 +03:00
|
|
|
~name:enumT
|
|
|
|
~cases:(cases_of_list [consA, injC; consB, injD])
|
2023-06-07 19:10:50 +03:00
|
|
|
nomark
|
|
|
|
in
|
|
|
|
Alcotest.(check string)
|
|
|
|
"same string "
|
|
|
|
"before=match\n\
|
|
|
|
\ (match 1\n\
|
|
|
|
\ with\n\
|
|
|
|
\ | A → (λ (x: any) → A 20)\n\
|
|
|
|
\ | B → (λ (x: any) → B B x))\n\
|
|
|
|
\ with\n\
|
|
|
|
\ | A → (λ (x: any) → C x)\n\
|
|
|
|
\ | B → (λ (x: any) → D x)\n\
|
|
|
|
after=match 1\n\
|
|
|
|
\ with\n\
|
|
|
|
\ | A → (λ (x: any) → C 20)\n\
|
|
|
|
\ | B → (λ (x: any) → D B x)\n"
|
|
|
|
(Format.asprintf "before=@[%a@]@.after=%a@." Expr.format (Expr.unbox matchA)
|
|
|
|
Expr.format
|
2023-04-19 19:26:50 +03:00
|
|
|
(Expr.unbox (optimize_expr Program.empty_ctx (Expr.unbox matchA))))
|