mirror of
https://github.com/CatalaLang/catala.git
synced 2024-09-20 00:41:05 +03:00
Better optimizations with values instead of literals
This commit is contained in:
parent
6cf1b768d2
commit
0d90dcea00
@ -189,6 +189,9 @@ let empty_thunked_term : expr Pos.marked =
|
|||||||
(Bindlib.box (ELit LEmptyError, Pos.no_pos))
|
(Bindlib.box (ELit LEmptyError, Pos.no_pos))
|
||||||
Pos.no_pos [ (TLit TUnit, Pos.no_pos) ] Pos.no_pos)
|
Pos.no_pos [ (TLit TUnit, Pos.no_pos) ] Pos.no_pos)
|
||||||
|
|
||||||
|
let is_value (e : expr Pos.marked) : bool =
|
||||||
|
match Pos.unmark e with ELit _ | EAbs _ | EOp _ -> true | _ -> false
|
||||||
|
|
||||||
let build_whole_scope_expr (ctx : decl_ctx) (body : scope_body) (pos_scope : Pos.t) =
|
let build_whole_scope_expr (ctx : decl_ctx) (body : scope_body) (pos_scope : Pos.t) =
|
||||||
let body_expr =
|
let body_expr =
|
||||||
List.fold_right
|
List.fold_right
|
||||||
|
@ -168,7 +168,9 @@ type scope_body = {
|
|||||||
|
|
||||||
type program = { decl_ctx : decl_ctx; scopes : (ScopeName.t * expr Bindlib.var * scope_body) list }
|
type program = { decl_ctx : decl_ctx; scopes : (ScopeName.t * expr Bindlib.var * scope_body) list }
|
||||||
|
|
||||||
(** {1 Variable helpers} *)
|
(** {1 Helpers} *)
|
||||||
|
|
||||||
|
(** {2 Variables}*)
|
||||||
|
|
||||||
module Var : sig
|
module Var : sig
|
||||||
type t = expr Bindlib.var
|
type t = expr Bindlib.var
|
||||||
@ -206,8 +208,12 @@ val make_let_in :
|
|||||||
Pos.t ->
|
Pos.t ->
|
||||||
expr Pos.marked Bindlib.box
|
expr Pos.marked Bindlib.box
|
||||||
|
|
||||||
|
(**{2 Other}*)
|
||||||
|
|
||||||
val empty_thunked_term : expr Pos.marked
|
val empty_thunked_term : expr Pos.marked
|
||||||
|
|
||||||
|
val is_value : expr Pos.marked -> bool
|
||||||
|
|
||||||
(** {1 AST manipulation helpers}*)
|
(** {1 AST manipulation helpers}*)
|
||||||
|
|
||||||
val build_whole_scope_expr : decl_ctx -> scope_body -> Pos.t -> expr Pos.marked Bindlib.box
|
val build_whole_scope_expr : decl_ctx -> scope_body -> Pos.t -> expr Pos.marked Bindlib.box
|
||||||
|
@ -291,7 +291,7 @@ and evaluate_expr (ctx : Ast.decl_ctx) (e : A.expr Pos.marked) : A.expr Pos.mark
|
|||||||
"function has not been reduced to a lambda at evaluation (should not happen if the \
|
"function has not been reduced to a lambda at evaluation (should not happen if the \
|
||||||
term was well-typed"
|
term was well-typed"
|
||||||
(Pos.get_position e))
|
(Pos.get_position e))
|
||||||
| EAbs _ | ELit _ | EOp _ -> e (* thse are values *)
|
| EAbs _ | ELit _ | EOp _ -> e (* these are values *)
|
||||||
| ETuple (es, s) ->
|
| ETuple (es, s) ->
|
||||||
let new_es = List.map (evaluate_expr ctx) es in
|
let new_es = List.map (evaluate_expr ctx) es in
|
||||||
if List.exists is_empty_error new_es then Pos.same_pos_as (A.ELit LEmptyError) e
|
if List.exists is_empty_error new_es then Pos.same_pos_as (A.ELit LEmptyError) e
|
||||||
|
@ -106,14 +106,14 @@ let rec partial_evaluation (ctx : partial_evaluation_ctx) (e : expr Pos.marked)
|
|||||||
with
|
with
|
||||||
| exceptions, just, cons
|
| exceptions, just, cons
|
||||||
when List.fold_left
|
when List.fold_left
|
||||||
(fun nb except -> match Pos.unmark except with ELit _ -> nb + 1 | _ -> nb)
|
(fun nb except -> if is_value except then nb + 1 else nb)
|
||||||
0 exceptions
|
0 exceptions
|
||||||
> 1 ->
|
> 1 ->
|
||||||
(* at this point we know a conflict error will be triggered so we just feed the
|
(* 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 *)
|
expression to the interpreter that will print the beautiful right error message *)
|
||||||
Interpreter.evaluate_expr ctx.decl_ctx (EDefault (exceptions, just, cons), pos)
|
Interpreter.evaluate_expr ctx.decl_ctx (EDefault (exceptions, just, cons), pos)
|
||||||
| [ ((ELit _, _) as except) ], _, _ ->
|
| [ except ], _, _ when is_value except ->
|
||||||
(* if there is only one exception and it is a non-empty literal it is always chosen *)
|
(* if there is only one exception and it is a non-empty value it is always chosen *)
|
||||||
except
|
except
|
||||||
| ( [],
|
| ( [],
|
||||||
((ELit (LBool true) | EApp ((EOp (Unop (Log _)), _), [ (ELit (LBool true), _) ])), _),
|
((ELit (LBool true) | EApp ((EOp (Unop (Log _)), _), [ (ELit (LBool true), _) ])), _),
|
||||||
|
@ -60,7 +60,7 @@ if __name__ == '__main__':
|
|||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
action = args.action[0]
|
action = args.action[0]
|
||||||
if action == "bench":
|
if action == "bench":
|
||||||
iterations = 10000
|
iterations = 1000
|
||||||
print("Iterating {} iterations of the family benefits computation. Total time (s):".format(
|
print("Iterating {} iterations of the family benefits computation. Total time (s):".format(
|
||||||
iterations))
|
iterations))
|
||||||
print(timeit.timeit(benchmark_iteration, number=iterations))
|
print(timeit.timeit(benchmark_iteration, number=iterations))
|
||||||
|
Loading…
Reference in New Issue
Block a user