From 6acf91a3f2263854a2c7043caa276ce104d0b6e2 Mon Sep 17 00:00:00 2001 From: Tom Jack Date: Wed, 8 Apr 2020 19:11:01 -0500 Subject: [PATCH] Perform basic eta contraction in Self_mini_c --- src/bin/expect_tests/contract_tests.ml | 10 ++-------- src/passes/11-self_mini_c/self_mini_c.ml | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/bin/expect_tests/contract_tests.ml b/src/bin/expect_tests/contract_tests.ml index 52c625c2d..1d9ff5c4c 100644 --- a/src/bin/expect_tests/contract_tests.ml +++ b/src/bin/expect_tests/contract_tests.ml @@ -10,10 +10,10 @@ let%expect_test _ = [%expect {| 1872 bytes |}] ; run_ligo_good [ "measure-contract" ; contract "multisig.ligo" ; "main" ] ; - [%expect {| 1282 bytes |}] ; + [%expect {| 1267 bytes |}] ; run_ligo_good [ "measure-contract" ; contract "multisig-v2.ligo" ; "main" ] ; - [%expect {| 2974 bytes |}] ; + [%expect {| 2959 bytes |}] ; run_ligo_good [ "measure-contract" ; contract "vote.mligo" ; "main" ] ; [%expect {| 589 bytes |}] ; @@ -301,9 +301,6 @@ let%expect_test _ = storage (pair (pair (list %auth key) (nat %counter)) (pair (string %id) (nat %threshold))) ; code { DUP ; - CAR ; - DIP { DUP ; CDR } ; - PAIR ; DUP ; CAR ; DIP { DUP } ; @@ -768,9 +765,6 @@ let%expect_test _ = CAR ; PAIR } ; DUP ; - CAR ; - DIP { DUP ; CDR } ; - PAIR ; DIP { DROP 17 } } ; DIP { DROP } } { DUP ; diff --git a/src/passes/11-self_mini_c/self_mini_c.ml b/src/passes/11-self_mini_c/self_mini_c.ml index 95e03a661..39a3a4c72 100644 --- a/src/passes/11-self_mini_c/self_mini_c.ml +++ b/src/passes/11-self_mini_c/self_mini_c.ml @@ -159,6 +159,25 @@ let betas : bool ref -> expression -> expression = fun changed -> map_expression (beta changed) +let eta : bool ref -> expression -> expression = + fun changed e -> + match e.content with + | E_constant {cons_name = C_PAIR; arguments = [ { content = E_constant {cons_name = C_CAR; arguments = [ e1 ]} ; type_value = _ } ; + { content = E_constant {cons_name = C_CDR; arguments = [ e2 ]} ; type_value = _ }]} -> + (match (e1.content, e2.content) with + | E_variable x1, E_variable x2 -> + if Var.equal x1 x2 + then + (changed := true; + { e with content = e1.content }) + else e + | _ -> e) + | _ -> e + +let etas : bool ref -> expression -> expression = + fun changed -> + map_expression (eta changed) + let contract_check = let all = [Michelson_restrictions.self_in_lambdas] in let all_e = List.map Helpers.map_sub_level_expression all in @@ -169,6 +188,7 @@ let rec all_expression : expression -> expression = let changed = ref false in let e = inline_lets changed e in let e = betas changed e in + let e = etas changed e in if !changed then all_expression e else e