From c4796d56956cda5bb427bf23a28a40866396e481 Mon Sep 17 00:00:00 2001 From: Sander Spies Date: Thu, 26 Mar 2020 17:53:46 +0100 Subject: [PATCH 01/13] Describe nested updates. Put record patches section in a pascaligo syntax block. --- .../docs/language-basics/maps-records.md | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/gitlab-pages/docs/language-basics/maps-records.md b/gitlab-pages/docs/language-basics/maps-records.md index 837290d39..76dd2e7b9 100644 --- a/gitlab-pages/docs/language-basics/maps-records.md +++ b/gitlab-pages/docs/language-basics/maps-records.md @@ -230,6 +230,106 @@ xy_translate "({x:2,y:3,z:1}, {dx:3,dy:4})" You have to understand that `p` has not been changed by the functional update: a nameless new version of it has been created and returned. +#### Nested updates + +A unique feature of LIGO is the ability to perform nested updates on records. + +For example if you have the following record structure: + + + +```pascaligo +type color is +| Blue +| Green + +type preferences is record [ + color : color; + other : int; +] + +type account is record [ + id : int; + preferences : preferences; +] +``` + + + + +```cameligo +type color = + Blue +| Green + +type preferences = { + color : color; + other : int; +} + +type account = { + id: int; + preferences: preferences; +} +``` + + + + +```reasonligo +type color = + Blue +| Green; + +type preferences = { + color : color, + other : int +} + +type account = { + id : int, + preferences : preferences +} +``` + + + +You can update the nested record with the following code: + + + +```pascaligo + +function change_color_preference (const account : account; const color : color ) : account is + block { + account := account with record [preferences.color = color] + } with account + +``` + + + + +```cameligo +let change_color_preference (account : account) (color : color) : account = + { account with preferences.color = color } +``` + + + + +```reasonligo +let change_color_preference = (account : account, color : color): account => + { ...account, preferences.color: color }; +``` + + + +Note that all the records in the path will get updated. In this example that's +`account` and `preferences`. + + + ### Record Patches Another way to understand what it means to update a record value is to @@ -318,6 +418,8 @@ xy_translate "(record [x=2;y=3;z=1], record [dx=3;dy=4])" The hiding of a variable by another (here `p`) is called `shadowing`. + + ## Maps *Maps* are a data structure which associate values of the same type to From 4003aa4e06e20ff13b9e1f75804dbd90bf9c1c51 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Wulfman Date: Fri, 27 Mar 2020 14:57:56 +0100 Subject: [PATCH 02/13] add for, for_each, while construct in ast_imperatve;\n Move the treatment of loops and handling of the assignment in imperative_to_sugar --- src/bin/expect_tests/contract_tests.ml | 170 +++------ src/passes/2-concrete_to_imperative/dune | 1 - .../2-concrete_to_imperative/pascaligo.ml | 323 ++-------------- src/passes/3-self_ast_imperative/helpers.ml | 34 ++ src/passes/4-imperative_to_sugar/dune | 1 + .../imperative_to_sugar.ml | 350 +++++++++++++++-- src/passes/5-self_ast_sugar/helpers.ml | 356 ++++++++++++++++++ src/passes/5-self_ast_sugar/self_ast_sugar.ml | 25 ++ src/passes/6-sugar_to_core/sugar_to_core.ml | 2 +- src/stages/1-ast_imperative/PP.ml | 21 ++ src/stages/1-ast_imperative/combinators.ml | 5 + src/stages/1-ast_imperative/combinators.mli | 3 + src/stages/1-ast_imperative/misc.ml | 4 +- src/stages/1-ast_imperative/types.ml | 29 ++ src/stages/2-ast_sugar/PP.ml | 11 +- src/stages/2-ast_sugar/combinators.ml | 4 +- src/stages/2-ast_sugar/combinators.mli | 2 +- src/stages/2-ast_sugar/types.ml | 12 +- src/test/contracts/loop.ligo | 2 +- 19 files changed, 911 insertions(+), 444 deletions(-) create mode 100644 src/passes/5-self_ast_sugar/helpers.ml create mode 100644 src/passes/5-self_ast_sugar/self_ast_sugar.ml diff --git a/src/bin/expect_tests/contract_tests.ml b/src/bin/expect_tests/contract_tests.ml index 984a0163b..3e78f213c 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 {| 1870 bytes |}] ; run_ligo_good [ "measure-contract" ; contract "multisig.ligo" ; "main" ] ; - [%expect {| 1324 bytes |}] ; + [%expect {| 1294 bytes |}] ; run_ligo_good [ "measure-contract" ; contract "multisig-v2.ligo" ; "main" ] ; - [%expect {| 3231 bytes |}] ; + [%expect {| 2935 bytes |}] ; run_ligo_good [ "measure-contract" ; contract "vote.mligo" ; "main" ] ; [%expect {| 589 bytes |}] ; @@ -312,11 +312,8 @@ let%expect_test _ = SWAP ; CAR ; CDR ; - DUP ; - DIP { DIP 2 { DUP } ; DIG 2 } ; - PAIR ; - DIP { DIP { DUP } ; SWAP } ; - PAIR ; + DIP { DUP } ; + SWAP ; DIP 3 { DUP } ; DIG 3 ; CAR ; @@ -333,31 +330,31 @@ let%expect_test _ = PAIR ; DUP ; CAR ; - CAR ; + CDR ; DIP { DUP } ; SWAP ; CAR ; - CDR ; + CAR ; DIP 2 { DUP } ; DIG 2 ; CDR ; - DIP 2 { DUP } ; - DIG 2 ; - DIP { DIP { DUP } ; SWAP } ; + DIP { DUP } ; + SWAP ; + DIP { DIP 2 { DUP } ; DIG 2 } ; PAIR ; - DIP 3 { DUP } ; - DIG 3 ; + DIP 2 { DUP } ; + DIG 2 ; IF_CONS - { DIP 4 { DUP } ; - DIG 4 ; + { DIP 5 { DUP } ; + DIG 5 ; DIP 4 { DUP } ; DIG 4 ; CAR ; DIP { DIP { DUP } ; SWAP ; HASH_KEY } ; COMPARE ; EQ ; - IF { DIP 5 { DUP } ; - DIG 5 ; + IF { DIP 6 { DUP } ; + DIG 6 ; DIP 2 { DUP } ; DIG 2 ; DIP { DIP 5 { DUP } ; @@ -371,8 +368,8 @@ let%expect_test _ = PAIR ; PACK } } ; CHECK_SIGNATURE ; - IF { DIP 6 { DUP } ; - DIG 6 ; + IF { DIP 7 { DUP } ; + DIG 7 ; PUSH nat 1 ; ADD ; DIP { DUP } ; @@ -407,9 +404,10 @@ let%expect_test _ = CAR ; DIP 2 { DUP } ; DIG 2 ; + CAR ; + SWAP ; CDR ; SWAP ; - CAR ; PAIR ; SWAP ; CDR ; @@ -421,10 +419,9 @@ let%expect_test _ = CAR ; DIP 3 { DUP } ; DIG 3 ; - CAR ; - SWAP ; CDR ; SWAP ; + CAR ; PAIR ; SWAP ; CDR ; @@ -460,16 +457,14 @@ let%expect_test _ = DIP { DROP 2 } } ; DIP 3 { DUP } ; DIG 3 ; - CAR ; DIP { DUP } ; - PAIR ; - DIP { DROP 3 } } ; - DUP ; - CAR ; - CAR ; + SWAP ; + DIP { DROP 4 } } ; + DIP 2 { DUP } ; + DIG 2 ; UNIT ; EXEC ; - DIP { DUP ; CDR } ; + DIP { DUP } ; PAIR ; DIP { DROP 7 } } } |} ] @@ -628,13 +623,9 @@ let%expect_test _ = GT ; IF { PUSH string "Maximum number of proposal reached" ; FAILWITH } { PUSH unit Unit } ; - DIP 8 { DUP } ; - DIG 8 ; - DIP { DIP 3 { DUP } ; DIG 3 } ; - PAIR ; - DIP { DIP 7 { DUP } ; DIG 7 ; NIL operation ; SWAP ; PAIR } ; - PAIR ; - DIP { DIP 2 { DUP } ; DIG 2 } ; + DIP 2 { DUP } ; + DIG 2 ; + NIL operation ; PAIR ; DIP 4 { DUP } ; DIG 4 ; @@ -689,32 +680,26 @@ let%expect_test _ = SWAP ; CDR ; CAR ; - DIP 2 { DUP } ; - DIG 2 ; - CDR ; - CDR ; DIP { DUP } ; SWAP ; DIP { DUP } ; - PAIR ; - DIP { DIP 2 { DUP } ; DIG 2 } ; - PAIR ; - DIP 2 { DUP } ; - DIG 2 ; - DIP { DIP 12 { DUP } ; DIG 12 } ; + SWAP ; + DIP { DIP 11 { DUP } ; DIG 11 } ; MEM ; - IF { DIP 3 { DUP } ; - DIG 3 ; - DIP 3 { DUP } ; - DIG 3 ; - DIP { DIP 2 { DUP } ; - DIG 2 ; + IF { DIP 2 { DUP } ; + DIG 2 ; + DIP 2 { DUP } ; + DIG 2 ; + DIP { DIP 4 { DUP } ; + DIG 4 ; + CDR ; + CDR ; PUSH nat 1 ; SWAP ; SUB ; ABS ; SOME ; - DIP { DIP 4 { DUP } ; DIG 4 ; CDR ; CAR ; CAR } } ; + DIP { DIP 3 { DUP } ; DIG 3 ; CDR ; CAR ; CAR } } ; UPDATE ; DIP { DUP ; CAR ; SWAP ; CDR ; DUP ; CDR ; SWAP ; CAR ; CDR } ; PAIR ; @@ -723,23 +708,21 @@ let%expect_test _ = PAIR ; DIP { DUP } ; SWAP ; - CAR ; DIP { DUP } ; - PAIR ; - DIP { DROP } } + SWAP ; + DIP { DROP 2 } } { DUP } ; + DIP 4 { DUP } ; + DIG 4 ; DIP 5 { DUP } ; DIG 5 ; - DIP 6 { DUP } ; - DIG 6 ; CAR ; DIP 2 { DUP } ; DIG 2 ; - CDR ; DIP { DROP ; CDR } ; PAIR ; CAR ; - DIP { DROP 6 } } ; + DIP { DROP 5 } } ; DIP 4 { DUP } ; DIG 4 ; DIP 4 { DUP } ; @@ -749,12 +732,10 @@ let%expect_test _ = PAIR ; DIP 3 { DUP } ; DIG 3 ; - DIP { DUP ; CDR ; SWAP ; CAR ; DUP ; CAR ; SWAP ; CDR ; CAR } ; SWAP ; - PAIR ; + CDR ; SWAP ; PAIR ; - PAIR ; DIP 2 { DUP } ; DIG 2 ; SWAP ; @@ -785,8 +766,6 @@ let%expect_test _ = PAIR } ; DUP ; CAR ; - CDR ; - CDR ; DIP { DUP ; CDR } ; PAIR ; DIP { DROP 15 } } ; @@ -800,9 +779,8 @@ let%expect_test _ = SWAP ; CAR ; PACK ; - DUP ; - DIP { DIP { DUP } ; SWAP } ; - PAIR ; + DIP { DUP } ; + SWAP ; DIP { DUP } ; SWAP ; DIP { DIP 2 { DUP } ; DIG 2 ; CAR ; CDR ; CDR } ; @@ -851,12 +829,7 @@ let%expect_test _ = DIP { DROP 2 } } { DUP } ; DUP ; - DIP 3 { DUP } ; - DIG 3 ; - DIP { DIP 6 { DUP } ; DIG 6 } ; - PAIR ; - DIP { DUP } ; - PAIR ; + DUP ; DIP 4 { DUP } ; DIG 4 ; SIZE ; @@ -878,10 +851,9 @@ let%expect_test _ = PAIR ; DIP { DUP } ; SWAP ; - CAR ; DIP { DUP } ; - PAIR ; - DIP { DROP } } + SWAP ; + DIP { DROP 2 } } { DUP ; DIP 2 { DUP } ; DIG 2 ; @@ -898,47 +870,15 @@ let%expect_test _ = SWAP ; PAIR ; PAIR ; - SWAP ; - CAR ; - PAIR } ; + DIP { DROP } } ; DIP 7 { DUP } ; DIG 7 ; DIP 3 { DUP } ; DIG 3 ; + DIP { DROP ; DUP } ; SWAP ; - CAR ; - PAIR ; - DIP { DUP } ; - SWAP ; - CAR ; - CDR ; - SWAP ; - CDR ; - SWAP ; - PAIR ; - DIP { DUP } ; - SWAP ; - CDR ; - SWAP ; - CAR ; - PAIR ; - DIP { DUP } ; - SWAP ; - CAR ; - CDR ; - SWAP ; - CDR ; - SWAP ; - PAIR ; - DIP { DUP } ; - SWAP ; - CDR ; - SWAP ; - CAR ; - PAIR ; - DIP { DROP 7 } } ; + DIP { DROP 8 } } ; DUP ; - CDR ; NIL operation ; PAIR ; DIP { DROP 6 } } ; @@ -1174,7 +1114,7 @@ let%expect_test _ = let%expect_test _ = run_ligo_bad [ "compile-contract" ; bad_contract "create_contract_toplevel.mligo" ; "main" ] ; [%expect {| -ligo: in file "create_contract_toplevel.mligo", line 4, character 35 to line 8, character 8. No free variable allowed in this lambda: variable 'store' {"expression":"CREATE_CONTRACT(lambda (#P:Some(( nat * string ))) : None return let rhs#654 = #P in let p = rhs#654.0 in let s = rhs#654.1 in ( list[] : (TO_list(operation)) , store ) , NONE() : (TO_option(key_hash)) , 300000000mutez , \"un\")","location":"in file \"create_contract_toplevel.mligo\", line 4, character 35 to line 8, character 8"} +ligo: in file "create_contract_toplevel.mligo", line 4, character 35 to line 8, character 8. No free variable allowed in this lambda: variable 'store' {"expression":"CREATE_CONTRACT(lambda (#P:Some(( nat * string ))) : None return let rhs#701 = #P in let p = rhs#701.0 in let s = rhs#701.1 in ( list[] : (TO_list(operation)) , store ) , NONE() : (TO_option(key_hash)) , 300000000mutez , \"un\")","location":"in file \"create_contract_toplevel.mligo\", line 4, character 35 to line 8, character 8"} If you're not sure how to fix this error, you can @@ -1187,7 +1127,7 @@ ligo: in file "create_contract_toplevel.mligo", line 4, character 35 to line 8, run_ligo_bad [ "compile-contract" ; bad_contract "create_contract_var.mligo" ; "main" ] ; [%expect {| -ligo: in file "create_contract_var.mligo", line 6, character 35 to line 10, character 5. No free variable allowed in this lambda: variable 'a' {"expression":"CREATE_CONTRACT(lambda (#P:Some(( nat * int ))) : None return let rhs#657 = #P in let p = rhs#657.0 in let s = rhs#657.1 in ( list[] : (TO_list(operation)) , a ) , NONE() : (TO_option(key_hash)) , 300000000mutez , 1)","location":"in file \"create_contract_var.mligo\", line 6, character 35 to line 10, character 5"} +ligo: in file "create_contract_var.mligo", line 6, character 35 to line 10, character 5. No free variable allowed in this lambda: variable 'a' {"expression":"CREATE_CONTRACT(lambda (#P:Some(( nat * int ))) : None return let rhs#704 = #P in let p = rhs#704.0 in let s = rhs#704.1 in ( list[] : (TO_list(operation)) , a ) , NONE() : (TO_option(key_hash)) , 300000000mutez , 1)","location":"in file \"create_contract_var.mligo\", line 6, character 35 to line 10, character 5"} If you're not sure how to fix this error, you can diff --git a/src/passes/2-concrete_to_imperative/dune b/src/passes/2-concrete_to_imperative/dune index c3f316ce4..1aa60f304 100644 --- a/src/passes/2-concrete_to_imperative/dune +++ b/src/passes/2-concrete_to_imperative/dune @@ -6,7 +6,6 @@ tezos-utils parser ast_imperative - self_ast_imperative operators) (modules cameligo pascaligo concrete_to_imperative) (preprocess diff --git a/src/passes/2-concrete_to_imperative/pascaligo.ml b/src/passes/2-concrete_to_imperative/pascaligo.ml index 7823cfb4e..53287557d 100644 --- a/src/passes/2-concrete_to_imperative/pascaligo.ml +++ b/src/passes/2-concrete_to_imperative/pascaligo.ml @@ -14,90 +14,6 @@ let pseq_to_list = function | Some lst -> npseq_to_list lst let get_value : 'a Raw.reg -> 'a = fun x -> x.value -and repair_mutable_variable_in_matching (for_body : expression) (element_names : expression_variable list) (env : expression_variable) = - let%bind captured_names = Self_ast_imperative.fold_map_expression - (* TODO : these should use Variables sets *) - (fun (decl_var,free_var : expression_variable list * expression_variable list) (ass_exp : expression) -> - match ass_exp.expression_content with - | E_let_in {let_binder;mut=false;rhs;let_result} -> - let (name,_) = let_binder in - ok (true,(name::decl_var, free_var),e_let_in let_binder false false rhs let_result) - | E_let_in {let_binder;mut=true; rhs;let_result} -> - let (name,_) = let_binder in - if List.mem name decl_var then - ok (true,(decl_var, free_var), e_let_in let_binder false false rhs let_result) - else( - let free_var = if (List.mem name free_var) then free_var else name::free_var in - let expr = e_let_in (env,None) false false (e_update (e_variable env) (Var.to_name name) (e_variable name)) let_result in - ok (true,(decl_var, free_var), e_let_in let_binder false false rhs expr) - ) - | E_variable name -> - if List.mem name decl_var || List.mem name free_var || Var.equal name env then - ok (true,(decl_var, free_var), e_variable name) - else - ok (true, (decl_var, name::free_var), e_variable name) - | E_constant {cons_name=C_MAP_FOLD;arguments= _} - | E_constant {cons_name=C_SET_FOLD;arguments= _} - | E_constant {cons_name=C_LIST_FOLD;arguments= _} - | E_matching _ -> ok @@ (false, (decl_var,free_var),ass_exp) - | _ -> ok (true, (decl_var, free_var),ass_exp) - ) - (element_names,[]) - for_body in - ok @@ captured_names - -and repair_mutable_variable_in_loops (for_body : expression) (element_names : expression_variable list) (env : expression_variable) = - let%bind captured_names = Self_ast_imperative.fold_map_expression - (* TODO : these should use Variables sets *) - (fun (decl_var,free_var : expression_variable list * expression_variable list) (ass_exp : expression) -> - match ass_exp.expression_content with - | E_let_in {let_binder;mut=false;rhs;let_result} -> - let (name,_) = let_binder in - ok (true,(name::decl_var, free_var),e_let_in let_binder false false rhs let_result) - | E_let_in {let_binder;mut=true; rhs;let_result} -> - let (name,_) = let_binder in - if List.mem name decl_var then - ok (true,(decl_var, free_var), e_let_in let_binder false false rhs let_result) - else( - let free_var = if (List.mem name free_var) then free_var else name::free_var in - let expr = e_let_in (env,None) false false ( - e_update (e_variable env) ("0") - (e_update (e_accessor (e_variable env) "0") (Var.to_name name) (e_variable name)) - ) - let_result in - ok (true,(decl_var, free_var), e_let_in let_binder false false rhs expr) - ) - | E_variable name -> - if List.mem name decl_var || List.mem name free_var || Var.equal name env then - ok (true,(decl_var, free_var), e_variable name) - else - ok (true,(decl_var, name::free_var), e_variable name) - | E_constant {cons_name=C_MAP_FOLD;arguments= _} - | E_constant {cons_name=C_SET_FOLD;arguments= _} - | E_constant {cons_name=C_LIST_FOLD;arguments= _} - | E_matching _ -> ok @@ (false,(decl_var,free_var),ass_exp) - | _ -> ok (true,(decl_var, free_var),ass_exp) - ) - (element_names,[]) - for_body in - ok @@ captured_names - -and store_mutable_variable (free_vars : expression_variable list) = - if (List.length free_vars == 0) then - e_unit () - else - let aux var = (Var.to_name var, e_variable var) in - e_record_ez (List.map aux free_vars) - -and restore_mutable_variable (expr : expression->expression) (free_vars : expression_variable list) (env :expression_variable) = - let aux (f:expression -> expression) (ev:expression_variable) = - ok @@ fun expr -> f (e_let_in (ev,None) true false (e_accessor (e_variable env) (Var.to_name ev)) expr) - in - let%bind ef = bind_fold_list aux (fun e -> e) free_vars in - ok @@ fun expr'_opt -> match expr'_opt with - | None -> ok @@ expr (ef (e_skip ())) - | Some expr' -> ok @@ expr (ef expr') - module Errors = struct @@ -433,10 +349,7 @@ let rec compile_expression (t:Raw.expr) : expr result = let%bind expr = compile_expression c.test in let%bind match_true = compile_expression c.ifso in let%bind match_false = compile_expression c.ifnot in - let match_expr = e_matching expr ~loc (Match_bool {match_true; match_false}) in - let env = Var.fresh () in - let%bind (_, match_expr) = repair_mutable_variable_in_matching match_expr [] env in - return @@ match_expr + return @@ e_matching expr ~loc (Match_bool {match_true; match_false}) | ECase c -> ( let (c , loc) = r_split c in @@ -450,10 +363,7 @@ let rec compile_expression (t:Raw.expr) : expr result = @@ List.map get_value @@ npseq_to_list c.cases.value in let%bind cases = compile_cases lst in - let match_expr = e_matching ~loc e cases in - let env = Var.fresh () in - let%bind (_, match_expr) = repair_mutable_variable_in_matching match_expr [] env in - return @@ match_expr + return @@ e_matching ~loc e cases ) | EMap (MapInj mi) -> ( let (mi , loc) = r_split mi in @@ -819,35 +729,6 @@ and compile_statement_list statements = hook (compile_data_declaration d :: acc) statements in bind_list @@ hook [] (List.rev statements) -and get_case_variables (t:Raw.pattern) : expression_variable list result = - match t with - | PConstr PFalse _ - | PConstr PTrue _ - | PConstr PNone _ -> ok @@ [] - | PConstr PSomeApp v -> (let (_,v) = v.value in get_case_variables (v.value.inside)) - | PConstr PConstrApp v -> ( - match v.value with - | constr, None -> ok @@ [ Var.of_name constr.value] - | constr, pat_opt -> - let%bind pat = - trace_option (unsupported_cst_constr t) @@ - pat_opt in - let pat = npseq_to_list pat.value.inside in - let%bind var = bind_map_list get_case_variables pat in - ok @@ [Var.of_name constr.value ] @ (List.concat var) - ) - | PList PNil _ -> ok @@ [] - | PList PCons c -> ( - match c.value with - | a, [(_, b)] -> - let%bind a = get_case_variables a in - let%bind b = get_case_variables b in - ok @@ a@b - | _ -> fail @@ unsupported_deep_list_patterns c - ) - | PVar v -> ok @@ [Var.of_name v.value] - | p -> fail @@ unsupported_cst_constr p - and compile_single_instruction : Raw.instruction -> (_ -> expression result) result = fun t -> match t with @@ -877,14 +758,33 @@ and compile_single_instruction : Raw.instruction -> (_ -> expression result) res return_statement @@ e_skip ~loc () ) | Loop (While l) -> - compile_while_loop l.value + let (wl, loc) = r_split l in + let%bind condition = compile_expression wl.cond in + let%bind body = compile_block wl.block.value in + let%bind body = body @@ None in + return_statement @@ e_while ~loc condition body | Loop (For (ForInt fi)) -> ( - let%bind loop = compile_for_int fi.value in - ok loop + let (fi,loc) = r_split fi in + let binder = Var.of_name fi.assign.value.name.value in + let%bind start = compile_expression fi.assign.value.expr in + let%bind bound = compile_expression fi.bound in + let increment = e_int 1 in + let%bind body = compile_block fi.block.value in + let%bind body = body @@ None in + return_statement @@ e_for ~loc binder start bound increment body ) | Loop (For (ForCollect fc)) -> - let%bind loop = compile_for_collect fc.value in - ok loop + let (fc,loc) = r_split fc in + let binder = (Var.of_name fc.var.value, Option.map (fun x -> Var.of_name (snd x:string Raw.reg).value) fc.bind_to) in + let%bind collection = compile_expression fc.expr in + let collection_type = match fc.collection with + | Map _ -> Map + | Set _ -> Set + | List _ -> List + in + let%bind body = compile_block fc.block.value in + let%bind body = body @@ None in + return_statement @@ e_for_each ~loc binder collection collection_type body | Cond c -> ( let (c , loc) = r_split c in let%bind expr = compile_expression c.test in @@ -906,26 +806,10 @@ and compile_single_instruction : Raw.instruction -> (_ -> expression result) res compile_block value | ShortBlock {value; _} -> compile_statements @@ fst value.inside in - let env = Var.fresh () in - let%bind match_true' = match_true None in - let%bind match_false' = match_false None in - let%bind match_true = match_true @@ Some (e_variable env) in - let%bind match_false = match_false @@ Some (e_variable env) in - - let%bind ((_,free_vars_true), match_true) = repair_mutable_variable_in_matching match_true [] env in - let%bind ((_,free_vars_false), match_false) = repair_mutable_variable_in_matching match_false [] env in - let free_vars = free_vars_true @ free_vars_false in - if (List.length free_vars != 0) then - let match_expr = e_matching expr ~loc (Match_bool {match_true; match_false}) in - let return_expr = fun expr -> - e_let_in (env,None) false false (store_mutable_variable free_vars) @@ - e_let_in (env,None) false false match_expr @@ - expr - in - restore_mutable_variable return_expr free_vars env - else - return_statement @@ e_matching expr ~loc (Match_bool {match_true=match_true'; match_false=match_false'}) + let%bind match_true = match_true None in + let%bind match_false = match_false None in + return_statement @@ e_matching expr ~loc (Match_bool {match_true; match_false}) ) | Assign a -> ( let (a , loc) = r_split a in @@ -954,9 +838,8 @@ and compile_single_instruction : Raw.instruction -> (_ -> expression result) res | CaseInstr c -> ( let (c , loc) = r_split c in let%bind expr = compile_expression c.expr in - let env = Var.fresh () in - let%bind (fv,cases) = - let aux fv (x : Raw.if_clause Raw.case_clause Raw.reg) = + let%bind cases = + let aux (x : Raw.if_clause Raw.case_clause Raw.reg) = let%bind case_clause = match x.value.rhs with ClauseInstr i -> @@ -967,28 +850,13 @@ and compile_single_instruction : Raw.instruction -> (_ -> expression result) res compile_block value | ShortBlock {value; _} -> compile_statements @@ fst value.inside in - let%bind case_clause'= case_clause @@ None in - let%bind case_clause = case_clause @@ Some(e_variable env) in - let%bind case_vars = get_case_variables x.value.pattern in - let%bind ((_,free_vars), case_clause) = repair_mutable_variable_in_matching case_clause case_vars env in - ok (free_vars::fv,(x.value.pattern, case_clause, case_clause')) in - bind_fold_map_list aux [] (npseq_to_list c.cases.value) in - let free_vars = List.concat fv in - if (List.length free_vars == 0) then ( - let cases = List.map (fun case -> let (a,_,b) = case in (a,b)) cases in - let%bind m = compile_cases cases in - return_statement @@ e_matching ~loc expr m - ) else ( - let cases = List.map (fun case -> let (a,b,_) = case in (a,b)) cases in - let%bind m = compile_cases cases in - let match_expr = e_matching ~loc expr m in - let return_expr = fun expr -> - e_let_in (env,None) false false (store_mutable_variable free_vars) @@ - e_let_in (env,None) false false match_expr @@ - expr - in - restore_mutable_variable return_expr free_vars env - ) + let%bind case_clause = case_clause None in + ok (x.value.pattern, case_clause) in + bind_list + @@ List.map aux + @@ npseq_to_list c.cases.value in + let%bind m = compile_cases cases in + return_statement @@ e_matching ~loc expr m ) | RecordPatch r -> ( let reg = r.region in @@ -1204,121 +1072,6 @@ and compile_statements : Raw.statements -> (_ -> expression result) result = and compile_block : Raw.block -> (_ -> expression result) result = fun t -> compile_statements t.statements -and compile_while_loop : Raw.while_loop -> (_ -> expression result) result = fun wl -> - let env_rec = Var.fresh () in - let binder = Var.fresh () in - - let%bind cond = compile_expression wl.cond in - let ctrl = - (e_variable binder) - in - - let%bind for_body = compile_block wl.block.value in - let%bind for_body = for_body @@ Some( ctrl ) in - let%bind ((_,captured_name_list),for_body) = repair_mutable_variable_in_loops for_body [] binder in - - let aux name expr= - e_let_in (name,None) false false (e_accessor (e_accessor (e_variable binder) "0") (Var.to_name name)) expr - in - let init_rec = e_tuple [store_mutable_variable @@ captured_name_list] in - let restore = fun expr -> List.fold_right aux captured_name_list expr in - let continue_expr = e_constant C_FOLD_CONTINUE [for_body] in - let stop_expr = e_constant C_FOLD_STOP [e_variable binder] in - let aux_func = - e_lambda binder None None @@ - restore @@ - e_cond cond continue_expr stop_expr in - let loop = e_constant C_FOLD_WHILE [aux_func; e_variable env_rec] in - let return_expr = fun expr -> - e_let_in (env_rec,None) false false init_rec @@ - e_let_in (env_rec,None) false false loop @@ - e_let_in (env_rec,None) false false (e_accessor (e_variable env_rec) "0") @@ - expr - in - restore_mutable_variable return_expr captured_name_list env_rec - - -and compile_for_int : Raw.for_int -> (_ -> expression result) result = fun fi -> - let env_rec = Var.fresh () in - let binder = Var.fresh () in - let name = fi.assign.value.name.value in - let it = Var.of_name name in - let var = e_variable it in - (*Make the cond and the step *) - let%bind value = compile_expression fi.assign.value.expr in - let%bind bound = compile_expression fi.bound in - let cond = e_annotation (e_constant C_LE [var ; bound]) t_bool in - let step = e_int 1 in - let continue_expr = e_constant C_FOLD_CONTINUE [(e_variable binder)] in - let ctrl = - e_let_in (it,Some t_int) false false (e_constant C_ADD [ var ; step ]) @@ - e_let_in (binder, None) false false (e_update (e_variable binder) "1" var)@@ - continue_expr - in - (* Modify the body loop*) - let%bind for_body = compile_block fi.block.value in - let%bind for_body = for_body @@ Some ctrl in - let%bind ((_,captured_name_list),for_body) = repair_mutable_variable_in_loops for_body [it] binder in - - let aux name expr= - e_let_in (name,None) false false (e_accessor (e_accessor (e_variable binder) "0") (Var.to_name name)) expr - in - - (* restores the initial value of the free_var*) - let restore = fun expr -> List.fold_right aux captured_name_list expr in - - (*Prep the lambda for the fold*) - let stop_expr = e_constant C_FOLD_STOP [e_variable binder] in - let aux_func = e_lambda binder None None @@ - e_let_in (it,Some t_int) false false (e_accessor (e_variable binder) "1") @@ - e_cond cond (restore for_body) (stop_expr) in - - (* Make the fold_while en precharge the vakye *) - let loop = e_constant C_FOLD_WHILE [aux_func; e_variable env_rec] in - let init_rec = e_pair (store_mutable_variable @@ captured_name_list) var in - - let return_expr = fun expr -> - e_let_in (it, Some t_int) false false value @@ - e_let_in (env_rec,None) false false init_rec @@ - e_let_in (env_rec,None) false false loop @@ - e_let_in (env_rec,None) false false (e_accessor (e_variable env_rec) "0") @@ - expr - in - restore_mutable_variable return_expr captured_name_list env_rec - -and compile_for_collect : Raw.for_collect -> (_ -> expression result) result = fun fc -> - let binder = Var.of_name "arguments" in - let%bind element_names = ok @@ match fc.bind_to with - | Some v -> [Var.of_name fc.var.value;Var.of_name (snd v).value] - | None -> [Var.of_name fc.var.value] in - - let env = Var.fresh () in - let%bind for_body = compile_block fc.block.value in - let%bind for_body = for_body @@ Some (e_accessor (e_variable binder) "0") in - let%bind ((_,free_vars), for_body) = repair_mutable_variable_in_loops for_body element_names binder in - - let init_record = store_mutable_variable free_vars in - let%bind collect = compile_expression fc.expr in - let aux name expr= - e_let_in (name,None) false false (e_accessor (e_accessor (e_variable binder) "0") (Var.to_name name)) expr - in - let restore = fun expr -> List.fold_right aux free_vars expr in - let restore = match fc.collection with - | Map _ -> (match fc.bind_to with - | Some v -> fun expr -> restore (e_let_in (Var.of_name fc.var.value, None) false false (e_accessor (e_accessor (e_variable binder) "1") "0") - (e_let_in (Var.of_name (snd v).value, None) false false (e_accessor (e_accessor (e_variable binder) "1") "1") expr)) - | None -> fun expr -> restore (e_let_in (Var.of_name fc.var.value, None) false false (e_accessor (e_accessor (e_variable binder) "1") "0") expr) - ) - | _ -> fun expr -> restore (e_let_in (Var.of_name fc.var.value, None) false false (e_accessor (e_variable binder) "1") expr) - in - let lambda = e_lambda binder None None (restore for_body) in - let op_name = match fc.collection with - | Map _ -> C_MAP_FOLD | Set _ -> C_SET_FOLD | List _ -> C_LIST_FOLD in - let fold = fun expr -> - e_let_in (env,None) false false (e_constant op_name [lambda; collect ; init_record]) @@ - expr - in - restore_mutable_variable fold free_vars env and compile_declaration_list declarations : declaration Location.wrap list result = let open Raw in diff --git a/src/passes/3-self_ast_imperative/helpers.ml b/src/passes/3-self_ast_imperative/helpers.ml index fc15f29e1..42ee86259 100644 --- a/src/passes/3-self_ast_imperative/helpers.ml +++ b/src/passes/3-self_ast_imperative/helpers.ml @@ -63,6 +63,17 @@ let rec fold_expression : 'a folder -> 'a -> expression -> 'a result = fun f ini let ab = (expr1,expr2) in let%bind res = bind_fold_pair self init' ab in ok res + | E_for {body; _} -> + let%bind res = self init' body in + ok res + | E_for_each {collection; body; _} -> + let%bind res = self init' collection in + let%bind res = self res body in + ok res + | E_while {condition; body} -> + let%bind res = self init' condition in + let%bind res = self res body in + ok res and fold_cases : 'a folder -> 'a -> matching_expr -> 'a result = fun f init m -> @@ -176,6 +187,18 @@ let rec map_expression : exp_mapper -> expression -> expression result = fun f e let%bind (expr1,expr2) = bind_map_pair self (expr1,expr2) in return @@ E_sequence {expr1;expr2} ) + | E_for {binder; start; final; increment; body} -> + let%bind body = self body in + return @@ E_for {binder; start; final; increment; body} + | E_for_each {binder; collection; collection_type; body} -> + let%bind collection = self collection in + let%bind body = self body in + return @@ E_for_each {binder; collection; collection_type; body} + | E_while {condition; body} -> + let%bind condition = self condition in + let%bind body = self body in + return @@ E_while {condition; body} + | E_literal _ | E_variable _ | E_skip as e' -> return e' and map_type_expression : ty_exp_mapper -> type_expression -> type_expression result = fun f te -> @@ -323,6 +346,17 @@ let rec fold_map_expression : 'a fold_mapper -> 'a -> expression -> ('a * expres let%bind (res,(expr1,expr2)) = bind_fold_map_pair self init' (expr1,expr2) in ok (res, return @@ E_sequence {expr1;expr2}) ) + | E_for {binder; start; final; increment; body} -> + let%bind (res, body) = self init' body in + ok (res, return @@ E_for {binder; start; final; increment; body}) + | E_for_each {binder; collection; collection_type; body} -> + let%bind res,collection = self init' collection in + let%bind res,body = self res body in + ok (res, return @@ E_for_each {binder; collection; collection_type; body}) + | E_while {condition; body} -> + let%bind res,condition = self init' condition in + let%bind res,body = self res body in + ok (res, return @@ E_while {condition; body}) | E_literal _ | E_variable _ | E_skip as e' -> ok (init', return e') and fold_map_cases : 'a fold_mapper -> 'a -> matching_expr -> ('a * matching_expr) result = fun f init m -> diff --git a/src/passes/4-imperative_to_sugar/dune b/src/passes/4-imperative_to_sugar/dune index 445998b90..66f996558 100644 --- a/src/passes/4-imperative_to_sugar/dune +++ b/src/passes/4-imperative_to_sugar/dune @@ -5,6 +5,7 @@ simple-utils ast_imperative ast_sugar + self_ast_sugar proto-alpha-utils ) (preprocess diff --git a/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml b/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml index 9cdfed78e..a461dc9ae 100644 --- a/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml +++ b/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml @@ -2,6 +2,105 @@ module I = Ast_imperative module O = Ast_sugar open Trace +module Errors = struct + let bad_collection expr = + let title () = "" in + let message () = Format.asprintf "\nCannot loop over this collection : %a\n" I.PP.expression expr in + let data = [ + ("location", + fun () -> Format.asprintf "%a" Location.pp expr.location) + ] in + error ~data title message +end + +let rec add_to_end (expression: O.expression) to_add = + match expression.expression_content with + | O.E_let_in lt -> + let lt = {lt with let_result = add_to_end lt.let_result to_add} in + {expression with expression_content = O.E_let_in lt} + | O.E_sequence seq -> + let seq = {seq with expr2 = add_to_end seq.expr2 to_add} in + {expression with expression_content = O.E_sequence seq} + | _ -> O.e_sequence expression to_add + +let repair_mutable_variable_in_matching (match_body : O.expression) (element_names : O.expression_variable list) (env : I.expression_variable) = + let%bind ((dv,fv),mb) = Self_ast_sugar.fold_map_expression + (* TODO : these should use Variables sets *) + (fun (decl_var,free_var : O.expression_variable list * O.expression_variable list) (ass_exp : O.expression) -> + match ass_exp.expression_content with + | E_let_in {let_binder;mut=false;rhs;let_result} -> + let (name,_) = let_binder in + ok (true,(name::decl_var, free_var),O.e_let_in let_binder false false rhs let_result) + | E_let_in {let_binder;mut=true; rhs;let_result} -> + let (name,_) = let_binder in + if List.mem name decl_var then + ok (true,(decl_var, free_var), O.e_let_in let_binder false false rhs let_result) + else( + let free_var = if (List.mem name free_var) then free_var else name::free_var in + let expr = O.e_let_in (env,None) false false (O.e_update (O.e_variable env) (Var.to_name name) (O.e_variable name)) let_result in + ok (true,(decl_var, free_var), O.e_let_in let_binder false false rhs expr) + ) + | E_constant {cons_name=C_MAP_FOLD;arguments= _} + | E_constant {cons_name=C_SET_FOLD;arguments= _} + | E_constant {cons_name=C_LIST_FOLD;arguments= _} + | E_matching _ -> ok @@ (false, (decl_var,free_var),ass_exp) + | _ -> ok (true, (decl_var, free_var),ass_exp) + ) + (element_names,[]) + match_body in + ok @@ ((dv,fv),mb) + +and repair_mutable_variable_in_loops (for_body : O.expression) (element_names : O.expression_variable list) (env : O.expression_variable) = + let%bind ((dv,fv),fb) = Self_ast_sugar.fold_map_expression + (* TODO : these should use Variables sets *) + (fun (decl_var,free_var : O.expression_variable list * O.expression_variable list) (ass_exp : O.expression) -> + (* Format.printf "debug: dv:%a; fv:%a; expr:%a \n%!" + (I.PP.list_sep_d I.PP.expression_variable) decl_var + (I.PP.list_sep_d I.PP.expression_variable) decl_var + O.PP.expression ass_exp + ;*) + match ass_exp.expression_content with + | E_let_in {let_binder;mut=false;} -> + let (name,_) = let_binder in + ok (true,(name::decl_var, free_var),ass_exp) + | E_let_in {let_binder;mut=true; rhs;let_result} -> + let (name,_) = let_binder in + if List.mem name decl_var then + ok (true,(decl_var, free_var), O.e_let_in let_binder false false rhs let_result) + else( + let free_var = if (List.mem name free_var) then free_var else name::free_var in + let expr = O.e_let_in (env,None) false false ( + O.e_update (O.e_variable env) ("0") + (O.e_update (O.e_accessor (O.e_variable env) "0") (Var.to_name name) (O.e_variable name)) + ) + let_result in + ok (true,(decl_var, free_var), O.e_let_in let_binder false false rhs expr) + ) + | E_constant {cons_name=C_MAP_FOLD;arguments= _} + | E_constant {cons_name=C_SET_FOLD;arguments= _} + | E_constant {cons_name=C_LIST_FOLD;arguments= _} + | E_matching _ -> ok @@ (false,(decl_var,free_var),ass_exp) + | _ -> ok (true,(decl_var, free_var),ass_exp) + ) + (element_names,[]) + for_body in + ok @@ ((dv,fv),fb) + +and store_mutable_variable (free_vars : I.expression_variable list) = + if (List.length free_vars == 0) then + O.e_unit () + else + let aux var = (Var.to_name var, O.e_variable var) in + O.e_record_ez (List.map aux free_vars) + +and restore_mutable_variable (expr : O.expression->O.expression_content) (free_vars : O.expression_variable list) (env : O.expression_variable) = + let aux (f: O.expression -> O.expression) (ev: O.expression_variable) = + fun expr -> f (O.e_let_in (ev,None) true false (O.e_accessor (O.e_variable env) (Var.to_name ev)) expr) + in + let ef = List.fold_left aux (fun e -> e) free_vars in + expr (ef (O.e_skip ())) + + let rec compile_type_expression : I.type_expression -> O.type_expression result = fun te -> let return te = ok @@ O.make_t te in @@ -79,19 +178,18 @@ let rec compile_expression : I.expression -> O.expression result = let%bind fun_type = compile_type_expression fun_type in let%bind lambda = compile_lambda lambda in return @@ O.E_recursive {fun_name;fun_type;lambda} - | I.E_let_in {let_binder;mut=_;inline;rhs;let_result} -> + | I.E_let_in {let_binder;mut;inline;rhs;let_result} -> let (binder,ty_opt) = let_binder in let%bind ty_opt = bind_map_option compile_type_expression ty_opt in let%bind rhs = compile_expression rhs in let%bind let_result = compile_expression let_result in - return @@ O.E_let_in {let_binder=(binder,ty_opt);inline;rhs;let_result} + return @@ O.E_let_in {let_binder=(binder,ty_opt);mut;inline;rhs;let_result} | I.E_constructor {constructor;element} -> let%bind element = compile_expression element in return @@ O.E_constructor {constructor;element} - | I.E_matching {matchee; cases} -> - let%bind matchee = compile_expression matchee in - let%bind cases = compile_matching cases in - return @@ O.E_matching {matchee;cases} + | I.E_matching m -> + let%bind m = compile_matching m in + return @@ m | I.E_record record -> let record = I.LMap.to_kv_list record in let%bind record = @@ -136,43 +234,237 @@ let rec compile_expression : I.expression -> O.expression result = | I.E_sequence {expr1; expr2} -> let%bind expr1 = compile_expression expr1 in let%bind expr2 = compile_expression expr2 in - return @@ O.E_sequence {expr1; expr2} + ok @@ add_to_end expr1 expr2 | I.E_skip -> return @@ O.E_skip + | I.E_for f -> + let%bind f = compile_for f in + return @@ f + | I.E_for_each fe -> + let%bind fe = compile_for_each fe in + return @@ fe + | I.E_while w -> + let%bind w = compile_while w in + return @@ w + and compile_lambda : I.lambda -> O.lambda result = fun {binder;input_type;output_type;result}-> let%bind input_type = bind_map_option compile_type_expression input_type in let%bind output_type = bind_map_option compile_type_expression output_type in let%bind result = compile_expression result in ok @@ O.{binder;input_type;output_type;result} -and compile_matching : I.matching_expr -> O.matching_expr result = - fun m -> - match m with + +and compile_matching : I.matching -> O.expression_content result = + fun {matchee;cases} -> + let%bind matchee = compile_expression matchee in + match cases with | I.Match_bool {match_true;match_false} -> - let%bind match_true = compile_expression match_true in - let%bind match_false = compile_expression match_false in - ok @@ O.Match_bool {match_true;match_false} - | I.Match_list {match_nil;match_cons} -> - let%bind match_nil = compile_expression match_nil in - let (hd,tl,expr,tv) = match_cons in - let%bind expr = compile_expression expr in - ok @@ O.Match_list {match_nil; match_cons=(hd,tl,expr,tv)} + let%bind match_true' = compile_expression match_true in + let%bind match_false' = compile_expression match_false in + let env = Var.fresh () in + let%bind ((_,free_vars_true), match_true) = repair_mutable_variable_in_matching match_true' [] env in + let%bind ((_,free_vars_false), match_false) = repair_mutable_variable_in_matching match_false' [] env in + let match_true = add_to_end match_true (O.e_variable env) in + let match_false = add_to_end match_false (O.e_variable env) in + + let free_vars = List.sort_uniq Var.compare @@ free_vars_true @ free_vars_false in + if (List.length free_vars != 0) then + let match_expr = O.e_matching matchee (O.Match_bool {match_true; match_false}) in + let return_expr = fun expr -> + O.E_let_in {let_binder=(env,None); mut=false; inline=false;rhs=(store_mutable_variable free_vars); + let_result=O.e_let_in (env,None) false false match_expr @@ + expr + } + in + ok @@ restore_mutable_variable return_expr free_vars env + else + ok @@ O.E_matching {matchee;cases=O.Match_bool {match_true=match_true';match_false=match_false'}} | I.Match_option {match_none;match_some} -> - let%bind match_none = compile_expression match_none in + let%bind match_none' = compile_expression match_none in let (n,expr,tv) = match_some in - let%bind expr = compile_expression expr in - ok @@ O.Match_option {match_none; match_some=(n,expr,tv)} + let%bind expr' = compile_expression expr in + let env = Var.fresh () in + let%bind ((_,free_vars_none), match_none) = repair_mutable_variable_in_matching match_none' [] env in + let%bind ((_,free_vars_some), expr) = repair_mutable_variable_in_matching expr' [n] env in + let match_none = add_to_end match_none (O.e_variable env) in + let expr = add_to_end expr (O.e_variable env) in + let free_vars = List.sort_uniq Var.compare @@ free_vars_none @ free_vars_some in + if (List.length free_vars != 0) then + let match_expr = O.e_matching matchee (O.Match_option {match_none; match_some=(n,expr,tv)}) in + let return_expr = fun expr -> + O.E_let_in {let_binder=(env,None); mut=false; inline=false;rhs=(store_mutable_variable free_vars); + let_result=O.e_let_in (env,None) false false match_expr @@ + expr + } + in + ok @@ restore_mutable_variable return_expr free_vars env + else + ok @@ O.E_matching {matchee; cases=O.Match_option {match_none=match_none'; match_some=(n,expr',tv)}} + | I.Match_list {match_nil;match_cons} -> + let%bind match_nil' = compile_expression match_nil in + let (hd,tl,expr,tv) = match_cons in + let%bind expr' = compile_expression expr in + let env = Var.fresh () in + let%bind ((_,free_vars_nil), match_nil) = repair_mutable_variable_in_matching match_nil' [] env in + let%bind ((_,free_vars_cons), expr) = repair_mutable_variable_in_matching expr' [hd;tl] env in + let match_nil = add_to_end match_nil (O.e_variable env) in + let expr = add_to_end expr (O.e_variable env) in + let free_vars = List.sort_uniq Var.compare @@ free_vars_nil @ free_vars_cons in + if (List.length free_vars != 0) then + let match_expr = O.e_matching matchee (O.Match_list {match_nil; match_cons=(hd,tl,expr,tv)}) in + let return_expr = fun expr -> + O.E_let_in {let_binder=(env,None); mut=false; inline=false;rhs=(store_mutable_variable free_vars); + let_result=O.e_let_in (env,None) false false match_expr @@ + expr + } + in + ok @@ restore_mutable_variable return_expr free_vars env + else + ok @@ O.E_matching {matchee;cases=O.Match_list {match_nil=match_nil'; match_cons=(hd,tl,expr',tv)}} | I.Match_tuple ((lst,expr), tv) -> let%bind expr = compile_expression expr in - ok @@ O.Match_tuple ((lst,expr), tv) + ok @@ O.E_matching {matchee; cases=O.Match_tuple ((lst,expr), tv)} | I.Match_variant (lst,tv) -> - let%bind lst = bind_map_list ( - fun ((c,n),expr) -> - let%bind expr = compile_expression expr in - ok @@ ((c,n),expr) - ) lst - in - ok @@ O.Match_variant (lst,tv) + let env = Var.fresh () in + let aux fv ((c,n),expr) = + let%bind expr = compile_expression expr in + let%bind ((_,free_vars), case_clause) = repair_mutable_variable_in_matching expr [n] env in + let case_clause'= expr in + let case_clause = add_to_end case_clause (O.e_variable env) in + ok (free_vars::fv,((c,n), case_clause, case_clause')) in + let%bind (fv,cases) = bind_fold_map_list aux [] lst in + let free_vars = List.sort_uniq Var.compare @@ List.concat fv in + if (List.length free_vars == 0) then ( + let cases = List.map (fun case -> let (a,_,b) = case in (a,b)) cases in + ok @@ O.E_matching{matchee; cases=O.Match_variant (cases,tv)} + ) else ( + let cases = List.map (fun case -> let (a,b,_) = case in (a,b)) cases in + let match_expr = O.e_matching matchee @@ O.Match_variant (cases,tv) in + let return_expr = fun expr -> + O.E_let_in {let_binder=(env,None); mut=false; inline=false; rhs=(store_mutable_variable free_vars); + let_result=O.e_let_in (env,None) false false match_expr @@ + expr + } + in + ok @@ restore_mutable_variable return_expr free_vars env + ) +and compile_while I.{condition;body} = + let env_rec = Var.fresh () in + let binder = Var.fresh () in + + let%bind cond = compile_expression condition in + let ctrl = + (O.e_variable binder) + in + + let%bind for_body = compile_expression body in + let%bind ((_,captured_name_list),for_body) = repair_mutable_variable_in_loops for_body [] binder in + let for_body = add_to_end for_body ctrl in + + let aux name expr= + O.e_let_in (name,None) false false (O.e_accessor (O.e_accessor (O.e_variable binder) "0") (Var.to_name name)) expr + in + let init_rec = O.e_tuple [store_mutable_variable @@ captured_name_list] in + let restore = fun expr -> List.fold_right aux captured_name_list expr in + let continue_expr = O.e_constant C_FOLD_CONTINUE [for_body] in + let stop_expr = O.e_constant C_FOLD_STOP [O.e_variable binder] in + let aux_func = + O.e_lambda binder None None @@ + restore @@ + O.e_cond cond continue_expr stop_expr in + let loop = O.e_constant C_FOLD_WHILE [aux_func; O.e_variable env_rec] in + let let_binder = (env_rec,None) in + let return_expr = fun expr -> + O.E_let_in {let_binder; mut=false; inline=false; rhs=init_rec; let_result= + O.e_let_in let_binder false false loop @@ + O.e_let_in let_binder false false (O.e_accessor (O.e_variable env_rec) "0") @@ + expr + } + in + ok @@ restore_mutable_variable return_expr captured_name_list env_rec + + +and compile_for I.{binder;start;final;increment;body} = + let env_rec = Var.fresh () in + (*Make the cond and the step *) + let cond = I.e_annotation (I.e_constant C_LE [I.e_variable binder ; final]) I.t_bool in + let%bind cond = compile_expression cond in + let%bind step = compile_expression increment in + let continue_expr = O.e_constant C_FOLD_CONTINUE [(O.e_variable env_rec)] in + let ctrl = + O.e_let_in (binder,Some O.t_int) false false (O.e_constant C_ADD [ O.e_variable binder ; step ]) @@ + O.e_let_in (env_rec, None) false false (O.e_update (O.e_variable env_rec) "1" @@ O.e_variable binder)@@ + continue_expr + in + (* Modify the body loop*) + let%bind body = compile_expression body in + let%bind ((_,captured_name_list),for_body) = repair_mutable_variable_in_loops body [binder] env_rec in + let for_body = add_to_end for_body ctrl in + + let aux name expr= + O.e_let_in (name,None) false false (O.e_accessor (O.e_accessor (O.e_variable env_rec) "0") (Var.to_name name)) expr + in + + (* restores the initial value of the free_var*) + let restore = fun expr -> List.fold_right aux captured_name_list expr in + + (*Prep the lambda for the fold*) + let stop_expr = O.e_constant C_FOLD_STOP [O.e_variable env_rec] in + let aux_func = O.e_lambda env_rec None None @@ + O.e_let_in (binder,Some O.t_int) false false (O.e_accessor (O.e_variable env_rec) "1") @@ + O.e_cond cond (restore for_body) (stop_expr) in + + (* Make the fold_while en precharge the vakye *) + let loop = O.e_constant C_FOLD_WHILE [aux_func; O.e_variable env_rec] in + let init_rec = O.e_pair (store_mutable_variable captured_name_list) @@ O.e_variable binder in + + let%bind start = compile_expression start in + let let_binder = (env_rec,None) in + let return_expr = fun expr -> + O.E_let_in {let_binder=(binder, Some O.t_int);mut=false; inline=false;rhs=start;let_result= + O.e_let_in let_binder false false init_rec @@ + O.e_let_in let_binder false false loop @@ + O.e_let_in let_binder false false (O.e_accessor (O.e_variable env_rec) "0") @@ + expr + } + in + ok @@ restore_mutable_variable return_expr captured_name_list env_rec + +and compile_for_each I.{binder;collection;collection_type; body} = + let args = Var.fresh () in + let%bind element_names = ok @@ match snd binder with + | Some v -> [fst binder;v] + | None -> [fst binder] + in + + let env = Var.fresh () in + let%bind body = compile_expression body in + let%bind ((_,free_vars), body) = repair_mutable_variable_in_loops body element_names args in + let for_body = add_to_end body @@ (O.e_accessor (O.e_variable args) "0") in + + let init_record = store_mutable_variable free_vars in + let%bind collect = compile_expression collection in + let aux name expr= + O.e_let_in (name,None) false false (O.e_accessor (O.e_accessor (O.e_variable args) "0") (Var.to_name name)) expr + in + let restore = fun expr -> List.fold_right aux free_vars expr in + let restore = match collection_type with + | Map -> (match snd binder with + | Some v -> fun expr -> restore (O.e_let_in (fst binder, None) false false (O.e_accessor (O.e_accessor (O.e_variable args) "1") "0") + (O.e_let_in (v, None) false false (O.e_accessor (O.e_accessor (O.e_variable args) "1") "1") expr)) + | None -> fun expr -> restore (O.e_let_in (fst binder, None) false false (O.e_accessor (O.e_accessor (O.e_variable args) "1") "0") expr) + ) + | _ -> fun expr -> restore (O.e_let_in (fst binder, None) false false (O.e_accessor (O.e_variable args) "1") expr) + in + let lambda = O.e_lambda args None None (restore for_body) in + let%bind op_name = match collection_type with + | Map -> ok @@ O.C_MAP_FOLD | Set -> ok @@ O.C_SET_FOLD | List -> ok @@ O.C_LIST_FOLD + in + let fold = fun expr -> + O.E_let_in {let_binder=(env,None);mut=false; inline=false;rhs=(O.e_constant op_name [lambda; collect ; init_record]); + let_result=expr;} + in + ok @@ restore_mutable_variable fold free_vars env let compile_declaration : I.declaration Location.wrap -> _ = fun {wrap_content=declaration;location} -> let return decl = ok @@ Location.wrap ~loc:location decl in diff --git a/src/passes/5-self_ast_sugar/helpers.ml b/src/passes/5-self_ast_sugar/helpers.ml new file mode 100644 index 000000000..71597ce5e --- /dev/null +++ b/src/passes/5-self_ast_sugar/helpers.ml @@ -0,0 +1,356 @@ +open Ast_sugar +open Trace +open Stage_common.Helpers + +type 'a folder = 'a -> expression -> 'a result +let rec fold_expression : 'a folder -> 'a -> expression -> 'a result = fun f init e -> + let self = fold_expression f in + let%bind init' = f init e in + match e.expression_content with + | E_literal _ | E_variable _ | E_skip -> ok init' + | E_list lst | E_set lst | E_constant {arguments=lst} -> ( + let%bind res = bind_fold_list self init' lst in + ok res + ) + | E_map lst | E_big_map lst -> ( + let%bind res = bind_fold_list (bind_fold_pair self) init' lst in + ok res + ) + | E_look_up ab -> + let%bind res = bind_fold_pair self init' ab in + ok res + | E_application {lamb;args} -> ( + let ab = (lamb,args) in + let%bind res = bind_fold_pair self init' ab in + ok res + ) + | E_lambda { binder = _ ; input_type = _ ; output_type = _ ; result = e } + | E_ascription {anno_expr=e; _} | E_constructor {element=e} -> ( + let%bind res = self init' e in + ok res + ) + | E_matching {matchee=e; cases} -> ( + let%bind res = self init' e in + let%bind res = fold_cases f res cases in + ok res + ) + | E_record m -> ( + let aux init'' _ expr = + let%bind res = fold_expression self init'' expr in + ok res + in + let%bind res = bind_fold_lmap aux (ok init') m in + ok res + ) + | E_record_update {record;update} -> ( + let%bind res = self init' record in + let%bind res = fold_expression self res update in + ok res + ) + | E_record_accessor {expr} -> ( + let%bind res = self init' expr in + ok res + ) + | E_let_in { let_binder = _ ; rhs ; let_result } -> ( + let%bind res = self init' rhs in + let%bind res = self res let_result in + ok res + ) + | E_recursive { lambda={result=e;_}; _} -> + let%bind res = self init' e in + ok res + | E_sequence {expr1;expr2} -> + let ab = (expr1,expr2) in + let%bind res = bind_fold_pair self init' ab in + ok res + + +and fold_cases : 'a folder -> 'a -> matching_expr -> 'a result = fun f init m -> + match m with + | Match_bool { match_true ; match_false } -> ( + let%bind res = fold_expression f init match_true in + let%bind res = fold_expression f res match_false in + ok res + ) + | Match_list { match_nil ; match_cons = (_ , _ , cons, _) } -> ( + let%bind res = fold_expression f init match_nil in + let%bind res = fold_expression f res cons in + ok res + ) + | Match_option { match_none ; match_some = (_ , some, _) } -> ( + let%bind res = fold_expression f init match_none in + let%bind res = fold_expression f res some in + ok res + ) + | Match_tuple ((_ , e), _) -> ( + let%bind res = fold_expression f init e in + ok res + ) + | Match_variant (lst, _) -> ( + let aux init' ((_ , _) , e) = + let%bind res' = fold_expression f init' e in + ok res' in + let%bind res = bind_fold_list aux init lst in + ok res + ) + +type exp_mapper = expression -> expression result +type ty_exp_mapper = type_expression -> type_expression result +type abs_mapper = + | Expression of exp_mapper + | Type_expression of ty_exp_mapper +let rec map_expression : exp_mapper -> expression -> expression result = fun f e -> + let self = map_expression f in + let%bind e' = f e in + let return expression_content = ok { e' with expression_content } in + match e'.expression_content with + | E_list lst -> ( + let%bind lst' = bind_map_list self lst in + return @@ E_list lst' + ) + | E_set lst -> ( + let%bind lst' = bind_map_list self lst in + return @@ E_set lst' + ) + | E_map lst -> ( + let%bind lst' = bind_map_list (bind_map_pair self) lst in + return @@ E_map lst' + ) + | E_big_map lst -> ( + let%bind lst' = bind_map_list (bind_map_pair self) lst in + return @@ E_big_map lst' + ) + | E_look_up ab -> ( + let%bind ab' = bind_map_pair self ab in + return @@ E_look_up ab' + ) + | E_ascription ascr -> ( + let%bind e' = self ascr.anno_expr in + return @@ E_ascription {ascr with anno_expr=e'} + ) + | E_matching {matchee=e;cases} -> ( + let%bind e' = self e in + let%bind cases' = map_cases f cases in + return @@ E_matching {matchee=e';cases=cases'} + ) + | E_record_accessor acc -> ( + let%bind e' = self acc.expr in + return @@ E_record_accessor {acc with expr = e'} + ) + | E_record m -> ( + let%bind m' = bind_map_lmap self m in + return @@ E_record m' + ) + | E_record_update {record; path; update} -> ( + let%bind record = self record in + let%bind update = self update in + return @@ E_record_update {record;path;update} + ) + | E_constructor c -> ( + let%bind e' = self c.element in + return @@ E_constructor {c with element = e'} + ) + | E_application {lamb;args} -> ( + let ab = (lamb,args) in + let%bind (lamb,args) = bind_map_pair self ab in + return @@ E_application {lamb;args} + ) + | E_let_in { let_binder ; mut; rhs ; let_result; inline } -> ( + let%bind rhs = self rhs in + let%bind let_result = self let_result in + return @@ E_let_in { let_binder ; mut; rhs ; let_result; inline } + ) + | E_lambda { binder ; input_type ; output_type ; result } -> ( + let%bind result = self result in + return @@ E_lambda { binder ; input_type ; output_type ; result } + ) + | E_recursive { fun_name; fun_type; lambda} -> + let%bind result = self lambda.result in + let lambda = {lambda with result} in + return @@ E_recursive { fun_name; fun_type; lambda} + | E_constant c -> ( + let%bind args = bind_map_list self c.arguments in + return @@ E_constant {c with arguments=args} + ) + | E_sequence {expr1;expr2} -> ( + let%bind (expr1,expr2) = bind_map_pair self (expr1,expr2) in + return @@ E_sequence {expr1;expr2} + ) + | E_literal _ | E_variable _ | E_skip as e' -> return e' + +and map_type_expression : ty_exp_mapper -> type_expression -> type_expression result = fun f te -> + let self = map_type_expression f in + let%bind te' = f te in + let return type_content = ok { te' with type_content } in + match te'.type_content with + | T_sum temap -> + let%bind temap' = bind_map_cmap self temap in + return @@ (T_sum temap') + | T_record temap -> + let%bind temap' = bind_map_lmap self temap in + return @@ (T_record temap') + | T_arrow {type1 ; type2} -> + let%bind type1' = self type1 in + let%bind type2' = self type2 in + return @@ (T_arrow {type1=type1' ; type2=type2'}) + | T_operator _ + | T_variable _ | T_constant _ -> ok te' + +and map_cases : exp_mapper -> matching_expr -> matching_expr result = fun f m -> + match m with + | Match_bool { match_true ; match_false } -> ( + let%bind match_true = map_expression f match_true in + let%bind match_false = map_expression f match_false in + ok @@ Match_bool { match_true ; match_false } + ) + | Match_list { match_nil ; match_cons = (hd , tl , cons, _) } -> ( + let%bind match_nil = map_expression f match_nil in + let%bind cons = map_expression f cons in + ok @@ Match_list { match_nil ; match_cons = (hd , tl , cons, ()) } + ) + | Match_option { match_none ; match_some = (name , some, _) } -> ( + let%bind match_none = map_expression f match_none in + let%bind some = map_expression f some in + ok @@ Match_option { match_none ; match_some = (name , some, ()) } + ) + | Match_tuple ((names , e), _) -> ( + let%bind e' = map_expression f e in + ok @@ Match_tuple ((names , e'), []) + ) + | Match_variant (lst, _) -> ( + let aux ((a , b) , e) = + let%bind e' = map_expression f e in + ok ((a , b) , e') + in + let%bind lst' = bind_map_list aux lst in + ok @@ Match_variant (lst', ()) + ) + +and map_program : abs_mapper -> program -> program result = fun m p -> + let aux = fun (x : declaration) -> + match x,m with + | (Declaration_constant (t , o , i, e), Expression m') -> ( + let%bind e' = map_expression m' e in + ok (Declaration_constant (t , o , i, e')) + ) + | (Declaration_type (tv,te), Type_expression m') -> ( + let%bind te' = map_type_expression m' te in + ok (Declaration_type (tv, te')) + ) + | decl,_ -> ok decl + (* | Declaration_type of (type_variable * type_expression) *) + in + bind_map_list (bind_map_location aux) p + +type 'a fold_mapper = 'a -> expression -> (bool * 'a * expression) result +let rec fold_map_expression : 'a fold_mapper -> 'a -> expression -> ('a * expression) result = fun f a e -> + let self = fold_map_expression f in + let%bind (continue, init',e') = f a e in + if (not continue) then ok(init',e') + else + let return expression_content = { e' with expression_content } in + match e'.expression_content with + | E_list lst -> ( + let%bind (res, lst') = bind_fold_map_list self init' lst in + ok (res, return @@ E_list lst') + ) + | E_set lst -> ( + let%bind (res, lst') = bind_fold_map_list self init' lst in + ok (res, return @@ E_set lst') + ) + | E_map lst -> ( + let%bind (res, lst') = bind_fold_map_list (bind_fold_map_pair self) init' lst in + ok (res, return @@ E_map lst') + ) + | E_big_map lst -> ( + let%bind (res, lst') = bind_fold_map_list (bind_fold_map_pair self) init' lst in + ok (res, return @@ E_big_map lst') + ) + | E_look_up ab -> ( + let%bind (res, ab') = bind_fold_map_pair self init' ab in + ok (res, return @@ E_look_up ab') + ) + | E_ascription ascr -> ( + let%bind (res,e') = self init' ascr.anno_expr in + ok (res, return @@ E_ascription {ascr with anno_expr=e'}) + ) + | E_matching {matchee=e;cases} -> ( + let%bind (res, e') = self init' e in + let%bind (res,cases') = fold_map_cases f res cases in + ok (res, return @@ E_matching {matchee=e';cases=cases'}) + ) + | E_record_accessor acc -> ( + let%bind (res, e') = self init' acc.expr in + ok (res, return @@ E_record_accessor {acc with expr = e'}) + ) + | E_record m -> ( + let%bind (res, lst') = bind_fold_map_list (fun res (k,e) -> let%bind (res,e) = self res e in ok (res,(k,e))) init' (LMap.to_kv_list m) in + let m' = LMap.of_list lst' in + ok (res, return @@ E_record m') + ) + | E_record_update {record; path; update} -> ( + let%bind (res, record) = self init' record in + let%bind (res, update) = self res update in + ok (res, return @@ E_record_update {record;path;update}) + ) + | E_constructor c -> ( + let%bind (res,e') = self init' c.element in + ok (res, return @@ E_constructor {c with element = e'}) + ) + | E_application {lamb;args} -> ( + let ab = (lamb,args) in + let%bind (res,(a,b)) = bind_fold_map_pair self init' ab in + ok (res, return @@ E_application {lamb=a;args=b}) + ) + | E_let_in { let_binder ; mut; rhs ; let_result; inline } -> ( + let%bind (res,rhs) = self init' rhs in + let%bind (res,let_result) = self res let_result in + ok (res, return @@ E_let_in { let_binder ; mut; rhs ; let_result ; inline }) + ) + | E_lambda { binder ; input_type ; output_type ; result } -> ( + let%bind (res,result) = self init' result in + ok ( res, return @@ E_lambda { binder ; input_type ; output_type ; result }) + ) + | E_recursive { fun_name; fun_type; lambda} -> + let%bind (res, result) = self init' lambda.result in + let lambda = {lambda with result} in + ok ( res, return @@ E_recursive { fun_name; fun_type; lambda}) + | E_constant c -> ( + let%bind (res,args) = bind_fold_map_list self init' c.arguments in + ok (res, return @@ E_constant {c with arguments=args}) + ) + | E_sequence {expr1;expr2} -> ( + let%bind (res,(expr1,expr2)) = bind_fold_map_pair self init' (expr1,expr2) in + ok (res, return @@ E_sequence {expr1;expr2}) + ) + | E_literal _ | E_variable _ | E_skip as e' -> ok (init', return e') + +and fold_map_cases : 'a fold_mapper -> 'a -> matching_expr -> ('a * matching_expr) result = fun f init m -> + match m with + | Match_bool { match_true ; match_false } -> ( + let%bind (init, match_true) = fold_map_expression f init match_true in + let%bind (init, match_false) = fold_map_expression f init match_false in + ok @@ (init, Match_bool { match_true ; match_false }) + ) + | Match_list { match_nil ; match_cons = (hd , tl , cons, _) } -> ( + let%bind (init, match_nil) = fold_map_expression f init match_nil in + let%bind (init, cons) = fold_map_expression f init cons in + ok @@ (init, Match_list { match_nil ; match_cons = (hd , tl , cons, ()) }) + ) + | Match_option { match_none ; match_some = (name , some, _) } -> ( + let%bind (init, match_none) = fold_map_expression f init match_none in + let%bind (init, some) = fold_map_expression f init some in + ok @@ (init, Match_option { match_none ; match_some = (name , some, ()) }) + ) + | Match_tuple ((names , e), _) -> ( + let%bind (init, e') = fold_map_expression f init e in + ok @@ (init, Match_tuple ((names , e'), [])) + ) + | Match_variant (lst, _) -> ( + let aux init ((a , b) , e) = + let%bind (init,e') = fold_map_expression f init e in + ok (init, ((a , b) , e')) + in + let%bind (init,lst') = bind_fold_map_list aux init lst in + ok @@ (init, Match_variant (lst', ())) + ) diff --git a/src/passes/5-self_ast_sugar/self_ast_sugar.ml b/src/passes/5-self_ast_sugar/self_ast_sugar.ml new file mode 100644 index 000000000..5bac36236 --- /dev/null +++ b/src/passes/5-self_ast_sugar/self_ast_sugar.ml @@ -0,0 +1,25 @@ +open Trace + +let all_expression_mapper = [ +] + +let all_type_expression_mapper = [ +] + +let all_exp = List.map (fun el -> Helpers.Expression el) all_expression_mapper +let all_ty = List.map (fun el -> Helpers.Type_expression el) all_type_expression_mapper + +let all_program = + let all_p = List.map Helpers.map_program all_exp in + let all_p2 = List.map Helpers.map_program all_ty in + bind_chain (List.append all_p all_p2) + +let all_expression = + let all_p = List.map Helpers.map_expression all_expression_mapper in + bind_chain all_p + +let map_expression = Helpers.map_expression + +let fold_expression = Helpers.fold_expression + +let fold_map_expression = Helpers.fold_map_expression diff --git a/src/passes/6-sugar_to_core/sugar_to_core.ml b/src/passes/6-sugar_to_core/sugar_to_core.ml index 261bb1e81..48c43f6d7 100644 --- a/src/passes/6-sugar_to_core/sugar_to_core.ml +++ b/src/passes/6-sugar_to_core/sugar_to_core.ml @@ -277,7 +277,7 @@ let rec uncompile_expression : O.expression -> I.expression result = let%bind ty_opt = bind_map_option uncompile_type_expression ty_opt in let%bind rhs = uncompile_expression rhs in let%bind let_result = uncompile_expression let_result in - return @@ I.E_let_in {let_binder=(binder,ty_opt);inline;rhs;let_result} + return @@ I.E_let_in {let_binder=(binder,ty_opt);mut=false;inline;rhs;let_result} | O.E_constructor {constructor;element} -> let%bind element = uncompile_expression element in return @@ I.E_constructor {constructor;element} diff --git a/src/stages/1-ast_imperative/PP.ml b/src/stages/1-ast_imperative/PP.ml index ebb4bd83e..d25b84dee 100644 --- a/src/stages/1-ast_imperative/PP.ml +++ b/src/stages/1-ast_imperative/PP.ml @@ -66,6 +66,27 @@ and expression_content ppf (ec : expression_content) = fprintf ppf "%a;\n%a" expression expr1 expression expr2 | E_skip -> fprintf ppf "skip" + | E_for {binder; start; final; increment; body} -> + fprintf ppf "for %a from %a to %a by %a do %a" + expression_variable binder + expression start + expression final + expression increment + expression body + | E_for_each {binder; collection; body; _} -> + fprintf ppf "for each %a in %a do %a" + option_map binder + expression collection + expression body + | E_while {condition; body} -> + fprintf ppf "while %a do %a" + expression condition + expression body + +and option_map ppf (k,v_opt) = + match v_opt with + | None -> fprintf ppf "%a" expression_variable k + | Some v -> fprintf ppf "%a -> %a" expression_variable k expression_variable v and option_type_name ppf ((n, ty_opt) : expression_variable * type_expression option) = diff --git a/src/stages/1-ast_imperative/combinators.ml b/src/stages/1-ast_imperative/combinators.ml index 92a1dbe73..47ec55fb6 100644 --- a/src/stages/1-ast_imperative/combinators.ml +++ b/src/stages/1-ast_imperative/combinators.ml @@ -130,6 +130,11 @@ let e_binop ?loc name a b = make_expr ?loc @@ E_constant {cons_name = name ; ar let e_constant ?loc name lst = make_expr ?loc @@ E_constant {cons_name=name ; arguments = lst} let e_look_up ?loc x y = make_expr ?loc @@ E_look_up (x , y) let e_sequence ?loc expr1 expr2 = make_expr ?loc @@ E_sequence {expr1; expr2} + +let e_while ?loc condition body = make_expr ?loc @@ E_while {condition; body} +let e_for ?loc binder start final increment body = make_expr ?loc @@ E_for {binder;start;final;increment;body} +let e_for_each ?loc binder collection collection_type body = make_expr ?loc @@ E_for_each {binder;collection;collection_type;body} + let e_cond ?loc expr match_true match_false = e_matching expr ?loc (Match_bool {match_true; match_false}) (* let e_assign ?loc a b c = location_wrap ?loc @@ E_assign (Var.of_name a , b , c) (* TODO handlethat*) diff --git a/src/stages/1-ast_imperative/combinators.mli b/src/stages/1-ast_imperative/combinators.mli index ca2f2d552..4d5c4db7a 100644 --- a/src/stages/1-ast_imperative/combinators.mli +++ b/src/stages/1-ast_imperative/combinators.mli @@ -115,6 +115,9 @@ val e_assign_with_let : ?loc:Location.t -> string -> string list -> expression - (* val get_e_accessor : expression' -> ( expression * access_path ) result *) +val e_while : ?loc:Location.t -> expression -> expression -> expression +val e_for : ?loc:Location.t -> expression_variable -> expression -> expression -> expression -> expression -> expression +val e_for_each : ?loc:Location.t -> expression_variable * expression_variable option -> expression -> collect_type -> expression -> expression val assert_e_accessor : expression_content -> unit result diff --git a/src/stages/1-ast_imperative/misc.ml b/src/stages/1-ast_imperative/misc.ml index 324529525..54e0303cf 100644 --- a/src/stages/1-ast_imperative/misc.ml +++ b/src/stages/1-ast_imperative/misc.ml @@ -184,7 +184,9 @@ let rec assert_value_eq (a, b: (expression * expression )) : unit result = | (E_application _, _) | (E_let_in _, _) | (E_recursive _,_) | (E_record_accessor _, _) | (E_look_up _, _) | (E_matching _, _) - | (E_sequence _, _) | (E_skip, _) -> simple_fail "comparing not a value" + | (E_sequence _, _) | (E_skip, _) + | (E_for _, _) | (E_for_each _, _) + | (E_while _, _) -> simple_fail "comparing not a value" let is_value_eq (a , b) = to_bool @@ assert_value_eq (a , b) diff --git a/src/stages/1-ast_imperative/types.ml b/src/stages/1-ast_imperative/types.ml index 4d0d0bd68..08200dbd7 100644 --- a/src/stages/1-ast_imperative/types.ml +++ b/src/stages/1-ast_imperative/types.ml @@ -54,6 +54,10 @@ and expression_content = | E_list of expression list | E_set of expression list | E_look_up of (expression * expression) + (* Imperative *) + | E_for of for_ + | E_for_each of for_each + | E_while of while_loop and constant = { cons_name: constant' (* this is at the end because it is huge *) @@ -101,6 +105,31 @@ and sequence = { expr2: expression ; } +and for_ = { + binder : expression_variable; + start : expression; + final : expression; + increment : expression; + body : expression; +} + +and for_each = { + binder : expression_variable * expression_variable option; + collection : expression; + collection_type : collect_type; + body : expression; +} + +and collect_type = + | Map + | Set + | List + +and while_loop = { + condition : expression; + body : expression; +} + and environment_element_definition = | ED_binder | ED_declaration of (expression * free_variables) diff --git a/src/stages/2-ast_sugar/PP.ml b/src/stages/2-ast_sugar/PP.ml index d4a4ead08..1b65a8046 100644 --- a/src/stages/2-ast_sugar/PP.ml +++ b/src/stages/2-ast_sugar/PP.ml @@ -56,10 +56,15 @@ and expression_content ppf (ec : expression_content) = | E_matching {matchee; cases; _} -> fprintf ppf "match %a with %a" expression matchee (matching expression) cases - | E_let_in { let_binder ; rhs ; let_result; inline } -> - fprintf ppf "let %a = %a%a in %a" option_type_name let_binder expression rhs option_inline inline expression let_result + | E_let_in { let_binder ; rhs ; let_result; inline; mut} -> + fprintf ppf "let %a%a = %a%a in %a" + option_type_name let_binder + option_mut mut + expression rhs + option_inline inline + expression let_result | E_sequence {expr1;expr2} -> - fprintf ppf "%a;\n%a" expression expr1 expression expr2 + fprintf ppf "{ %a; %a }" expression expr1 expression expr2 | E_ascription {anno_expr; type_annotation} -> fprintf ppf "%a : %a" expression anno_expr type_expression type_annotation | E_skip -> diff --git a/src/stages/2-ast_sugar/combinators.ml b/src/stages/2-ast_sugar/combinators.ml index 759d6fc55..f53344c37 100644 --- a/src/stages/2-ast_sugar/combinators.ml +++ b/src/stages/2-ast_sugar/combinators.ml @@ -122,8 +122,8 @@ let e_accessor ?loc a b = make_expr ?loc @@ E_record_accessor {expr = a; label= let e_accessor_list ?loc a b = List.fold_left (fun a b -> e_accessor ?loc a b) a b let e_variable ?loc v = make_expr ?loc @@ E_variable v let e_skip ?loc () = make_expr ?loc @@ E_skip -let e_let_in ?loc (binder, ascr) inline rhs let_result = - make_expr ?loc @@ E_let_in { let_binder = (binder, ascr) ; rhs ; let_result; inline } +let e_let_in ?loc (binder, ascr) mut inline rhs let_result = + make_expr ?loc @@ E_let_in { let_binder = (binder, ascr) ; rhs ; let_result; inline; mut } let e_annotation ?loc anno_expr ty = make_expr ?loc @@ E_ascription {anno_expr; type_annotation = ty} let e_application ?loc a b = make_expr ?loc @@ E_application {lamb=a ; args=b} let e_binop ?loc name a b = make_expr ?loc @@ E_constant {cons_name = name ; arguments = [a ; b]} diff --git a/src/stages/2-ast_sugar/combinators.mli b/src/stages/2-ast_sugar/combinators.mli index e9d3dd144..ca2f2d552 100644 --- a/src/stages/2-ast_sugar/combinators.mli +++ b/src/stages/2-ast_sugar/combinators.mli @@ -86,7 +86,7 @@ val e_variable : ?loc:Location.t -> expression_variable -> expression val e_skip : ?loc:Location.t -> unit -> expression val e_sequence : ?loc:Location.t -> expression -> expression -> expression val e_cond: ?loc:Location.t -> expression -> expression -> expression -> expression -val e_let_in : ?loc:Location.t -> ( expression_variable * type_expression option ) -> bool -> expression -> expression -> expression +val e_let_in : ?loc:Location.t -> ( expression_variable * type_expression option ) -> bool -> bool -> expression -> expression -> expression val e_annotation : ?loc:Location.t -> expression -> type_expression -> expression val e_application : ?loc:Location.t -> expression -> expression -> expression val e_binop : ?loc:Location.t -> constant' -> expression -> expression -> expression diff --git a/src/stages/2-ast_sugar/types.ml b/src/stages/2-ast_sugar/types.ml index 81091dea7..658d30e35 100644 --- a/src/stages/2-ast_sugar/types.ml +++ b/src/stages/2-ast_sugar/types.ml @@ -76,11 +76,13 @@ and recursive = { lambda : lambda; } -and let_in = - { let_binder: expression_variable * type_expression option - ; rhs: expression - ; let_result: expression - ; inline: bool } +and let_in = { + let_binder: expression_variable * type_expression option ; + rhs: expression ; + let_result: expression ; + inline: bool ; + mut: bool; + } and constructor = {constructor: constructor'; element: expression} diff --git a/src/test/contracts/loop.ligo b/src/test/contracts/loop.ligo index 7866a5cdb..03c1671ba 100644 --- a/src/test/contracts/loop.ligo +++ b/src/test/contracts/loop.ligo @@ -54,9 +54,9 @@ function for_collection_if_and_local_var (var nee : unit) : int is block { var acc : int := 0; const theone : int = 1; + const thetwo : int = 2; var myset : set (int) := set [1; 2; 3]; for x in set myset block { - const thetwo : int = 2; if x = theone then acc := acc + x else if x = thetwo then acc := acc + thetwo else acc := acc + 10 From 21e8298a4eae0d7cc0505e8962e307dc26112f80 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Wulfman Date: Mon, 23 Mar 2020 16:00:50 +0100 Subject: [PATCH 03/13] add assign back --- src/passes/10-interpreter/interpreter.ml | 4 +- src/passes/10-transpiler/transpiler.ml | 9 +- .../2-concrete_to_imperative/cameligo.ml | 9 +- .../2-concrete_to_imperative/pascaligo.ml | 40 ++++----- src/passes/3-self_ast_imperative/helpers.ml | 31 ++++--- .../imperative_to_sugar.ml | 88 +++++++++++++------ src/passes/5-self_ast_sugar/helpers.ml | 12 +-- src/passes/6-sugar_to_core/sugar_to_core.ml | 12 +-- src/passes/8-typer-new/typer.ml | 10 +-- src/passes/8-typer-old/typer.ml | 10 +-- src/passes/9-self_ast_typed/helpers.ml | 12 +-- src/passes/9-self_ast_typed/tail_recursion.ml | 4 +- src/stages/1-ast_imperative/PP.ml | 21 +++-- src/stages/1-ast_imperative/combinators.ml | 28 +++--- src/stages/1-ast_imperative/combinators.mli | 5 +- src/stages/1-ast_imperative/misc.ml | 1 + src/stages/1-ast_imperative/types.ml | 15 +++- src/stages/2-ast_sugar/PP.ml | 4 +- src/stages/2-ast_sugar/combinators.ml | 53 ++++------- src/stages/2-ast_sugar/combinators.mli | 57 ++++++------ src/stages/2-ast_sugar/types.ml | 2 +- src/stages/3-ast_core/PP.ml | 2 +- src/stages/3-ast_core/combinators.ml | 4 +- src/stages/3-ast_core/types.ml | 2 +- src/stages/4-ast_typed/PP.ml | 2 +- src/stages/4-ast_typed/combinators.ml | 2 +- src/stages/4-ast_typed/misc.ml | 2 +- src/stages/4-ast_typed/misc_smart.ml | 2 +- src/stages/4-ast_typed/types.ml | 2 +- src/stages/typesystem/misc.ml | 6 +- src/test/multisig_v2_tests.ml | 2 +- 31 files changed, 242 insertions(+), 211 deletions(-) diff --git a/src/passes/10-interpreter/interpreter.ml b/src/passes/10-interpreter/interpreter.ml index 1a04d35c4..43e78312a 100644 --- a/src/passes/10-interpreter/interpreter.ml +++ b/src/passes/10-interpreter/interpreter.ml @@ -316,8 +316,8 @@ and eval : Ast_typed.expression -> env -> value result ok (label,v')) (LMap.to_kv_list recmap) in ok @@ V_Record (LMap.of_list lv') - | E_record_accessor { expr ; label} -> ( - let%bind record' = eval expr env in + | E_record_accessor { record ; label} -> ( + let%bind record' = eval record env in match record' with | V_Record recmap -> let%bind a = trace_option (simple_error "unknown record field") @@ diff --git a/src/passes/10-transpiler/transpiler.ml b/src/passes/10-transpiler/transpiler.ml index 2d726d2df..b83e84bf6 100644 --- a/src/passes/10-transpiler/transpiler.ml +++ b/src/passes/10-transpiler/transpiler.ml @@ -301,11 +301,12 @@ and transpile_annotated_expression (ae:AST.expression) : expression result = trace_strong (corner_case ~loc:__LOC__ "record build") @@ Append_tree.fold_ne (transpile_annotated_expression) aux node ) - | E_record_accessor {expr; label} -> - let%bind ty' = transpile_type (get_type_expression expr) in + | E_record_accessor {record; label} -> + let ty = get_type_expression record in + let%bind ty' = transpile_type ty in let%bind ty_lmap = trace_strong (corner_case ~loc:__LOC__ "not a record") @@ - get_t_record (get_type_expression expr) in + get_t_record ty in let%bind ty'_lmap = Stage_common.Helpers.bind_map_lmap transpile_type ty_lmap in let%bind path = trace_strong (corner_case ~loc:__LOC__ "record access") @@ @@ -315,7 +316,7 @@ and transpile_annotated_expression (ae:AST.expression) : expression result = | `Left -> C_CAR | `Right -> C_CDR in Combinators.Expression.make_tpl (E_constant {cons_name=c;arguments=[pred]} , ty) in - let%bind record' = transpile_annotated_expression expr in + let%bind record' = transpile_annotated_expression record in let expr = List.fold_left aux record' path in ok expr | E_record_update {record; path; update} -> diff --git a/src/passes/2-concrete_to_imperative/cameligo.ml b/src/passes/2-concrete_to_imperative/cameligo.ml index f6b2ef165..371e0c784 100644 --- a/src/passes/2-concrete_to_imperative/cameligo.ml +++ b/src/passes/2-concrete_to_imperative/cameligo.ml @@ -383,11 +383,10 @@ let rec compile_expression : match variables with | hd :: [] -> if (List.length prep_vars = 1) - then e_let_in hd false inline rhs_b_expr body - else e_let_in hd false inline (e_accessor rhs_b_expr (string_of_int ((List.length prep_vars) - 1))) body + then e_let_in hd inline rhs_b_expr body + else e_let_in hd inline (e_accessor rhs_b_expr (string_of_int ((List.length prep_vars) - 1))) body | hd :: tl -> e_let_in hd - false inline (e_accessor rhs_b_expr (string_of_int ((List.length prep_vars) - (List.length tl) - 1))) (chain_let_in tl body) @@ -408,7 +407,7 @@ let rec compile_expression : let%bind ret_expr = if List.length prep_vars = 1 then ok (chain_let_in prep_vars body) (* Bind the right hand side so we only evaluate it once *) - else ok (e_let_in (rhs_b, ty_opt) false inline rhs' (chain_let_in prep_vars body)) + else ok (e_let_in (rhs_b, ty_opt) inline rhs' (chain_let_in prep_vars body)) in let%bind ret_expr = match kwd_rec with | None -> ok @@ ret_expr @@ -572,7 +571,7 @@ let rec compile_expression : | Raw.PVar y -> let var_name = Var.of_name y.value in let%bind type_expr = compile_type_expression x'.type_expr in - return @@ e_let_in (var_name , Some type_expr) false false e rhs + return @@ e_let_in (var_name , Some type_expr) false e rhs | _ -> default_action () ) | _ -> default_action () diff --git a/src/passes/2-concrete_to_imperative/pascaligo.ml b/src/passes/2-concrete_to_imperative/pascaligo.ml index 53287557d..2eb055351 100644 --- a/src/passes/2-concrete_to_imperative/pascaligo.ml +++ b/src/passes/2-concrete_to_imperative/pascaligo.ml @@ -14,8 +14,6 @@ let pseq_to_list = function | Some lst -> npseq_to_list lst let get_value : 'a Raw.reg -> 'a = fun x -> x.value - - module Errors = struct let unsupported_cst_constr p = let title () = "" in @@ -134,10 +132,10 @@ let r_split = Location.r_split [return_statement] is used for non-let-in statements. *) -let return_let_in ?loc binder mut inline rhs = ok @@ fun expr'_opt -> +let return_let_in ?loc binder inline rhs = ok @@ fun expr'_opt -> match expr'_opt with - | None -> ok @@ e_let_in ?loc binder mut inline rhs (e_skip ()) - | Some expr' -> ok @@ e_let_in ?loc binder mut inline rhs expr' + | None -> ok @@ e_let_in ?loc binder inline rhs (e_skip ()) + | Some expr' -> ok @@ e_let_in ?loc binder inline rhs expr' let return_statement expr = ok @@ fun expr'_opt -> match expr'_opt with @@ -525,7 +523,7 @@ and compile_data_declaration : Raw.data_decl -> _ result = let name = x.name.value in let%bind t = compile_type_expression x.var_type in let%bind expression = compile_expression x.init in - return_let_in ~loc (Var.of_name name, Some t) false false expression + return_let_in ~loc (Var.of_name name, Some t) false expression | LocalConst x -> let (x , loc) = r_split x in let name = x.name.value in @@ -537,7 +535,7 @@ and compile_data_declaration : Raw.data_decl -> _ result = | Some {value; _} -> npseq_to_list value.ne_elements |> List.exists (fun Region.{value; _} -> value = "\"inline\"") - in return_let_in ~loc (Var.of_name name, Some t) false inline expression + in return_let_in ~loc (Var.of_name name, Some t) inline expression | LocalFun f -> let (f , loc) = r_split f in let%bind (binder, expr) = compile_fun_decl ~loc f in @@ -547,7 +545,7 @@ and compile_data_declaration : Raw.data_decl -> _ result = | Some {value; _} -> npseq_to_list value.ne_elements |> List.exists (fun Region.{value; _} -> value = "\"inline\"") - in return_let_in ~loc binder false inline expr + in return_let_in ~loc binder inline expr and compile_param : Raw.param_decl -> (string * type_expression) result = @@ -618,7 +616,7 @@ and compile_fun_decl : let expr = e_accessor (e_variable arguments_name) (string_of_int i) in let type_variable = Some type_expr in - let ass = return_let_in (Var.of_name param , type_variable) false inline expr in + let ass = return_let_in (Var.of_name param , type_variable) inline expr in ass in bind_list @@ List.mapi aux params in @@ -681,7 +679,7 @@ and compile_fun_expression : let aux = fun i (param, param_type) -> let expr = e_accessor (e_variable arguments_name) (string_of_int i) in let type_variable = Some param_type in - let ass = return_let_in (Var.of_name param , type_variable) false false expr in + let ass = return_let_in (Var.of_name param , type_variable) false expr in ass in bind_list @@ List.mapi aux params in @@ -817,8 +815,7 @@ and compile_single_instruction : Raw.instruction -> (_ -> expression result) res match a.lhs with | Path path -> ( let (name , path') = compile_path path in - let (let_binder, mut, rhs, inline) = e_assign_with_let ~loc name path' value_expr in - return_let_in let_binder mut inline rhs + return_statement @@ e_ez_assign ~loc name path' value_expr ) | MapPath v -> ( let v' = v.value in @@ -831,8 +828,7 @@ and compile_single_instruction : Raw.instruction -> (_ -> expression result) res in let%bind key_expr = compile_expression v'.index.value.inside in let expr' = e_map_add key_expr value_expr map in - let (let_binder, mut, rhs, inline) = e_assign_with_let ~loc varname path expr' in - return_let_in let_binder mut inline rhs + return_statement @@ e_ez_assign ~loc varname path expr' ) ) | CaseInstr c -> ( @@ -872,9 +868,7 @@ and compile_single_instruction : Raw.instruction -> (_ -> expression result) res let u : Raw.update = {record=r.path;kwd_with=r.kwd_with; updates=update} in let%bind expr = compile_update {value=u;region=reg} in let (name , access_path) = compile_path r.path in - let loc = Some loc in - let (binder, mut, rhs, inline) = e_assign_with_let ?loc name access_path expr in - return_let_in binder mut inline rhs + return_statement @@ e_ez_assign ~loc name access_path expr ) | MapPatch patch -> ( @@ -897,8 +891,7 @@ and compile_single_instruction : Raw.instruction -> (_ -> expression result) res inj (e_accessor_list ~loc (e_variable (Var.of_name name)) access_path) in - let (binder, mut, rhs, inline) = e_assign_with_let ~loc name access_path assigns in - return_let_in binder mut inline rhs + return_statement @@ e_ez_assign ~loc name access_path assigns ) | SetPatch patch -> ( let (setp, loc) = r_split patch in @@ -913,8 +906,7 @@ and compile_single_instruction : Raw.instruction -> (_ -> expression result) res let assigns = List.fold_right (fun hd s -> e_constant C_SET_ADD [hd ; s]) inj (e_accessor_list ~loc (e_variable (Var.of_name name)) access_path) in - let (binder, mut, rhs, inline) = e_assign_with_let ~loc name access_path assigns in - return_let_in binder mut inline rhs + return_statement @@ e_ez_assign ~loc name access_path assigns ) | MapRemove r -> ( let (v , loc) = r_split r in @@ -928,8 +920,7 @@ and compile_single_instruction : Raw.instruction -> (_ -> expression result) res in let%bind key' = compile_expression key in let expr = e_constant ~loc C_MAP_REMOVE [key' ; map] in - let (binder, mut, rhs, inline) = e_assign_with_let ~loc varname path expr in - return_let_in binder mut inline rhs + return_statement @@ e_ez_assign ~loc varname path expr ) | SetRemove r -> ( let (set_rm, loc) = r_split r in @@ -942,8 +933,7 @@ and compile_single_instruction : Raw.instruction -> (_ -> expression result) res in let%bind removed' = compile_expression set_rm.element in let expr = e_constant ~loc C_SET_REMOVE [removed' ; set] in - let (binder, mut, rhs, inline) = e_assign_with_let ~loc varname path expr in - return_let_in binder mut inline rhs + return_statement @@ e_ez_assign ~loc varname path expr ) and compile_path : Raw.path -> string * string list = fun p -> diff --git a/src/passes/3-self_ast_imperative/helpers.ml b/src/passes/3-self_ast_imperative/helpers.ml index 42ee86259..f70b21aaf 100644 --- a/src/passes/3-self_ast_imperative/helpers.ml +++ b/src/passes/3-self_ast_imperative/helpers.ml @@ -47,8 +47,8 @@ let rec fold_expression : 'a folder -> 'a -> expression -> 'a result = fun f ini let%bind res = fold_expression self res update in ok res ) - | E_record_accessor {expr} -> ( - let%bind res = self init' expr in + | E_record_accessor {record} -> ( + let%bind res = self init' record in ok res ) | E_let_in { let_binder = _ ; rhs ; let_result } -> ( @@ -63,6 +63,9 @@ let rec fold_expression : 'a folder -> 'a -> expression -> 'a result = fun f ini let ab = (expr1,expr2) in let%bind res = bind_fold_pair self init' ab in ok res + | E_assign {variable=_;access_path=_;expression} -> + let%bind res = self init' expression in + ok res | E_for {body; _} -> let%bind res = self init' body in ok res @@ -74,6 +77,7 @@ let rec fold_expression : 'a folder -> 'a -> expression -> 'a result = fun f ini let%bind res = self init' condition in let%bind res = self res body in ok res + and fold_cases : 'a folder -> 'a -> matching_expr -> 'a result = fun f init m -> @@ -145,8 +149,8 @@ let rec map_expression : exp_mapper -> expression -> expression result = fun f e return @@ E_matching {matchee=e';cases=cases'} ) | E_record_accessor acc -> ( - let%bind e' = self acc.expr in - return @@ E_record_accessor {acc with expr = e'} + let%bind e' = self acc.record in + return @@ E_record_accessor {acc with record = e'} ) | E_record m -> ( let%bind m' = bind_map_lmap self m in @@ -166,10 +170,10 @@ let rec map_expression : exp_mapper -> expression -> expression result = fun f e let%bind (lamb,args) = bind_map_pair self ab in return @@ E_application {lamb;args} ) - | E_let_in { let_binder ; mut; rhs ; let_result; inline } -> ( + | E_let_in { let_binder ; rhs ; let_result; inline } -> ( let%bind rhs = self rhs in let%bind let_result = self let_result in - return @@ E_let_in { let_binder ; mut; rhs ; let_result; inline } + return @@ E_let_in { let_binder ; rhs ; let_result; inline } ) | E_lambda { binder ; input_type ; output_type ; result } -> ( let%bind result = self result in @@ -187,6 +191,10 @@ let rec map_expression : exp_mapper -> expression -> expression result = fun f e let%bind (expr1,expr2) = bind_map_pair self (expr1,expr2) in return @@ E_sequence {expr1;expr2} ) + | E_assign {variable;access_path;expression} -> ( + let%bind expression = self expression in + return @@ E_assign {variable;access_path;expression} + ) | E_for {binder; start; final; increment; body} -> let%bind body = self body in return @@ E_for {binder; start; final; increment; body} @@ -303,8 +311,8 @@ let rec fold_map_expression : 'a fold_mapper -> 'a -> expression -> ('a * expres ok (res, return @@ E_matching {matchee=e';cases=cases'}) ) | E_record_accessor acc -> ( - let%bind (res, e') = self init' acc.expr in - ok (res, return @@ E_record_accessor {acc with expr = e'}) + let%bind (res, e') = self init' acc.record in + ok (res, return @@ E_record_accessor {acc with record = e'}) ) | E_record m -> ( let%bind (res, lst') = bind_fold_map_list (fun res (k,e) -> let%bind (res,e) = self res e in ok (res,(k,e))) init' (LMap.to_kv_list m) in @@ -325,10 +333,10 @@ let rec fold_map_expression : 'a fold_mapper -> 'a -> expression -> ('a * expres let%bind (res,(a,b)) = bind_fold_map_pair self init' ab in ok (res, return @@ E_application {lamb=a;args=b}) ) - | E_let_in { let_binder ; mut; rhs ; let_result; inline } -> ( + | E_let_in { let_binder ; rhs ; let_result; inline } -> ( let%bind (res,rhs) = self init' rhs in let%bind (res,let_result) = self res let_result in - ok (res, return @@ E_let_in { let_binder ; mut; rhs ; let_result ; inline }) + ok (res, return @@ E_let_in { let_binder ; rhs ; let_result ; inline }) ) | E_lambda { binder ; input_type ; output_type ; result } -> ( let%bind (res,result) = self init' result in @@ -346,6 +354,9 @@ let rec fold_map_expression : 'a fold_mapper -> 'a -> expression -> ('a * expres let%bind (res,(expr1,expr2)) = bind_fold_map_pair self init' (expr1,expr2) in ok (res, return @@ E_sequence {expr1;expr2}) ) + | E_assign {variable;access_path;expression} -> + let%bind (res, expression) = self init' expression in + ok (res, return @@ E_assign {variable;access_path;expression}) | E_for {binder; start; final; increment; body} -> let%bind (res, body) = self init' body in ok (res, return @@ E_for {binder; start; final; increment; body}) diff --git a/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml b/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml index a461dc9ae..33c56626b 100644 --- a/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml +++ b/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml @@ -37,7 +37,7 @@ let repair_mutable_variable_in_matching (match_body : O.expression) (element_nam ok (true,(decl_var, free_var), O.e_let_in let_binder false false rhs let_result) else( let free_var = if (List.mem name free_var) then free_var else name::free_var in - let expr = O.e_let_in (env,None) false false (O.e_update (O.e_variable env) (Var.to_name name) (O.e_variable name)) let_result in + let expr = O.e_let_in (env,None) false false (O.e_record_update (O.e_variable env) (Var.to_name name) (O.e_variable name)) let_result in ok (true,(decl_var, free_var), O.e_let_in let_binder false false rhs expr) ) | E_constant {cons_name=C_MAP_FOLD;arguments= _} @@ -70,8 +70,8 @@ and repair_mutable_variable_in_loops (for_body : O.expression) (element_names : else( let free_var = if (List.mem name free_var) then free_var else name::free_var in let expr = O.e_let_in (env,None) false false ( - O.e_update (O.e_variable env) ("0") - (O.e_update (O.e_accessor (O.e_variable env) "0") (Var.to_name name) (O.e_variable name)) + O.e_record_update (O.e_variable env) ("0") + (O.e_record_update (O.e_record_accessor (O.e_variable env) "0") (Var.to_name name) (O.e_variable name)) ) let_result in ok (true,(decl_var, free_var), O.e_let_in let_binder false false rhs expr) @@ -95,7 +95,7 @@ and store_mutable_variable (free_vars : I.expression_variable list) = and restore_mutable_variable (expr : O.expression->O.expression_content) (free_vars : O.expression_variable list) (env : O.expression_variable) = let aux (f: O.expression -> O.expression) (ev: O.expression_variable) = - fun expr -> f (O.e_let_in (ev,None) true false (O.e_accessor (O.e_variable env) (Var.to_name ev)) expr) + fun expr -> f (O.e_let_in (ev,None) true false (O.e_record_accessor (O.e_variable env) (Var.to_name ev)) expr) in let ef = List.fold_left aux (fun e -> e) free_vars in expr (ef (O.e_skip ())) @@ -178,12 +178,12 @@ let rec compile_expression : I.expression -> O.expression result = let%bind fun_type = compile_type_expression fun_type in let%bind lambda = compile_lambda lambda in return @@ O.E_recursive {fun_name;fun_type;lambda} - | I.E_let_in {let_binder;mut;inline;rhs;let_result} -> + | I.E_let_in {let_binder;inline;rhs;let_result} -> let (binder,ty_opt) = let_binder in let%bind ty_opt = bind_map_option compile_type_expression ty_opt in let%bind rhs = compile_expression rhs in let%bind let_result = compile_expression let_result in - return @@ O.E_let_in {let_binder=(binder,ty_opt);mut;inline;rhs;let_result} + return @@ O.E_let_in {let_binder=(binder,ty_opt);mut=false;inline;rhs;let_result} | I.E_constructor {constructor;element} -> let%bind element = compile_expression element in return @@ O.E_constructor {constructor;element} @@ -199,9 +199,9 @@ let rec compile_expression : I.expression -> O.expression result = ) record in return @@ O.E_record (O.LMap.of_list record) - | I.E_record_accessor {expr;label} -> - let%bind expr = compile_expression expr in - return @@ O.E_record_accessor {expr;label} + | I.E_record_accessor {record;label} -> + let%bind record = compile_expression record in + return @@ O.E_record_accessor {record;label} | I.E_record_update {record;path;update} -> let%bind record = compile_expression record in let%bind update = compile_expression update in @@ -231,11 +231,18 @@ let rec compile_expression : I.expression -> O.expression result = let%bind anno_expr = compile_expression anno_expr in let%bind type_annotation = compile_type_expression type_annotation in return @@ O.E_ascription {anno_expr; type_annotation} + | I.E_sequence {expr1={expression_content=I.E_assign expr1;_}; expr2} -> + let%bind expr2 = compile_expression expr2 in + let%bind ret = compile_assign expr1 @@ expr2 in + return @@ ret | I.E_sequence {expr1; expr2} -> let%bind expr1 = compile_expression expr1 in let%bind expr2 = compile_expression expr2 in ok @@ add_to_end expr1 expr2 | I.E_skip -> return @@ O.E_skip + | I.E_assign ass -> + let%bind content = compile_assign ass @@ O.e_skip () in + return @@ content | I.E_for f -> let%bind f = compile_for f in return @@ f @@ -246,6 +253,37 @@ let rec compile_expression : I.expression -> O.expression result = let%bind w = compile_while w in return @@ w +and compile_assign {variable; access_path; expression} expr = + let accessor ?loc s a = + match a with + I.Access_tuple _i -> failwith "adding tuple soon" + | I.Access_record a -> ok @@ O.e_record_accessor ?loc s a + | I.Access_map k -> + let%bind k = compile_expression k in + ok @@ O.e_constant ?loc C_MAP_FIND_OPT [k;s] + in + let update ?loc (s:O.expression) a e = + match a with + I.Access_tuple _i -> failwith "adding tuple soon" + | I.Access_record a -> ok @@ O.e_record_update ?loc s a e + | I.Access_map k -> + let%bind k = compile_expression k in + ok @@ O.e_constant ?loc C_UPDATE [k;O.e_some (e);s] + in + let aux (s, e : O.expression * _) lst = + let%bind s' = accessor ~loc:s.location s lst in + let e' = fun expr -> + let%bind u = update ~loc:s.location s lst (expr) + in e u + in + ok @@ (s',e') + in + let%bind (_,rhs) = bind_fold_list aux (O.e_variable variable, fun e -> ok @@ e) access_path in + let%bind expression = compile_expression expression in + let%bind rhs = rhs @@ expression in + ok @@ O.E_let_in {let_binder=(variable,None); mut=true; rhs; let_result=expr;inline = false} + + and compile_lambda : I.lambda -> O.lambda result = fun {binder;input_type;output_type;result}-> let%bind input_type = bind_map_option compile_type_expression input_type in @@ -362,7 +400,7 @@ and compile_while I.{condition;body} = let for_body = add_to_end for_body ctrl in let aux name expr= - O.e_let_in (name,None) false false (O.e_accessor (O.e_accessor (O.e_variable binder) "0") (Var.to_name name)) expr + O.e_let_in (name,None) false false (O.e_record_accessor (O.e_record_accessor (O.e_variable binder) "0") (Var.to_name name)) expr in let init_rec = O.e_tuple [store_mutable_variable @@ captured_name_list] in let restore = fun expr -> List.fold_right aux captured_name_list expr in @@ -377,7 +415,7 @@ and compile_while I.{condition;body} = let return_expr = fun expr -> O.E_let_in {let_binder; mut=false; inline=false; rhs=init_rec; let_result= O.e_let_in let_binder false false loop @@ - O.e_let_in let_binder false false (O.e_accessor (O.e_variable env_rec) "0") @@ + O.e_let_in let_binder false false (O.e_record_accessor (O.e_variable env_rec) "0") @@ expr } in @@ -393,7 +431,7 @@ and compile_for I.{binder;start;final;increment;body} = let continue_expr = O.e_constant C_FOLD_CONTINUE [(O.e_variable env_rec)] in let ctrl = O.e_let_in (binder,Some O.t_int) false false (O.e_constant C_ADD [ O.e_variable binder ; step ]) @@ - O.e_let_in (env_rec, None) false false (O.e_update (O.e_variable env_rec) "1" @@ O.e_variable binder)@@ + O.e_let_in (env_rec, None) false false (O.e_record_update (O.e_variable env_rec) "1" @@ O.e_variable binder)@@ continue_expr in (* Modify the body loop*) @@ -402,7 +440,7 @@ and compile_for I.{binder;start;final;increment;body} = let for_body = add_to_end for_body ctrl in let aux name expr= - O.e_let_in (name,None) false false (O.e_accessor (O.e_accessor (O.e_variable env_rec) "0") (Var.to_name name)) expr + O.e_let_in (name,None) false false (O.e_record_accessor (O.e_record_accessor (O.e_variable env_rec) "0") (Var.to_name name)) expr in (* restores the initial value of the free_var*) @@ -411,7 +449,7 @@ and compile_for I.{binder;start;final;increment;body} = (*Prep the lambda for the fold*) let stop_expr = O.e_constant C_FOLD_STOP [O.e_variable env_rec] in let aux_func = O.e_lambda env_rec None None @@ - O.e_let_in (binder,Some O.t_int) false false (O.e_accessor (O.e_variable env_rec) "1") @@ + O.e_let_in (binder,Some O.t_int) false false (O.e_record_accessor (O.e_variable env_rec) "1") @@ O.e_cond cond (restore for_body) (stop_expr) in (* Make the fold_while en precharge the vakye *) @@ -424,7 +462,7 @@ and compile_for I.{binder;start;final;increment;body} = O.E_let_in {let_binder=(binder, Some O.t_int);mut=false; inline=false;rhs=start;let_result= O.e_let_in let_binder false false init_rec @@ O.e_let_in let_binder false false loop @@ - O.e_let_in let_binder false false (O.e_accessor (O.e_variable env_rec) "0") @@ + O.e_let_in let_binder false false (O.e_record_accessor (O.e_variable env_rec) "0") @@ expr } in @@ -440,21 +478,21 @@ and compile_for_each I.{binder;collection;collection_type; body} = let env = Var.fresh () in let%bind body = compile_expression body in let%bind ((_,free_vars), body) = repair_mutable_variable_in_loops body element_names args in - let for_body = add_to_end body @@ (O.e_accessor (O.e_variable args) "0") in + let for_body = add_to_end body @@ (O.e_record_accessor (O.e_variable args) "0") in let init_record = store_mutable_variable free_vars in let%bind collect = compile_expression collection in let aux name expr= - O.e_let_in (name,None) false false (O.e_accessor (O.e_accessor (O.e_variable args) "0") (Var.to_name name)) expr + O.e_let_in (name,None) false false (O.e_record_accessor (O.e_record_accessor (O.e_variable args) "0") (Var.to_name name)) expr in let restore = fun expr -> List.fold_right aux free_vars expr in let restore = match collection_type with | Map -> (match snd binder with - | Some v -> fun expr -> restore (O.e_let_in (fst binder, None) false false (O.e_accessor (O.e_accessor (O.e_variable args) "1") "0") - (O.e_let_in (v, None) false false (O.e_accessor (O.e_accessor (O.e_variable args) "1") "1") expr)) - | None -> fun expr -> restore (O.e_let_in (fst binder, None) false false (O.e_accessor (O.e_accessor (O.e_variable args) "1") "0") expr) + | Some v -> fun expr -> restore (O.e_let_in (fst binder, None) false false (O.e_record_accessor (O.e_record_accessor (O.e_variable args) "1") "0") + (O.e_let_in (v, None) false false (O.e_record_accessor (O.e_record_accessor (O.e_variable args) "1") "1") expr)) + | None -> fun expr -> restore (O.e_let_in (fst binder, None) false false (O.e_record_accessor (O.e_record_accessor (O.e_variable args) "1") "0") expr) ) - | _ -> fun expr -> restore (O.e_let_in (fst binder, None) false false (O.e_accessor (O.e_variable args) "1") expr) + | _ -> fun expr -> restore (O.e_let_in (fst binder, None) false false (O.e_record_accessor (O.e_variable args) "1") expr) in let lambda = O.e_lambda args None None (restore for_body) in let%bind op_name = match collection_type with @@ -564,7 +602,7 @@ let rec uncompile_expression : O.expression -> I.expression result = let%bind ty_opt = bind_map_option uncompile_type_expression ty_opt in let%bind rhs = uncompile_expression rhs in let%bind let_result = uncompile_expression let_result in - return @@ I.E_let_in {let_binder=(binder,ty_opt);mut=false;inline;rhs;let_result} + return @@ I.E_let_in {let_binder=(binder,ty_opt);inline;rhs;let_result} | O.E_constructor {constructor;element} -> let%bind element = uncompile_expression element in return @@ I.E_constructor {constructor;element} @@ -581,9 +619,9 @@ let rec uncompile_expression : O.expression -> I.expression result = ) record in return @@ I.E_record (O.LMap.of_list record) - | O.E_record_accessor {expr;label} -> - let%bind expr = uncompile_expression expr in - return @@ I.E_record_accessor {expr;label} + | O.E_record_accessor {record;label} -> + let%bind record = uncompile_expression record in + return @@ I.E_record_accessor {record;label} | O.E_record_update {record;path;update} -> let%bind record = uncompile_expression record in let%bind update = uncompile_expression update in diff --git a/src/passes/5-self_ast_sugar/helpers.ml b/src/passes/5-self_ast_sugar/helpers.ml index 71597ce5e..0e0ddbd45 100644 --- a/src/passes/5-self_ast_sugar/helpers.ml +++ b/src/passes/5-self_ast_sugar/helpers.ml @@ -47,8 +47,8 @@ let rec fold_expression : 'a folder -> 'a -> expression -> 'a result = fun f ini let%bind res = fold_expression self res update in ok res ) - | E_record_accessor {expr} -> ( - let%bind res = self init' expr in + | E_record_accessor {record} -> ( + let%bind res = self init' record in ok res ) | E_let_in { let_binder = _ ; rhs ; let_result } -> ( @@ -134,8 +134,8 @@ let rec map_expression : exp_mapper -> expression -> expression result = fun f e return @@ E_matching {matchee=e';cases=cases'} ) | E_record_accessor acc -> ( - let%bind e' = self acc.expr in - return @@ E_record_accessor {acc with expr = e'} + let%bind e' = self acc.record in + return @@ E_record_accessor {acc with record = e'} ) | E_record m -> ( let%bind m' = bind_map_lmap self m in @@ -280,8 +280,8 @@ let rec fold_map_expression : 'a fold_mapper -> 'a -> expression -> ('a * expres ok (res, return @@ E_matching {matchee=e';cases=cases'}) ) | E_record_accessor acc -> ( - let%bind (res, e') = self init' acc.expr in - ok (res, return @@ E_record_accessor {acc with expr = e'}) + let%bind (res, e') = self init' acc.record in + ok (res, return @@ E_record_accessor {acc with record = e'}) ) | E_record m -> ( let%bind (res, lst') = bind_fold_map_list (fun res (k,e) -> let%bind (res,e) = self res e in ok (res,(k,e))) init' (LMap.to_kv_list m) in diff --git a/src/passes/6-sugar_to_core/sugar_to_core.ml b/src/passes/6-sugar_to_core/sugar_to_core.ml index 48c43f6d7..e75b9c2ff 100644 --- a/src/passes/6-sugar_to_core/sugar_to_core.ml +++ b/src/passes/6-sugar_to_core/sugar_to_core.ml @@ -101,9 +101,9 @@ let rec compile_expression : I.expression -> O.expression result = ) record in return @@ O.E_record (O.LMap.of_list record) - | I.E_record_accessor {expr;label} -> - let%bind expr = compile_expression expr in - return @@ O.E_record_accessor {expr;label} + | I.E_record_accessor {record;label} -> + let%bind record = compile_expression record in + return @@ O.E_record_accessor {record;label} | I.E_record_update {record;path;update} -> let%bind record = compile_expression record in let%bind update = compile_expression update in @@ -294,9 +294,9 @@ let rec uncompile_expression : O.expression -> I.expression result = ) record in return @@ I.E_record (O.LMap.of_list record) - | O.E_record_accessor {expr;label} -> - let%bind expr = uncompile_expression expr in - return @@ I.E_record_accessor {expr;label} + | O.E_record_accessor {record;label} -> + let%bind record = uncompile_expression record in + return @@ I.E_record_accessor {record;label} | O.E_record_update {record;path;update} -> let%bind record = uncompile_expression record in let%bind update = uncompile_expression update in diff --git a/src/passes/8-typer-new/typer.ml b/src/passes/8-typer-new/typer.ml index 0dcae68d0..f446c1076 100644 --- a/src/passes/8-typer-new/typer.ml +++ b/src/passes/8-typer-new/typer.ml @@ -452,10 +452,10 @@ and type_expression : environment -> Solver.state -> ?tv_opt:O.type_expression - * | Some (T_constant ("address" , [])) -> return (E_literal (Literal_address s)) (t_address ()) * | _ -> return (E_literal (Literal_string s)) (t_string ()) * ) *) - | E_record_accessor {expr;label} -> ( - let%bind (base' , state') = type_expression e state expr in + | E_record_accessor {record;label} -> ( + let%bind (base' , state') = type_expression e state record in let wrapped = Wrap.access_label ~base:base'.type_expression ~label in - return_wrapped (E_record_accessor {expr=base';label}) state' wrapped + return_wrapped (E_record_accessor {record=base';label}) state' wrapped ) (* Sum *) @@ -1055,8 +1055,8 @@ let rec untype_expression (e:O.expression) : (I.expression) result = let%bind r' = bind_smap @@ Map.String.map untype_expression r in return (e_record r') - | E_record_accessor {expr; label} -> - let%bind r' = untype_expression expr in + | E_record_accessor {record; label} -> + let%bind r' = untype_expression record in let Label s = label in return (e_accessor r' s) | E_record_update {record; path; update} -> diff --git a/src/passes/8-typer-old/typer.ml b/src/passes/8-typer-old/typer.ml index 5a11d6184..d26828230 100644 --- a/src/passes/8-typer-old/typer.ml +++ b/src/passes/8-typer-old/typer.ml @@ -450,8 +450,8 @@ and type_expression' : environment -> ?tv_opt:O.type_expression -> I.expression return (e_address s) (t_address ()) | E_literal (Literal_operation op) -> return (e_operation op) (t_operation ()) - | E_record_accessor {expr;label} -> - let%bind e' = type_expression' e expr in + | E_record_accessor {record;label} -> + let%bind e' = type_expression' e record in let aux (prev:O.expression) (a:I.label) : O.expression result = let property = a in let%bind r_tv = get_t_record prev.type_expression in @@ -459,7 +459,7 @@ and type_expression' : environment -> ?tv_opt:O.type_expression -> I.expression generic_try (bad_record_access property ae prev.type_expression ae.location) @@ (fun () -> I.LMap.find property r_tv) in let location = ae.location in - ok @@ make_a_e ~location (E_record_accessor {expr=prev; label=property}) tv e + ok @@ make_a_e ~location (E_record_accessor {record=prev; label=property}) tv e in let%bind ae = trace (simple_info "accessing") @@ aux e' label in @@ -861,8 +861,8 @@ let rec untype_expression (e:O.expression) : (I.expression) result = let%bind r' = bind_smap @@ Map.String.map untype_expression r in return (e_record r') - | E_record_accessor {expr; label} -> - let%bind r' = untype_expression expr in + | E_record_accessor {record; label} -> + let%bind r' = untype_expression record in let Label s = label in return (e_accessor r' s) | E_record_update {record=r; path=l; update=e} -> diff --git a/src/passes/9-self_ast_typed/helpers.ml b/src/passes/9-self_ast_typed/helpers.ml index 2da8a766e..90a9fe9cc 100644 --- a/src/passes/9-self_ast_typed/helpers.ml +++ b/src/passes/9-self_ast_typed/helpers.ml @@ -48,8 +48,8 @@ let rec fold_expression : 'a folder -> 'a -> expression -> 'a result = fun f ini let%bind res = fold_expression self res update in ok res ) - | E_record_accessor {expr} -> ( - let%bind res = self init' expr in + | E_record_accessor {record} -> ( + let%bind res = self init' record in ok res ) | E_let_in { let_binder = _ ; rhs ; let_result } -> ( @@ -119,8 +119,8 @@ let rec map_expression : mapper -> expression -> expression result = fun f e -> return @@ E_matching {matchee=e';cases=cases'} ) | E_record_accessor acc -> ( - let%bind e' = self acc.expr in - return @@ E_record_accessor {acc with expr = e'} + let%bind e' = self acc.record in + return @@ E_record_accessor {acc with record = e'} ) | E_record m -> ( let%bind m' = bind_map_lmap self m in @@ -234,8 +234,8 @@ let rec fold_map_expression : 'a fold_mapper -> 'a -> expression -> ('a * expres ok (res, return @@ E_matching {matchee=e';cases=cases'}) ) | E_record_accessor acc -> ( - let%bind (res, e') = self init' acc.expr in - ok (res, return @@ E_record_accessor {acc with expr = e'}) + let%bind (res, e') = self init' acc.record in + ok (res, return @@ E_record_accessor {acc with record = e'}) ) | E_record m -> ( let%bind (res, lst') = bind_fold_map_list (fun res (k,e) -> let%bind (res,e) = self res e in ok (res,(k,e))) init' (LMap.to_kv_list m) in diff --git a/src/passes/9-self_ast_typed/tail_recursion.ml b/src/passes/9-self_ast_typed/tail_recursion.ml index 610484a38..5e3953bc3 100644 --- a/src/passes/9-self_ast_typed/tail_recursion.ml +++ b/src/passes/9-self_ast_typed/tail_recursion.ml @@ -49,8 +49,8 @@ let rec check_recursive_call : expression_variable -> bool -> expression -> unit let es = LMap.to_list elm in let%bind _ = bind_map_list (check_recursive_call n false) es in ok () - | E_record_accessor {expr;_} -> - let%bind _ = check_recursive_call n false expr in + | E_record_accessor {record;_} -> + let%bind _ = check_recursive_call n false record in ok () | E_record_update {record;update;_} -> let%bind _ = check_recursive_call n false record in diff --git a/src/stages/1-ast_imperative/PP.ml b/src/stages/1-ast_imperative/PP.ml index d25b84dee..1c8a243ff 100644 --- a/src/stages/1-ast_imperative/PP.ml +++ b/src/stages/1-ast_imperative/PP.ml @@ -27,8 +27,8 @@ and expression_content ppf (ec : expression_content) = c.arguments | E_record m -> fprintf ppf "%a" (tuple_or_record_sep_expr expression) m - | E_record_accessor ra -> - fprintf ppf "%a.%a" expression ra.expr label ra.label + | E_record_accessor {record; label=l}-> + fprintf ppf "%a.%a" expression record label l | E_record_update {record; path; update} -> fprintf ppf "{ %a with { %a = %a } }" expression record label path expression update | E_map m -> @@ -57,15 +57,20 @@ and expression_content ppf (ec : expression_content) = expression_variable fun_name type_expression fun_type expression_content (E_lambda lambda) - | E_let_in { let_binder ; mut; rhs ; let_result; inline } -> - fprintf ppf "let %a%a = %a%a in %a" option_mut mut option_type_name let_binder expression rhs option_inline inline expression let_result + | E_let_in { let_binder ; rhs ; let_result; inline } -> + fprintf ppf "let %a = %a%a in %a" option_type_name let_binder expression rhs option_inline inline expression let_result | E_ascription {anno_expr; type_annotation} -> fprintf ppf "%a : %a" expression anno_expr type_expression type_annotation | E_sequence {expr1;expr2} -> - fprintf ppf "%a;\n%a" expression expr1 expression expr2 + fprintf ppf "{ %a; @. %a}" expression expr1 expression expr2 | E_skip -> fprintf ppf "skip" + | E_assign {variable; access_path; expression=e} -> + fprintf ppf "%a%a := %a" + expression_variable variable + (list_sep (fun ppf a -> fprintf ppf ".%a" accessor a) (fun ppf () -> fprintf ppf "")) access_path + expression e | E_for {binder; start; final; increment; body} -> fprintf ppf "for %a from %a to %a by %a do %a" expression_variable binder @@ -83,6 +88,12 @@ and expression_content ppf (ec : expression_content) = expression condition expression body +and accessor ppf a = + match a with + | Access_tuple i -> fprintf ppf "%d" i + | Access_record s -> fprintf ppf "%s" s + | Access_map e -> fprintf ppf "%a" expression e + and option_map ppf (k,v_opt) = match v_opt with | None -> fprintf ppf "%a" expression_variable k diff --git a/src/stages/1-ast_imperative/combinators.ml b/src/stages/1-ast_imperative/combinators.ml index 47ec55fb6..bc66d4ac0 100644 --- a/src/stages/1-ast_imperative/combinators.ml +++ b/src/stages/1-ast_imperative/combinators.ml @@ -118,12 +118,12 @@ let e_list ?loc lst : expression = make_expr ?loc @@ E_list lst let e_constructor ?loc s a : expression = make_expr ?loc @@ E_constructor { constructor = Constructor s; element = a} let e_matching ?loc a b : expression = make_expr ?loc @@ E_matching {matchee=a;cases=b} let e_matching_bool ?loc a b c : expression = e_matching ?loc a (Match_bool {match_true = b ; match_false = c}) -let e_accessor ?loc a b = make_expr ?loc @@ E_record_accessor {expr = a; label= Label b} +let e_accessor ?loc a b = make_expr ?loc @@ E_record_accessor {record = a; label= Label b} let e_accessor_list ?loc a b = List.fold_left (fun a b -> e_accessor ?loc a b) a b let e_variable ?loc v = make_expr ?loc @@ E_variable v let e_skip ?loc () = make_expr ?loc @@ E_skip -let e_let_in ?loc (binder, ascr) mut inline rhs let_result = - make_expr ?loc @@ E_let_in { let_binder = (binder, ascr) ; mut; rhs ; let_result; inline } +let e_let_in ?loc (binder, ascr) inline rhs let_result = + make_expr ?loc @@ E_let_in { let_binder = (binder, ascr) ; rhs ; let_result; inline } let e_annotation ?loc anno_expr ty = make_expr ?loc @@ E_ascription {anno_expr; type_annotation = ty} let e_application ?loc a b = make_expr ?loc @@ E_application {lamb=a ; args=b} let e_binop ?loc name a b = make_expr ?loc @@ E_constant {cons_name = name ; arguments = [a ; b]} @@ -191,24 +191,16 @@ let e_lambda ?loc (binder : expression_variable) let e_recursive ?loc fun_name fun_type lambda = make_expr ?loc @@ E_recursive {fun_name; fun_type; lambda} -let e_assign_with_let ?loc var access_path expr = - let var = Var.of_name (var) in - match access_path with - | [] -> (var, None), true, expr, false - - | lst -> - let rec aux path record= match path with - | [] -> failwith "acces_path cannot be empty" - | [e] -> e_update ?loc record e expr - | elem::tail -> - let next_record = e_accessor record elem in - e_update ?loc record elem (aux tail next_record ) - in - (var, None), true, (aux lst (e_variable var)), false +let e_assign ?loc variable access_path expression = + make_expr ?loc @@ E_assign {variable;access_path;expression} +let e_ez_assign ?loc variable access_path expression = + let variable = Var.of_name variable in + let access_path = List.map (fun s -> Access_record s) access_path in + e_assign ?loc variable access_path expression let get_e_accessor = fun t -> match t with - | E_record_accessor {expr; label} -> ok (expr , label) + | E_record_accessor {record; label} -> ok (record , label) | _ -> simple_fail "not an accessor" let assert_e_accessor = fun t -> diff --git a/src/stages/1-ast_imperative/combinators.mli b/src/stages/1-ast_imperative/combinators.mli index 4d5c4db7a..1cf5986c9 100644 --- a/src/stages/1-ast_imperative/combinators.mli +++ b/src/stages/1-ast_imperative/combinators.mli @@ -86,7 +86,7 @@ val e_variable : ?loc:Location.t -> expression_variable -> expression val e_skip : ?loc:Location.t -> unit -> expression val e_sequence : ?loc:Location.t -> expression -> expression -> expression val e_cond: ?loc:Location.t -> expression -> expression -> expression -> expression -val e_let_in : ?loc:Location.t -> ( expression_variable * type_expression option ) -> bool -> bool -> expression -> expression -> expression +val e_let_in : ?loc:Location.t -> ( expression_variable * type_expression option ) -> bool -> expression -> expression -> expression val e_annotation : ?loc:Location.t -> expression -> type_expression -> expression val e_application : ?loc:Location.t -> expression -> expression -> expression val e_binop : ?loc:Location.t -> constant' -> expression -> expression -> expression @@ -110,7 +110,8 @@ val e_lambda : ?loc:Location.t -> expression_variable -> type_expression option val e_recursive : ?loc:Location.t -> expression_variable -> type_expression -> lambda -> expression val e_record : ?loc:Location.t -> expr Map.String.t -> expression val e_update : ?loc:Location.t -> expression -> string -> expression -> expression -val e_assign_with_let : ?loc:Location.t -> string -> string list -> expression -> ((expression_variable*type_expression option)*bool*expression*bool) +val e_assign : ?loc:Location.t -> expression_variable -> access list -> expression -> expression +val e_ez_assign : ?loc:Location.t -> string -> string list -> expression -> expression (* val get_e_accessor : expression' -> ( expression * access_path ) result diff --git a/src/stages/1-ast_imperative/misc.ml b/src/stages/1-ast_imperative/misc.ml index 54e0303cf..7a2615e04 100644 --- a/src/stages/1-ast_imperative/misc.ml +++ b/src/stages/1-ast_imperative/misc.ml @@ -185,6 +185,7 @@ let rec assert_value_eq (a, b: (expression * expression )) : unit result = | (E_recursive _,_) | (E_record_accessor _, _) | (E_look_up _, _) | (E_matching _, _) | (E_sequence _, _) | (E_skip, _) + | (E_assign _, _) | (E_for _, _) | (E_for_each _, _) | (E_while _, _) -> simple_fail "comparing not a value" diff --git a/src/stages/1-ast_imperative/types.ml b/src/stages/1-ast_imperative/types.ml index 08200dbd7..480d612fb 100644 --- a/src/stages/1-ast_imperative/types.ml +++ b/src/stages/1-ast_imperative/types.ml @@ -55,6 +55,7 @@ and expression_content = | E_set of expression list | E_look_up of (expression * expression) (* Imperative *) + | E_assign of assign | E_for of for_ | E_for_each of for_each | E_while of while_loop @@ -82,14 +83,13 @@ and recursive = { and let_in = { let_binder: expression_variable * type_expression option - ; mut: bool ; rhs: expression ; let_result: expression ; inline: bool } and constructor = {constructor: constructor'; element: expression} -and accessor = {expr: expression; label: label} +and accessor = {record: expression; label: label} and update = {record: expression; path: label ; update: expression} @@ -105,6 +105,17 @@ and sequence = { expr2: expression ; } +and assign = { + variable : expression_variable; + access_path : access list; + expression : expression; +} + +and access = + | Access_tuple of int + | Access_record of string + | Access_map of expr + and for_ = { binder : expression_variable; start : expression; diff --git a/src/stages/2-ast_sugar/PP.ml b/src/stages/2-ast_sugar/PP.ml index 1b65a8046..ca4184aff 100644 --- a/src/stages/2-ast_sugar/PP.ml +++ b/src/stages/2-ast_sugar/PP.ml @@ -28,7 +28,7 @@ and expression_content ppf (ec : expression_content) = | E_record m -> fprintf ppf "%a" (tuple_or_record_sep_expr expression) m | E_record_accessor ra -> - fprintf ppf "%a.%a" expression ra.expr label ra.label + fprintf ppf "%a.%a" expression ra.record label ra.label | E_record_update {record; path; update} -> fprintf ppf "{ %a with { %a = %a } }" expression record label path expression update | E_map m -> @@ -64,7 +64,7 @@ and expression_content ppf (ec : expression_content) = option_inline inline expression let_result | E_sequence {expr1;expr2} -> - fprintf ppf "{ %a; %a }" expression expr1 expression expr2 + fprintf ppf "{ %a; @. %a}" expression expr1 expression expr2 | E_ascription {anno_expr; type_annotation} -> fprintf ppf "%a : %a" expression anno_expr type_expression type_annotation | E_skip -> diff --git a/src/stages/2-ast_sugar/combinators.ml b/src/stages/2-ast_sugar/combinators.ml index f53344c37..d5099ab28 100644 --- a/src/stages/2-ast_sugar/combinators.ml +++ b/src/stages/2-ast_sugar/combinators.ml @@ -107,33 +107,27 @@ let e_bytes_raw ?loc (b: bytes) : expression = make_expr ?loc @@ E_literal (Literal_bytes b) let e_bytes_string ?loc (s: string) : expression = make_expr ?loc @@ E_literal (Literal_bytes (Hex.to_bytes (Hex.of_string s))) -let e_big_map ?loc lst : expression = make_expr ?loc @@ E_big_map lst let e_some ?loc s : expression = make_expr ?loc @@ E_constant {cons_name = C_SOME; arguments = [s]} let e_none ?loc () : expression = make_expr ?loc @@ E_constant {cons_name = C_NONE; arguments = []} -let e_string_cat ?loc sl sr : expression = make_expr ?loc @@ E_constant {cons_name = C_CONCAT; arguments = [sl ; sr ]} -let e_map_add ?loc k v old : expression = make_expr ?loc @@ E_constant {cons_name = C_MAP_ADD; arguments = [k ; v ; old]} -let e_map ?loc lst : expression = make_expr ?loc @@ E_map lst -let e_set ?loc lst : expression = make_expr ?loc @@ E_set lst -let e_list ?loc lst : expression = make_expr ?loc @@ E_list lst let e_constructor ?loc s a : expression = make_expr ?loc @@ E_constructor { constructor = Constructor s; element = a} let e_matching ?loc a b : expression = make_expr ?loc @@ E_matching {matchee=a;cases=b} let e_matching_bool ?loc a b c : expression = e_matching ?loc a (Match_bool {match_true = b ; match_false = c}) -let e_accessor ?loc a b = make_expr ?loc @@ E_record_accessor {expr = a; label= Label b} -let e_accessor_list ?loc a b = List.fold_left (fun a b -> e_accessor ?loc a b) a b let e_variable ?loc v = make_expr ?loc @@ E_variable v -let e_skip ?loc () = make_expr ?loc @@ E_skip let e_let_in ?loc (binder, ascr) mut inline rhs let_result = make_expr ?loc @@ E_let_in { let_binder = (binder, ascr) ; rhs ; let_result; inline; mut } -let e_annotation ?loc anno_expr ty = make_expr ?loc @@ E_ascription {anno_expr; type_annotation = ty} let e_application ?loc a b = make_expr ?loc @@ E_application {lamb=a ; args=b} -let e_binop ?loc name a b = make_expr ?loc @@ E_constant {cons_name = name ; arguments = [a ; b]} let e_constant ?loc name lst = make_expr ?loc @@ E_constant {cons_name=name ; arguments = lst} -let e_look_up ?loc x y = make_expr ?loc @@ E_look_up (x , y) + +let e_annotation ?loc anno_expr ty = make_expr ?loc @@ E_ascription {anno_expr; type_annotation = ty} + let e_sequence ?loc expr1 expr2 = make_expr ?loc @@ E_sequence {expr1; expr2} -let e_cond ?loc expr match_true match_false = e_matching expr ?loc (Match_bool {match_true; match_false}) -(* -let e_assign ?loc a b c = location_wrap ?loc @@ E_assign (Var.of_name a , b , c) (* TODO handlethat*) -*) +let e_skip ?loc () = make_expr ?loc @@ E_skip + +let e_list ?loc lst : expression = make_expr ?loc @@ E_list lst +let e_set ?loc lst : expression = make_expr ?loc @@ E_set lst +let e_map ?loc lst : expression = make_expr ?loc @@ E_map lst +let e_big_map ?loc lst : expression = make_expr ?loc @@ E_big_map lst + let ez_match_variant (lst : ((string * string) * 'a) list) = let lst = List.map (fun ((c,n),a) -> ((Constructor c, Var.of_name n), a) ) lst in Match_variant (lst,()) @@ -145,19 +139,20 @@ let e_record_ez ?loc (lst : (string * expr) list) : expression = let e_record ?loc map = let lst = Map.String.to_kv_list map in e_record_ez ?loc lst +let e_record_accessor ?loc a b = make_expr ?loc @@ E_record_accessor {record = a; label= Label b} -let e_update ?loc record path update = +let e_record_update ?loc record path update = let path = Label path in make_expr ?loc @@ E_record_update {record; path; update} -let e_tuple ?loc lst : expression = e_record_ez ?loc (tuple_to_record lst) -let e_pair ?loc a b : expression = e_tuple ?loc [a;b] - let make_option_typed ?loc e t_opt = match t_opt with | None -> e | Some t -> e_annotation ?loc e t +let e_cond ?loc expr match_true match_false = e_matching expr ?loc (Match_bool {match_true; match_false}) +let e_tuple ?loc lst : expression = e_record_ez ?loc (tuple_to_record lst) +let e_pair ?loc a b : expression = e_tuple ?loc [a;b] let e_typed_none ?loc t_opt = let type_annotation = t_option t_opt in @@ -185,25 +180,9 @@ let e_lambda ?loc (binder : expression_variable) } let e_recursive ?loc fun_name fun_type lambda = make_expr ?loc @@ E_recursive {fun_name; fun_type; lambda} - -let e_assign_with_let ?loc var access_path expr = - let var = Var.of_name (var) in - match access_path with - | [] -> (var, None), true, expr, false - - | lst -> - let rec aux path record= match path with - | [] -> failwith "acces_path cannot be empty" - | [e] -> e_update ?loc record e expr - | elem::tail -> - let next_record = e_accessor record elem in - e_update ?loc record elem (aux tail next_record ) - in - (var, None), true, (aux lst (e_variable var)), false - let get_e_accessor = fun t -> match t with - | E_record_accessor {expr; label} -> ok (expr , label) + | E_record_accessor {record; label} -> ok (record , label) | _ -> simple_fail "not an accessor" let assert_e_accessor = fun t -> diff --git a/src/stages/2-ast_sugar/combinators.mli b/src/stages/2-ast_sugar/combinators.mli index ca2f2d552..d790512f6 100644 --- a/src/stages/2-ast_sugar/combinators.mli +++ b/src/stages/2-ast_sugar/combinators.mli @@ -65,34 +65,34 @@ val e'_bytes : string -> expression_content result val e_bytes_hex : ?loc:Location.t -> string -> expression result val e_bytes_raw : ?loc:Location.t -> bytes -> expression val e_bytes_string : ?loc:Location.t -> string -> expression -val e_big_map : ?loc:Location.t -> ( expr * expr ) list -> expression - -val e_record_ez : ?loc:Location.t -> ( string * expr ) list -> expression -val e_tuple : ?loc:Location.t -> expression list -> expression val e_some : ?loc:Location.t -> expression -> expression val e_none : ?loc:Location.t -> unit -> expression -val e_string_cat : ?loc:Location.t -> expression -> expression -> expression -val e_map_add : ?loc:Location.t -> expression -> expression -> expression -> expression -val e_map : ?loc:Location.t -> ( expression * expression ) list -> expression -val e_set : ?loc:Location.t -> expression list -> expression -val e_list : ?loc:Location.t -> expression list -> expression -val e_pair : ?loc:Location.t -> expression -> expression -> expression + +val e_variable : ?loc:Location.t -> expression_variable -> expression val e_constructor : ?loc:Location.t -> string -> expression -> expression +val e_constant : ?loc:Location.t -> constant' -> expression list -> expression + +val e_lambda : ?loc:Location.t -> expression_variable -> type_expression option -> type_expression option -> expression -> expression +val e_application : ?loc:Location.t -> expression -> expression -> expression +val e_recursive : ?loc:Location.t -> expression_variable -> type_expression -> lambda -> expression +val e_let_in : ?loc:Location.t -> ( expression_variable * type_expression option ) -> bool -> bool -> expression -> expression -> expression + +val e_record : ?loc:Location.t -> expr Map.String.t -> expression +val e_record_update : ?loc:Location.t -> expression -> string -> expression -> expression +val e_record_accessor : ?loc:Location.t -> expression -> string -> expression + +val e_annotation : ?loc:Location.t -> expression -> type_expression -> expression + +val e_sequence : ?loc:Location.t -> expression -> expression -> expression +val e_skip : ?loc:Location.t -> unit -> expression + +val e_list : ?loc:Location.t -> expression list -> expression +val e_set : ?loc:Location.t -> expression list -> expression +val e_map : ?loc:Location.t -> ( expression * expression ) list -> expression +val e_big_map : ?loc:Location.t -> ( expr * expr ) list -> expression + val e_matching : ?loc:Location.t -> expression -> matching_expr -> expression val e_matching_bool : ?loc:Location.t -> expression -> expression -> expression -> expression -val e_accessor : ?loc:Location.t -> expression -> string -> expression -val e_accessor_list : ?loc:Location.t -> expression -> string list -> expression -val e_variable : ?loc:Location.t -> expression_variable -> expression -val e_skip : ?loc:Location.t -> unit -> expression -val e_sequence : ?loc:Location.t -> expression -> expression -> expression -val e_cond: ?loc:Location.t -> expression -> expression -> expression -> expression -val e_let_in : ?loc:Location.t -> ( expression_variable * type_expression option ) -> bool -> bool -> expression -> expression -> expression -val e_annotation : ?loc:Location.t -> expression -> type_expression -> expression -val e_application : ?loc:Location.t -> expression -> expression -> expression -val e_binop : ?loc:Location.t -> constant' -> expression -> expression -> expression -val e_constant : ?loc:Location.t -> constant' -> expression list -> expression -val e_look_up : ?loc:Location.t -> expression -> expression -> expression -val ez_match_variant : ((string * string ) * 'a ) list -> ('a,unit) matching_content val e_matching_variant : ?loc:Location.t -> expression -> ((string * string) * expression) list -> expression val make_option_typed : ?loc:Location.t -> expression -> type_expression option -> expression @@ -103,15 +103,12 @@ val e_typed_list : ?loc:Location.t -> expression list -> type_expression -> expr val e_typed_map : ?loc:Location.t -> ( expression * expression ) list -> type_expression -> type_expression -> expression val e_typed_big_map : ?loc:Location.t -> ( expression * expression ) list -> type_expression -> type_expression -> expression - val e_typed_set : ?loc:Location.t -> expression list -> type_expression -> expression -val e_lambda : ?loc:Location.t -> expression_variable -> type_expression option -> type_expression option -> expression -> expression -val e_recursive : ?loc:Location.t -> expression_variable -> type_expression -> lambda -> expression -val e_record : ?loc:Location.t -> expr Map.String.t -> expression -val e_update : ?loc:Location.t -> expression -> string -> expression -> expression -val e_assign_with_let : ?loc:Location.t -> string -> string list -> expression -> ((expression_variable*type_expression option)*bool*expression*bool) - +val e_record_ez : ?loc:Location.t -> (string * expression) list -> expression +val e_cond: ?loc:Location.t -> expression -> expression -> expression -> expression +val e_tuple : ?loc:Location.t -> expression list -> expression +val e_pair : ?loc:Location.t -> expression -> expression -> expression (* val get_e_accessor : expression' -> ( expression * access_path ) result *) diff --git a/src/stages/2-ast_sugar/types.ml b/src/stages/2-ast_sugar/types.ml index 658d30e35..5ad052e46 100644 --- a/src/stages/2-ast_sugar/types.ml +++ b/src/stages/2-ast_sugar/types.ml @@ -86,7 +86,7 @@ and let_in = { and constructor = {constructor: constructor'; element: expression} -and accessor = {expr: expression; label: label} +and accessor = {record: expression; label: label} and update = {record: expression; path: label ; update: expression} diff --git a/src/stages/3-ast_core/PP.ml b/src/stages/3-ast_core/PP.ml index 3410a96fb..0d648065e 100644 --- a/src/stages/3-ast_core/PP.ml +++ b/src/stages/3-ast_core/PP.ml @@ -28,7 +28,7 @@ and expression_content ppf (ec : expression_content) = | E_record m -> fprintf ppf "%a" (tuple_or_record_sep_expr expression) m | E_record_accessor ra -> - fprintf ppf "%a.%a" expression ra.expr label ra.label + fprintf ppf "%a.%a" expression ra.record label ra.label | E_record_update {record; path; update} -> fprintf ppf "{ %a with { %a = %a } }" expression record label path expression update | E_map m -> diff --git a/src/stages/3-ast_core/combinators.ml b/src/stages/3-ast_core/combinators.ml index db2417902..90d1cebd8 100644 --- a/src/stages/3-ast_core/combinators.ml +++ b/src/stages/3-ast_core/combinators.ml @@ -118,7 +118,7 @@ let e_list ?loc lst : expression = make_expr ?loc @@ E_list lst let e_constructor ?loc s a : expression = make_expr ?loc @@ E_constructor { constructor = Constructor s; element = a} let e_matching ?loc a b : expression = make_expr ?loc @@ E_matching {matchee=a;cases=b} let e_matching_bool ?loc a b c : expression = e_matching ?loc a (Match_bool {match_true = b ; match_false = c}) -let e_accessor ?loc a b = make_expr ?loc @@ E_record_accessor {expr = a; label= Label b} +let e_accessor ?loc a b = make_expr ?loc @@ E_record_accessor {record = a; label= Label b} let e_accessor_list ?loc a b = List.fold_left (fun a b -> e_accessor ?loc a b) a b let e_variable ?loc v = make_expr ?loc @@ E_variable v let e_let_in ?loc (binder, ascr) inline rhs let_result = @@ -201,7 +201,7 @@ let e_assign_with_let ?loc var access_path expr = let get_e_accessor = fun t -> match t with - | E_record_accessor {expr; label} -> ok (expr , label) + | E_record_accessor {record; label} -> ok (record , label) | _ -> simple_fail "not an accessor" let assert_e_accessor = fun t -> diff --git a/src/stages/3-ast_core/types.ml b/src/stages/3-ast_core/types.ml index 2b8f0dbc2..81ba90fa2 100644 --- a/src/stages/3-ast_core/types.ml +++ b/src/stages/3-ast_core/types.ml @@ -82,7 +82,7 @@ and let_in = and constructor = {constructor: constructor'; element: expression} -and accessor = {expr: expression; label: label} +and accessor = {record: expression; label: label} and update = {record: expression; path: label ; update: expression} diff --git a/src/stages/4-ast_typed/PP.ml b/src/stages/4-ast_typed/PP.ml index 1325d0476..ab655c80b 100644 --- a/src/stages/4-ast_typed/PP.ml +++ b/src/stages/4-ast_typed/PP.ml @@ -29,7 +29,7 @@ and expression_content ppf (ec: expression_content) = | E_record m -> fprintf ppf "%a" (tuple_or_record_sep_expr expression) m | E_record_accessor ra -> - fprintf ppf "%a.%a" expression ra.expr label ra.label + fprintf ppf "%a.%a" expression ra.record label ra.label | E_record_update {record; path; update} -> fprintf ppf "{ %a with { %a = %a } }" expression record label path expression update | E_map m -> diff --git a/src/stages/4-ast_typed/combinators.ml b/src/stages/4-ast_typed/combinators.ml index 014ab8f2f..5abb2a2bc 100644 --- a/src/stages/4-ast_typed/combinators.ml +++ b/src/stages/4-ast_typed/combinators.ml @@ -337,7 +337,7 @@ let get_a_bool (t:expression) = let get_a_record_accessor = fun t -> match t.expression_content with - | E_record_accessor {expr ; label} -> ok (expr , label) + | E_record_accessor {record ; label} -> ok (record , label) | _ -> simple_fail "not an accessor" let get_declaration_by_name : program -> string -> declaration result = fun p name -> diff --git a/src/stages/4-ast_typed/misc.ml b/src/stages/4-ast_typed/misc.ml index 04efcca5f..533520fe7 100644 --- a/src/stages/4-ast_typed/misc.ml +++ b/src/stages/4-ast_typed/misc.ml @@ -209,7 +209,7 @@ module Free_variables = struct | E_application {lamb;args} -> unions @@ List.map self [ lamb ; args ] | E_constructor {element;_} -> self element | E_record m -> unions @@ List.map self @@ LMap.to_list m - | E_record_accessor {expr;_} -> self expr + | E_record_accessor {record;_} -> self record | E_record_update {record; update;_} -> union (self record) @@ self update | E_list lst -> unions @@ List.map self lst | E_set lst -> unions @@ List.map self lst diff --git a/src/stages/4-ast_typed/misc_smart.ml b/src/stages/4-ast_typed/misc_smart.ml index 0040f7d90..1d1110a1c 100644 --- a/src/stages/4-ast_typed/misc_smart.ml +++ b/src/stages/4-ast_typed/misc_smart.ml @@ -70,7 +70,7 @@ module Captured_variables = struct | E_record m -> let%bind lst' = bind_map_list self @@ LMap.to_list m in ok @@ unions lst' - | E_record_accessor {expr;_} -> self expr + | E_record_accessor {record;_} -> self record | E_record_update {record;update;_} -> let%bind r = self record in let%bind e = self update in diff --git a/src/stages/4-ast_typed/types.ml b/src/stages/4-ast_typed/types.ml index fdb24969b..5fbe79554 100644 --- a/src/stages/4-ast_typed/types.ml +++ b/src/stages/4-ast_typed/types.ml @@ -92,7 +92,7 @@ and constructor = { } and accessor = { - expr: expression ; + record: expression ; label: label ; } diff --git a/src/stages/typesystem/misc.ml b/src/stages/typesystem/misc.ml index 4b60cf454..affe9fd37 100644 --- a/src/stages/typesystem/misc.ml +++ b/src/stages/typesystem/misc.ml @@ -182,10 +182,10 @@ module Substitution = struct * let val_ = s_expression ~v ~expr val_ in * ok @@ (key , val_)) aemap in * ok @@ T.E_record aemap *) - | T.E_record_accessor {expr=e;label} -> - let%bind expr = s_expression ~substs e in + | T.E_record_accessor {record=e;label} -> + let%bind record = s_expression ~substs e in let%bind label = s_label ~substs label in - ok @@ T.E_record_accessor {expr;label} + ok @@ T.E_record_accessor {record;label} | T.E_record_update {record;path;update}-> let%bind record = s_expression ~substs record in let%bind update = s_expression ~substs update in diff --git a/src/test/multisig_v2_tests.ml b/src/test/multisig_v2_tests.ml index 8b2b8972b..24912c289 100644 --- a/src/test/multisig_v2_tests.ml +++ b/src/test/multisig_v2_tests.ml @@ -33,7 +33,7 @@ let empty_message = e_lambda (Var.of_name "arguments") empty_op_list let empty_message2 = e_lambda (Var.of_name "arguments") (Some t_bytes) (Some (t_list t_operation)) - ( e_let_in ((Var.of_name "foo"),Some t_unit) false false (e_unit ()) empty_op_list) + ( e_let_in ((Var.of_name "foo"),Some t_unit) false (e_unit ()) empty_op_list) let send_param msg = e_constructor "Send" msg let withdraw_param = e_constructor "Withdraw" empty_message From d91753e4934618726a7f96427b8047b25747791b Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Wulfman Date: Mon, 30 Mar 2020 14:40:28 +0200 Subject: [PATCH 04/13] remove case e_sequance (e_assing, expr) --- src/bin/expect_tests/contract_tests.ml | 4 ++-- src/passes/4-imperative_to_sugar/imperative_to_sugar.ml | 4 ---- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/bin/expect_tests/contract_tests.ml b/src/bin/expect_tests/contract_tests.ml index 3e78f213c..311d96b41 100644 --- a/src/bin/expect_tests/contract_tests.ml +++ b/src/bin/expect_tests/contract_tests.ml @@ -1114,7 +1114,7 @@ let%expect_test _ = let%expect_test _ = run_ligo_bad [ "compile-contract" ; bad_contract "create_contract_toplevel.mligo" ; "main" ] ; [%expect {| -ligo: in file "create_contract_toplevel.mligo", line 4, character 35 to line 8, character 8. No free variable allowed in this lambda: variable 'store' {"expression":"CREATE_CONTRACT(lambda (#P:Some(( nat * string ))) : None return let rhs#701 = #P in let p = rhs#701.0 in let s = rhs#701.1 in ( list[] : (TO_list(operation)) , store ) , NONE() : (TO_option(key_hash)) , 300000000mutez , \"un\")","location":"in file \"create_contract_toplevel.mligo\", line 4, character 35 to line 8, character 8"} +ligo: in file "create_contract_toplevel.mligo", line 4, character 35 to line 8, character 8. No free variable allowed in this lambda: variable 'store' {"expression":"CREATE_CONTRACT(lambda (#P:Some(( nat * string ))) : None return let rhs#702 = #P in let p = rhs#702.0 in let s = rhs#702.1 in ( list[] : (TO_list(operation)) , store ) , NONE() : (TO_option(key_hash)) , 300000000mutez , \"un\")","location":"in file \"create_contract_toplevel.mligo\", line 4, character 35 to line 8, character 8"} If you're not sure how to fix this error, you can @@ -1127,7 +1127,7 @@ ligo: in file "create_contract_toplevel.mligo", line 4, character 35 to line 8, run_ligo_bad [ "compile-contract" ; bad_contract "create_contract_var.mligo" ; "main" ] ; [%expect {| -ligo: in file "create_contract_var.mligo", line 6, character 35 to line 10, character 5. No free variable allowed in this lambda: variable 'a' {"expression":"CREATE_CONTRACT(lambda (#P:Some(( nat * int ))) : None return let rhs#704 = #P in let p = rhs#704.0 in let s = rhs#704.1 in ( list[] : (TO_list(operation)) , a ) , NONE() : (TO_option(key_hash)) , 300000000mutez , 1)","location":"in file \"create_contract_var.mligo\", line 6, character 35 to line 10, character 5"} +ligo: in file "create_contract_var.mligo", line 6, character 35 to line 10, character 5. No free variable allowed in this lambda: variable 'a' {"expression":"CREATE_CONTRACT(lambda (#P:Some(( nat * int ))) : None return let rhs#705 = #P in let p = rhs#705.0 in let s = rhs#705.1 in ( list[] : (TO_list(operation)) , a ) , NONE() : (TO_option(key_hash)) , 300000000mutez , 1)","location":"in file \"create_contract_var.mligo\", line 6, character 35 to line 10, character 5"} If you're not sure how to fix this error, you can diff --git a/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml b/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml index 33c56626b..2ac37fc71 100644 --- a/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml +++ b/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml @@ -231,10 +231,6 @@ let rec compile_expression : I.expression -> O.expression result = let%bind anno_expr = compile_expression anno_expr in let%bind type_annotation = compile_type_expression type_annotation in return @@ O.E_ascription {anno_expr; type_annotation} - | I.E_sequence {expr1={expression_content=I.E_assign expr1;_}; expr2} -> - let%bind expr2 = compile_expression expr2 in - let%bind ret = compile_assign expr1 @@ expr2 in - return @@ ret | I.E_sequence {expr1; expr2} -> let%bind expr1 = compile_expression expr1 in let%bind expr2 = compile_expression expr2 in From c5d9c9ffa37538a9b42a96ec8575b2f0fdc05b7a Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Wulfman Date: Mon, 16 Mar 2020 18:09:12 +0100 Subject: [PATCH 05/13] remove look_up --- src/passes/10-interpreter/interpreter.ml | 3 --- src/passes/10-transpiler/transpiler.ml | 5 ----- src/passes/6-sugar_to_core/sugar_to_core.ml | 4 ++-- src/passes/8-typer-new/typer.ml | 14 -------------- src/passes/8-typer-old/typer.ml | 8 -------- src/passes/9-self_ast_typed/helpers.ml | 11 ----------- src/passes/9-self_ast_typed/tail_recursion.ml | 4 ---- src/stages/3-ast_core/PP.ml | 2 -- src/stages/3-ast_core/combinators.ml | 1 - src/stages/3-ast_core/combinators.mli | 1 - src/stages/3-ast_core/misc.ml | 4 ++-- src/stages/3-ast_core/types.ml | 1 - src/stages/4-ast_typed/PP.ml | 2 -- src/stages/4-ast_typed/misc.ml | 3 +-- src/stages/4-ast_typed/misc_smart.ml | 3 --- src/stages/4-ast_typed/types.ml | 1 - src/stages/typesystem/misc.ml | 4 ---- 17 files changed, 5 insertions(+), 66 deletions(-) diff --git a/src/passes/10-interpreter/interpreter.ml b/src/passes/10-interpreter/interpreter.ml index 43e78312a..77d30cb71 100644 --- a/src/passes/10-interpreter/interpreter.ml +++ b/src/passes/10-interpreter/interpreter.ml @@ -378,9 +378,6 @@ and eval : Ast_typed.expression -> env -> value result ) | E_recursive {fun_name; fun_type=_; lambda} -> ok @@ V_Func_rec (fun_name, lambda.binder, lambda.result, env) - | E_look_up _ -> - let serr = Format.asprintf "Unsupported construct :\n %a\n" Ast_typed.PP.expression term in - simple_fail serr let dummy : Ast_typed.program -> string result = fun prg -> diff --git a/src/passes/10-transpiler/transpiler.ml b/src/passes/10-transpiler/transpiler.ml index b83e84bf6..b7935bbe4 100644 --- a/src/passes/10-transpiler/transpiler.ml +++ b/src/passes/10-transpiler/transpiler.ml @@ -234,7 +234,6 @@ and tree_of_sum : AST.type_expression -> (AST.constructor' * AST.type_expression and transpile_annotated_expression (ae:AST.expression) : expression result = let%bind tv = transpile_type ae.type_expression in let return ?(tv = tv) expr = ok @@ Combinators.Expression.make_tpl (expr, tv) in - let f = transpile_annotated_expression in let info = let title () = "translating expression" in let content () = Format.asprintf "%a" Location.pp ae.location in @@ -439,10 +438,6 @@ and transpile_annotated_expression (ae:AST.expression) : expression result = let init = return @@ E_make_empty_big_map (src, dst) in List.fold_left aux init m ) - | E_look_up dsi -> ( - let%bind (ds', i') = bind_map_pair f dsi in - return @@ E_constant {cons_name=C_MAP_FIND_OPT;arguments=[i' ; ds']} - ) | E_matching {matchee=expr; cases=m} -> ( let%bind expr' = transpile_annotated_expression expr in match m with diff --git a/src/passes/6-sugar_to_core/sugar_to_core.ml b/src/passes/6-sugar_to_core/sugar_to_core.ml index e75b9c2ff..e7a3f20fc 100644 --- a/src/passes/6-sugar_to_core/sugar_to_core.ml +++ b/src/passes/6-sugar_to_core/sugar_to_core.ml @@ -127,8 +127,8 @@ let rec compile_expression : I.expression -> O.expression result = let%bind set = bind_map_list compile_expression set in return @@ O.E_set set | I.E_look_up look_up -> - let%bind look_up = bind_map_pair compile_expression look_up in - return @@ O.E_look_up look_up + let%bind (path, index) = bind_map_pair compile_expression look_up in + return @@ O.E_constant {cons_name=C_MAP_FIND_OPT;arguments=[index;path]} | I.E_ascription {anno_expr; type_annotation} -> let%bind anno_expr = compile_expression anno_expr in let%bind type_annotation = idle_type_expression type_annotation in diff --git a/src/passes/8-typer-new/typer.ml b/src/passes/8-typer-new/typer.ml index f446c1076..94a1682bd 100644 --- a/src/passes/8-typer-new/typer.ml +++ b/src/passes/8-typer-new/typer.ml @@ -685,17 +685,6 @@ and type_expression : environment -> Solver.state -> ?tv_opt:O.type_expression - let wrapped = Wrap.application f'.type_expression args.type_expression in return_wrapped (E_application {lamb=f';args}) state'' wrapped - (* | E_look_up dsi -> - * let%bind (ds, ind) = bind_map_pair (type_expression e) dsi in - * let%bind (src, dst) = bind_map_or (get_t_map , get_t_big_map) ds.type_annotation in - * let%bind _ = O.assert_type_value_eq (ind.type_annotation, src) in - * return (E_look_up (ds , ind)) (t_option dst ()) *) - - | E_look_up dsi -> - let aux' state' elt = type_expression e state' elt >>? swap in - let%bind (state'' , (ds , ind)) = bind_fold_map_pair aux' state dsi in - let wrapped = Wrap.look_up ds.type_expression ind.type_expression in - return_wrapped (E_look_up (ds , ind)) state'' wrapped (* Advanced *) (* | E_matching (ex, m) -> ( @@ -1076,9 +1065,6 @@ let rec untype_expression (e:O.expression) : (I.expression) result = | E_set lst -> let%bind lst' = bind_map_list untype_expression lst in return (e_set lst') - | E_look_up dsi -> - let%bind (a , b) = bind_map_pair untype_expression dsi in - return (e_look_up a b) | E_matching {matchee;cases} -> let%bind ae' = untype_expression matchee in let%bind m' = untype_matching untype_expression cases in diff --git a/src/passes/8-typer-old/typer.ml b/src/passes/8-typer-old/typer.ml index d26828230..2b6764dea 100644 --- a/src/passes/8-typer-old/typer.ml +++ b/src/passes/8-typer-old/typer.ml @@ -703,11 +703,6 @@ and type_expression' : environment -> ?tv_opt:O.type_expression -> I.expression lamb'.location in return (E_application {lamb=lamb'; args=args'}) tv - | E_look_up dsi -> - let%bind (ds, ind) = bind_map_pair (type_expression' e) dsi in - let%bind (src, dst) = bind_map_or (get_t_map , get_t_big_map) ds.type_expression in - let%bind _ = O.assert_type_expression_eq (ind.type_expression, src) in - return (E_look_up (ds , ind)) (t_option dst ()) (* Advanced *) | E_matching {matchee;cases} -> ( let%bind ex' = type_expression' e matchee in @@ -882,9 +877,6 @@ let rec untype_expression (e:O.expression) : (I.expression) result = | E_set lst -> let%bind lst' = bind_map_list untype_expression lst in return (e_set lst') - | E_look_up dsi -> - let%bind (a , b) = bind_map_pair untype_expression dsi in - return (e_look_up a b) | E_matching {matchee;cases} -> let%bind ae' = untype_expression matchee in let%bind m' = untype_matching untype_expression cases in diff --git a/src/passes/9-self_ast_typed/helpers.ml b/src/passes/9-self_ast_typed/helpers.ml index 90a9fe9cc..658a05f31 100644 --- a/src/passes/9-self_ast_typed/helpers.ml +++ b/src/passes/9-self_ast_typed/helpers.ml @@ -16,9 +16,6 @@ let rec fold_expression : 'a folder -> 'a -> expression -> 'a result = fun f ini let%bind res = bind_fold_list (bind_fold_pair self) init' lst in ok res ) - | E_look_up ab -> - let%bind res = bind_fold_pair self init' ab in - ok res | E_application {lamb; args} -> ( let ab = (lamb, args) in let%bind res = bind_fold_pair self init' ab in @@ -109,10 +106,6 @@ let rec map_expression : mapper -> expression -> expression result = fun f e -> let%bind lst' = bind_map_list (bind_map_pair self) lst in return @@ E_big_map lst' ) - | E_look_up ab -> ( - let%bind ab' = bind_map_pair self ab in - return @@ E_look_up ab' - ) | E_matching {matchee=e;cases} -> ( let%bind e' = self e in let%bind cases' = map_cases f cases in @@ -224,10 +217,6 @@ let rec fold_map_expression : 'a fold_mapper -> 'a -> expression -> ('a * expres let%bind (res, lst') = bind_fold_map_list (bind_fold_map_pair self) init' lst in ok (res, return @@ E_big_map lst') ) - | E_look_up ab -> ( - let%bind (res, ab') = bind_fold_map_pair self init' ab in - ok (res, return @@ E_look_up ab') - ) | E_matching {matchee=e;cases} -> ( let%bind (res, e') = self init' e in let%bind (res,cases') = fold_map_cases f res cases in diff --git a/src/passes/9-self_ast_typed/tail_recursion.ml b/src/passes/9-self_ast_typed/tail_recursion.ml index 5e3953bc3..c95f10c06 100644 --- a/src/passes/9-self_ast_typed/tail_recursion.ml +++ b/src/passes/9-self_ast_typed/tail_recursion.ml @@ -67,10 +67,6 @@ let rec check_recursive_call : expression_variable -> bool -> expression -> unit | E_list el | E_set el -> let%bind _ = bind_map_list (check_recursive_call n false) el in ok () - | E_look_up (e1,e2) -> - let%bind _ = check_recursive_call n false e1 in - let%bind _ = check_recursive_call n false e2 in - ok () and check_recursive_call_in_matching = fun n final_path c -> match c with diff --git a/src/stages/3-ast_core/PP.ml b/src/stages/3-ast_core/PP.ml index 0d648065e..9673beef7 100644 --- a/src/stages/3-ast_core/PP.ml +++ b/src/stages/3-ast_core/PP.ml @@ -39,8 +39,6 @@ and expression_content ppf (ec : expression_content) = fprintf ppf "list[%a]" (list_sep_d expression) lst | E_set lst -> fprintf ppf "set[%a]" (list_sep_d expression) lst - | E_look_up (ds, ind) -> - fprintf ppf "(%a)[%a]" expression ds expression ind | E_lambda {binder; input_type; output_type; result} -> fprintf ppf "lambda (%a:%a) : %a return %a" expression_variable binder diff --git a/src/stages/3-ast_core/combinators.ml b/src/stages/3-ast_core/combinators.ml index 90d1cebd8..91a77d217 100644 --- a/src/stages/3-ast_core/combinators.ml +++ b/src/stages/3-ast_core/combinators.ml @@ -127,7 +127,6 @@ let e_annotation ?loc anno_expr ty = make_expr ?loc @@ E_ascription {anno_expr; let e_application ?loc a b = make_expr ?loc @@ E_application {lamb=a ; args=b} let e_binop ?loc name a b = make_expr ?loc @@ E_constant {cons_name = name ; arguments = [a ; b]} let e_constant ?loc name lst = make_expr ?loc @@ E_constant {cons_name=name ; arguments = lst} -let e_look_up ?loc x y = make_expr ?loc @@ E_look_up (x , y) let e_cond ?loc expr match_true match_false = e_matching expr ?loc (Match_bool {match_true; match_false}) (* let e_assign ?loc a b c = location_wrap ?loc @@ E_assign (Var.of_name a , b , c) (* TODO handlethat*) diff --git a/src/stages/3-ast_core/combinators.mli b/src/stages/3-ast_core/combinators.mli index ae0aab1bd..a16a36600 100644 --- a/src/stages/3-ast_core/combinators.mli +++ b/src/stages/3-ast_core/combinators.mli @@ -89,7 +89,6 @@ val e_annotation : ?loc:Location.t -> expression -> type_expression -> expressio val e_application : ?loc:Location.t -> expression -> expression -> expression val e_binop : ?loc:Location.t -> constant' -> expression -> expression -> expression val e_constant : ?loc:Location.t -> constant' -> expression list -> expression -val e_look_up : ?loc:Location.t -> expression -> expression -> expression val ez_match_variant : ((string * string ) * 'a ) list -> ('a,unit) matching_content val e_matching_variant : ?loc:Location.t -> expression -> ((string * string) * expression) list -> expression diff --git a/src/stages/3-ast_core/misc.ml b/src/stages/3-ast_core/misc.ml index a09efa475..96a9f3750 100644 --- a/src/stages/3-ast_core/misc.ml +++ b/src/stages/3-ast_core/misc.ml @@ -183,8 +183,8 @@ let rec assert_value_eq (a, b: (expression * expression )) : unit result = | (E_variable _, _) | (E_lambda _, _) | (E_application _, _) | (E_let_in _, _) | (E_recursive _,_) | (E_record_accessor _, _) - | (E_look_up _, _) | (E_matching _, _) - -> simple_fail "comparing not a value" + | (E_matching _, _) + -> simple_fail "comparing not a value" let is_value_eq (a , b) = to_bool @@ assert_value_eq (a , b) diff --git a/src/stages/3-ast_core/types.ml b/src/stages/3-ast_core/types.ml index 81ba90fa2..4ad14d148 100644 --- a/src/stages/3-ast_core/types.ml +++ b/src/stages/3-ast_core/types.ml @@ -49,7 +49,6 @@ and expression_content = | E_big_map of (expression * expression) list (*move to operator *) | E_list of expression list | E_set of expression list - | E_look_up of (expression * expression) (* Advanced *) | E_ascription of ascription diff --git a/src/stages/4-ast_typed/PP.ml b/src/stages/4-ast_typed/PP.ml index ab655c80b..3fe4f02c8 100644 --- a/src/stages/4-ast_typed/PP.ml +++ b/src/stages/4-ast_typed/PP.ml @@ -40,8 +40,6 @@ and expression_content ppf (ec: expression_content) = fprintf ppf "list[%a]" (list_sep_d expression) lst | E_set lst -> fprintf ppf "set[%a]" (list_sep_d expression) lst - | E_look_up (ds, ind) -> - fprintf ppf "(%a)[%a]" expression ds expression ind | E_lambda {binder; result} -> fprintf ppf "lambda (%a) return %a" expression_variable binder expression result diff --git a/src/stages/4-ast_typed/misc.ml b/src/stages/4-ast_typed/misc.ml index 533520fe7..5def088a8 100644 --- a/src/stages/4-ast_typed/misc.ml +++ b/src/stages/4-ast_typed/misc.ml @@ -214,7 +214,6 @@ module Free_variables = struct | E_list lst -> unions @@ List.map self lst | E_set lst -> unions @@ List.map self lst | (E_map m | E_big_map m) -> unions @@ List.map self @@ List.concat @@ List.map (fun (a, b) -> [ a ; b ]) m - | E_look_up (a , b) -> unions @@ List.map self [ a ; b ] | E_matching {matchee; cases;_} -> union (self matchee) (matching_expression b cases) | E_let_in { let_binder; rhs; let_result; _} -> let b' = union (singleton let_binder) b in @@ -534,7 +533,7 @@ let rec assert_value_eq (a, b: (expression*expression)) : unit result = | (E_literal _, _) | (E_variable _, _) | (E_application _, _) | (E_lambda _, _) | (E_let_in _, _) | (E_recursive _, _) | (E_record_accessor _, _) | (E_record_update _,_) - | (E_look_up _, _) | (E_matching _, _) + | (E_matching _, _) -> fail @@ error_uncomparable_values "can't compare sequences nor loops" a b let merge_annotation (a:type_expression option) (b:type_expression option) err : type_expression result = diff --git a/src/stages/4-ast_typed/misc_smart.ml b/src/stages/4-ast_typed/misc_smart.ml index 1d1110a1c..763bfa399 100644 --- a/src/stages/4-ast_typed/misc_smart.ml +++ b/src/stages/4-ast_typed/misc_smart.ml @@ -84,9 +84,6 @@ module Captured_variables = struct | (E_map m | E_big_map m) -> let%bind lst' = bind_map_list self @@ List.concat @@ List.map (fun (a, b) -> [ a ; b ]) m in ok @@ unions lst' - | E_look_up (a , b) -> - let%bind lst' = bind_map_list self [ a ; b ] in - ok @@ unions lst' | E_matching {matchee;cases;_} -> let%bind a' = self matchee in let%bind cs' = matching_expression b cases in diff --git a/src/stages/4-ast_typed/types.ml b/src/stages/4-ast_typed/types.ml index 5fbe79554..fa303e21c 100644 --- a/src/stages/4-ast_typed/types.ml +++ b/src/stages/4-ast_typed/types.ml @@ -55,7 +55,6 @@ and expression_content = | E_big_map of (expression * expression) list (*move to operator *) | E_list of expression list | E_set of expression list - | E_look_up of (expression * expression) and constant = { cons_name: constant' diff --git a/src/stages/typesystem/misc.ml b/src/stages/typesystem/misc.ml index affe9fd37..c74e9e8f1 100644 --- a/src/stages/typesystem/misc.ml +++ b/src/stages/typesystem/misc.ml @@ -210,10 +210,6 @@ module Substitution = struct | T.E_set vals -> let%bind vals = bind_map_list (s_expression ~substs) vals in ok @@ T.E_set vals - | T.E_look_up (val1, val2) -> - let%bind val1 = s_expression ~substs val1 in - let%bind val2 = s_expression ~substs val2 in - ok @@ T.E_look_up (val1 , val2) | T.E_matching {matchee;cases} -> let%bind matchee = s_expression ~substs matchee in let%bind cases = s_matching_expr ~substs cases in From a39c900b72f9753bc770e127621c79734c72fb87 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Wulfman Date: Mon, 16 Mar 2020 18:34:43 +0100 Subject: [PATCH 06/13] remove set and list --- src/bin/expect_tests/contract_tests.ml | 124 ++++++++++-------- src/bin/expect_tests/typer_error_tests.ml | 2 +- src/passes/10-interpreter/interpreter.ml | 13 +- src/passes/10-transpiler/transpiler.ml | 20 --- src/passes/10-transpiler/untranspiler.ml | 11 +- src/passes/11-self_mini_c/helpers.ml | 8 +- src/passes/11-self_mini_c/self_mini_c.ml | 2 - src/passes/11-self_mini_c/subst.ml | 6 +- src/passes/12-compiler/compiler_program.ml | 13 +- src/passes/6-sugar_to_core/sugar_to_core.ml | 27 ++-- src/passes/8-typer-new/typer.ml | 17 --- src/passes/8-typer-old/typer.ml | 63 +++------ src/passes/9-self_ast_typed/helpers.ml | 18 +-- src/passes/9-self_ast_typed/tail_recursion.ml | 3 - src/passes/operators/operators.ml | 8 +- src/stages/1-ast_imperative/combinators.ml | 5 +- src/stages/1-ast_imperative/combinators.mli | 1 + src/stages/3-ast_core/PP.ml | 4 - src/stages/3-ast_core/combinators.ml | 25 ++-- src/stages/3-ast_core/combinators.mli | 8 -- src/stages/3-ast_core/misc.ml | 35 ++--- src/stages/3-ast_core/types.ml | 2 - src/stages/4-ast_typed/PP.ml | 4 - src/stages/4-ast_typed/combinators.ml | 2 - src/stages/4-ast_typed/combinators.mli | 2 - .../4-ast_typed/combinators_environment.ml | 1 - .../4-ast_typed/combinators_environment.mli | 1 - src/stages/4-ast_typed/misc.ml | 20 --- src/stages/4-ast_typed/misc_smart.ml | 6 - src/stages/4-ast_typed/types.ml | 2 - src/stages/5-mini_c/PP.ml | 4 +- src/stages/5-mini_c/misc.ml | 2 - src/stages/5-mini_c/types.ml | 2 - src/stages/common/PP.ml | 2 + src/stages/common/types.ml | 2 + src/stages/typesystem/misc.ml | 6 - 36 files changed, 157 insertions(+), 314 deletions(-) diff --git a/src/bin/expect_tests/contract_tests.ml b/src/bin/expect_tests/contract_tests.ml index 311d96b41..913a89352 100644 --- a/src/bin/expect_tests/contract_tests.ml +++ b/src/bin/expect_tests/contract_tests.ml @@ -7,13 +7,13 @@ let bad_contract basename = let%expect_test _ = run_ligo_good [ "measure-contract" ; contract "coase.ligo" ; "main" ] ; - [%expect {| 1870 bytes |}] ; + [%expect {| 1872 bytes |}] ; run_ligo_good [ "measure-contract" ; contract "multisig.ligo" ; "main" ] ; [%expect {| 1294 bytes |}] ; run_ligo_good [ "measure-contract" ; contract "multisig-v2.ligo" ; "main" ] ; - [%expect {| 2935 bytes |}] ; + [%expect {| 3268 bytes |}] ; run_ligo_good [ "measure-contract" ; contract "vote.mligo" ; "main" ] ; [%expect {| 589 bytes |}] ; @@ -227,16 +227,17 @@ let%expect_test _ = NIL operation ; SWAP ; CONS ; - DIP { DIP 4 { DUP } ; - DIG 4 ; - DIP 4 { DUP } ; - DIG 4 ; + DUP ; + DIP { DIP 5 { DUP } ; + DIG 5 ; + DIP 5 { DUP } ; + DIG 5 ; DIP { DUP ; CDR ; SWAP ; CAR ; CAR } ; SWAP ; PAIR ; PAIR } ; PAIR ; - DIP { DROP 13 } } ; + DIP { DROP 14 } } ; DIP { DROP } } { DUP ; DIP { DIP { DUP } ; SWAP } ; @@ -513,19 +514,19 @@ let%expect_test _ = GT ; IF { PUSH string "Message size exceed maximum limit" ; FAILWITH } { PUSH unit Unit } ; - DIP 4 { DUP } ; - DIG 4 ; EMPTY_SET address ; + DUP ; + DIP { DIP 5 { DUP } ; DIG 5 } ; PAIR ; - DIP 2 { DUP } ; - DIG 2 ; - DIP { DIP 5 { DUP } ; DIG 5 ; CAR ; CDR ; CDR } ; + DIP 3 { DUP } ; + DIG 3 ; + DIP { DIP 6 { DUP } ; DIG 6 ; CAR ; CDR ; CDR } ; GET ; IF_NONE - { DIP 5 { DUP } ; - DIG 5 ; - DIP 6 { DUP } ; + { DIP 6 { DUP } ; DIG 6 ; + DIP 7 { DUP } ; + DIG 7 ; CDR ; CAR ; CAR ; @@ -535,7 +536,7 @@ let%expect_test _ = PUSH nat 1 ; ADD ; SOME ; - DIP { DIP 6 { DUP } ; DIG 6 ; CDR ; CAR ; CAR } ; + DIP { DIP 7 { DUP } ; DIG 7 ; CDR ; CAR ; CAR } ; SENDER ; UPDATE ; DIP { DUP ; CAR ; SWAP ; CDR ; DUP ; CDR ; SWAP ; CAR ; CDR } ; @@ -543,31 +544,33 @@ let%expect_test _ = PAIR ; SWAP ; PAIR ; - DIP { DUP } ; - SWAP ; - CAR ; - DIP { DUP } ; - PAIR ; EMPTY_SET address ; PUSH bool True ; SENDER ; UPDATE ; + DIP 2 { DUP } ; + DIG 2 ; + DIP 2 { DUP } ; + DIG 2 ; SWAP ; + CAR ; + PAIR ; CDR ; + DIP { DUP } ; SWAP ; PAIR ; - DIP { DROP } } - { DIP 6 { DUP } ; - DIG 6 ; + DIP { DROP 2 } } + { DIP 7 { DUP } ; + DIG 7 ; DIP { DUP } ; SWAP ; SENDER ; MEM ; IF { DUP } - { DIP 7 { DUP } ; - DIG 7 ; - DIP 8 { DUP } ; + { DIP 8 { DUP } ; DIG 8 ; + DIP 9 { DUP } ; + DIG 9 ; CDR ; CAR ; CAR ; @@ -577,7 +580,7 @@ let%expect_test _ = PUSH nat 1 ; ADD ; SOME ; - DIP { DIP 8 { DUP } ; DIG 8 ; CDR ; CAR ; CAR } ; + DIP { DIP 9 { DUP } ; DIG 9 ; CDR ; CAR ; CAR } ; SENDER ; UPDATE ; DIP { DUP ; CAR ; SWAP ; CDR ; DUP ; CDR ; SWAP ; CAR ; CDR } ; @@ -623,21 +626,26 @@ let%expect_test _ = GT ; IF { PUSH string "Maximum number of proposal reached" ; FAILWITH } { PUSH unit Unit } ; - DIP 2 { DUP } ; - DIG 2 ; NIL operation ; + DIP 10 { DUP } ; + DIG 10 ; + DIP { DIP 4 { DUP } ; DIG 4 } ; PAIR ; - DIP 4 { DUP } ; - DIG 4 ; + DIP { DIP 9 { DUP } ; DIG 9 ; DIP { DUP } ; PAIR } ; + PAIR ; + DIP { DIP 3 { DUP } ; DIG 3 } ; + PAIR ; + DIP 5 { DUP } ; + DIG 5 ; SIZE ; - DIP { DIP 3 { DUP } ; DIG 3 ; CDR ; CDR } ; + DIP { DIP 4 { DUP } ; DIG 4 ; CDR ; CDR } ; COMPARE ; GE ; - IF { DIP 3 { DUP } ; - DIG 3 ; - DIP 9 { DUP } ; - DIG 9 ; - DIP { DIP 4 { DUP } ; DIG 4 ; CAR ; CDR ; CDR ; NONE (set address) } ; + IF { DIP 4 { DUP } ; + DIG 4 ; + DIP 11 { DUP } ; + DIG 11 ; + DIP { DIP 5 { DUP } ; DIG 5 ; CAR ; CDR ; CDR ; NONE (set address) } ; UPDATE ; DIP { DUP ; CDR ; SWAP ; CAR ; DUP ; CAR ; SWAP ; CDR ; CAR } ; SWAP ; @@ -649,7 +657,7 @@ let%expect_test _ = CDR ; CAR ; CDR ; - DIP { DIP 10 { DUP } ; DIG 10 } ; + DIP { DIP 12 { DUP } ; DIG 12 } ; EXEC ; DIP { DUP } ; SWAP ; @@ -658,7 +666,7 @@ let%expect_test _ = CDR ; CAR ; CDR ; - DIP { DIP 11 { DUP } ; DIG 11 } ; + DIP { DIP 13 { DUP } ; DIG 13 } ; CONCAT ; SHA256 ; DIP { DUP ; CAR ; SWAP ; CDR ; DUP ; CDR ; SWAP ; CAR ; CAR } ; @@ -683,8 +691,12 @@ let%expect_test _ = DIP { DUP } ; SWAP ; DIP { DUP } ; - SWAP ; - DIP { DIP 11 { DUP } ; DIG 11 } ; + PAIR ; + DIP { DIP 2 { DUP } ; DIG 2 } ; + PAIR ; + DIP 2 { DUP } ; + DIG 2 ; + DIP { DIP 13 { DUP } ; DIG 13 } ; MEM ; IF { DIP 2 { DUP } ; DIG 2 ; @@ -746,14 +758,14 @@ let%expect_test _ = PAIR ; DIP { DROP 4 } } { DUP ; - DIP 4 { DUP } ; - DIG 4 ; - DIP 10 { DUP } ; - DIG 10 ; - DIP { DIP 6 { DUP } ; - DIG 6 ; + DIP 5 { DUP } ; + DIG 5 ; + DIP 12 { DUP } ; + DIG 12 ; + DIP { DIP 7 { DUP } ; + DIG 7 ; SOME ; - DIP { DIP 5 { DUP } ; DIG 5 ; CAR ; CDR ; CDR } } ; + DIP { DIP 6 { DUP } ; DIG 6 ; CAR ; CDR ; CDR } } ; UPDATE ; DIP { DUP ; CDR ; SWAP ; CAR ; DUP ; CAR ; SWAP ; CDR ; CAR } ; SWAP ; @@ -768,7 +780,7 @@ let%expect_test _ = CAR ; DIP { DUP ; CDR } ; PAIR ; - DIP { DROP 15 } } ; + DIP { DROP 17 } } ; DIP { DROP } } { DUP ; DIP { DIP { DUP } ; SWAP } ; @@ -1033,11 +1045,11 @@ let%expect_test _ = let%expect_test _ = run_ligo_good [ "dry-run" ; contract "redeclaration.ligo" ; "main" ; "unit" ; "0" ] ; - [%expect {|( list[] , 0 ) |}] + [%expect {|( LIST_EMPTY() , 0 ) |}] let%expect_test _ = run_ligo_good [ "dry-run" ; contract "double_main.ligo" ; "main" ; "unit" ; "0" ] ; - [%expect {|( list[] , 2 ) |}] + [%expect {|( LIST_EMPTY() , 2 ) |}] let%expect_test _ = run_ligo_good [ "compile-contract" ; contract "subtle_nontail_fail.mligo" ; "main" ] ; @@ -1095,7 +1107,7 @@ let%expect_test _ = let%expect_test _ = run_ligo_good [ "dry-run" ; contract "super-counter.mligo" ; "main" ; "test_param" ; "test_storage" ] ; [%expect {| - ( list[] , 3 ) |}] + ( LIST_EMPTY() , 3 ) |}] let%expect_test _ = run_ligo_bad [ "compile-contract" ; bad_contract "redundant_constructors.mligo" ; "main" ] ; @@ -1114,7 +1126,7 @@ let%expect_test _ = let%expect_test _ = run_ligo_bad [ "compile-contract" ; bad_contract "create_contract_toplevel.mligo" ; "main" ] ; [%expect {| -ligo: in file "create_contract_toplevel.mligo", line 4, character 35 to line 8, character 8. No free variable allowed in this lambda: variable 'store' {"expression":"CREATE_CONTRACT(lambda (#P:Some(( nat * string ))) : None return let rhs#702 = #P in let p = rhs#702.0 in let s = rhs#702.1 in ( list[] : (TO_list(operation)) , store ) , NONE() : (TO_option(key_hash)) , 300000000mutez , \"un\")","location":"in file \"create_contract_toplevel.mligo\", line 4, character 35 to line 8, character 8"} +ligo: in file "create_contract_toplevel.mligo", line 4, character 35 to line 8, character 8. No free variable allowed in this lambda: variable 'store' {"expression":"CREATE_CONTRACT(lambda (#P:Some(( nat * string ))) : None return let rhs#654 = #P in let p = rhs#654.0 in let s = rhs#654.1 in ( LIST_EMPTY() : (TO_list(operation)) , store ) , NONE() : (TO_option(key_hash)) , 300000000mutez , \"un\")","location":"in file \"create_contract_toplevel.mligo\", line 4, character 35 to line 8, character 8"} If you're not sure how to fix this error, you can @@ -1127,7 +1139,7 @@ ligo: in file "create_contract_toplevel.mligo", line 4, character 35 to line 8, run_ligo_bad [ "compile-contract" ; bad_contract "create_contract_var.mligo" ; "main" ] ; [%expect {| -ligo: in file "create_contract_var.mligo", line 6, character 35 to line 10, character 5. No free variable allowed in this lambda: variable 'a' {"expression":"CREATE_CONTRACT(lambda (#P:Some(( nat * int ))) : None return let rhs#705 = #P in let p = rhs#705.0 in let s = rhs#705.1 in ( list[] : (TO_list(operation)) , a ) , NONE() : (TO_option(key_hash)) , 300000000mutez , 1)","location":"in file \"create_contract_var.mligo\", line 6, character 35 to line 10, character 5"} +ligo: in file "create_contract_var.mligo", line 6, character 35 to line 10, character 5. No free variable allowed in this lambda: variable 'a' {"expression":"CREATE_CONTRACT(lambda (#P:Some(( nat * int ))) : None return let rhs#657 = #P in let p = rhs#657.0 in let s = rhs#657.1 in ( LIST_EMPTY() : (TO_list(operation)) , a ) , NONE() : (TO_option(key_hash)) , 300000000mutez , 1)","location":"in file \"create_contract_var.mligo\", line 6, character 35 to line 10, character 5"} If you're not sure how to fix this error, you can @@ -1336,4 +1348,4 @@ let%expect_test _ = * Visit our documentation: https://ligolang.org/docs/intro/what-and-why/ * Ask a question on our Discord: https://discord.gg/9rhYaEt * Open a gitlab issue: https://gitlab.com/ligolang/ligo/issues/new - * Check the changelog by running 'ligo changelog' |}] \ No newline at end of file + * Check the changelog by running 'ligo changelog' |}] diff --git a/src/bin/expect_tests/typer_error_tests.ml b/src/bin/expect_tests/typer_error_tests.ml index ecf5ce7e3..facf1e75c 100644 --- a/src/bin/expect_tests/typer_error_tests.ml +++ b/src/bin/expect_tests/typer_error_tests.ml @@ -175,7 +175,7 @@ let%expect_test _ = let%expect_test _ = run_ligo_good [ "interpret" ; "Set.literal [ (1,(2,3)) ; (2,(3,4)) ]" ; "--syntax=cameligo" ] ; [%expect {| - set[( 2 , ( 3 , 4 ) ) , ( 1 , ( 2 , 3 ) )] |}]; + SET_ADD(( 2 , ( 3 , 4 ) ) , SET_ADD(( 1 , ( 2 , 3 ) ) , SET_EMPTY())) |}]; run_ligo_bad [ "interpret" ; "Set.literal [ (1,2,3) ; (2,3,4) ]" ; "--syntax=cameligo" ] ; [%expect {| diff --git a/src/passes/10-interpreter/interpreter.ml b/src/passes/10-interpreter/interpreter.ml index 77d30cb71..6925a0243 100644 --- a/src/passes/10-interpreter/interpreter.ml +++ b/src/passes/10-interpreter/interpreter.ml @@ -119,6 +119,7 @@ let rec apply_operator : Ast_typed.constant' -> value list -> value result = | ( C_OR , [ V_Ct (C_bool a' ) ; V_Ct (C_bool b' ) ] ) -> return_ct @@ C_bool (a' || b') | ( C_AND , [ V_Ct (C_bool a' ) ; V_Ct (C_bool b' ) ] ) -> return_ct @@ C_bool (a' && b') | ( C_XOR , [ V_Ct (C_bool a' ) ; V_Ct (C_bool b' ) ] ) -> return_ct @@ C_bool ( (a' || b') && (not (a' && b')) ) + | ( C_LIST_EMPTY, []) -> ok @@ V_List ([]) | ( C_LIST_MAP , [ V_Func_val (arg_name, body, env) ; V_List (elts) ] ) -> let%bind elts' = bind_map_list (fun elt -> @@ -188,6 +189,7 @@ let rec apply_operator : Ast_typed.constant' -> value list -> value result = | "None" -> ok @@ V_Map (List.remove_assoc k kvs) | _ -> simple_fail "update without an option" ) + | ( C_SET_EMPTY, []) -> ok @@ V_Set ([]) | ( C_SET_ADD , [ v ; V_Set l ] ) -> ok @@ V_Set (List.sort_uniq compare (v::l)) | ( C_SET_FOLD , [ V_Func_val (arg_name, body, env) ; V_Set elts ; init ] ) -> bind_fold_list @@ -294,17 +296,6 @@ and eval : Ast_typed.expression -> env -> value result (fun kv -> bind_map_pair (fun (el:Ast_typed.expression) -> eval el env) kv) kvlist in ok @@ V_Map kvlist' - | E_list expl -> - let%bind expl' = bind_map_list - (fun (exp:Ast_typed.expression) -> eval exp env) - expl in - ok @@ V_List expl' - | E_set expl -> - let%bind expl' = bind_map_list - (fun (exp:Ast_typed.expression) -> eval exp env) - (List.sort_uniq compare expl) - in - ok @@ V_Set expl' | E_literal l -> eval_literal l | E_variable var -> diff --git a/src/passes/10-transpiler/transpiler.ml b/src/passes/10-transpiler/transpiler.ml index b7935bbe4..82f048614 100644 --- a/src/passes/10-transpiler/transpiler.ml +++ b/src/passes/10-transpiler/transpiler.ml @@ -390,26 +390,6 @@ and transpile_annotated_expression (ae:AST.expression) : expression result = transpile_lambda l io | E_recursive r -> transpile_recursive r - | E_list lst -> ( - let%bind t = - trace_strong (corner_case ~loc:__LOC__ "not a list") @@ - get_t_list tv in - let%bind lst' = bind_map_list (transpile_annotated_expression) lst in - let aux : expression -> expression -> expression result = fun prev cur -> - return @@ E_constant {cons_name=C_CONS;arguments=[cur ; prev]} in - let%bind (init : expression) = return @@ E_make_empty_list t in - bind_fold_right_list aux init lst' - ) - | E_set lst -> ( - let%bind t = - trace_strong (corner_case ~loc:__LOC__ "not a set") @@ - get_t_set tv in - let%bind lst' = bind_map_list (transpile_annotated_expression) lst in - let aux : expression -> expression -> expression result = fun prev cur -> - return @@ E_constant {cons_name=C_SET_ADD;arguments=[cur ; prev]} in - let%bind (init : expression) = return @@ E_make_empty_set t in - bind_fold_list aux init lst' - ) | E_map m -> ( let%bind (src, dst) = trace_strong (corner_case ~loc:__LOC__ "not a map") @@ diff --git a/src/passes/10-transpiler/untranspiler.ml b/src/passes/10-transpiler/untranspiler.ml index 49f9cde37..e9a924c53 100644 --- a/src/passes/10-transpiler/untranspiler.ml +++ b/src/passes/10-transpiler/untranspiler.ml @@ -181,7 +181,10 @@ let rec untranspile (v : value) (t : AST.type_expression) : AST.expression resul let%bind lst' = let aux = fun e -> untranspile e ty in bind_map_list aux lst in - return (E_list lst') + let aux = fun prev cur -> + return @@ E_constant {cons_name=C_CONS;arguments=[cur ; prev]} in + let%bind init = return @@ E_constant {cons_name=C_LIST_EMPTY;arguments=[]} in + bind_fold_right_list aux init lst' ) | TC_arrow _ -> ( let%bind n = @@ -196,7 +199,11 @@ let rec untranspile (v : value) (t : AST.type_expression) : AST.expression resul let%bind lst' = let aux = fun e -> untranspile e ty in bind_map_list aux lst in - return (E_set lst') + let lst' = List.sort_uniq compare lst' in + let aux = fun prev cur -> + return @@ E_constant {cons_name=C_SET_ADD;arguments=[cur ; prev]} in + let%bind init = return @@ E_constant {cons_name=C_SET_EMPTY;arguments=[]} in + bind_fold_list aux init lst' ) | TC_contract _ -> fail @@ bad_untranspile "contract" v diff --git a/src/passes/11-self_mini_c/helpers.ml b/src/passes/11-self_mini_c/helpers.ml index 6b77cf2ee..ea6d1355c 100644 --- a/src/passes/11-self_mini_c/helpers.ml +++ b/src/passes/11-self_mini_c/helpers.ml @@ -27,10 +27,6 @@ let rec fold_expression : 'a folder -> 'a -> expression -> 'a result = fun f ini | E_variable _ | E_skip | E_make_none _ | E_make_empty_map _ | E_make_empty_big_map _ - | E_make_empty_list _ - | E_make_empty_set _ -> ( - ok init' - ) | E_literal _ -> ok init' | E_constant (c) -> ( let%bind res = bind_fold_list self init' c.arguments in @@ -95,9 +91,7 @@ let rec map_expression : mapper -> expression -> expression result = fun f e -> match e'.content with | E_variable _ | E_literal _ | E_skip | E_make_none _ | E_make_empty_map _ - | E_make_empty_big_map _ - | E_make_empty_list _ - | E_make_empty_set _ as em -> return em + | E_make_empty_big_map _ as em -> return em | E_constant (c) -> ( let%bind lst = bind_map_list self c.arguments in return @@ E_constant {cons_name = c.cons_name; arguments = lst} 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 66a0a06b8..56ee521df 100644 --- a/src/passes/11-self_mini_c/self_mini_c.ml +++ b/src/passes/11-self_mini_c/self_mini_c.ml @@ -49,8 +49,6 @@ let rec is_pure : expression -> bool = fun e -> | E_variable _ | E_make_empty_map _ | E_make_empty_big_map _ - | E_make_empty_list _ - | E_make_empty_set _ | E_make_none _ -> true diff --git a/src/passes/11-self_mini_c/subst.ml b/src/passes/11-self_mini_c/subst.ml index c2103c9f5..1914cbe2c 100644 --- a/src/passes/11-self_mini_c/subst.ml +++ b/src/passes/11-self_mini_c/subst.ml @@ -42,8 +42,6 @@ let rec replace : expression -> var_name -> var_name -> expression = return @@ E_variable z | E_make_empty_map _ -> e | E_make_empty_big_map _ -> e - | E_make_empty_list _ -> e - | E_make_empty_set _ -> e | E_make_none _ -> e | E_iterator (name, ((v, tv), body), expr) -> let body = replace body in @@ -176,9 +174,7 @@ let rec subst_expression : body:expression -> x:var_name -> expr:expression -> e (* All that follows is boilerplate *) | E_literal _ | E_skip | E_make_none _ | E_make_empty_map (_,_) - | E_make_empty_big_map _ - | E_make_empty_list _ - | E_make_empty_set _ as em -> return em + | E_make_empty_big_map _ as em -> return em | E_constant (c) -> ( let lst = List.map self c.arguments in return @@ E_constant {cons_name = c.cons_name; arguments = lst } diff --git a/src/passes/12-compiler/compiler_program.ml b/src/passes/12-compiler/compiler_program.ml index 9a2001298..2ebfcbc34 100644 --- a/src/passes/12-compiler/compiler_program.ml +++ b/src/passes/12-compiler/compiler_program.ml @@ -66,10 +66,15 @@ let rec get_operator : constant' -> type_value -> expression list -> predicate r let%bind m_ty = Compiler_type.type_ ty in ok @@ simple_unary @@ prim ~children:[m_ty] I_RIGHT ) + | C_LIST_EMPTY -> ( + let%bind ty' = Mini_c.get_t_list ty in + let%bind m_ty = Compiler_type.type_ ty' in + ok @@ simple_constant @@ i_nil m_ty + ) | C_SET_EMPTY -> ( let%bind ty' = Mini_c.get_t_set ty in let%bind m_ty = Compiler_type.type_ ty' in - ok @@ simple_constant @@ prim ~children:[m_ty] I_EMPTY_SET + ok @@ simple_constant @@ i_empty_set m_ty ) | C_BYTES_UNPACK -> ( let%bind ty' = Mini_c.get_t_option ty in @@ -303,12 +308,6 @@ and translate_expression (expr:expression) (env:environment) : michelson result | E_make_empty_big_map sd -> let%bind (src, dst) = bind_map_pair Compiler_type.type_ sd in return @@ i_empty_big_map src dst - | E_make_empty_list t -> - let%bind t' = Compiler_type.type_ t in - return @@ i_nil t' - | E_make_empty_set t -> - let%bind t' = Compiler_type.type_ t in - return @@ i_empty_set t' | E_make_none o -> let%bind o' = Compiler_type.type_ o in return @@ i_none o' diff --git a/src/passes/6-sugar_to_core/sugar_to_core.ml b/src/passes/6-sugar_to_core/sugar_to_core.ml index e7a3f20fc..6c664a45b 100644 --- a/src/passes/6-sugar_to_core/sugar_to_core.ml +++ b/src/passes/6-sugar_to_core/sugar_to_core.ml @@ -121,11 +121,19 @@ let rec compile_expression : I.expression -> O.expression result = in return @@ O.E_big_map big_map | I.E_list lst -> - let%bind lst = bind_map_list compile_expression lst in - return @@ O.E_list lst - | I.E_set set -> - let%bind set = bind_map_list compile_expression set in - return @@ O.E_set set + let%bind lst' = bind_map_list (compile_expression) lst in + let aux = fun prev cur -> + return @@ E_constant {cons_name=C_CONS;arguments=[cur ; prev]} in + let%bind init = return @@ E_constant {cons_name=C_LIST_EMPTY;arguments=[]} in + bind_fold_right_list aux init lst' + | I.E_set set -> ( + let%bind lst' = bind_map_list (compile_expression) set in + let lst' = List.sort_uniq compare lst' in + let aux = fun prev cur -> + return @@ E_constant {cons_name=C_SET_ADD;arguments=[cur ; prev]} in + let%bind init = return @@ E_constant {cons_name=C_SET_EMPTY;arguments=[]} in + bind_fold_list aux init lst' + ) | I.E_look_up look_up -> let%bind (path, index) = bind_map_pair compile_expression look_up in return @@ O.E_constant {cons_name=C_MAP_FIND_OPT;arguments=[index;path]} @@ -313,15 +321,6 @@ let rec uncompile_expression : O.expression -> I.expression result = ) big_map in return @@ I.E_big_map big_map - | O.E_list lst -> - let%bind lst = bind_map_list uncompile_expression lst in - return @@ I.E_list lst - | O.E_set set -> - let%bind set = bind_map_list uncompile_expression set in - return @@ I.E_set set - | O.E_look_up look_up -> - let%bind look_up = bind_map_pair uncompile_expression look_up in - return @@ I.E_look_up look_up | O.E_ascription {anno_expr; type_annotation} -> let%bind anno_expr = uncompile_expression anno_expr in let%bind type_annotation = uncompile_type_expression type_annotation in diff --git a/src/passes/8-typer-new/typer.ml b/src/passes/8-typer-new/typer.ml index 94a1682bd..754659597 100644 --- a/src/passes/8-typer-new/typer.ml +++ b/src/passes/8-typer-new/typer.ml @@ -578,17 +578,6 @@ and type_expression : environment -> Solver.state -> ?tv_opt:O.type_expression - return (E_map lst') tv *) - | E_list lst -> - let%bind (state', lst') = - bind_fold_map_list (fun state' elt -> type_expression e state' elt >>? swap) state lst in - let wrapped = Wrap.list (List.map (fun x -> O.(x.type_expression)) lst') in - return_wrapped (E_list lst') state' wrapped - | E_set set -> - let aux = fun state' elt -> type_expression e state' elt >>? swap in - let%bind (state', set') = - bind_fold_map_list aux state set in - let wrapped = Wrap.set (List.map (fun x -> O.(x.type_expression)) set') in - return_wrapped (E_set set') state' wrapped | E_map map -> let aux' state' elt = type_expression e state' elt >>? swap in let aux = fun state' elt -> bind_fold_map_pair aux' state' elt in @@ -1059,12 +1048,6 @@ let rec untype_expression (e:O.expression) : (I.expression) result = | E_big_map m -> let%bind m' = bind_map_list (bind_map_pair untype_expression) m in return (e_big_map m') - | E_list lst -> - let%bind lst' = bind_map_list untype_expression lst in - return (e_list lst') - | E_set lst -> - let%bind lst' = bind_map_list untype_expression lst in - return (e_set lst') | E_matching {matchee;cases} -> let%bind ae' = untype_expression matchee in let%bind m' = untype_matching untype_expression cases in diff --git a/src/passes/8-typer-old/typer.ml b/src/passes/8-typer-old/typer.ml index 2b6764dea..879c44b19 100644 --- a/src/passes/8-typer-old/typer.ml +++ b/src/passes/8-typer-old/typer.ml @@ -511,48 +511,6 @@ and type_expression' : environment -> ?tv_opt:O.type_expression -> I.expression let%bind () = O.assert_type_expression_eq (tv, get_type_expression update) in return (E_record_update {record; path; update}) wrapped (* Data-structure *) - | E_list lst -> - let%bind lst' = bind_map_list (type_expression' e) lst in - let%bind tv = - let aux opt c = - match opt with - | None -> ok (Some c) - | Some c' -> - let%bind _eq = Ast_typed.assert_type_expression_eq (c, c') in - ok (Some c') in - let%bind init = match tv_opt with - | None -> ok None - | Some ty -> - let%bind ty' = get_t_list ty in - ok (Some ty') in - let%bind ty = - let%bind opt = bind_fold_list aux init - @@ List.map get_type_expression lst' in - trace_option (needs_annotation ae "empty list") opt in - ok (t_list ty ()) - in - return (E_list lst') tv - | E_set lst -> - let%bind lst' = bind_map_list (type_expression' e) lst in - let%bind tv = - let aux opt c = - match opt with - | None -> ok (Some c) - | Some c' -> - let%bind _eq = Ast_typed.assert_type_expression_eq (c, c') in - ok (Some c') in - let%bind init = match tv_opt with - | None -> ok None - | Some ty -> - let%bind ty' = get_t_set ty in - ok (Some ty') in - let%bind ty = - let%bind opt = bind_fold_list aux init - @@ List.map get_type_expression lst' in - trace_option (needs_annotation ae "empty set") opt in - ok (t_set ty ()) - in - return (E_set lst') tv | E_map lst -> let%bind lst' = bind_map_list (bind_map_pair (type_expression' e)) lst in let%bind tv = @@ -682,6 +640,21 @@ and type_expression' : environment -> ?tv_opt:O.type_expression -> I.expression let%bind (name', tv) = type_constant cons_name tv_lst tv_opt in return (E_constant {cons_name=name';arguments=lst'}) tv + | E_constant {cons_name=C_SET_ADD|C_CONS as cst;arguments=[key;set]} -> + let%bind key' = type_expression' e key in + let tv_key = get_type_expression key' in + let tv = match tv_opt with + Some (tv) -> tv + | None -> match cst with + C_SET_ADD -> t_set tv_key () + | C_CONS -> t_list tv_key () + | _ -> failwith "impossible" + in + let%bind set' = type_expression' e ~tv_opt:tv set in + let tv_set = get_type_expression set' in + let tv_lst = [tv_key;tv_set] in + let%bind (name', tv) = type_constant cst tv_lst tv_opt in + return (E_constant {cons_name=name';arguments=[key';set']}) tv | E_constant {cons_name;arguments} -> let%bind lst' = bind_list @@ List.map (type_expression' e) arguments in let tv_lst = List.map get_type_expression lst' in @@ -871,12 +844,6 @@ let rec untype_expression (e:O.expression) : (I.expression) result = | E_big_map m -> let%bind m' = bind_map_list (bind_map_pair untype_expression) m in return (e_big_map m') - | E_list lst -> - let%bind lst' = bind_map_list untype_expression lst in - return (e_list lst') - | E_set lst -> - let%bind lst' = bind_map_list untype_expression lst in - return (e_set lst') | E_matching {matchee;cases} -> let%bind ae' = untype_expression matchee in let%bind m' = untype_matching untype_expression cases in diff --git a/src/passes/9-self_ast_typed/helpers.ml b/src/passes/9-self_ast_typed/helpers.ml index 658a05f31..76d6c0f21 100644 --- a/src/passes/9-self_ast_typed/helpers.ml +++ b/src/passes/9-self_ast_typed/helpers.ml @@ -8,7 +8,7 @@ let rec fold_expression : 'a folder -> 'a -> expression -> 'a result = fun f ini let%bind init' = f init e in match e.expression_content with | E_literal _ | E_variable _ -> ok init' - | E_list lst | E_set lst | E_constant {arguments=lst} -> ( + | E_constant {arguments=lst} -> ( let%bind res = bind_fold_list self init' lst in ok res ) @@ -90,14 +90,6 @@ let rec map_expression : mapper -> expression -> expression result = fun f e -> let%bind e' = f e in let return expression_content = ok { e' with expression_content } in match e'.expression_content with - | E_list lst -> ( - let%bind lst' = bind_map_list self lst in - return @@ E_list lst' - ) - | E_set lst -> ( - let%bind lst' = bind_map_list self lst in - return @@ E_set lst' - ) | E_map lst -> ( let%bind lst' = bind_map_list (bind_map_pair self) lst in return @@ E_map lst' @@ -201,14 +193,6 @@ let rec fold_map_expression : 'a fold_mapper -> 'a -> expression -> ('a * expres else let return expression_content = { e' with expression_content } in match e'.expression_content with - | E_list lst -> ( - let%bind (res, lst') = bind_fold_map_list self init' lst in - ok (res, return @@ E_list lst') - ) - | E_set lst -> ( - let%bind (res, lst') = bind_fold_map_list self init' lst in - ok (res, return @@ E_set lst') - ) | E_map lst -> ( let%bind (res, lst') = bind_fold_map_list (bind_fold_map_pair self) init' lst in ok (res, return @@ E_map lst') diff --git a/src/passes/9-self_ast_typed/tail_recursion.ml b/src/passes/9-self_ast_typed/tail_recursion.ml index c95f10c06..f5f30e923 100644 --- a/src/passes/9-self_ast_typed/tail_recursion.ml +++ b/src/passes/9-self_ast_typed/tail_recursion.ml @@ -64,9 +64,6 @@ let rec check_recursive_call : expression_variable -> bool -> expression -> unit in let%bind _ = bind_map_list aux eel in ok () - | E_list el | E_set el -> - let%bind _ = bind_map_list (check_recursive_call n false) el in - ok () and check_recursive_call_in_matching = fun n final_path c -> match c with diff --git a/src/passes/operators/operators.ml b/src/passes/operators/operators.ml index 472b67506..8e011b316 100644 --- a/src/passes/operators/operators.ml +++ b/src/passes/operators/operators.ml @@ -949,6 +949,11 @@ module Typer = struct then ok (t_unit ()) else fail @@ Operator_errors.type_error "bad set iter" key arg () + let list_empty = typer_0 "LIST_EMPTY" @@ fun tv_opt -> + match tv_opt with + | None -> simple_fail "untyped LIST_EMPTY" + | Some t -> ok t + let list_iter = typer_2 "LIST_ITER" @@ fun body lst -> let%bind (arg , res) = get_t_function body in let%bind () = Assert.assert_true (eq_1 res (t_unit ())) in @@ -1145,7 +1150,6 @@ module Typer = struct | C_SLICE -> ok @@ slice ; | C_BYTES_PACK -> ok @@ bytes_pack ; | C_BYTES_UNPACK -> ok @@ bytes_unpack ; - | C_CONS -> ok @@ cons ; (* SET *) | C_SET_EMPTY -> ok @@ set_empty ; | C_SET_ADD -> ok @@ set_add ; @@ -1155,6 +1159,8 @@ module Typer = struct | C_SET_MEM -> ok @@ set_mem ; (* LIST *) + | C_CONS -> ok @@ cons ; + | C_LIST_EMPTY -> ok @@ list_empty ; | C_LIST_ITER -> ok @@ list_iter ; | C_LIST_MAP -> ok @@ list_map ; | C_LIST_FOLD -> ok @@ list_fold ; diff --git a/src/stages/1-ast_imperative/combinators.ml b/src/stages/1-ast_imperative/combinators.ml index bc66d4ac0..e49f811b3 100644 --- a/src/stages/1-ast_imperative/combinators.ml +++ b/src/stages/1-ast_imperative/combinators.ml @@ -168,8 +168,9 @@ let e_typed_none ?loc t_opt = let type_annotation = t_option t_opt in e_annotation ?loc (e_none ?loc ()) type_annotation -let e_typed_list ?loc lst t = - e_annotation ?loc (e_list lst) (t_list t) +let e_typed_list ?loc lst t = e_annotation ?loc (e_list lst) (t_list t) +let e_typed_list_literal ?loc lst t = + e_annotation ?loc (e_constant C_LIST_LITERAL lst) (t_list t) let e_typed_map ?loc lst k v = e_annotation ?loc (e_map lst) (t_map k v) let e_typed_big_map ?loc lst k v = e_annotation ?loc (e_big_map lst) (t_big_map k v) diff --git a/src/stages/1-ast_imperative/combinators.mli b/src/stages/1-ast_imperative/combinators.mli index 1cf5986c9..3d0ae94b9 100644 --- a/src/stages/1-ast_imperative/combinators.mli +++ b/src/stages/1-ast_imperative/combinators.mli @@ -100,6 +100,7 @@ val make_option_typed : ?loc:Location.t -> expression -> type_expression option val e_typed_none : ?loc:Location.t -> type_expression -> expression val e_typed_list : ?loc:Location.t -> expression list -> type_expression -> expression +val e_typed_list_literal : ?loc:Location.t -> expression list -> type_expression -> expression val e_typed_map : ?loc:Location.t -> ( expression * expression ) list -> type_expression -> type_expression -> expression val e_typed_big_map : ?loc:Location.t -> ( expression * expression ) list -> type_expression -> type_expression -> expression diff --git a/src/stages/3-ast_core/PP.ml b/src/stages/3-ast_core/PP.ml index 9673beef7..149a92631 100644 --- a/src/stages/3-ast_core/PP.ml +++ b/src/stages/3-ast_core/PP.ml @@ -35,10 +35,6 @@ and expression_content ppf (ec : expression_content) = fprintf ppf "map[%a]" (list_sep_d assoc_expression) m | E_big_map m -> fprintf ppf "big_map[%a]" (list_sep_d assoc_expression) m - | E_list lst -> - fprintf ppf "list[%a]" (list_sep_d expression) lst - | E_set lst -> - fprintf ppf "set[%a]" (list_sep_d expression) lst | E_lambda {binder; input_type; output_type; result} -> fprintf ppf "lambda (%a:%a) : %a return %a" expression_variable binder diff --git a/src/stages/3-ast_core/combinators.ml b/src/stages/3-ast_core/combinators.ml index 91a77d217..a1fdd1242 100644 --- a/src/stages/3-ast_core/combinators.ml +++ b/src/stages/3-ast_core/combinators.ml @@ -113,8 +113,6 @@ let e_none ?loc () : expression = make_expr ?loc @@ E_constant {cons_name = C_NO let e_string_cat ?loc sl sr : expression = make_expr ?loc @@ E_constant {cons_name = C_CONCAT; arguments = [sl ; sr ]} let e_map_add ?loc k v old : expression = make_expr ?loc @@ E_constant {cons_name = C_MAP_ADD; arguments = [k ; v ; old]} let e_map ?loc lst : expression = make_expr ?loc @@ E_map lst -let e_set ?loc lst : expression = make_expr ?loc @@ E_set lst -let e_list ?loc lst : expression = make_expr ?loc @@ E_list lst let e_constructor ?loc s a : expression = make_expr ?loc @@ E_constructor { constructor = Constructor s; element = a} let e_matching ?loc a b : expression = make_expr ?loc @@ E_matching {matchee=a;cases=b} let e_matching_bool ?loc a b c : expression = e_matching ?loc a (Match_bool {match_true = b ; match_false = c}) @@ -160,14 +158,9 @@ let e_typed_none ?loc t_opt = let type_annotation = t_option t_opt in e_annotation ?loc (e_none ?loc ()) type_annotation -let e_typed_list ?loc lst t = - e_annotation ?loc (e_list lst) (t_list t) - let e_typed_map ?loc lst k v = e_annotation ?loc (e_map lst) (t_map k v) let e_typed_big_map ?loc lst k v = e_annotation ?loc (e_big_map lst) (t_big_map k v) -let e_typed_set ?loc lst k = e_annotation ?loc (e_set lst) (t_set k) - let e_lambda ?loc (binder : expression_variable) (input_type : type_expression option) @@ -220,9 +213,16 @@ let get_e_pair = fun t -> | _ -> simple_fail "not a pair" let get_e_list = fun t -> - match t with - | E_list lst -> ok lst - | _ -> simple_fail "not a list" + let rec aux t = + match t with + E_constant {cons_name=C_CONS;arguments=[key;lst]} -> + let%bind lst = aux lst.expression_content in + ok @@ key::(lst) + | E_constant {cons_name=C_LIST_EMPTY;arguments=[]} -> + ok @@ [] + | _ -> simple_fail "not a list" + in + aux t let tuple_of_record (m: _ LMap.t) = let aux i = @@ -249,11 +249,6 @@ let extract_pair : expression -> (expression * expression) result = fun e -> ) | _ -> fail @@ bad_kind "pair" e.location -let extract_list : expression -> (expression list) result = fun e -> - match e.expression_content with - | E_list lst -> ok lst - | _ -> fail @@ bad_kind "list" e.location - let extract_record : expression -> (label * expression) list result = fun e -> match e.expression_content with | E_record lst -> ok @@ LMap.to_kv_list lst diff --git a/src/stages/3-ast_core/combinators.mli b/src/stages/3-ast_core/combinators.mli index a16a36600..dfb78bf43 100644 --- a/src/stages/3-ast_core/combinators.mli +++ b/src/stages/3-ast_core/combinators.mli @@ -74,8 +74,6 @@ val e_none : ?loc:Location.t -> unit -> expression val e_string_cat : ?loc:Location.t -> expression -> expression -> expression val e_map_add : ?loc:Location.t -> expression -> expression -> expression -> expression val e_map : ?loc:Location.t -> ( expression * expression ) list -> expression -val e_set : ?loc:Location.t -> expression list -> expression -val e_list : ?loc:Location.t -> expression list -> expression val e_pair : ?loc:Location.t -> expression -> expression -> expression val e_constructor : ?loc:Location.t -> string -> expression -> expression val e_matching : ?loc:Location.t -> expression -> matching_expr -> expression @@ -96,13 +94,9 @@ val make_option_typed : ?loc:Location.t -> expression -> type_expression option val e_typed_none : ?loc:Location.t -> type_expression -> expression -val e_typed_list : ?loc:Location.t -> expression list -> type_expression -> expression - val e_typed_map : ?loc:Location.t -> ( expression * expression ) list -> type_expression -> type_expression -> expression val e_typed_big_map : ?loc:Location.t -> ( expression * expression ) list -> type_expression -> type_expression -> expression -val e_typed_set : ?loc:Location.t -> expression list -> type_expression -> expression - val e_lambda : ?loc:Location.t -> expression_variable -> type_expression option -> type_expression option -> expression -> expression val e_recursive : ?loc:Location.t -> expression_variable -> type_expression -> lambda -> expression val e_record : ?loc:Location.t -> expr Map.String.t -> expression @@ -125,8 +119,6 @@ val is_e_failwith : expression -> bool *) val extract_pair : expression -> ( expression * expression ) result -val extract_list : expression -> (expression list) result - val extract_record : expression -> (label * expression) list result val extract_map : expression -> (expression * expression) list result diff --git a/src/stages/3-ast_core/misc.ml b/src/stages/3-ast_core/misc.ml index 96a9f3750..fc5319eed 100644 --- a/src/stages/3-ast_core/misc.ml +++ b/src/stages/3-ast_core/misc.ml @@ -88,6 +88,19 @@ let rec assert_value_eq (a, b: (expression * expression )) : unit result = assert_literal_eq (a, b) | E_literal _ , _ -> simple_fail "comparing a literal with not a literal" + | E_constant {cons_name=C_SET_LITERAL;arguments=lsta}, + E_constant {cons_name=C_SET_LITERAL;arguments=lstb} -> ( + let lsta' = List.sort (compare) lsta in + let lstb' = List.sort (compare) lstb in + let%bind lst = + generic_try (simple_error "set of different lengths") + (fun () -> List.combine lsta' lstb') in + let%bind _all = bind_map_list assert_value_eq lst in + ok () + ) + | E_constant {cons_name=C_SET_LITERAL;_}, _ -> + simple_fail "comparing set with other expression" + | E_constant (ca) , E_constant (cb) when ca.cons_name = cb.cons_name -> ( let%bind lst = generic_try (simple_error "constants with different number of elements") @@ -156,28 +169,6 @@ let rec assert_value_eq (a, b: (expression * expression )) : unit result = | (E_map _ | E_big_map _), _ -> simple_fail "comparing map with other expression" - | E_list lsta, E_list lstb -> ( - let%bind lst = - generic_try (simple_error "list of different lengths") - (fun () -> List.combine lsta lstb) in - let%bind _all = bind_map_list assert_value_eq lst in - ok () - ) - | E_list _, _ -> - simple_fail "comparing list with other expression" - - | E_set lsta, E_set lstb -> ( - let lsta' = List.sort (compare) lsta in - let lstb' = List.sort (compare) lstb in - let%bind lst = - generic_try (simple_error "set of different lengths") - (fun () -> List.combine lsta' lstb') in - let%bind _all = bind_map_list assert_value_eq lst in - ok () - ) - | E_set _, _ -> - simple_fail "comparing set with other expression" - | (E_ascription a , _b') -> assert_value_eq (a.anno_expr , b) | (_a' , E_ascription b) -> assert_value_eq (a , b.anno_expr) | (E_variable _, _) | (E_lambda _, _) diff --git a/src/stages/3-ast_core/types.ml b/src/stages/3-ast_core/types.ml index 4ad14d148..ae3bb2283 100644 --- a/src/stages/3-ast_core/types.ml +++ b/src/stages/3-ast_core/types.ml @@ -47,8 +47,6 @@ and expression_content = (* TODO : move to constant*) | E_map of (expression * expression) list (*move to operator *) | E_big_map of (expression * expression) list (*move to operator *) - | E_list of expression list - | E_set of expression list (* Advanced *) | E_ascription of ascription diff --git a/src/stages/4-ast_typed/PP.ml b/src/stages/4-ast_typed/PP.ml index 3fe4f02c8..62e085c51 100644 --- a/src/stages/4-ast_typed/PP.ml +++ b/src/stages/4-ast_typed/PP.ml @@ -36,10 +36,6 @@ and expression_content ppf (ec: expression_content) = fprintf ppf "map[%a]" (list_sep_d assoc_expression) m | E_big_map m -> fprintf ppf "big_map[%a]" (list_sep_d assoc_expression) m - | E_list lst -> - fprintf ppf "list[%a]" (list_sep_d expression) lst - | E_set lst -> - fprintf ppf "set[%a]" (list_sep_d expression) lst | E_lambda {binder; result} -> fprintf ppf "lambda (%a) return %a" expression_variable binder expression result diff --git a/src/stages/4-ast_typed/combinators.ml b/src/stages/4-ast_typed/combinators.ml index 5abb2a2bc..9a8bf2af5 100644 --- a/src/stages/4-ast_typed/combinators.ml +++ b/src/stages/4-ast_typed/combinators.ml @@ -296,7 +296,6 @@ let e_lambda l : expression_content = E_lambda l let e_pair a b : expression_content = ez_e_record [(Label "0",a);(Label "1", b)] let e_application lamb args : expression_content = E_application {lamb;args} let e_variable v : expression_content = E_variable v -let e_list lst : expression_content = E_list lst let e_let_in let_binder inline rhs let_result = E_let_in { let_binder ; rhs ; let_result; inline } let e_a_unit = make_a_e (e_unit ()) (t_unit ()) @@ -315,7 +314,6 @@ let e_a_application a b = make_a_e (e_application a b) (get_type_expression b) let e_a_variable v ty = make_a_e (e_variable v) ty let ez_e_a_record r = make_a_e (ez_e_record r) (ez_t_record (List.map (fun (x, y) -> x, y.type_expression) r) ()) let e_a_map lst k v = make_a_e (e_map lst) (t_map k v ()) -let e_a_list lst t = make_a_e (e_list lst) (t_list t ()) let e_a_let_in binder expr body attributes = make_a_e (e_let_in binder expr body attributes) (get_type_expression body) diff --git a/src/stages/4-ast_typed/combinators.mli b/src/stages/4-ast_typed/combinators.mli index a42abd3cc..9bbd75504 100644 --- a/src/stages/4-ast_typed/combinators.mli +++ b/src/stages/4-ast_typed/combinators.mli @@ -128,7 +128,6 @@ val e_lambda : lambda -> expression_content val e_pair : expression -> expression -> expression_content val e_application : expression -> expr -> expression_content val e_variable : expression_variable -> expression_content -val e_list : expression list -> expression_content val e_let_in : expression_variable -> inline -> expression -> expression -> expression_content val e_a_unit : full_environment -> expression @@ -147,7 +146,6 @@ val e_a_application : expression -> expression -> full_environment -> expression val e_a_variable : expression_variable -> type_expression -> full_environment -> expression val ez_e_a_record : ( label * expression ) list -> full_environment -> expression val e_a_map : ( expression * expression ) list -> type_expression -> type_expression -> full_environment -> expression -val e_a_list : expression list -> type_expression -> full_environment -> expression val e_a_let_in : expression_variable -> bool -> expression -> expression -> full_environment -> expression val get_a_int : expression -> int result diff --git a/src/stages/4-ast_typed/combinators_environment.ml b/src/stages/4-ast_typed/combinators_environment.ml index f92ef3aea..e296ae914 100644 --- a/src/stages/4-ast_typed/combinators_environment.ml +++ b/src/stages/4-ast_typed/combinators_environment.ml @@ -15,7 +15,6 @@ let e_a_empty_some s = e_a_some s Environment.full_empty let e_a_empty_none t = e_a_none t Environment.full_empty let e_a_empty_record r = e_a_record r Environment.full_empty let e_a_empty_map lst k v = e_a_map lst k v Environment.full_empty -let e_a_empty_list lst t = e_a_list lst t Environment.full_empty let ez_e_a_empty_record r = ez_e_a_record r Environment.full_empty let e_a_empty_lambda l i o = e_a_lambda l i o Environment.full_empty diff --git a/src/stages/4-ast_typed/combinators_environment.mli b/src/stages/4-ast_typed/combinators_environment.mli index da4b2cfb9..ceb438afe 100644 --- a/src/stages/4-ast_typed/combinators_environment.mli +++ b/src/stages/4-ast_typed/combinators_environment.mli @@ -14,7 +14,6 @@ val e_a_empty_some : expression -> expression val e_a_empty_none : type_expression -> expression val e_a_empty_record : expression label_map -> expression val e_a_empty_map : (expression * expression ) list -> type_expression -> type_expression -> expression -val e_a_empty_list : expression list -> type_expression -> expression val ez_e_a_empty_record : ( label * expression ) list -> expression val e_a_empty_lambda : lambda -> type_expression -> type_expression -> expression diff --git a/src/stages/4-ast_typed/misc.ml b/src/stages/4-ast_typed/misc.ml index 5def088a8..6966fe414 100644 --- a/src/stages/4-ast_typed/misc.ml +++ b/src/stages/4-ast_typed/misc.ml @@ -211,8 +211,6 @@ module Free_variables = struct | E_record m -> unions @@ List.map self @@ LMap.to_list m | E_record_accessor {record;_} -> self record | E_record_update {record; update;_} -> union (self record) @@ self update - | E_list lst -> unions @@ List.map self lst - | E_set lst -> unions @@ List.map self lst | (E_map m | E_big_map m) -> unions @@ List.map self @@ List.concat @@ List.map (fun (a, b) -> [ a ; b ]) m | E_matching {matchee; cases;_} -> union (self matchee) (matching_expression b cases) | E_let_in { let_binder; rhs; let_result; _} -> @@ -512,24 +510,6 @@ let rec assert_value_eq (a, b: (expression*expression)) : unit result = | (E_map _ | E_big_map _), _ -> fail @@ different_values_because_different_types "map vs. non-map" a b - | E_list lsta, E_list lstb -> ( - let%bind lst = - generic_try (different_size_values "lists of different lengths" a b) - (fun () -> List.combine lsta lstb) in - let%bind _all = bind_map_list assert_value_eq lst in - ok () - ) - | E_list _, _ -> - fail @@ different_values_because_different_types "list vs. non-list" a b - | E_set lsta, E_set lstb -> ( - let%bind lst = - generic_try (different_size_values "sets of different lengths" a b) - (fun () -> List.combine lsta lstb) in - let%bind _all = bind_map_list assert_value_eq lst in - ok () - ) - | E_set _, _ -> - fail @@ different_values_because_different_types "set vs. non-set" a b | (E_literal _, _) | (E_variable _, _) | (E_application _, _) | (E_lambda _, _) | (E_let_in _, _) | (E_recursive _, _) | (E_record_accessor _, _) | (E_record_update _,_) diff --git a/src/stages/4-ast_typed/misc_smart.ml b/src/stages/4-ast_typed/misc_smart.ml index 763bfa399..fa34a5014 100644 --- a/src/stages/4-ast_typed/misc_smart.ml +++ b/src/stages/4-ast_typed/misc_smart.ml @@ -75,12 +75,6 @@ module Captured_variables = struct let%bind r = self record in let%bind e = self update in ok @@ union r e - | E_list lst -> - let%bind lst' = bind_map_list self lst in - ok @@ unions lst' - | E_set lst -> - let%bind lst' = bind_map_list self lst in - ok @@ unions lst' | (E_map m | E_big_map m) -> let%bind lst' = bind_map_list self @@ List.concat @@ List.map (fun (a, b) -> [ a ; b ]) m in ok @@ unions lst' diff --git a/src/stages/4-ast_typed/types.ml b/src/stages/4-ast_typed/types.ml index fa303e21c..b892d499f 100644 --- a/src/stages/4-ast_typed/types.ml +++ b/src/stages/4-ast_typed/types.ml @@ -53,8 +53,6 @@ and expression_content = (* TODO : move to constant*) | E_map of (expression * expression) list (*move to operator *) | E_big_map of (expression * expression) list (*move to operator *) - | E_list of expression list - | E_set of expression list and constant = { cons_name: constant' diff --git a/src/stages/5-mini_c/PP.ml b/src/stages/5-mini_c/PP.ml index dfbbfdd64..8f0316253 100644 --- a/src/stages/5-mini_c/PP.ml +++ b/src/stages/5-mini_c/PP.ml @@ -88,8 +88,6 @@ and expression' ppf (e:expression') = match e with | E_literal v -> fprintf ppf "L(%a)" value v | E_make_empty_map _ -> fprintf ppf "map[]" | E_make_empty_big_map _ -> fprintf ppf "big_map[]" - | E_make_empty_list _ -> fprintf ppf "list[]" - | E_make_empty_set _ -> fprintf ppf "set[]" | E_make_none _ -> fprintf ppf "none" | E_if_bool (c, a, b) -> fprintf ppf "%a ? %a : %a" expression c expression a expression b | E_if_none (c, n, ((name, _) , s)) -> fprintf ppf "%a ?? %a : %a -> %a" expression c expression n Var.pp name expression s @@ -199,6 +197,8 @@ and constant ppf : constant' -> unit = function | C_SET_FOLD -> fprintf ppf "SET_FOLD" | C_SET_MEM -> fprintf ppf "SET_MEM" (* List *) + | C_LIST_EMPTY -> fprintf ppf "LIST_EMPTY" + | C_LIST_LITERAL -> fprintf ppf "LIST_LITERAL" | C_LIST_ITER -> fprintf ppf "LIST_ITER" | C_LIST_MAP -> fprintf ppf "LIST_MAP" | C_LIST_FOLD -> fprintf ppf "LIST_FOLD" diff --git a/src/stages/5-mini_c/misc.ml b/src/stages/5-mini_c/misc.ml index 31c816178..bbcee5ed1 100644 --- a/src/stages/5-mini_c/misc.ml +++ b/src/stages/5-mini_c/misc.ml @@ -46,8 +46,6 @@ module Free_variables = struct | E_variable n -> var_name b n | E_make_empty_map _ -> empty | E_make_empty_big_map _ -> empty - | E_make_empty_list _ -> empty - | E_make_empty_set _ -> empty | E_make_none _ -> empty | E_iterator (_, ((v, _), body), expr) -> unions [ expression (union (singleton v) b) body ; diff --git a/src/stages/5-mini_c/types.ml b/src/stages/5-mini_c/types.ml index b1e419b8b..f16054a25 100644 --- a/src/stages/5-mini_c/types.ml +++ b/src/stages/5-mini_c/types.ml @@ -61,8 +61,6 @@ and expression' = | E_variable of var_name | E_make_empty_map of (type_value * type_value) | E_make_empty_big_map of (type_value * type_value) - | E_make_empty_list of type_value - | E_make_empty_set of type_value | E_make_none of type_value | E_iterator of constant' * ((var_name * type_value) * expression) * expression | E_fold of (((var_name * type_value) * expression) * expression * expression) diff --git a/src/stages/common/PP.ml b/src/stages/common/PP.ml index c3557f3d5..f8d594a89 100644 --- a/src/stages/common/PP.ml +++ b/src/stages/common/PP.ml @@ -105,6 +105,8 @@ let constant ppf : constant' -> unit = function | C_SET_FOLD -> fprintf ppf "SET_FOLD" | C_SET_MEM -> fprintf ppf "SET_MEM" (* List *) + | C_LIST_EMPTY -> fprintf ppf "LIST_EMPTY" + | C_LIST_LITERAL -> fprintf ppf "LIST_LITERAL" | C_LIST_ITER -> fprintf ppf "LIST_ITER" | C_LIST_MAP -> fprintf ppf "LIST_MAP" | C_LIST_FOLD -> fprintf ppf "LIST_FOLD" diff --git a/src/stages/common/types.ml b/src/stages/common/types.ml index 20964d3b6..6ffdb5485 100644 --- a/src/stages/common/types.ml +++ b/src/stages/common/types.ml @@ -247,6 +247,8 @@ and constant' = | C_SET_FOLD | C_SET_MEM (* List *) + | C_LIST_EMPTY + | C_LIST_LITERAL | C_LIST_ITER | C_LIST_MAP | C_LIST_FOLD diff --git a/src/stages/typesystem/misc.ml b/src/stages/typesystem/misc.ml index c74e9e8f1..ce7937c7e 100644 --- a/src/stages/typesystem/misc.ml +++ b/src/stages/typesystem/misc.ml @@ -204,12 +204,6 @@ module Substitution = struct ok @@ (val1 , val2) ) val_val_list in ok @@ T.E_big_map val_val_list - | T.E_list vals -> - let%bind vals = bind_map_list (s_expression ~substs) vals in - ok @@ T.E_list vals - | T.E_set vals -> - let%bind vals = bind_map_list (s_expression ~substs) vals in - ok @@ T.E_set vals | T.E_matching {matchee;cases} -> let%bind matchee = s_expression ~substs matchee in let%bind cases = s_matching_expr ~substs cases in From 330c48e66a26bce91cf2cec70d30b33fff4672c5 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Wulfman Date: Thu, 19 Mar 2020 16:55:13 +0100 Subject: [PATCH 07/13] remove Map and BIG_Map --- src/passes/10-interpreter/interpreter.ml | 5 - src/passes/10-transpiler/transpiler.ml | 28 ---- src/passes/10-transpiler/untranspiler.ml | 26 ++-- src/passes/11-self_mini_c/helpers.ml | 5 +- src/passes/11-self_mini_c/self_mini_c.ml | 2 - src/passes/11-self_mini_c/subst.ml | 5 +- src/passes/12-compiler/compiler_program.ml | 16 ++- src/passes/6-sugar_to_core/sugar_to_core.ml | 38 +++-- src/passes/8-typer-new/typer.ml | 130 ------------------ src/passes/8-typer-old/typer.ml | 94 ++++--------- src/passes/9-self_ast_typed/helpers.ml | 20 --- src/passes/9-self_ast_typed/tail_recursion.ml | 8 -- src/passes/operators/operators.ml | 6 + src/stages/3-ast_core/PP.ml | 4 - src/stages/3-ast_core/combinators.ml | 18 +-- src/stages/3-ast_core/combinators.mli | 5 - src/stages/3-ast_core/misc.ml | 30 ---- src/stages/3-ast_core/types.ml | 4 - src/stages/4-ast_typed/PP.ml | 4 - src/stages/4-ast_typed/combinators.ml | 3 - src/stages/4-ast_typed/combinators.mli | 2 - .../4-ast_typed/combinators_environment.ml | 1 - .../4-ast_typed/combinators_environment.mli | 1 - src/stages/4-ast_typed/misc.ml | 17 --- src/stages/4-ast_typed/misc_smart.ml | 3 - src/stages/4-ast_typed/types.ml | 4 - src/stages/5-mini_c/PP.ml | 3 +- src/stages/5-mini_c/misc.ml | 2 - src/stages/5-mini_c/types.ml | 2 - src/stages/common/PP.ml | 1 + src/stages/common/types.ml | 1 + src/stages/typesystem/misc.ml | 14 -- 32 files changed, 92 insertions(+), 410 deletions(-) diff --git a/src/passes/10-interpreter/interpreter.ml b/src/passes/10-interpreter/interpreter.ml index 6925a0243..2c06cab83 100644 --- a/src/passes/10-interpreter/interpreter.ml +++ b/src/passes/10-interpreter/interpreter.ml @@ -291,11 +291,6 @@ and eval : Ast_typed.expression -> env -> value result let%bind rhs' = eval rhs env in eval let_result (Env.extend env (let_binder,rhs')) ) - | E_map kvlist | E_big_map kvlist -> - let%bind kvlist' = bind_map_list - (fun kv -> bind_map_pair (fun (el:Ast_typed.expression) -> eval el env) kv) - kvlist in - ok @@ V_Map kvlist' | E_literal l -> eval_literal l | E_variable var -> diff --git a/src/passes/10-transpiler/transpiler.ml b/src/passes/10-transpiler/transpiler.ml index 82f048614..8554b2d8e 100644 --- a/src/passes/10-transpiler/transpiler.ml +++ b/src/passes/10-transpiler/transpiler.ml @@ -390,34 +390,6 @@ and transpile_annotated_expression (ae:AST.expression) : expression result = transpile_lambda l io | E_recursive r -> transpile_recursive r - | E_map m -> ( - let%bind (src, dst) = - trace_strong (corner_case ~loc:__LOC__ "not a map") @@ - Mini_c.Combinators.get_t_map tv in - let aux : expression result -> (AST.expression * AST.expression) -> expression result = fun prev (k, v) -> - let%bind prev' = prev in - let%bind (k', v') = - let v' = e_a_some v ae.environment in - bind_map_pair (transpile_annotated_expression) (k , v') in - return @@ E_constant {cons_name=C_UPDATE;arguments=[k' ; v' ; prev']} - in - let init = return @@ E_make_empty_map (src, dst) in - List.fold_left aux init m - ) - | E_big_map m -> ( - let%bind (src, dst) = - trace_strong (corner_case ~loc:__LOC__ "not a map") @@ - Mini_c.Combinators.get_t_big_map tv in - let aux : expression result -> (AST.expression * AST.expression) -> expression result = fun prev (k, v) -> - let%bind prev' = prev in - let%bind (k', v') = - let v' = e_a_some v ae.environment in - bind_map_pair (transpile_annotated_expression) (k , v') in - return @@ E_constant {cons_name=C_UPDATE;arguments=[k' ; v' ; prev']} - in - let init = return @@ E_make_empty_big_map (src, dst) in - List.fold_left aux init m - ) | E_matching {matchee=expr; cases=m} -> ( let%bind expr' = transpile_annotated_expression expr in match m with diff --git a/src/passes/10-transpiler/untranspiler.ml b/src/passes/10-transpiler/untranspiler.ml index e9a924c53..9a7149eed 100644 --- a/src/passes/10-transpiler/untranspiler.ml +++ b/src/passes/10-transpiler/untranspiler.ml @@ -151,28 +151,38 @@ let rec untranspile (v : value) (t : AST.type_expression) : AST.expression resul ok (e_a_empty_some s') ) | TC_map (k_ty,v_ty)-> ( - let%bind lst = + let%bind map = trace_strong (wrong_mini_c_value "map" v) @@ get_map v in - let%bind lst' = + let%bind map' = let aux = fun (k, v) -> let%bind k' = untranspile k k_ty in let%bind v' = untranspile v v_ty in ok (k', v') in - bind_map_list aux lst in - return (E_map lst') + bind_map_list aux map in + let aux = fun prev (k, v) -> + let (k', v') = (k , v ) in + return @@ E_constant {cons_name=C_MAP_ADD;arguments=[k' ; v' ; prev]} + in + let%bind init = return @@ E_constant {cons_name=C_MAP_EMPTY;arguments=[]} in + bind_fold_list aux init map' ) | TC_big_map (k_ty, v_ty) -> ( - let%bind lst = + let%bind map = trace_strong (wrong_mini_c_value "big_map" v) @@ get_big_map v in - let%bind lst' = + let%bind map' = let aux = fun (k, v) -> let%bind k' = untranspile k k_ty in let%bind v' = untranspile v v_ty in ok (k', v') in - bind_map_list aux lst in - return (E_big_map lst') + bind_map_list aux map in + let map' = List.sort_uniq compare map' in + let aux = fun prev (k, v) -> + return @@ E_constant {cons_name=C_MAP_ADD;arguments=[k ; v ; prev]} + in + let%bind init = return @@ E_constant {cons_name=C_MAP_EMPTY;arguments=[]} in + bind_fold_list aux init map' ) | TC_list ty -> ( let%bind lst = diff --git a/src/passes/11-self_mini_c/helpers.ml b/src/passes/11-self_mini_c/helpers.ml index ea6d1355c..fd35db639 100644 --- a/src/passes/11-self_mini_c/helpers.ml +++ b/src/passes/11-self_mini_c/helpers.ml @@ -25,8 +25,6 @@ let rec fold_expression : 'a folder -> 'a -> expression -> 'a result = fun f ini let%bind init' = f init e in match e.content with | E_variable _ | E_skip | E_make_none _ - | E_make_empty_map _ - | E_make_empty_big_map _ | E_literal _ -> ok init' | E_constant (c) -> ( let%bind res = bind_fold_list self init' c.arguments in @@ -90,8 +88,7 @@ let rec map_expression : mapper -> expression -> expression result = fun f e -> let return content = ok { e' with content } in match e'.content with | E_variable _ | E_literal _ | E_skip | E_make_none _ - | E_make_empty_map _ - | E_make_empty_big_map _ as em -> return em + as em -> return em | E_constant (c) -> ( let%bind lst = bind_map_list self c.arguments in return @@ E_constant {cons_name = c.cons_name; arguments = lst} 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 56ee521df..95e03a661 100644 --- a/src/passes/11-self_mini_c/self_mini_c.ml +++ b/src/passes/11-self_mini_c/self_mini_c.ml @@ -47,8 +47,6 @@ let rec is_pure : expression -> bool = fun e -> | E_closure _ | E_skip | E_variable _ - | E_make_empty_map _ - | E_make_empty_big_map _ | E_make_none _ -> true diff --git a/src/passes/11-self_mini_c/subst.ml b/src/passes/11-self_mini_c/subst.ml index 1914cbe2c..1637a7bbe 100644 --- a/src/passes/11-self_mini_c/subst.ml +++ b/src/passes/11-self_mini_c/subst.ml @@ -40,8 +40,6 @@ let rec replace : expression -> var_name -> var_name -> expression = | E_variable z -> let z = replace_var z in return @@ E_variable z - | E_make_empty_map _ -> e - | E_make_empty_big_map _ -> e | E_make_none _ -> e | E_iterator (name, ((v, tv), body), expr) -> let body = replace body in @@ -173,8 +171,7 @@ let rec subst_expression : body:expression -> x:var_name -> expr:expression -> e ) (* All that follows is boilerplate *) | E_literal _ | E_skip | E_make_none _ - | E_make_empty_map (_,_) - | E_make_empty_big_map _ as em -> return em + as em -> return em | E_constant (c) -> ( let lst = List.map self c.arguments in return @@ E_constant {cons_name = c.cons_name; arguments = lst } diff --git a/src/passes/12-compiler/compiler_program.ml b/src/passes/12-compiler/compiler_program.ml index 2ebfcbc34..200bd0cf9 100644 --- a/src/passes/12-compiler/compiler_program.ml +++ b/src/passes/12-compiler/compiler_program.ml @@ -76,6 +76,16 @@ let rec get_operator : constant' -> type_value -> expression list -> predicate r let%bind m_ty = Compiler_type.type_ ty' in ok @@ simple_constant @@ i_empty_set m_ty ) + | C_MAP_EMPTY -> ( + let%bind sd = Mini_c.get_t_map ty in + let%bind (src, dst) = bind_map_pair Compiler_type.type_ sd in + ok @@ simple_constant @@ i_empty_map src dst + ) + | C_BIG_MAP_EMPTY -> ( + let%bind sd = Mini_c.get_t_big_map ty in + let%bind (src, dst) = bind_map_pair Compiler_type.type_ sd in + ok @@ simple_constant @@ i_empty_big_map src dst + ) | C_BYTES_UNPACK -> ( let%bind ty' = Mini_c.get_t_option ty in let%bind m_ty = Compiler_type.type_ ty' in @@ -302,12 +312,6 @@ and translate_expression (expr:expression) (env:environment) : michelson result error title content in trace error @@ return code - | E_make_empty_map sd -> - let%bind (src, dst) = bind_map_pair Compiler_type.type_ sd in - return @@ i_empty_map src dst - | E_make_empty_big_map sd -> - let%bind (src, dst) = bind_map_pair Compiler_type.type_ sd in - return @@ i_empty_big_map src dst | E_make_none o -> let%bind o' = Compiler_type.type_ o in return @@ i_none o' diff --git a/src/passes/6-sugar_to_core/sugar_to_core.ml b/src/passes/6-sugar_to_core/sugar_to_core.ml index 6c664a45b..e79ab336f 100644 --- a/src/passes/6-sugar_to_core/sugar_to_core.ml +++ b/src/passes/6-sugar_to_core/sugar_to_core.ml @@ -108,18 +108,24 @@ let rec compile_expression : I.expression -> O.expression result = let%bind record = compile_expression record in let%bind update = compile_expression update in return @@ O.E_record_update {record;path;update} - | I.E_map map -> - let%bind map = bind_map_list ( - bind_map_pair compile_expression - ) map + | I.E_map map -> ( + let map = List.sort_uniq compare map in + let aux = fun prev (k, v) -> + let%bind (k', v') = bind_map_pair (compile_expression) (k, v) in + return @@ E_constant {cons_name=C_MAP_ADD;arguments=[k' ; v' ; prev]} in - return @@ O.E_map map - | I.E_big_map big_map -> - let%bind big_map = bind_map_list ( - bind_map_pair compile_expression - ) big_map + let%bind init = return @@ E_constant {cons_name=C_MAP_EMPTY;arguments=[]} in + bind_fold_list aux init map + ) + | I.E_big_map big_map -> ( + let map = List.sort_uniq compare big_map in + let aux = fun prev (k, v) -> + let%bind (k', v') = bind_map_pair (compile_expression) (k, v) in + return @@ E_constant {cons_name=C_BIG_MAP_ADD;arguments=[k' ; v' ; prev]} in - return @@ O.E_big_map big_map + let%bind init = return @@ E_constant {cons_name=C_BIG_MAP_EMPTY;arguments=[]} in + bind_fold_list aux init map + ) | I.E_list lst -> let%bind lst' = bind_map_list (compile_expression) lst in let aux = fun prev cur -> @@ -309,18 +315,6 @@ let rec uncompile_expression : O.expression -> I.expression result = let%bind record = uncompile_expression record in let%bind update = uncompile_expression update in return @@ I.E_record_update {record;path;update} - | O.E_map map -> - let%bind map = bind_map_list ( - bind_map_pair uncompile_expression - ) map - in - return @@ I.E_map map - | O.E_big_map big_map -> - let%bind big_map = bind_map_list ( - bind_map_pair uncompile_expression - ) big_map - in - return @@ I.E_big_map big_map | O.E_ascription {anno_expr; type_annotation} -> let%bind anno_expr = uncompile_expression anno_expr in let%bind type_annotation = uncompile_type_expression type_annotation in diff --git a/src/passes/8-typer-new/typer.ml b/src/passes/8-typer-new/typer.ml index 754659597..6166666bc 100644 --- a/src/passes/8-typer-new/typer.ml +++ b/src/passes/8-typer-new/typer.ml @@ -163,7 +163,6 @@ end open Errors -let swap (a,b) = ok (b,a) (* let rec type_program (p:I.program) : O.program result = let aux (e, acc:(environment * O.declaration Location.wrap list)) (d:I.declaration Location.wrap) = @@ -503,129 +502,6 @@ and type_expression : environment -> Solver.state -> ?tv_opt:O.type_expression - return_wrapped (E_record_update {record; path; update}) state (Wrap.record wrapped) (* Data-structure *) -(* - | E_list lst -> - let%bind lst' = bind_map_list (type_expression e) lst in - let%bind tv = - let aux opt c = - match opt with - | None -> ok (Some c) - | Some c' -> - let%bind _eq = Ast_typed.assert_type_value_eq (c, c') in - ok (Some c') in - let%bind init = match tv_opt with - | None -> ok None - | Some ty -> - let%bind ty' = get_t_list ty in - ok (Some ty') in - let%bind ty = - let%bind opt = bind_fold_list aux init - @@ List.map get_type_annotation lst' in - trace_option (needs_annotation ae "empty list") opt in - ok (t_list ty ()) - in - return (E_list lst') tv - | E_set lst -> - let%bind lst' = bind_map_list (type_expression e) lst in - let%bind tv = - let aux opt c = - match opt with - | None -> ok (Some c) - | Some c' -> - let%bind _eq = Ast_typed.assert_type_value_eq (c, c') in - ok (Some c') in - let%bind init = match tv_opt with - | None -> ok None - | Some ty -> - let%bind ty' = get_t_set ty in - ok (Some ty') in - let%bind ty = - let%bind opt = bind_fold_list aux init - @@ List.map get_type_annotation lst' in - trace_option (needs_annotation ae "empty set") opt in - ok (t_set ty ()) - in - return (E_set lst') tv - | E_map lst -> - let%bind lst' = bind_map_list (bind_map_pair (type_expression e)) lst in - let%bind tv = - let aux opt c = - match opt with - | None -> ok (Some c) - | Some c' -> - let%bind _eq = Ast_typed.assert_type_value_eq (c, c') in - ok (Some c') in - let%bind key_type = - let%bind sub = - bind_fold_list aux None - @@ List.map get_type_annotation - @@ List.map fst lst' in - let%bind annot = bind_map_option get_t_map_key tv_opt in - trace (simple_info "empty map expression without a type annotation") @@ - O.merge_annotation annot sub (needs_annotation ae "this map literal") - in - let%bind value_type = - let%bind sub = - bind_fold_list aux None - @@ List.map get_type_annotation - @@ List.map snd lst' in - let%bind annot = bind_map_option get_t_map_value tv_opt in - trace (simple_info "empty map expression without a type annotation") @@ - O.merge_annotation annot sub (needs_annotation ae "this map literal") - in - ok (t_map key_type value_type ()) - in - return (E_map lst') tv -*) - - | E_map map -> - let aux' state' elt = type_expression e state' elt >>? swap in - let aux = fun state' elt -> bind_fold_map_pair aux' state' elt in - let%bind (state', map') = - bind_fold_map_list aux state map in - let aux (x, y) = O.(x.type_expression , y.type_expression) in - let wrapped = Wrap.map (List.map aux map') in - return_wrapped (E_map map') state' wrapped - - (* | E_big_map lst -> - * let%bind lst' = bind_map_list (bind_map_pair (type_expression e)) lst in - * let%bind tv = - * let aux opt c = - * match opt with - * | None -> ok (Some c) - * | Some c' -> - * let%bind _eq = Ast_typed.assert_type_value_eq (c, c') in - * ok (Some c') in - * let%bind key_type = - * let%bind sub = - * bind_fold_list aux None - * @@ List.map get_type_annotation - * @@ List.map fst lst' in - * let%bind annot = bind_map_option get_t_big_map_key tv_opt in - * trace (simple_info "empty map expression without a type annotation") @@ - * O.merge_annotation annot sub (needs_annotation ae "this map literal") - * in - * let%bind value_type = - * let%bind sub = - * bind_fold_list aux None - * @@ List.map get_type_annotation - * @@ List.map snd lst' in - * let%bind annot = bind_map_option get_t_big_map_value tv_opt in - * trace (simple_info "empty map expression without a type annotation") @@ - * O.merge_annotation annot sub (needs_annotation ae "this map literal") - * in - * ok (t_big_map key_type value_type ()) - * in - * return (E_big_map lst') tv *) - | E_big_map big_map -> - let aux' state' elt = type_expression e state' elt >>? swap in - let aux = fun state' elt -> bind_fold_map_pair aux' state' elt in - let%bind (state', big_map') = - bind_fold_map_list aux state big_map in - let aux (x, y) = O.(x.type_expression , y.type_expression) in - let wrapped = Wrap.big_map (List.map aux big_map') in - return_wrapped (E_big_map big_map') state' wrapped - (* | E_lambda { * binder ; * input_type ; @@ -1042,12 +918,6 @@ let rec untype_expression (e:O.expression) : (I.expression) result = let%bind e = untype_expression update in let Label l = path in return (e_update r' l e) - | E_map m -> - let%bind m' = bind_map_list (bind_map_pair untype_expression) m in - return (e_map m') - | E_big_map m -> - let%bind m' = bind_map_list (bind_map_pair untype_expression) m in - return (e_big_map m') | E_matching {matchee;cases} -> let%bind ae' = untype_expression matchee in let%bind m' = untype_matching untype_expression cases in diff --git a/src/passes/8-typer-old/typer.ml b/src/passes/8-typer-old/typer.ml index 879c44b19..ce64fa5b9 100644 --- a/src/passes/8-typer-old/typer.ml +++ b/src/passes/8-typer-old/typer.ml @@ -511,66 +511,6 @@ and type_expression' : environment -> ?tv_opt:O.type_expression -> I.expression let%bind () = O.assert_type_expression_eq (tv, get_type_expression update) in return (E_record_update {record; path; update}) wrapped (* Data-structure *) - | E_map lst -> - let%bind lst' = bind_map_list (bind_map_pair (type_expression' e)) lst in - let%bind tv = - let aux opt c = - match opt with - | None -> ok (Some c) - | Some c' -> - let%bind _eq = Ast_typed.assert_type_expression_eq (c, c') in - ok (Some c') in - let%bind key_type = - let%bind sub = - bind_fold_list aux None - @@ List.map get_type_expression - @@ List.map fst lst' in - let%bind annot = bind_map_option get_t_map_key tv_opt in - trace (simple_info "empty map expression without a type annotation") @@ - O.merge_annotation annot sub (needs_annotation ae "this map literal") - in - let%bind value_type = - let%bind sub = - bind_fold_list aux None - @@ List.map get_type_expression - @@ List.map snd lst' in - let%bind annot = bind_map_option get_t_map_value tv_opt in - trace (simple_info "empty map expression without a type annotation") @@ - O.merge_annotation annot sub (needs_annotation ae "this map literal") - in - ok (t_map key_type value_type ()) - in - return (E_map lst') tv - | E_big_map lst -> - let%bind lst' = bind_map_list (bind_map_pair (type_expression' e)) lst in - let%bind tv = - let aux opt c = - match opt with - | None -> ok (Some c) - | Some c' -> - let%bind _eq = Ast_typed.assert_type_expression_eq (c, c') in - ok (Some c') in - let%bind key_type = - let%bind sub = - bind_fold_list aux None - @@ List.map get_type_expression - @@ List.map fst lst' in - let%bind annot = bind_map_option get_t_big_map_key tv_opt in - trace (simple_info "empty map expression without a type annotation") @@ - O.merge_annotation annot sub (needs_annotation ae "this map literal") - in - let%bind value_type = - let%bind sub = - bind_fold_list aux None - @@ List.map get_type_expression - @@ List.map snd lst' in - let%bind annot = bind_map_option get_t_big_map_value tv_opt in - trace (simple_info "empty map expression without a type annotation") @@ - O.merge_annotation annot sub (needs_annotation ae "this map literal") - in - ok (t_big_map key_type value_type ()) - in - return (E_big_map lst') tv | E_lambda lambda -> let%bind (lambda, lambda_type) = type_lambda e lambda in return (E_lambda lambda ) lambda_type @@ -655,6 +595,34 @@ and type_expression' : environment -> ?tv_opt:O.type_expression -> I.expression let tv_lst = [tv_key;tv_set] in let%bind (name', tv) = type_constant cst tv_lst tv_opt in return (E_constant {cons_name=name';arguments=[key';set']}) tv + | E_constant {cons_name=C_MAP_ADD as cst; arguments=[key;value;map]} -> + let%bind key' = type_expression' e key in + let%bind val' = type_expression' e value in + let tv_key = get_type_expression key' in + let tv_val = get_type_expression val' in + let tv = match tv_opt with + Some (tv) -> tv + | None -> t_map tv_key tv_val () + in + let%bind map' = type_expression' e ~tv_opt:tv map in + let tv_map = get_type_expression map' in + let tv_lst = [tv_key;tv_val;tv_map] in + let%bind (name', tv) = type_constant cst tv_lst tv_opt in + return (E_constant {cons_name=name';arguments=[key';val';map']}) tv + | E_constant {cons_name=C_BIG_MAP_ADD as cst; arguments=[key;value;map]} -> + let%bind key' = type_expression' e key in + let%bind val' = type_expression' e value in + let tv_key = get_type_expression key' in + let tv_val = get_type_expression val' in + let tv = match tv_opt with + Some (tv) -> tv + | None -> t_big_map tv_key tv_val () + in + let%bind map' = type_expression' e ~tv_opt:tv map in + let tv_map = get_type_expression map' in + let tv_lst = [tv_key;tv_val;tv_map] in + let%bind (name', tv) = type_constant cst tv_lst tv_opt in + return (E_constant {cons_name=name';arguments=[key';val';map']}) tv | E_constant {cons_name;arguments} -> let%bind lst' = bind_list @@ List.map (type_expression' e) arguments in let tv_lst = List.map get_type_expression lst' in @@ -838,12 +806,6 @@ let rec untype_expression (e:O.expression) : (I.expression) result = let%bind e = untype_expression e in let Label l = l in return (e_update r' l e) - | E_map m -> - let%bind m' = bind_map_list (bind_map_pair untype_expression) m in - return (e_map m') - | E_big_map m -> - let%bind m' = bind_map_list (bind_map_pair untype_expression) m in - return (e_big_map m') | E_matching {matchee;cases} -> let%bind ae' = untype_expression matchee in let%bind m' = untype_matching untype_expression cases in diff --git a/src/passes/9-self_ast_typed/helpers.ml b/src/passes/9-self_ast_typed/helpers.ml index 76d6c0f21..92e887282 100644 --- a/src/passes/9-self_ast_typed/helpers.ml +++ b/src/passes/9-self_ast_typed/helpers.ml @@ -12,10 +12,6 @@ let rec fold_expression : 'a folder -> 'a -> expression -> 'a result = fun f ini let%bind res = bind_fold_list self init' lst in ok res ) - | E_map lst | E_big_map lst -> ( - let%bind res = bind_fold_list (bind_fold_pair self) init' lst in - ok res - ) | E_application {lamb; args} -> ( let ab = (lamb, args) in let%bind res = bind_fold_pair self init' ab in @@ -90,14 +86,6 @@ let rec map_expression : mapper -> expression -> expression result = fun f e -> let%bind e' = f e in let return expression_content = ok { e' with expression_content } in match e'.expression_content with - | E_map lst -> ( - let%bind lst' = bind_map_list (bind_map_pair self) lst in - return @@ E_map lst' - ) - | E_big_map lst -> ( - let%bind lst' = bind_map_list (bind_map_pair self) lst in - return @@ E_big_map lst' - ) | E_matching {matchee=e;cases} -> ( let%bind e' = self e in let%bind cases' = map_cases f cases in @@ -193,14 +181,6 @@ let rec fold_map_expression : 'a fold_mapper -> 'a -> expression -> ('a * expres else let return expression_content = { e' with expression_content } in match e'.expression_content with - | E_map lst -> ( - let%bind (res, lst') = bind_fold_map_list (bind_fold_map_pair self) init' lst in - ok (res, return @@ E_map lst') - ) - | E_big_map lst -> ( - let%bind (res, lst') = bind_fold_map_list (bind_fold_map_pair self) init' lst in - ok (res, return @@ E_big_map lst') - ) | E_matching {matchee=e;cases} -> ( let%bind (res, e') = self init' e in let%bind (res,cases') = fold_map_cases f res cases in diff --git a/src/passes/9-self_ast_typed/tail_recursion.ml b/src/passes/9-self_ast_typed/tail_recursion.ml index f5f30e923..00847e79f 100644 --- a/src/passes/9-self_ast_typed/tail_recursion.ml +++ b/src/passes/9-self_ast_typed/tail_recursion.ml @@ -56,14 +56,6 @@ let rec check_recursive_call : expression_variable -> bool -> expression -> unit let%bind _ = check_recursive_call n false record in let%bind _ = check_recursive_call n false update in ok () - | E_map eel | E_big_map eel-> - let aux (e1,e2) = - let%bind _ = check_recursive_call n false e1 in - let%bind _ = check_recursive_call n false e2 in - ok () - in - let%bind _ = bind_map_list aux eel in - ok () and check_recursive_call_in_matching = fun n final_path c -> match c with diff --git a/src/passes/operators/operators.ml b/src/passes/operators/operators.ml index 8e011b316..9e6a093af 100644 --- a/src/passes/operators/operators.ml +++ b/src/passes/operators/operators.ml @@ -621,6 +621,11 @@ module Typer = struct let%bind () = assert_type_expression_eq (src , k) in ok m + let map_empty = typer_0 "MAP_EMPTY" @@ fun tv_opt -> + match tv_opt with + | None -> simple_fail "untyped MAP_EMPTY" + | Some t -> ok t + let map_add : typer = typer_3 "MAP_ADD" @@ fun k v m -> let%bind (src, dst) = bind_map_or (get_t_map , get_t_big_map) m in let%bind () = assert_type_expression_eq (src, k) in @@ -1165,6 +1170,7 @@ module Typer = struct | C_LIST_MAP -> ok @@ list_map ; | C_LIST_FOLD -> ok @@ list_fold ; (* MAP *) + | C_MAP_EMPTY -> ok @@ map_empty ; | C_MAP_ADD -> ok @@ map_add ; | C_MAP_REMOVE -> ok @@ map_remove ; | C_MAP_UPDATE -> ok @@ map_update ; diff --git a/src/stages/3-ast_core/PP.ml b/src/stages/3-ast_core/PP.ml index 149a92631..10ab7e9d4 100644 --- a/src/stages/3-ast_core/PP.ml +++ b/src/stages/3-ast_core/PP.ml @@ -31,10 +31,6 @@ and expression_content ppf (ec : expression_content) = fprintf ppf "%a.%a" expression ra.record label ra.label | E_record_update {record; path; update} -> fprintf ppf "{ %a with { %a = %a } }" expression record label path expression update - | E_map m -> - fprintf ppf "map[%a]" (list_sep_d assoc_expression) m - | E_big_map m -> - fprintf ppf "big_map[%a]" (list_sep_d assoc_expression) m | E_lambda {binder; input_type; output_type; result} -> fprintf ppf "lambda (%a:%a) : %a return %a" expression_variable binder diff --git a/src/stages/3-ast_core/combinators.ml b/src/stages/3-ast_core/combinators.ml index a1fdd1242..2c5dbf56d 100644 --- a/src/stages/3-ast_core/combinators.ml +++ b/src/stages/3-ast_core/combinators.ml @@ -107,12 +107,10 @@ let e_bytes_raw ?loc (b: bytes) : expression = make_expr ?loc @@ E_literal (Literal_bytes b) let e_bytes_string ?loc (s: string) : expression = make_expr ?loc @@ E_literal (Literal_bytes (Hex.to_bytes (Hex.of_string s))) -let e_big_map ?loc lst : expression = make_expr ?loc @@ E_big_map lst let e_some ?loc s : expression = make_expr ?loc @@ E_constant {cons_name = C_SOME; arguments = [s]} let e_none ?loc () : expression = make_expr ?loc @@ E_constant {cons_name = C_NONE; arguments = []} let e_string_cat ?loc sl sr : expression = make_expr ?loc @@ E_constant {cons_name = C_CONCAT; arguments = [sl ; sr ]} let e_map_add ?loc k v old : expression = make_expr ?loc @@ E_constant {cons_name = C_MAP_ADD; arguments = [k ; v ; old]} -let e_map ?loc lst : expression = make_expr ?loc @@ E_map lst let e_constructor ?loc s a : expression = make_expr ?loc @@ E_constructor { constructor = Constructor s; element = a} let e_matching ?loc a b : expression = make_expr ?loc @@ E_matching {matchee=a;cases=b} let e_matching_bool ?loc a b c : expression = e_matching ?loc a (Match_bool {match_true = b ; match_false = c}) @@ -158,10 +156,6 @@ let e_typed_none ?loc t_opt = let type_annotation = t_option t_opt in e_annotation ?loc (e_none ?loc ()) type_annotation -let e_typed_map ?loc lst k v = e_annotation ?loc (e_map lst) (t_map k v) -let e_typed_big_map ?loc lst k v = e_annotation ?loc (e_big_map lst) (t_big_map k v) - - let e_lambda ?loc (binder : expression_variable) (input_type : type_expression option) (output_type : type_expression option) @@ -255,6 +249,12 @@ let extract_record : expression -> (label * expression) list result = fun e -> | _ -> fail @@ bad_kind "record" e.location let extract_map : expression -> (expression * expression) list result = fun e -> - match e.expression_content with - | E_map lst -> ok lst - | _ -> fail @@ bad_kind "map" e.location + let rec aux e = + match e.expression_content with + E_constant {cons_name=C_UPDATE; arguments=[k;v;map]} -> + let%bind map = aux map in + ok @@ (k,v)::map + | E_constant {cons_name=C_MAP_EMPTY; arguments=[]} -> ok @@ [] + | _ -> fail @@ bad_kind "map" e.location + in + aux e diff --git a/src/stages/3-ast_core/combinators.mli b/src/stages/3-ast_core/combinators.mli index dfb78bf43..40a2a8496 100644 --- a/src/stages/3-ast_core/combinators.mli +++ b/src/stages/3-ast_core/combinators.mli @@ -65,7 +65,6 @@ val e'_bytes : string -> expression_content result val e_bytes_hex : ?loc:Location.t -> string -> expression result val e_bytes_raw : ?loc:Location.t -> bytes -> expression val e_bytes_string : ?loc:Location.t -> string -> expression -val e_big_map : ?loc:Location.t -> ( expr * expr ) list -> expression val e_record_ez : ?loc:Location.t -> ( string * expr ) list -> expression val e_tuple : ?loc:Location.t -> expression list -> expression @@ -73,7 +72,6 @@ val e_some : ?loc:Location.t -> expression -> expression val e_none : ?loc:Location.t -> unit -> expression val e_string_cat : ?loc:Location.t -> expression -> expression -> expression val e_map_add : ?loc:Location.t -> expression -> expression -> expression -> expression -val e_map : ?loc:Location.t -> ( expression * expression ) list -> expression val e_pair : ?loc:Location.t -> expression -> expression -> expression val e_constructor : ?loc:Location.t -> string -> expression -> expression val e_matching : ?loc:Location.t -> expression -> matching_expr -> expression @@ -94,9 +92,6 @@ val make_option_typed : ?loc:Location.t -> expression -> type_expression option val e_typed_none : ?loc:Location.t -> type_expression -> expression -val e_typed_map : ?loc:Location.t -> ( expression * expression ) list -> type_expression -> type_expression -> expression -val e_typed_big_map : ?loc:Location.t -> ( expression * expression ) list -> type_expression -> type_expression -> expression - val e_lambda : ?loc:Location.t -> expression_variable -> type_expression option -> type_expression option -> expression -> expression val e_recursive : ?loc:Location.t -> expression_variable -> type_expression -> lambda -> expression val e_record : ?loc:Location.t -> expr Map.String.t -> expression diff --git a/src/stages/3-ast_core/misc.ml b/src/stages/3-ast_core/misc.ml index fc5319eed..ad38b9fec 100644 --- a/src/stages/3-ast_core/misc.ml +++ b/src/stages/3-ast_core/misc.ml @@ -88,19 +88,6 @@ let rec assert_value_eq (a, b: (expression * expression )) : unit result = assert_literal_eq (a, b) | E_literal _ , _ -> simple_fail "comparing a literal with not a literal" - | E_constant {cons_name=C_SET_LITERAL;arguments=lsta}, - E_constant {cons_name=C_SET_LITERAL;arguments=lstb} -> ( - let lsta' = List.sort (compare) lsta in - let lstb' = List.sort (compare) lstb in - let%bind lst = - generic_try (simple_error "set of different lengths") - (fun () -> List.combine lsta' lstb') in - let%bind _all = bind_map_list assert_value_eq lst in - ok () - ) - | E_constant {cons_name=C_SET_LITERAL;_}, _ -> - simple_fail "comparing set with other expression" - | E_constant (ca) , E_constant (cb) when ca.cons_name = cb.cons_name -> ( let%bind lst = generic_try (simple_error "constants with different number of elements") @@ -152,23 +139,6 @@ let rec assert_value_eq (a, b: (expression * expression )) : unit result = ok () | E_record_update _, _ -> simple_fail "comparing record update with other expression" - - | (E_map lsta, E_map lstb | E_big_map lsta, E_big_map lstb) -> ( - let%bind lst = generic_try (simple_error "maps of different lengths") - (fun () -> - let lsta' = List.sort compare lsta in - let lstb' = List.sort compare lstb in - List.combine lsta' lstb') in - let aux = fun ((ka, va), (kb, vb)) -> - let%bind _ = assert_value_eq (ka, kb) in - let%bind _ = assert_value_eq (va, vb) in - ok () in - let%bind _all = bind_map_list aux lst in - ok () - ) - | (E_map _ | E_big_map _), _ -> - simple_fail "comparing map with other expression" - | (E_ascription a , _b') -> assert_value_eq (a.anno_expr , b) | (_a' , E_ascription b) -> assert_value_eq (a , b.anno_expr) | (E_variable _, _) | (E_lambda _, _) diff --git a/src/stages/3-ast_core/types.ml b/src/stages/3-ast_core/types.ml index ae3bb2283..ac59228c7 100644 --- a/src/stages/3-ast_core/types.ml +++ b/src/stages/3-ast_core/types.ml @@ -43,10 +43,6 @@ and expression_content = | E_record of expression label_map | E_record_accessor of accessor | E_record_update of update - (* Data Structures *) - (* TODO : move to constant*) - | E_map of (expression * expression) list (*move to operator *) - | E_big_map of (expression * expression) list (*move to operator *) (* Advanced *) | E_ascription of ascription diff --git a/src/stages/4-ast_typed/PP.ml b/src/stages/4-ast_typed/PP.ml index 62e085c51..7461dd7de 100644 --- a/src/stages/4-ast_typed/PP.ml +++ b/src/stages/4-ast_typed/PP.ml @@ -32,10 +32,6 @@ and expression_content ppf (ec: expression_content) = fprintf ppf "%a.%a" expression ra.record label ra.label | E_record_update {record; path; update} -> fprintf ppf "{ %a with { %a = %a } }" expression record label path expression update - | E_map m -> - fprintf ppf "map[%a]" (list_sep_d assoc_expression) m - | E_big_map m -> - fprintf ppf "big_map[%a]" (list_sep_d assoc_expression) m | E_lambda {binder; result} -> fprintf ppf "lambda (%a) return %a" expression_variable binder expression result diff --git a/src/stages/4-ast_typed/combinators.ml b/src/stages/4-ast_typed/combinators.ml index 9a8bf2af5..9961dbc0a 100644 --- a/src/stages/4-ast_typed/combinators.ml +++ b/src/stages/4-ast_typed/combinators.ml @@ -276,8 +276,6 @@ let ez_e_record (lst : (label * expression) list) : expression_content = let e_some s : expression_content = E_constant {cons_name=C_SOME;arguments=[s]} let e_none (): expression_content = E_constant {cons_name=C_NONE; arguments=[]} -let e_map lst : expression_content = E_map lst - let e_unit () : expression_content = E_literal (Literal_unit) let e_int n : expression_content = E_literal (Literal_int n) let e_nat n : expression_content = E_literal (Literal_nat n) @@ -313,7 +311,6 @@ let e_a_record r = make_a_e (e_record r) (t_record (LMap.map get_type_expression let e_a_application a b = make_a_e (e_application a b) (get_type_expression b) let e_a_variable v ty = make_a_e (e_variable v) ty let ez_e_a_record r = make_a_e (ez_e_record r) (ez_t_record (List.map (fun (x, y) -> x, y.type_expression) r) ()) -let e_a_map lst k v = make_a_e (e_map lst) (t_map k v ()) let e_a_let_in binder expr body attributes = make_a_e (e_let_in binder expr body attributes) (get_type_expression body) diff --git a/src/stages/4-ast_typed/combinators.mli b/src/stages/4-ast_typed/combinators.mli index 9bbd75504..dd88ffd00 100644 --- a/src/stages/4-ast_typed/combinators.mli +++ b/src/stages/4-ast_typed/combinators.mli @@ -109,7 +109,6 @@ val ez_e_record : ( string * expression ) list -> expression *) val e_some : expression -> expression_content val e_none : unit -> expression_content -val e_map : ( expression * expression ) list -> expression_content val e_unit : unit -> expression_content val e_int : int -> expression_content val e_nat : int -> expression_content @@ -145,7 +144,6 @@ val e_a_record : expression label_map -> full_environment -> expression val e_a_application : expression -> expression -> full_environment -> expression val e_a_variable : expression_variable -> type_expression -> full_environment -> expression val ez_e_a_record : ( label * expression ) list -> full_environment -> expression -val e_a_map : ( expression * expression ) list -> type_expression -> type_expression -> full_environment -> expression val e_a_let_in : expression_variable -> bool -> expression -> expression -> full_environment -> expression val get_a_int : expression -> int result diff --git a/src/stages/4-ast_typed/combinators_environment.ml b/src/stages/4-ast_typed/combinators_environment.ml index e296ae914..f73c2b305 100644 --- a/src/stages/4-ast_typed/combinators_environment.ml +++ b/src/stages/4-ast_typed/combinators_environment.ml @@ -14,7 +14,6 @@ let e_a_empty_pair a b = e_a_pair a b Environment.full_empty let e_a_empty_some s = e_a_some s Environment.full_empty let e_a_empty_none t = e_a_none t Environment.full_empty let e_a_empty_record r = e_a_record r Environment.full_empty -let e_a_empty_map lst k v = e_a_map lst k v Environment.full_empty let ez_e_a_empty_record r = ez_e_a_record r Environment.full_empty let e_a_empty_lambda l i o = e_a_lambda l i o Environment.full_empty diff --git a/src/stages/4-ast_typed/combinators_environment.mli b/src/stages/4-ast_typed/combinators_environment.mli index ceb438afe..830ac7ee2 100644 --- a/src/stages/4-ast_typed/combinators_environment.mli +++ b/src/stages/4-ast_typed/combinators_environment.mli @@ -13,7 +13,6 @@ val e_a_empty_pair : expression -> expression -> expression val e_a_empty_some : expression -> expression val e_a_empty_none : type_expression -> expression val e_a_empty_record : expression label_map -> expression -val e_a_empty_map : (expression * expression ) list -> type_expression -> type_expression -> expression val ez_e_a_empty_record : ( label * expression ) list -> expression val e_a_empty_lambda : lambda -> type_expression -> type_expression -> expression diff --git a/src/stages/4-ast_typed/misc.ml b/src/stages/4-ast_typed/misc.ml index 6966fe414..f101f3857 100644 --- a/src/stages/4-ast_typed/misc.ml +++ b/src/stages/4-ast_typed/misc.ml @@ -211,7 +211,6 @@ module Free_variables = struct | E_record m -> unions @@ List.map self @@ LMap.to_list m | E_record_accessor {record;_} -> self record | E_record_update {record; update;_} -> union (self record) @@ self update - | (E_map m | E_big_map m) -> unions @@ List.map self @@ List.concat @@ List.map (fun (a, b) -> [ a ; b ]) m | E_matching {matchee; cases;_} -> union (self matchee) (matching_expression b cases) | E_let_in { let_binder; rhs; let_result; _} -> let b' = union (singleton let_binder) b in @@ -494,22 +493,6 @@ let rec assert_value_eq (a, b: (expression*expression)) : unit result = | E_record _, _ -> fail @@ (different_values_because_different_types "record vs. non-record" a b) - | (E_map lsta, E_map lstb | E_big_map lsta, E_big_map lstb) -> ( - let%bind lst = generic_try (different_size_values "maps of different lengths" a b) - (fun () -> - let lsta' = List.sort compare lsta in - let lstb' = List.sort compare lstb in - List.combine lsta' lstb') in - let aux = fun ((ka, va), (kb, vb)) -> - let%bind _ = assert_value_eq (ka, kb) in - let%bind _ = assert_value_eq (va, vb) in - ok () in - let%bind _all = bind_map_list aux lst in - ok () - ) - | (E_map _ | E_big_map _), _ -> - fail @@ different_values_because_different_types "map vs. non-map" a b - | (E_literal _, _) | (E_variable _, _) | (E_application _, _) | (E_lambda _, _) | (E_let_in _, _) | (E_recursive _, _) | (E_record_accessor _, _) | (E_record_update _,_) diff --git a/src/stages/4-ast_typed/misc_smart.ml b/src/stages/4-ast_typed/misc_smart.ml index fa34a5014..b4a0b5095 100644 --- a/src/stages/4-ast_typed/misc_smart.ml +++ b/src/stages/4-ast_typed/misc_smart.ml @@ -75,9 +75,6 @@ module Captured_variables = struct let%bind r = self record in let%bind e = self update in ok @@ union r e - | (E_map m | E_big_map m) -> - let%bind lst' = bind_map_list self @@ List.concat @@ List.map (fun (a, b) -> [ a ; b ]) m in - ok @@ unions lst' | E_matching {matchee;cases;_} -> let%bind a' = self matchee in let%bind cs' = matching_expression b cases in diff --git a/src/stages/4-ast_typed/types.ml b/src/stages/4-ast_typed/types.ml index b892d499f..e5ef3dd36 100644 --- a/src/stages/4-ast_typed/types.ml +++ b/src/stages/4-ast_typed/types.ml @@ -49,10 +49,6 @@ and expression_content = | E_record of expression label_map | E_record_accessor of accessor | E_record_update of update - (* Data Structures *) - (* TODO : move to constant*) - | E_map of (expression * expression) list (*move to operator *) - | E_big_map of (expression * expression) list (*move to operator *) and constant = { cons_name: constant' diff --git a/src/stages/5-mini_c/PP.ml b/src/stages/5-mini_c/PP.ml index 8f0316253..1231f5ed6 100644 --- a/src/stages/5-mini_c/PP.ml +++ b/src/stages/5-mini_c/PP.ml @@ -86,8 +86,6 @@ and expression' ppf (e:expression') = match e with | E_constant c -> fprintf ppf "%a %a" constant c.cons_name (pp_print_list ~pp_sep:space_sep expression) c.arguments | E_literal v -> fprintf ppf "L(%a)" value v - | E_make_empty_map _ -> fprintf ppf "map[]" - | E_make_empty_big_map _ -> fprintf ppf "big_map[]" | E_make_none _ -> fprintf ppf "none" | E_if_bool (c, a, b) -> fprintf ppf "%a ? %a : %a" expression c expression a expression b | E_if_none (c, n, ((name, _) , s)) -> fprintf ppf "%a ?? %a : %a -> %a" expression c expression n Var.pp name expression s @@ -219,6 +217,7 @@ and constant ppf : constant' -> unit = function | C_MAP_FIND_OPT -> fprintf ppf "MAP_FIND_OP" (* Big Maps *) | C_BIG_MAP -> fprintf ppf "BIG_MAP" + | C_BIG_MAP_ADD -> fprintf ppf "BIG_MAP_ADD" | C_BIG_MAP_EMPTY -> fprintf ppf "BIG_MAP_EMPTY" | C_BIG_MAP_LITERAL -> fprintf ppf "BIG_MAP_LITERAL" (* Crypto *) diff --git a/src/stages/5-mini_c/misc.ml b/src/stages/5-mini_c/misc.ml index bbcee5ed1..5aef14523 100644 --- a/src/stages/5-mini_c/misc.ml +++ b/src/stages/5-mini_c/misc.ml @@ -44,8 +44,6 @@ module Free_variables = struct | E_constant (c) -> unions @@ List.map self c.arguments | E_application (f, x) -> unions @@ [ self f ; self x ] | E_variable n -> var_name b n - | E_make_empty_map _ -> empty - | E_make_empty_big_map _ -> empty | E_make_none _ -> empty | E_iterator (_, ((v, _), body), expr) -> unions [ expression (union (singleton v) b) body ; diff --git a/src/stages/5-mini_c/types.ml b/src/stages/5-mini_c/types.ml index f16054a25..8461df787 100644 --- a/src/stages/5-mini_c/types.ml +++ b/src/stages/5-mini_c/types.ml @@ -59,8 +59,6 @@ and expression' = | E_constant of constant | E_application of (expression * expression) | E_variable of var_name - | E_make_empty_map of (type_value * type_value) - | E_make_empty_big_map of (type_value * type_value) | E_make_none of type_value | E_iterator of constant' * ((var_name * type_value) * expression) * expression | E_fold of (((var_name * type_value) * expression) * expression * expression) diff --git a/src/stages/common/PP.ml b/src/stages/common/PP.ml index f8d594a89..b5da63ec4 100644 --- a/src/stages/common/PP.ml +++ b/src/stages/common/PP.ml @@ -127,6 +127,7 @@ let constant ppf : constant' -> unit = function | C_MAP_FIND_OPT -> fprintf ppf "MAP_FIND_OP" (* Big Maps *) | C_BIG_MAP -> fprintf ppf "BIG_MAP" + | C_BIG_MAP_ADD -> fprintf ppf "BIG_MAP_ADD" | C_BIG_MAP_EMPTY -> fprintf ppf "BIG_MAP_EMPTY" | C_BIG_MAP_LITERAL -> fprintf ppf "BIG_MAP_LITERAL" (* Crypto *) diff --git a/src/stages/common/types.ml b/src/stages/common/types.ml index 6ffdb5485..5d62b62c7 100644 --- a/src/stages/common/types.ml +++ b/src/stages/common/types.ml @@ -269,6 +269,7 @@ and constant' = | C_MAP_FIND_OPT (* Big Maps *) | C_BIG_MAP + | C_BIG_MAP_ADD | C_BIG_MAP_EMPTY | C_BIG_MAP_LITERAL (* Crypto *) diff --git a/src/stages/typesystem/misc.ml b/src/stages/typesystem/misc.ml index ce7937c7e..545534b77 100644 --- a/src/stages/typesystem/misc.ml +++ b/src/stages/typesystem/misc.ml @@ -190,20 +190,6 @@ module Substitution = struct let%bind record = s_expression ~substs record in let%bind update = s_expression ~substs update in ok @@ T.E_record_update {record;path;update} - | T.E_map val_val_list -> - let%bind val_val_list = bind_map_list (fun (val1 , val2) -> - let%bind val1 = s_expression ~substs val1 in - let%bind val2 = s_expression ~substs val2 in - ok @@ (val1 , val2) - ) val_val_list in - ok @@ T.E_map val_val_list - | T.E_big_map val_val_list -> - let%bind val_val_list = bind_map_list (fun (val1 , val2) -> - let%bind val1 = s_expression ~substs val1 in - let%bind val2 = s_expression ~substs val2 in - ok @@ (val1 , val2) - ) val_val_list in - ok @@ T.E_big_map val_val_list | T.E_matching {matchee;cases} -> let%bind matchee = s_expression ~substs matchee in let%bind cases = s_matching_expr ~substs cases in From 6a9547e9109adedf1c5607075b0405a5e35f31e6 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Wulfman Date: Wed, 25 Mar 2020 19:10:04 +0100 Subject: [PATCH 08/13] add type map_or_big_map to solve issue of typing big_map_empty from map_add --- .../expect_tests/ligo_interpreter_tests.ml | 6 +++--- src/passes/10-interpreter/interpreter.ml | 1 + src/passes/10-transpiler/transpiler.ml | 2 ++ src/passes/10-transpiler/untranspiler.ml | 16 ++++++++------- .../imperative_to_sugar.ml | 6 ++++++ src/passes/6-sugar_to_core/sugar_to_core.ml | 14 +++++++++---- src/passes/8-typer-new/solver.ml | 2 ++ src/passes/8-typer-new/typer.ml | 8 ++++++++ src/passes/8-typer-old/typer.ml | 20 +++++-------------- src/passes/operators/operators.ml | 12 ++++++++++- src/stages/4-ast_typed/combinators.ml | 3 +++ src/stages/4-ast_typed/combinators.mli | 1 + src/stages/4-ast_typed/misc.ml | 9 +++++---- src/stages/5-mini_c/PP.ml | 1 - src/stages/common/PP.ml | 2 +- src/stages/common/types.ml | 6 +++++- 16 files changed, 72 insertions(+), 37 deletions(-) diff --git a/src/bin/expect_tests/ligo_interpreter_tests.ml b/src/bin/expect_tests/ligo_interpreter_tests.ml index 9a6069338..5910c0669 100644 --- a/src/bin/expect_tests/ligo_interpreter_tests.ml +++ b/src/bin/expect_tests/ligo_interpreter_tests.ml @@ -43,12 +43,12 @@ let%expect_test _ = val map_finds = Some(2 : int) val map_finds_fail = "failed map find" : failure val map_empty = { ; 0 = ([]) ; 1 = ([]) } - val m = [ ; "one" : string -> 1 : int ; "two" : string -> 2 : int ; "three" : string -> 3 : int] + val m = [ ; "one" : string -> 1 : int ; "three" : string -> 3 : int ; "two" : string -> 2 : int] val map_fold = 4 : int val map_iter = unit - val map_map = [ ; "one" : string -> 4 : int ; "two" : string -> 5 : int ; "three" : string -> 8 : int] + val map_map = [ ; "one" : string -> 4 : int ; "three" : string -> 8 : int ; "two" : string -> 5 : int] val map_mem = { ; 0 = (true) ; 1 = (false) } - val map_remove = { ; 0 = ([ ; "two" : string -> 2 : int ; "three" : string -> 3 : int]) ; 1 = ([ ; "one" : string -> 1 : int ; "two" : string -> 2 : int ; "three" : string -> 3 : int]) } + val map_remove = { ; 0 = ([ ; "three" : string -> 3 : int ; "two" : string -> 2 : int]) ; 1 = ([ ; "one" : string -> 1 : int ; "three" : string -> 3 : int ; "two" : string -> 2 : int]) } val map_update = { ; 0 = ([ ; "one" : string -> 1 : int]) ; 1 = ([]) ; 2 = ([]) ; 3 = ([ ; "one" : string -> 1 : int]) } val s = { ; 1 : int ; 2 : int ; 3 : int} val set_add = { ; 0 = ({ ; 1 : int ; 2 : int ; 3 : int}) ; 1 = ({ ; 1 : int ; 2 : int ; 3 : int ; 4 : int}) ; 2 = ({ ; 1 : int}) } diff --git a/src/passes/10-interpreter/interpreter.ml b/src/passes/10-interpreter/interpreter.ml index 2c06cab83..d319d49a7 100644 --- a/src/passes/10-interpreter/interpreter.ml +++ b/src/passes/10-interpreter/interpreter.ml @@ -171,6 +171,7 @@ let rec apply_operator : Ast_typed.constant' -> value list -> value result = eval body env' ) init elts + | ( C_MAP_EMPTY , []) -> ok @@ V_Map ([]) | ( C_MAP_FOLD , [ V_Func_val (arg_name, body, env) ; V_Map kvs ; init ] ) -> bind_fold_list (fun prev kv -> diff --git a/src/passes/10-transpiler/transpiler.ml b/src/passes/10-transpiler/transpiler.ml index 8554b2d8e..553ee4425 100644 --- a/src/passes/10-transpiler/transpiler.ml +++ b/src/passes/10-transpiler/transpiler.ml @@ -141,6 +141,8 @@ let rec transpile_type (t:AST.type_expression) : type_value result = | T_operator (TC_big_map (key,value)) -> let%bind kv' = bind_map_pair transpile_type (key, value) in ok (T_big_map kv') + | T_operator (TC_map_or_big_map (_,_)) -> + fail @@ corner_case ~loc:"transpiler" "TC_map_or_big_map should be resolve before transpilation" | T_operator (TC_list t) -> let%bind t' = transpile_type t in ok (T_list t') diff --git a/src/passes/10-transpiler/untranspiler.ml b/src/passes/10-transpiler/untranspiler.ml index 9a7149eed..f87cab0cd 100644 --- a/src/passes/10-transpiler/untranspiler.ml +++ b/src/passes/10-transpiler/untranspiler.ml @@ -160,30 +160,32 @@ let rec untranspile (v : value) (t : AST.type_expression) : AST.expression resul let%bind v' = untranspile v v_ty in ok (k', v') in bind_map_list aux map in + let map' = List.sort_uniq compare map' in let aux = fun prev (k, v) -> let (k', v') = (k , v ) in return @@ E_constant {cons_name=C_MAP_ADD;arguments=[k' ; v' ; prev]} in let%bind init = return @@ E_constant {cons_name=C_MAP_EMPTY;arguments=[]} in - bind_fold_list aux init map' + bind_fold_right_list aux init map' ) | TC_big_map (k_ty, v_ty) -> ( - let%bind map = + let%bind big_map = trace_strong (wrong_mini_c_value "big_map" v) @@ get_big_map v in - let%bind map' = + let%bind big_map' = let aux = fun (k, v) -> let%bind k' = untranspile k k_ty in let%bind v' = untranspile v v_ty in ok (k', v') in - bind_map_list aux map in - let map' = List.sort_uniq compare map' in + bind_map_list aux big_map in + let big_map' = List.sort_uniq compare big_map' in let aux = fun prev (k, v) -> return @@ E_constant {cons_name=C_MAP_ADD;arguments=[k ; v ; prev]} in - let%bind init = return @@ E_constant {cons_name=C_MAP_EMPTY;arguments=[]} in - bind_fold_list aux init map' + let%bind init = return @@ E_constant {cons_name=C_BIG_MAP_EMPTY;arguments=[]} in + bind_fold_right_list aux init big_map' ) + | TC_map_or_big_map (_, _) -> fail @@ corner_case ~loc:"untranspiler" "should not be present in mini-c" | TC_list ty -> ( let%bind lst = trace_strong (wrong_mini_c_value "list" v) @@ diff --git a/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml b/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml index 2ac37fc71..7b214a2aa 100644 --- a/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml +++ b/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml @@ -154,6 +154,9 @@ and compile_type_operator : I.type_operator -> O.type_operator result = | TC_big_map (k,v) -> let%bind (k,v) = bind_map_pair compile_type_expression (k,v) in ok @@ O.TC_big_map (k,v) + | TC_map_or_big_map (k,v) -> + let%bind (k,v) = bind_map_pair compile_type_expression (k,v) in + ok @@ O.TC_map_or_big_map (k,v) | TC_arrow (i,o) -> let%bind (i,o) = bind_map_pair compile_type_expression (i,o) in ok @@ O.TC_arrow (i,o) @@ -569,6 +572,9 @@ and uncompile_type_operator : O.type_operator -> I.type_operator result = | TC_big_map (k,v) -> let%bind (k,v) = bind_map_pair uncompile_type_expression (k,v) in ok @@ I.TC_big_map (k,v) + | TC_map_or_big_map (k,v) -> + let%bind (k,v) = bind_map_pair uncompile_type_expression (k,v) in + ok @@ I.TC_map_or_big_map (k,v) | TC_arrow (i,o) -> let%bind (i,o) = bind_map_pair uncompile_type_expression (i,o) in ok @@ I.TC_arrow (i,o) diff --git a/src/passes/6-sugar_to_core/sugar_to_core.ml b/src/passes/6-sugar_to_core/sugar_to_core.ml index e79ab336f..986f1cc16 100644 --- a/src/passes/6-sugar_to_core/sugar_to_core.ml +++ b/src/passes/6-sugar_to_core/sugar_to_core.ml @@ -55,6 +55,9 @@ and idle_type_operator : I.type_operator -> O.type_operator result = | TC_big_map (k,v) -> let%bind (k,v) = bind_map_pair idle_type_expression (k,v) in ok @@ O.TC_big_map (k,v) + | TC_map_or_big_map (k,v) -> + let%bind (k,v) = bind_map_pair idle_type_expression (k,v) in + ok @@ O.TC_map_or_big_map (k,v) | TC_arrow (i,o) -> let%bind (i,o) = bind_map_pair idle_type_expression (i,o) in ok @@ O.TC_arrow (i,o) @@ -115,16 +118,16 @@ let rec compile_expression : I.expression -> O.expression result = return @@ E_constant {cons_name=C_MAP_ADD;arguments=[k' ; v' ; prev]} in let%bind init = return @@ E_constant {cons_name=C_MAP_EMPTY;arguments=[]} in - bind_fold_list aux init map + bind_fold_right_list aux init map ) | I.E_big_map big_map -> ( - let map = List.sort_uniq compare big_map in + let big_map = List.sort_uniq compare big_map in let aux = fun prev (k, v) -> let%bind (k', v') = bind_map_pair (compile_expression) (k, v) in - return @@ E_constant {cons_name=C_BIG_MAP_ADD;arguments=[k' ; v' ; prev]} + return @@ E_constant {cons_name=C_MAP_ADD;arguments=[k' ; v' ; prev]} in let%bind init = return @@ E_constant {cons_name=C_BIG_MAP_EMPTY;arguments=[]} in - bind_fold_list aux init map + bind_fold_right_list aux init big_map ) | I.E_list lst -> let%bind lst' = bind_map_list (compile_expression) lst in @@ -258,6 +261,9 @@ and uncompile_type_operator : O.type_operator -> I.type_operator result = | TC_big_map (k,v) -> let%bind (k,v) = bind_map_pair uncompile_type_expression (k,v) in ok @@ I.TC_big_map (k,v) + | TC_map_or_big_map (k,v) -> + let%bind (k,v) = bind_map_pair uncompile_type_expression (k,v) in + ok @@ I.TC_map_or_big_map (k,v) | TC_arrow (i,o) -> let%bind (i,o) = bind_map_pair uncompile_type_expression (i,o) in ok @@ I.TC_arrow (i,o) diff --git a/src/passes/8-typer-new/solver.ml b/src/passes/8-typer-new/solver.ml index 328cfe015..25409822f 100644 --- a/src/passes/8-typer-new/solver.ml +++ b/src/passes/8-typer-new/solver.ml @@ -70,6 +70,7 @@ module Wrap = struct | TC_set s -> (C_set, [s]) | TC_map ( k , v ) -> (C_map, [k;v]) | TC_big_map ( k , v) -> (C_big_map, [k;v]) + | TC_map_or_big_map ( k , v) -> (C_map, [k;v]) | TC_arrow ( arg , ret ) -> (C_arrow, [ arg ; ret ]) | TC_list l -> (C_list, [l]) | TC_contract c -> (C_contract, [c]) @@ -103,6 +104,7 @@ module Wrap = struct | TC_set s -> (C_set , [s]) | TC_map ( k , v ) -> (C_map , [k;v]) | TC_big_map ( k , v ) -> (C_big_map, [k;v]) + | TC_map_or_big_map ( k , v) -> (C_map, [k;v]) | TC_contract c -> (C_contract, [c]) | TC_arrow ( arg , ret ) -> (C_arrow, [ arg ; ret ]) ) diff --git a/src/passes/8-typer-new/typer.ml b/src/passes/8-typer-new/typer.ml index 6166666bc..c1d4065a6 100644 --- a/src/passes/8-typer-new/typer.ml +++ b/src/passes/8-typer-new/typer.ml @@ -345,6 +345,10 @@ and evaluate_type (e:environment) (t:I.type_expression) : O.type_expression resu let%bind k = evaluate_type e k in let%bind v = evaluate_type e v in ok @@ O.TC_big_map (k,v) + | TC_map_or_big_map (k,v) -> + let%bind k = evaluate_type e k in + let%bind v = evaluate_type e v in + ok @@ O.TC_map_or_big_map (k,v) | TC_contract c -> let%bind c = evaluate_type e c in ok @@ O.TC_contract c @@ -837,6 +841,10 @@ let rec untype_type_expression (t:O.type_expression) : (I.type_expression) resul let%bind k = untype_type_expression k in let%bind v = untype_type_expression v in ok @@ I.TC_big_map (k,v) + | O.TC_map_or_big_map (k,v) -> + let%bind k = untype_type_expression k in + let%bind v = untype_type_expression v in + ok @@ I.TC_map_or_big_map (k,v) | O.TC_arrow ( arg , ret ) -> let%bind arg' = untype_type_expression arg in let%bind ret' = untype_type_expression ret in diff --git a/src/passes/8-typer-old/typer.ml b/src/passes/8-typer-old/typer.ml index ce64fa5b9..4dc472468 100644 --- a/src/passes/8-typer-old/typer.ml +++ b/src/passes/8-typer-old/typer.ml @@ -381,6 +381,10 @@ and evaluate_type (e:environment) (t:I.type_expression) : O.type_expression resu let%bind k = evaluate_type e k in let%bind v = evaluate_type e v in ok @@ O.TC_big_map (k,v) + | TC_map_or_big_map (k,v) -> + let%bind k = evaluate_type e k in + let%bind v = evaluate_type e v in + ok @@ O.TC_map_or_big_map (k,v) | TC_arrow ( arg , ret ) -> let%bind arg' = evaluate_type e arg in let%bind ret' = evaluate_type e ret in @@ -602,21 +606,7 @@ and type_expression' : environment -> ?tv_opt:O.type_expression -> I.expression let tv_val = get_type_expression val' in let tv = match tv_opt with Some (tv) -> tv - | None -> t_map tv_key tv_val () - in - let%bind map' = type_expression' e ~tv_opt:tv map in - let tv_map = get_type_expression map' in - let tv_lst = [tv_key;tv_val;tv_map] in - let%bind (name', tv) = type_constant cst tv_lst tv_opt in - return (E_constant {cons_name=name';arguments=[key';val';map']}) tv - | E_constant {cons_name=C_BIG_MAP_ADD as cst; arguments=[key;value;map]} -> - let%bind key' = type_expression' e key in - let%bind val' = type_expression' e value in - let tv_key = get_type_expression key' in - let tv_val = get_type_expression val' in - let tv = match tv_opt with - Some (tv) -> tv - | None -> t_big_map tv_key tv_val () + | None -> t_map_or_big_map tv_key tv_val () in let%bind map' = type_expression' e ~tv_opt:tv map in let tv_map = get_type_expression map' in diff --git a/src/passes/operators/operators.ml b/src/passes/operators/operators.ml index 9e6a093af..4d9e76d9d 100644 --- a/src/passes/operators/operators.ml +++ b/src/passes/operators/operators.ml @@ -624,7 +624,16 @@ module Typer = struct let map_empty = typer_0 "MAP_EMPTY" @@ fun tv_opt -> match tv_opt with | None -> simple_fail "untyped MAP_EMPTY" - | Some t -> ok t + | Some t -> + let%bind (src, dst) = get_t_map t in + ok @@ t_map src dst () + + let big_map_empty = typer_0 "BIG_MAP_EMPTY" @@ fun tv_opt -> + match tv_opt with + | None -> simple_fail "untyped BIG_MAP_EMPTY" + | Some t -> + let%bind (src, dst) = get_t_big_map t in + ok @@ t_big_map src dst () let map_add : typer = typer_3 "MAP_ADD" @@ fun k v m -> let%bind (src, dst) = bind_map_or (get_t_map , get_t_big_map) m in @@ -1171,6 +1180,7 @@ module Typer = struct | C_LIST_FOLD -> ok @@ list_fold ; (* MAP *) | C_MAP_EMPTY -> ok @@ map_empty ; + | C_BIG_MAP_EMPTY -> ok @@ big_map_empty ; | C_MAP_ADD -> ok @@ map_add ; | C_MAP_REMOVE -> ok @@ map_remove ; | C_MAP_UPDATE -> ok @@ map_update ; diff --git a/src/stages/4-ast_typed/combinators.ml b/src/stages/4-ast_typed/combinators.ml index 9961dbc0a..8a399bb35 100644 --- a/src/stages/4-ast_typed/combinators.ml +++ b/src/stages/4-ast_typed/combinators.ml @@ -64,6 +64,7 @@ let t_pair a b ?s () : type_expression = ez_t_record [(Label "0",a) ; (Label " let t_map key value ?s () = make_t (T_operator (TC_map (key , value))) s let t_big_map key value ?s () = make_t (T_operator (TC_big_map (key , value))) s +let t_map_or_big_map key value ?s () = make_t (T_operator (TC_map_or_big_map (key,value))) s let t_sum m ?s () : type_expression = make_t (T_sum m) s let make_t_ez_sum (lst:(constructor' * type_expression) list) : type_expression = @@ -190,11 +191,13 @@ let get_t_record (t:type_expression) : type_expression label_map result = match let get_t_map (t:type_expression) : (type_expression * type_expression) result = match t.type_content with | T_operator (TC_map (k,v)) -> ok (k, v) + | T_operator (TC_map_or_big_map (k,v)) -> ok (k, v) | _ -> fail @@ Errors.not_a_x_type "map" t () let get_t_big_map (t:type_expression) : (type_expression * type_expression) result = match t.type_content with | T_operator (TC_big_map (k,v)) -> ok (k, v) + | T_operator (TC_map_or_big_map (k,v)) -> ok (k, v) | _ -> fail @@ Errors.not_a_x_type "big_map" t () let get_t_map_key : type_expression -> type_expression result = fun t -> diff --git a/src/stages/4-ast_typed/combinators.mli b/src/stages/4-ast_typed/combinators.mli index dd88ffd00..fadef09e3 100644 --- a/src/stages/4-ast_typed/combinators.mli +++ b/src/stages/4-ast_typed/combinators.mli @@ -31,6 +31,7 @@ val ez_t_record : ( label * type_expression ) list -> ?s:S.type_expression -> un val t_map : type_expression -> type_expression -> ?s:S.type_expression -> unit -> type_expression val t_big_map : type_expression -> type_expression -> ?s:S.type_expression -> unit -> type_expression +val t_map_or_big_map : type_expression -> type_expression -> ?s:S.type_expression -> unit -> type_expression val t_sum : type_expression constructor_map -> ?s:S.type_expression -> unit -> type_expression val make_t_ez_sum : ( constructor' * type_expression ) list -> type_expression val t_function : type_expression -> type_expression -> ?s:S.type_expression -> unit -> type_expression diff --git a/src/stages/4-ast_typed/misc.ml b/src/stages/4-ast_typed/misc.ml index f101f3857..8f6158109 100644 --- a/src/stages/4-ast_typed/misc.ml +++ b/src/stages/4-ast_typed/misc.ml @@ -338,10 +338,11 @@ let rec assert_type_expression_eq (a, b: (type_expression * type_expression)) : | TC_list la, TC_list lb | TC_contract la, TC_contract lb | TC_set la, TC_set lb -> ok @@ ([la], [lb]) - | TC_map (ka,va), TC_map (kb,vb) - | TC_big_map (ka,va), TC_big_map (kb,vb) -> ok @@ ([ka;va] ,[kb;vb]) - | (TC_option _ | TC_list _ | TC_contract _ | TC_set _ | TC_map _ | TC_big_map _ | TC_arrow _), - (TC_option _ | TC_list _ | TC_contract _ | TC_set _ | TC_map _ | TC_big_map _ | TC_arrow _ ) -> fail @@ different_operators opa opb + | (TC_map (ka,va) | TC_map_or_big_map (ka,va)), (TC_map (kb,vb) | TC_map_or_big_map (kb,vb)) + | (TC_big_map (ka,va) | TC_map_or_big_map (ka,va)), (TC_big_map (kb,vb) | TC_map_or_big_map (kb,vb)) + -> ok @@ ([ka;va] ,[kb;vb]) + | (TC_option _ | TC_list _ | TC_contract _ | TC_set _ | TC_map _ | TC_big_map _ | TC_map_or_big_map _ | TC_arrow _), + (TC_option _ | TC_list _ | TC_contract _ | TC_set _ | TC_map _ | TC_big_map _ | TC_map_or_big_map _ | TC_arrow _ ) -> fail @@ different_operators opa opb in if List.length lsta <> List.length lstb then fail @@ different_operator_number_of_arguments opa opb (List.length lsta) (List.length lstb) diff --git a/src/stages/5-mini_c/PP.ml b/src/stages/5-mini_c/PP.ml index 1231f5ed6..808b65873 100644 --- a/src/stages/5-mini_c/PP.ml +++ b/src/stages/5-mini_c/PP.ml @@ -217,7 +217,6 @@ and constant ppf : constant' -> unit = function | C_MAP_FIND_OPT -> fprintf ppf "MAP_FIND_OP" (* Big Maps *) | C_BIG_MAP -> fprintf ppf "BIG_MAP" - | C_BIG_MAP_ADD -> fprintf ppf "BIG_MAP_ADD" | C_BIG_MAP_EMPTY -> fprintf ppf "BIG_MAP_EMPTY" | C_BIG_MAP_LITERAL -> fprintf ppf "BIG_MAP_LITERAL" (* Crypto *) diff --git a/src/stages/common/PP.ml b/src/stages/common/PP.ml index b5da63ec4..559446668 100644 --- a/src/stages/common/PP.ml +++ b/src/stages/common/PP.ml @@ -127,7 +127,6 @@ let constant ppf : constant' -> unit = function | C_MAP_FIND_OPT -> fprintf ppf "MAP_FIND_OP" (* Big Maps *) | C_BIG_MAP -> fprintf ppf "BIG_MAP" - | C_BIG_MAP_ADD -> fprintf ppf "BIG_MAP_ADD" | C_BIG_MAP_EMPTY -> fprintf ppf "BIG_MAP_EMPTY" | C_BIG_MAP_LITERAL -> fprintf ppf "BIG_MAP_LITERAL" (* Crypto *) @@ -266,6 +265,7 @@ module Ast_PP_type (PARAMETER : AST_PARAMETER_TYPE) = struct | TC_set te -> Format.asprintf "set(%a)" f te | TC_map (k, v) -> Format.asprintf "Map (%a,%a)" f k f v | TC_big_map (k, v) -> Format.asprintf "Big Map (%a,%a)" f k f v + | TC_map_or_big_map (k, v) -> Format.asprintf "Map Or Big Map (%a,%a)" f k f v | TC_arrow (k, v) -> Format.asprintf "arrow (%a,%a)" f k f v | TC_contract te -> Format.asprintf "Contract (%a)" f te in diff --git a/src/stages/common/types.ml b/src/stages/common/types.ml index 5d62b62c7..8e3411527 100644 --- a/src/stages/common/types.ml +++ b/src/stages/common/types.ml @@ -53,6 +53,7 @@ module Ast_generic_type (PARAMETER : AST_PARAMETER_TYPE) = struct | TC_set of type_expression | TC_map of type_expression * type_expression | TC_big_map of type_expression * type_expression + | TC_map_or_big_map of type_expression * type_expression | TC_arrow of type_expression * type_expression @@ -66,6 +67,7 @@ module Ast_generic_type (PARAMETER : AST_PARAMETER_TYPE) = struct | TC_set x -> TC_set (f x) | TC_map (x , y) -> TC_map (f x , f y) | TC_big_map (x , y)-> TC_big_map (f x , f y) + | TC_map_or_big_map (x , y)-> TC_map_or_big_map (f x , f y) | TC_arrow (x, y) -> TC_arrow (f x, f y) let bind_map_type_operator f = function @@ -75,6 +77,7 @@ module Ast_generic_type (PARAMETER : AST_PARAMETER_TYPE) = struct | TC_set x -> let%bind x = f x in ok @@ TC_set x | TC_map (x , y) -> let%bind x = f x in let%bind y = f y in ok @@ TC_map (x , y) | TC_big_map (x , y)-> let%bind x = f x in let%bind y = f y in ok @@ TC_big_map (x , y) + | TC_map_or_big_map (x , y)-> let%bind x = f x in let%bind y = f y in ok @@ TC_map_or_big_map (x , y) | TC_arrow (x , y)-> let%bind x = f x in let%bind y = f y in ok @@ TC_arrow (x , y) let type_operator_name = function @@ -84,6 +87,7 @@ module Ast_generic_type (PARAMETER : AST_PARAMETER_TYPE) = struct | TC_set _ -> "TC_set" | TC_map _ -> "TC_map" | TC_big_map _ -> "TC_big_map" + | TC_map_or_big_map _ -> "TC_map_or_big_map" | TC_arrow _ -> "TC_arrow" let type_expression'_of_string = function @@ -122,6 +126,7 @@ module Ast_generic_type (PARAMETER : AST_PARAMETER_TYPE) = struct | TC_set x -> "TC_set" , [x] | TC_map (x , y) -> "TC_map" , [x ; y] | TC_big_map (x , y) -> "TC_big_map" , [x ; y] + | TC_map_or_big_map (x , y) -> "TC_map_or_big_map" , [x ; y] | TC_arrow (x , y) -> "TC_arrow" , [x ; y] let string_of_type_constant = function @@ -269,7 +274,6 @@ and constant' = | C_MAP_FIND_OPT (* Big Maps *) | C_BIG_MAP - | C_BIG_MAP_ADD | C_BIG_MAP_EMPTY | C_BIG_MAP_LITERAL (* Crypto *) From aac7dd3462de71b6e24db3ea021bb42bbe251121 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Wulfman Date: Fri, 27 Mar 2020 15:23:55 +0100 Subject: [PATCH 09/13] review 1 --- scripts/test_cli.sh | 2 +- src/bin/expect_tests/contract_tests.ml | 21 ++++++------------- src/passes/10-transpiler/transpiler.ml | 2 +- src/passes/10-transpiler/untranspiler.ml | 2 +- src/passes/8-typer-old/typer.ml | 6 +++--- .../9-self_ast_typed/no_nested_big_map.ml | 4 ++++ src/stages/3-ast_core/combinators.ml | 4 ++-- 7 files changed, 18 insertions(+), 23 deletions(-) diff --git a/scripts/test_cli.sh b/scripts/test_cli.sh index 5bda10f9f..c41a4db8e 100755 --- a/scripts/test_cli.sh +++ b/scripts/test_cli.sh @@ -7,7 +7,7 @@ dry_run_output=$(./scripts/ligo_ci.sh dry-run src/test/contracts/website2.ligo m expected_compiled_parameter="(Right 1)"; expected_compiled_storage=1; -expected_dry_run_output="( list[] , 2 )"; +expected_dry_run_output="( LIST_EMPTY() , 2 )"; if [ "$compiled_storage" != "$expected_compiled_storage" ]; then echo "Expected $expected_compiled_storage as compile-storage output, got $compiled_storage instead"; diff --git a/src/bin/expect_tests/contract_tests.ml b/src/bin/expect_tests/contract_tests.ml index 913a89352..3419ad93b 100644 --- a/src/bin/expect_tests/contract_tests.ml +++ b/src/bin/expect_tests/contract_tests.ml @@ -13,7 +13,7 @@ let%expect_test _ = [%expect {| 1294 bytes |}] ; run_ligo_good [ "measure-contract" ; contract "multisig-v2.ligo" ; "main" ] ; - [%expect {| 3268 bytes |}] ; + [%expect {| 2974 bytes |}] ; run_ligo_good [ "measure-contract" ; contract "vote.mligo" ; "main" ] ; [%expect {| 589 bytes |}] ; @@ -627,12 +627,7 @@ let%expect_test _ = IF { PUSH string "Maximum number of proposal reached" ; FAILWITH } { PUSH unit Unit } ; NIL operation ; - DIP 10 { DUP } ; - DIG 10 ; - DIP { DIP 4 { DUP } ; DIG 4 } ; - PAIR ; - DIP { DIP 9 { DUP } ; DIG 9 ; DIP { DUP } ; PAIR } ; - PAIR ; + DUP ; DIP { DIP 3 { DUP } ; DIG 3 } ; PAIR ; DIP 5 { DUP } ; @@ -691,12 +686,8 @@ let%expect_test _ = DIP { DUP } ; SWAP ; DIP { DUP } ; - PAIR ; - DIP { DIP 2 { DUP } ; DIG 2 } ; - PAIR ; - DIP 2 { DUP } ; - DIG 2 ; - DIP { DIP 13 { DUP } ; DIG 13 } ; + SWAP ; + DIP { DIP 12 { DUP } ; DIG 12 } ; MEM ; IF { DIP 2 { DUP } ; DIG 2 ; @@ -1126,7 +1117,7 @@ let%expect_test _ = let%expect_test _ = run_ligo_bad [ "compile-contract" ; bad_contract "create_contract_toplevel.mligo" ; "main" ] ; [%expect {| -ligo: in file "create_contract_toplevel.mligo", line 4, character 35 to line 8, character 8. No free variable allowed in this lambda: variable 'store' {"expression":"CREATE_CONTRACT(lambda (#P:Some(( nat * string ))) : None return let rhs#654 = #P in let p = rhs#654.0 in let s = rhs#654.1 in ( LIST_EMPTY() : (TO_list(operation)) , store ) , NONE() : (TO_option(key_hash)) , 300000000mutez , \"un\")","location":"in file \"create_contract_toplevel.mligo\", line 4, character 35 to line 8, character 8"} +ligo: in file "create_contract_toplevel.mligo", line 4, character 35 to line 8, character 8. No free variable allowed in this lambda: variable 'store' {"expression":"CREATE_CONTRACT(lambda (#P:Some(( nat * string ))) : None return let rhs#702 = #P in let p = rhs#702.0 in let s = rhs#702.1 in ( LIST_EMPTY() : (TO_list(operation)) , store ) , NONE() : (TO_option(key_hash)) , 300000000mutez , \"un\")","location":"in file \"create_contract_toplevel.mligo\", line 4, character 35 to line 8, character 8"} If you're not sure how to fix this error, you can @@ -1139,7 +1130,7 @@ ligo: in file "create_contract_toplevel.mligo", line 4, character 35 to line 8, run_ligo_bad [ "compile-contract" ; bad_contract "create_contract_var.mligo" ; "main" ] ; [%expect {| -ligo: in file "create_contract_var.mligo", line 6, character 35 to line 10, character 5. No free variable allowed in this lambda: variable 'a' {"expression":"CREATE_CONTRACT(lambda (#P:Some(( nat * int ))) : None return let rhs#657 = #P in let p = rhs#657.0 in let s = rhs#657.1 in ( LIST_EMPTY() : (TO_list(operation)) , a ) , NONE() : (TO_option(key_hash)) , 300000000mutez , 1)","location":"in file \"create_contract_var.mligo\", line 6, character 35 to line 10, character 5"} +ligo: in file "create_contract_var.mligo", line 6, character 35 to line 10, character 5. No free variable allowed in this lambda: variable 'a' {"expression":"CREATE_CONTRACT(lambda (#P:Some(( nat * int ))) : None return let rhs#705 = #P in let p = rhs#705.0 in let s = rhs#705.1 in ( LIST_EMPTY() : (TO_list(operation)) , a ) , NONE() : (TO_option(key_hash)) , 300000000mutez , 1)","location":"in file \"create_contract_var.mligo\", line 6, character 35 to line 10, character 5"} If you're not sure how to fix this error, you can diff --git a/src/passes/10-transpiler/transpiler.ml b/src/passes/10-transpiler/transpiler.ml index 553ee4425..04ceeaf8d 100644 --- a/src/passes/10-transpiler/transpiler.ml +++ b/src/passes/10-transpiler/transpiler.ml @@ -142,7 +142,7 @@ let rec transpile_type (t:AST.type_expression) : type_value result = let%bind kv' = bind_map_pair transpile_type (key, value) in ok (T_big_map kv') | T_operator (TC_map_or_big_map (_,_)) -> - fail @@ corner_case ~loc:"transpiler" "TC_map_or_big_map should be resolve before transpilation" + fail @@ corner_case ~loc:"transpiler" "TC_map_or_big_map should have been resolved before transpilation" | T_operator (TC_list t) -> let%bind t' = transpile_type t in ok (T_list t') diff --git a/src/passes/10-transpiler/untranspiler.ml b/src/passes/10-transpiler/untranspiler.ml index f87cab0cd..b8762ecf1 100644 --- a/src/passes/10-transpiler/untranspiler.ml +++ b/src/passes/10-transpiler/untranspiler.ml @@ -185,7 +185,7 @@ let rec untranspile (v : value) (t : AST.type_expression) : AST.expression resul let%bind init = return @@ E_constant {cons_name=C_BIG_MAP_EMPTY;arguments=[]} in bind_fold_right_list aux init big_map' ) - | TC_map_or_big_map (_, _) -> fail @@ corner_case ~loc:"untranspiler" "should not be present in mini-c" + | TC_map_or_big_map (_, _) -> fail @@ corner_case ~loc:"untranspiler" "TC_map_or_big_map t should not be present in mini-c" | TC_list ty -> ( let%bind lst = trace_strong (wrong_mini_c_value "list" v) @@ diff --git a/src/passes/8-typer-old/typer.ml b/src/passes/8-typer-old/typer.ml index 4dc472468..73a91118a 100644 --- a/src/passes/8-typer-old/typer.ml +++ b/src/passes/8-typer-old/typer.ml @@ -588,11 +588,11 @@ and type_expression' : environment -> ?tv_opt:O.type_expression -> I.expression let%bind key' = type_expression' e key in let tv_key = get_type_expression key' in let tv = match tv_opt with - Some (tv) -> tv + Some tv -> tv | None -> match cst with C_SET_ADD -> t_set tv_key () | C_CONS -> t_list tv_key () - | _ -> failwith "impossible" + | _ -> failwith "Only C_SET_ADD and C_CONS are possible because those were the two cases matched above" in let%bind set' = type_expression' e ~tv_opt:tv set in let tv_set = get_type_expression set' in @@ -605,7 +605,7 @@ and type_expression' : environment -> ?tv_opt:O.type_expression -> I.expression let tv_key = get_type_expression key' in let tv_val = get_type_expression val' in let tv = match tv_opt with - Some (tv) -> tv + Some tv -> tv | None -> t_map_or_big_map tv_key tv_val () in let%bind map' = type_expression' e ~tv_opt:tv map in diff --git a/src/passes/9-self_ast_typed/no_nested_big_map.ml b/src/passes/9-self_ast_typed/no_nested_big_map.ml index e1a130ce9..a0b2f869f 100644 --- a/src/passes/9-self_ast_typed/no_nested_big_map.ml +++ b/src/passes/9-self_ast_typed/no_nested_big_map.ml @@ -21,6 +21,10 @@ let rec check_no_nested_bigmap is_in_bigmap e = let%bind _ = check_no_nested_bigmap false key in let%bind _ = check_no_nested_bigmap true value in ok () + | T_operator (TC_map_or_big_map (key, value)) -> + let%bind _ = check_no_nested_bigmap false key in + let%bind _ = check_no_nested_bigmap true value in + ok () | T_operator (TC_contract t) | T_operator (TC_option t) | T_operator (TC_list t) diff --git a/src/stages/3-ast_core/combinators.ml b/src/stages/3-ast_core/combinators.ml index 2c5dbf56d..787b61438 100644 --- a/src/stages/3-ast_core/combinators.ml +++ b/src/stages/3-ast_core/combinators.ml @@ -251,10 +251,10 @@ let extract_record : expression -> (label * expression) list result = fun e -> let extract_map : expression -> (expression * expression) list result = fun e -> let rec aux e = match e.expression_content with - E_constant {cons_name=C_UPDATE; arguments=[k;v;map]} -> + E_constant {cons_name=C_UPDATE|C_MAP_ADD; arguments=[k;v;map]} -> let%bind map = aux map in ok @@ (k,v)::map - | E_constant {cons_name=C_MAP_EMPTY; arguments=[]} -> ok @@ [] + | E_constant {cons_name=C_MAP_EMPTY|C_BIG_MAP_EMPTY; arguments=[]} -> ok @@ [] | _ -> fail @@ bad_kind "map" e.location in aux e From 0d46be7425cce2f173a610d327872923f3bd9186 Mon Sep 17 00:00:00 2001 From: Sander Spies Date: Tue, 31 Mar 2020 17:00:17 +0200 Subject: [PATCH 10/13] Change not existing mutez type to existing tez type --- gitlab-pages/docs/reference/current.md | 18 +++++++++--------- src/passes/1-parser/pascaligo/Doc/pascaligo.md | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/gitlab-pages/docs/reference/current.md b/gitlab-pages/docs/reference/current.md index 56c719630..a08916711 100644 --- a/gitlab-pages/docs/reference/current.md +++ b/gitlab-pages/docs/reference/current.md @@ -21,13 +21,13 @@ type timestamp A date in the real world. -type mutez +type tez -type mutez +type tez -type mutez +type tez A specific type for tokens. @@ -83,13 +83,13 @@ type chain_id The identifier of a chain, used to indicate test or main chains. -function balance : mutez +function balance : tez -val balance : mutez +val balance : tez -let balance: mutez +let balance: tez Get the balance for the contract. @@ -263,13 +263,13 @@ let not_tomorrow: bool = (Tezos.now == in_24_hrs); -function amount : mutez +function amount : tez -val amount : mutez +val amount : tez -let amount: mutez +let amount: tez Get the amount of tez provided by the sender to complete this diff --git a/src/passes/1-parser/pascaligo/Doc/pascaligo.md b/src/passes/1-parser/pascaligo/Doc/pascaligo.md index 1415631fd..ba6010ea8 100644 --- a/src/passes/1-parser/pascaligo/Doc/pascaligo.md +++ b/src/passes/1-parser/pascaligo/Doc/pascaligo.md @@ -823,7 +823,7 @@ example, in verbose style: type store is record - goal : mutez; // Millionth of a tez + goal : tez; deadline : timestamp; backers : map (address, nat); funded : bool From 13e9405deef00d6eb3cc0c4cf0ca12320a9b6f2b Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Wulfman Date: Wed, 25 Mar 2020 15:22:26 +0100 Subject: [PATCH 11/13] adding tuples in ast_sugar --- src/passes/10-interpreter/interpreter.ml | 4 +- src/passes/10-transpiler/transpiler.ml | 16 +-- src/passes/10-transpiler/untranspiler.ml | 2 +- .../entrypoints_length_limit.ml | 2 +- src/passes/3-self_ast_imperative/helpers.ml | 48 ++++++- .../imperative_to_sugar.ml | 41 ++++-- src/passes/5-self_ast_sugar/helpers.ml | 48 ++++++- src/passes/6-sugar_to_core/sugar_to_core.ml | 38 +++-- src/passes/8-typer-new/typer.ml | 14 +- src/passes/8-typer-old/typer.ml | 14 +- src/passes/9-self_ast_typed/helpers.ml | 12 +- src/stages/1-ast_imperative/PP.ml | 46 +++++- src/stages/1-ast_imperative/combinators.ml | 48 ++----- src/stages/1-ast_imperative/misc.ml | 23 ++- src/stages/1-ast_imperative/types.ml | 47 +++++-- src/stages/2-ast_sugar/PP.ml | 40 +++++- src/stages/2-ast_sugar/combinators.ml | 42 ++---- src/stages/2-ast_sugar/combinators.mli | 6 +- src/stages/2-ast_sugar/misc.ml | 23 ++- src/stages/2-ast_sugar/types.ml | 47 +++++-- src/stages/3-ast_core/PP.ml | 2 +- src/stages/3-ast_core/combinators.ml | 29 ++-- src/stages/3-ast_core/combinators.mli | 8 +- src/stages/3-ast_core/types.ml | 9 +- src/stages/4-ast_typed/PP.ml | 2 +- src/stages/4-ast_typed/combinators.ml | 2 +- src/stages/4-ast_typed/types.ml | 10 +- src/stages/common/PP.ml | 132 +++++++----------- src/stages/common/helpers.ml | 20 +++ src/stages/common/helpers.mli | 5 + src/stages/typesystem/misc.ml | 8 +- src/test/contracts/tuple.ligo | 9 +- src/test/integration_tests.ml | 7 +- 33 files changed, 520 insertions(+), 284 deletions(-) diff --git a/src/passes/10-interpreter/interpreter.ml b/src/passes/10-interpreter/interpreter.ml index d319d49a7..f4d930298 100644 --- a/src/passes/10-interpreter/interpreter.ml +++ b/src/passes/10-interpreter/interpreter.ml @@ -303,12 +303,12 @@ and eval : Ast_typed.expression -> env -> value result ok (label,v')) (LMap.to_kv_list recmap) in ok @@ V_Record (LMap.of_list lv') - | E_record_accessor { record ; label} -> ( + | E_record_accessor { record ; path} -> ( let%bind record' = eval record env in match record' with | V_Record recmap -> let%bind a = trace_option (simple_error "unknown record field") @@ - LMap.find_opt label recmap in + LMap.find_opt path recmap in ok a | _ -> simple_fail "trying to access a non-record" ) diff --git a/src/passes/10-transpiler/transpiler.ml b/src/passes/10-transpiler/transpiler.ml index 04ceeaf8d..5f89f5a20 100644 --- a/src/passes/10-transpiler/transpiler.ml +++ b/src/passes/10-transpiler/transpiler.ml @@ -172,7 +172,7 @@ let rec transpile_type (t:AST.type_expression) : type_value result = aux node in ok @@ snd m' | T_record m -> - let node = Append_tree.of_list @@ kv_list_of_lmap m in + let node = Append_tree.of_list @@ Stage_common.Helpers.kv_list_of_record_or_tuple m in let aux a b : type_value annotated result = let%bind a = a in let%bind b = b in @@ -191,7 +191,7 @@ let rec transpile_type (t:AST.type_expression) : type_value result = ) let record_access_to_lr : type_value -> type_value AST.label_map -> AST.label -> (type_value * [`Left | `Right]) list result = fun ty tym ind -> - let tys = kv_list_of_lmap tym in + let tys = Stage_common.Helpers.kv_list_of_record_or_tuple tym in let node_tv = Append_tree.of_list tys in let%bind path = let aux (i , _) = i = ind in @@ -290,7 +290,8 @@ and transpile_annotated_expression (ae:AST.expression) : expression result = return ~tv ae ) | E_record m -> ( - let node = Append_tree.of_list @@ list_of_lmap m in + (*list_of_lmap to record_to_list*) + let node = Append_tree.of_list @@ Stage_common.Helpers.list_of_record_or_tuple m in let aux a b : expression result = let%bind a = a in let%bind b = b in @@ -302,16 +303,15 @@ and transpile_annotated_expression (ae:AST.expression) : expression result = trace_strong (corner_case ~loc:__LOC__ "record build") @@ Append_tree.fold_ne (transpile_annotated_expression) aux node ) - | E_record_accessor {record; label} -> - let ty = get_type_expression record in - let%bind ty' = transpile_type ty in + | E_record_accessor {record; path} -> + let%bind ty' = transpile_type (get_type_expression record) in let%bind ty_lmap = trace_strong (corner_case ~loc:__LOC__ "not a record") @@ - get_t_record ty in + get_t_record (get_type_expression record) in let%bind ty'_lmap = Stage_common.Helpers.bind_map_lmap transpile_type ty_lmap in let%bind path = trace_strong (corner_case ~loc:__LOC__ "record access") @@ - record_access_to_lr ty' ty'_lmap label in + record_access_to_lr ty' ty'_lmap path in let aux = fun pred (ty, lr) -> let c = match lr with | `Left -> C_CAR diff --git a/src/passes/10-transpiler/untranspiler.ml b/src/passes/10-transpiler/untranspiler.ml index b8762ecf1..3665f8063 100644 --- a/src/passes/10-transpiler/untranspiler.ml +++ b/src/passes/10-transpiler/untranspiler.ml @@ -232,7 +232,7 @@ let rec untranspile (v : value) (t : AST.type_expression) : AST.expression resul let%bind sub = untranspile v tv in return (E_constructor {constructor=Constructor name;element=sub}) | T_record m -> - let lst = kv_list_of_lmap m in + let lst = Stage_common.Helpers.kv_list_of_record_or_tuple m in let%bind node = match Append_tree.of_list lst with | Empty -> fail @@ corner_case ~loc:__LOC__ "empty record" | Full t -> ok t in diff --git a/src/passes/3-self_ast_imperative/entrypoints_length_limit.ml b/src/passes/3-self_ast_imperative/entrypoints_length_limit.ml index f2d5fc202..e9809c835 100644 --- a/src/passes/3-self_ast_imperative/entrypoints_length_limit.ml +++ b/src/passes/3-self_ast_imperative/entrypoints_length_limit.ml @@ -11,7 +11,7 @@ end open Errors let peephole_type_expression : type_expression -> type_expression result = fun e -> - let return type_content = ok { e with type_content } in + let return type_content = ok {type_content } in match e.type_content with | T_sum cmap -> let%bind _uu = bind_map_cmapi diff --git a/src/passes/3-self_ast_imperative/helpers.ml b/src/passes/3-self_ast_imperative/helpers.ml index f70b21aaf..c0af289dc 100644 --- a/src/passes/3-self_ast_imperative/helpers.ml +++ b/src/passes/3-self_ast_imperative/helpers.ml @@ -51,6 +51,23 @@ let rec fold_expression : 'a folder -> 'a -> expression -> 'a result = fun f ini let%bind res = self init' record in ok res ) + | E_tuple t -> ( + let aux init'' expr = + let%bind res = fold_expression self init'' expr in + ok res + in + let%bind res = bind_fold_list aux (init') t in + ok res + ) + | E_tuple_update {tuple;update} -> ( + let%bind res = self init' tuple in + let%bind res = fold_expression self res update in + ok res + ) + | E_tuple_accessor {tuple} -> ( + let%bind res = self init' tuple in + ok res + ) | E_let_in { let_binder = _ ; rhs ; let_result } -> ( let%bind res = self init' rhs in let%bind res = self res let_result in @@ -161,6 +178,19 @@ let rec map_expression : exp_mapper -> expression -> expression result = fun f e let%bind update = self update in return @@ E_record_update {record;path;update} ) + | E_tuple t -> ( + let%bind t' = bind_map_list self t in + return @@ E_tuple t' + ) + | E_tuple_update {tuple; path; update} -> ( + let%bind tuple = self tuple in + let%bind update = self update in + return @@ E_tuple_update {tuple; path; update} + ) + | E_tuple_accessor {tuple;path} -> ( + let%bind tuple = self tuple in + return @@ E_tuple_accessor {tuple;path} + ) | E_constructor c -> ( let%bind e' = self c.element in return @@ E_constructor {c with element = e'} @@ -212,7 +242,7 @@ let rec map_expression : exp_mapper -> expression -> expression result = fun f e and map_type_expression : ty_exp_mapper -> type_expression -> type_expression result = fun f te -> let self = map_type_expression f in let%bind te' = f te in - let return type_content = ok { te' with type_content } in + let return type_content = ok { type_content } in match te'.type_content with | T_sum temap -> let%bind temap' = bind_map_cmap self temap in @@ -220,6 +250,9 @@ and map_type_expression : ty_exp_mapper -> type_expression -> type_expression re | T_record temap -> let%bind temap' = bind_map_lmap self temap in return @@ (T_record temap') + | T_tuple telst -> + let%bind telst' = bind_map_list self telst in + return @@ (T_tuple telst') | T_arrow {type1 ; type2} -> let%bind type1' = self type1 in let%bind type2' = self type2 in @@ -324,6 +357,19 @@ let rec fold_map_expression : 'a fold_mapper -> 'a -> expression -> ('a * expres let%bind (res, update) = self res update in ok (res, return @@ E_record_update {record;path;update}) ) + | E_tuple t -> ( + let%bind (res, t') = bind_fold_map_list self init' t in + ok (res, return @@ E_tuple t') + ) + | E_tuple_update {tuple; path; update} -> ( + let%bind (res, tuple) = self init' tuple in + let%bind (res, update) = self res update in + ok (res, return @@ E_tuple_update {tuple;path;update}) + ) + | E_tuple_accessor {tuple; path} -> ( + let%bind (res, tuple) = self init' tuple in + ok (res, return @@ E_tuple_accessor {tuple; path}) + ) | E_constructor c -> ( let%bind (res,e') = self init' c.element in ok (res, return @@ E_constructor {c with element = e'}) diff --git a/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml b/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml index 7b214a2aa..89e5d99fb 100644 --- a/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml +++ b/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml @@ -123,6 +123,9 @@ let rec compile_type_expression : I.type_expression -> O.type_expression result ) record in return @@ O.T_record (O.LMap.of_list record) + | I.T_tuple tuple -> + let%bind tuple = bind_map_list compile_type_expression tuple in + return @@ O.T_tuple tuple | I.T_arrow {type1;type2} -> let%bind type1 = compile_type_expression type1 in let%bind type2 = compile_type_expression type2 in @@ -154,9 +157,6 @@ and compile_type_operator : I.type_operator -> O.type_operator result = | TC_big_map (k,v) -> let%bind (k,v) = bind_map_pair compile_type_expression (k,v) in ok @@ O.TC_big_map (k,v) - | TC_map_or_big_map (k,v) -> - let%bind (k,v) = bind_map_pair compile_type_expression (k,v) in - ok @@ O.TC_map_or_big_map (k,v) | TC_arrow (i,o) -> let%bind (i,o) = bind_map_pair compile_type_expression (i,o) in ok @@ O.TC_arrow (i,o) @@ -202,9 +202,9 @@ let rec compile_expression : I.expression -> O.expression result = ) record in return @@ O.E_record (O.LMap.of_list record) - | I.E_record_accessor {record;label} -> + | I.E_record_accessor {record;path} -> let%bind record = compile_expression record in - return @@ O.E_record_accessor {record;label} + return @@ O.E_record_accessor {record;path} | I.E_record_update {record;path;update} -> let%bind record = compile_expression record in let%bind update = compile_expression update in @@ -239,6 +239,16 @@ let rec compile_expression : I.expression -> O.expression result = let%bind expr2 = compile_expression expr2 in ok @@ add_to_end expr1 expr2 | I.E_skip -> return @@ O.E_skip + | I.E_tuple tuple -> + let%bind tuple = bind_map_list compile_expression tuple in + return @@ O.E_tuple (tuple) + | I.E_tuple_accessor {tuple;path} -> + let%bind tuple = compile_expression tuple in + return @@ O.E_tuple_accessor {tuple;path} + | I.E_tuple_update {tuple;path;update} -> + let%bind tuple = compile_expression tuple in + let%bind update = compile_expression update in + return @@ O.E_tuple_update {tuple;path;update} | I.E_assign ass -> let%bind content = compile_assign ass @@ O.e_skip () in return @@ content @@ -282,7 +292,6 @@ and compile_assign {variable; access_path; expression} expr = let%bind rhs = rhs @@ expression in ok @@ O.E_let_in {let_binder=(variable,None); mut=true; rhs; let_result=expr;inline = false} - and compile_lambda : I.lambda -> O.lambda result = fun {binder;input_type;output_type;result}-> let%bind input_type = bind_map_option compile_type_expression input_type in @@ -541,6 +550,9 @@ let rec uncompile_type_expression : O.type_expression -> I.type_expression resul ) record in return @@ I.T_record (O.LMap.of_list record) + | O.T_tuple tuple -> + let%bind tuple = bind_map_list uncompile_type_expression tuple in + return @@ I.T_tuple tuple | O.T_arrow {type1;type2} -> let%bind type1 = uncompile_type_expression type1 in let%bind type2 = uncompile_type_expression type2 in @@ -572,9 +584,6 @@ and uncompile_type_operator : O.type_operator -> I.type_operator result = | TC_big_map (k,v) -> let%bind (k,v) = bind_map_pair uncompile_type_expression (k,v) in ok @@ I.TC_big_map (k,v) - | TC_map_or_big_map (k,v) -> - let%bind (k,v) = bind_map_pair uncompile_type_expression (k,v) in - ok @@ I.TC_map_or_big_map (k,v) | TC_arrow (i,o) -> let%bind (i,o) = bind_map_pair uncompile_type_expression (i,o) in ok @@ I.TC_arrow (i,o) @@ -621,13 +630,23 @@ let rec uncompile_expression : O.expression -> I.expression result = ) record in return @@ I.E_record (O.LMap.of_list record) - | O.E_record_accessor {record;label} -> + | O.E_record_accessor {record;path} -> let%bind record = uncompile_expression record in - return @@ I.E_record_accessor {record;label} + return @@ I.E_record_accessor {record;path} | O.E_record_update {record;path;update} -> let%bind record = uncompile_expression record in let%bind update = uncompile_expression update in return @@ I.E_record_update {record;path;update} + | O.E_tuple tuple -> + let%bind tuple = bind_map_list uncompile_expression tuple in + return @@ I.E_tuple tuple + | O.E_tuple_accessor {tuple;path} -> + let%bind tuple = uncompile_expression tuple in + return @@ I.E_tuple_accessor {tuple;path} + | O.E_tuple_update {tuple;path;update} -> + let%bind tuple = uncompile_expression tuple in + let%bind update = uncompile_expression update in + return @@ I.E_tuple_update {tuple;path;update} | O.E_map map -> let%bind map = bind_map_list ( bind_map_pair uncompile_expression diff --git a/src/passes/5-self_ast_sugar/helpers.ml b/src/passes/5-self_ast_sugar/helpers.ml index 0e0ddbd45..46b9e4cfd 100644 --- a/src/passes/5-self_ast_sugar/helpers.ml +++ b/src/passes/5-self_ast_sugar/helpers.ml @@ -63,6 +63,23 @@ let rec fold_expression : 'a folder -> 'a -> expression -> 'a result = fun f ini let ab = (expr1,expr2) in let%bind res = bind_fold_pair self init' ab in ok res + | E_tuple t -> ( + let aux init'' expr = + let%bind res = fold_expression self init'' expr in + ok res + in + let%bind res = bind_fold_list aux (init') t in + ok res + ) + | E_tuple_update {tuple;update} -> ( + let%bind res = self init' tuple in + let%bind res = fold_expression self res update in + ok res + ) + | E_tuple_accessor {tuple} -> ( + let%bind res = self init' tuple in + ok res + ) and fold_cases : 'a folder -> 'a -> matching_expr -> 'a result = fun f init m -> @@ -176,12 +193,25 @@ let rec map_expression : exp_mapper -> expression -> expression result = fun f e let%bind (expr1,expr2) = bind_map_pair self (expr1,expr2) in return @@ E_sequence {expr1;expr2} ) + | E_tuple t -> ( + let%bind t' = bind_map_list self t in + return @@ E_tuple t' + ) + | E_tuple_update {tuple; path; update} -> ( + let%bind tuple = self tuple in + let%bind update = self update in + return @@ E_tuple_update {tuple; path; update} + ) + | E_tuple_accessor {tuple;path} -> ( + let%bind tuple = self tuple in + return @@ E_tuple_accessor {tuple;path} + ) | E_literal _ | E_variable _ | E_skip as e' -> return e' and map_type_expression : ty_exp_mapper -> type_expression -> type_expression result = fun f te -> let self = map_type_expression f in let%bind te' = f te in - let return type_content = ok { te' with type_content } in + let return type_content = ok { type_content } in match te'.type_content with | T_sum temap -> let%bind temap' = bind_map_cmap self temap in @@ -189,6 +219,9 @@ and map_type_expression : ty_exp_mapper -> type_expression -> type_expression re | T_record temap -> let%bind temap' = bind_map_lmap self temap in return @@ (T_record temap') + | T_tuple telst -> + let%bind telst' = bind_map_list self telst in + return @@ (T_tuple telst') | T_arrow {type1 ; type2} -> let%bind type1' = self type1 in let%bind type2' = self type2 in @@ -293,6 +326,19 @@ let rec fold_map_expression : 'a fold_mapper -> 'a -> expression -> ('a * expres let%bind (res, update) = self res update in ok (res, return @@ E_record_update {record;path;update}) ) + | E_tuple t -> ( + let%bind (res, t') = bind_fold_map_list self init' t in + ok (res, return @@ E_tuple t') + ) + | E_tuple_update {tuple; path; update} -> ( + let%bind (res, tuple) = self init' tuple in + let%bind (res, update) = self res update in + ok (res, return @@ E_tuple_update {tuple;path;update}) + ) + | E_tuple_accessor {tuple; path} -> ( + let%bind (res, tuple) = self init' tuple in + ok (res, return @@ E_tuple_accessor {tuple; path}) + ) | E_constructor c -> ( let%bind (res,e') = self init' c.element in ok (res, return @@ E_constructor {c with element = e'}) diff --git a/src/passes/6-sugar_to_core/sugar_to_core.ml b/src/passes/6-sugar_to_core/sugar_to_core.ml index 986f1cc16..1d6270dcb 100644 --- a/src/passes/6-sugar_to_core/sugar_to_core.ml +++ b/src/passes/6-sugar_to_core/sugar_to_core.ml @@ -24,6 +24,13 @@ let rec idle_type_expression : I.type_expression -> O.type_expression result = ) record in return @@ O.T_record (O.LMap.of_list record) + | I.T_tuple tuple -> + let aux (i,acc) el = + let%bind el = idle_type_expression el in + ok @@ (i+1,(O.Label (string_of_int i), el)::acc) in + let%bind (_, lst ) = bind_fold_list aux (0,[]) tuple in + let record = O.LMap.of_list lst in + return @@ O.T_record record | I.T_arrow {type1;type2} -> let%bind type1 = idle_type_expression type1 in let%bind type2 = idle_type_expression type2 in @@ -55,9 +62,6 @@ and idle_type_operator : I.type_operator -> O.type_operator result = | TC_big_map (k,v) -> let%bind (k,v) = bind_map_pair idle_type_expression (k,v) in ok @@ O.TC_big_map (k,v) - | TC_map_or_big_map (k,v) -> - let%bind (k,v) = bind_map_pair idle_type_expression (k,v) in - ok @@ O.TC_map_or_big_map (k,v) | TC_arrow (i,o) -> let%bind (i,o) = bind_map_pair idle_type_expression (i,o) in ok @@ O.TC_arrow (i,o) @@ -104,9 +108,9 @@ let rec compile_expression : I.expression -> O.expression result = ) record in return @@ O.E_record (O.LMap.of_list record) - | I.E_record_accessor {record;label} -> + | I.E_record_accessor {record;path} -> let%bind record = compile_expression record in - return @@ O.E_record_accessor {record;label} + return @@ O.E_record_accessor {record;path} | I.E_record_update {record;path;update} -> let%bind record = compile_expression record in let%bind update = compile_expression update in @@ -155,6 +159,22 @@ let rec compile_expression : I.expression -> O.expression result = let%bind expr2 = compile_expression expr2 in return @@ O.E_let_in {let_binder=(Var.of_name "_", Some O.t_unit); rhs=expr1;let_result=expr2; inline=false} | I.E_skip -> ok @@ O.e_unit ~loc:e.location () + | I.E_tuple t -> + let aux (i,acc) el = + let%bind el = compile_expression el in + ok @@ (i+1,(O.Label (string_of_int i), el)::acc) in + let%bind (_, lst ) = bind_fold_list aux (0,[]) t in + let m = O.LMap.of_list lst in + return @@ O.E_record m + | I.E_tuple_accessor {tuple;path} -> + let%bind record = compile_expression tuple in + let path = O.Label (string_of_int path) in + return @@ O.E_record_accessor {record;path} + | I.E_tuple_update {tuple;path;update} -> + let%bind record = compile_expression tuple in + let path = O.Label (string_of_int path) in + let%bind update = compile_expression update in + return @@ O.E_record_update {record;path;update} and compile_lambda : I.lambda -> O.lambda result = fun {binder;input_type;output_type;result}-> @@ -261,9 +281,7 @@ and uncompile_type_operator : O.type_operator -> I.type_operator result = | TC_big_map (k,v) -> let%bind (k,v) = bind_map_pair uncompile_type_expression (k,v) in ok @@ I.TC_big_map (k,v) - | TC_map_or_big_map (k,v) -> - let%bind (k,v) = bind_map_pair uncompile_type_expression (k,v) in - ok @@ I.TC_map_or_big_map (k,v) + | TC_map_or_big_map _ -> failwith "TC_map_or_big_map shouldn't be uncompiled" | TC_arrow (i,o) -> let%bind (i,o) = bind_map_pair uncompile_type_expression (i,o) in ok @@ I.TC_arrow (i,o) @@ -314,9 +332,9 @@ let rec uncompile_expression : O.expression -> I.expression result = ) record in return @@ I.E_record (O.LMap.of_list record) - | O.E_record_accessor {record;label} -> + | O.E_record_accessor {record;path} -> let%bind record = uncompile_expression record in - return @@ I.E_record_accessor {record;label} + return @@ I.E_record_accessor {record;path} | O.E_record_update {record;path;update} -> let%bind record = uncompile_expression record in let%bind update = uncompile_expression update in diff --git a/src/passes/8-typer-new/typer.ml b/src/passes/8-typer-new/typer.ml index c1d4065a6..f9f6cf66a 100644 --- a/src/passes/8-typer-new/typer.ml +++ b/src/passes/8-typer-new/typer.ml @@ -455,10 +455,10 @@ and type_expression : environment -> Solver.state -> ?tv_opt:O.type_expression - * | Some (T_constant ("address" , [])) -> return (E_literal (Literal_address s)) (t_address ()) * | _ -> return (E_literal (Literal_string s)) (t_string ()) * ) *) - | E_record_accessor {record;label} -> ( + | E_record_accessor {record;path} -> ( let%bind (base' , state') = type_expression e state record in - let wrapped = Wrap.access_label ~base:base'.type_expression ~label in - return_wrapped (E_record_accessor {record=base';label}) state' wrapped + let wrapped = Wrap.access_label ~base:base'.type_expression ~label:path in + return_wrapped (E_record_accessor {record=base';path}) state' wrapped ) (* Sum *) @@ -917,15 +917,15 @@ let rec untype_expression (e:O.expression) : (I.expression) result = let%bind r' = bind_smap @@ Map.String.map untype_expression r in return (e_record r') - | E_record_accessor {record; label} -> + | E_record_accessor {record; path} -> let%bind r' = untype_expression record in - let Label s = label in - return (e_accessor r' s) + let Label s = path in + return (e_record_accessor r' s) | E_record_update {record; path; update} -> let%bind r' = untype_expression record in let%bind e = untype_expression update in let Label l = path in - return (e_update r' l e) + return (e_record_update r' l e) | E_matching {matchee;cases} -> let%bind ae' = untype_expression matchee in let%bind m' = untype_matching untype_expression cases in diff --git a/src/passes/8-typer-old/typer.ml b/src/passes/8-typer-old/typer.ml index 73a91118a..96137dd56 100644 --- a/src/passes/8-typer-old/typer.ml +++ b/src/passes/8-typer-old/typer.ml @@ -454,7 +454,7 @@ and type_expression' : environment -> ?tv_opt:O.type_expression -> I.expression return (e_address s) (t_address ()) | E_literal (Literal_operation op) -> return (e_operation op) (t_operation ()) - | E_record_accessor {record;label} -> + | E_record_accessor {record;path} -> let%bind e' = type_expression' e record in let aux (prev:O.expression) (a:I.label) : O.expression result = let property = a in @@ -463,10 +463,10 @@ and type_expression' : environment -> ?tv_opt:O.type_expression -> I.expression generic_try (bad_record_access property ae prev.type_expression ae.location) @@ (fun () -> I.LMap.find property r_tv) in let location = ae.location in - ok @@ make_a_e ~location (E_record_accessor {record=prev; label=property}) tv e + ok @@ make_a_e ~location (E_record_accessor {record=prev; path=property}) tv e in let%bind ae = - trace (simple_info "accessing") @@ aux e' label in + trace (simple_info "accessing") @@ aux e' path in (* check type annotation of the final accessed element *) let%bind () = match tv_opt with @@ -787,15 +787,15 @@ let rec untype_expression (e:O.expression) : (I.expression) result = let%bind r' = bind_smap @@ Map.String.map untype_expression r in return (e_record r') - | E_record_accessor {record; label} -> + | E_record_accessor {record; path} -> let%bind r' = untype_expression record in - let Label s = label in - return (e_accessor r' s) + let Label s = path in + return (e_record_accessor r' s) | E_record_update {record=r; path=l; update=e} -> let%bind r' = untype_expression r in let%bind e = untype_expression e in let Label l = l in - return (e_update r' l e) + return (e_record_update r' l e) | E_matching {matchee;cases} -> let%bind ae' = untype_expression matchee in let%bind m' = untype_matching untype_expression cases in diff --git a/src/passes/9-self_ast_typed/helpers.ml b/src/passes/9-self_ast_typed/helpers.ml index 92e887282..e410786e9 100644 --- a/src/passes/9-self_ast_typed/helpers.ml +++ b/src/passes/9-self_ast_typed/helpers.ml @@ -91,9 +91,9 @@ let rec map_expression : mapper -> expression -> expression result = fun f e -> let%bind cases' = map_cases f cases in return @@ E_matching {matchee=e';cases=cases'} ) - | E_record_accessor acc -> ( - let%bind e' = self acc.record in - return @@ E_record_accessor {acc with record = e'} + | E_record_accessor {record; path} -> ( + let%bind record = self record in + return @@ E_record_accessor {record; path} ) | E_record m -> ( let%bind m' = bind_map_lmap self m in @@ -186,9 +186,9 @@ let rec fold_map_expression : 'a fold_mapper -> 'a -> expression -> ('a * expres let%bind (res,cases') = fold_map_cases f res cases in ok (res, return @@ E_matching {matchee=e';cases=cases'}) ) - | E_record_accessor acc -> ( - let%bind (res, e') = self init' acc.record in - ok (res, return @@ E_record_accessor {acc with record = e'}) + | E_record_accessor {record; path} -> ( + let%bind (res, record) = self init' record in + ok (res, return @@ E_record_accessor {record; path}) ) | E_record m -> ( let%bind (res, lst') = bind_fold_map_list (fun res (k,e) -> let%bind (res,e) = self res e in ok (res,(k,e))) init' (LMap.to_kv_list m) in diff --git a/src/stages/1-ast_imperative/PP.ml b/src/stages/1-ast_imperative/PP.ml index 1c8a243ff..667da637d 100644 --- a/src/stages/1-ast_imperative/PP.ml +++ b/src/stages/1-ast_imperative/PP.ml @@ -4,11 +4,45 @@ open Format open PP_helpers include Stage_common.PP -include Ast_PP_type(Ast_imperative_parameter) let expression_variable ppf (ev : expression_variable) : unit = fprintf ppf "%a" Var.pp ev +let rec type_expression' : + (formatter -> type_expression -> unit) + -> formatter + -> type_expression + -> unit = + fun f ppf te -> + match te.type_content with + | T_sum m -> fprintf ppf "sum[%a]" (cmap_sep_d f) m + | T_record m -> fprintf ppf "%a" (tuple_or_record_sep_type f) m + | T_tuple t -> fprintf ppf "%a" (list_sep_d f) t + | T_arrow a -> fprintf ppf "%a -> %a" f a.type1 f a.type2 + | T_variable tv -> type_variable ppf tv + | T_constant tc -> type_constant ppf tc + | T_operator to_ -> type_operator f ppf to_ + +and type_expression ppf (te : type_expression) : unit = + type_expression' type_expression ppf te + +and type_operator : + (formatter -> type_expression -> unit) + -> formatter + -> type_operator + -> unit = + fun f ppf to_ -> + let s = + match to_ with + | TC_option te -> Format.asprintf "option(%a)" f te + | TC_list te -> Format.asprintf "list(%a)" f te + | TC_set te -> Format.asprintf "set(%a)" f te + | TC_map (k, v) -> Format.asprintf "Map (%a,%a)" f k f v + | TC_big_map (k, v) -> Format.asprintf "Big Map (%a,%a)" f k f v + | TC_arrow (k, v) -> Format.asprintf "arrow (%a,%a)" f k f v + | TC_contract te -> Format.asprintf "Contract (%a)" f te + in + fprintf ppf "(TO_%s)" s let rec expression ppf (e : expression) = expression_content ppf e.expression_content @@ -27,8 +61,8 @@ and expression_content ppf (ec : expression_content) = c.arguments | E_record m -> fprintf ppf "%a" (tuple_or_record_sep_expr expression) m - | E_record_accessor {record; label=l}-> - fprintf ppf "%a.%a" expression record label l + | E_record_accessor ra -> + fprintf ppf "%a.%a" expression ra.record label ra.path | E_record_update {record; path; update} -> fprintf ppf "{ %a with { %a = %a } }" expression record label path expression update | E_map m -> @@ -66,6 +100,12 @@ and expression_content ppf (ec : expression_content) = fprintf ppf "{ %a; @. %a}" expression expr1 expression expr2 | E_skip -> fprintf ppf "skip" + | E_tuple t -> + fprintf ppf "%a" (list_sep_d expression) t + | E_tuple_accessor ta -> + fprintf ppf "%a.%d" expression ta.tuple ta.path + | E_tuple_update {tuple; path; update} -> + fprintf ppf "{ %a with { %d = %a } }" expression tuple path expression update | E_assign {variable; access_path; expression=e} -> fprintf ppf "%a%a := %a" expression_variable variable diff --git a/src/stages/1-ast_imperative/combinators.ml b/src/stages/1-ast_imperative/combinators.ml index e49f811b3..2dd149b8a 100644 --- a/src/stages/1-ast_imperative/combinators.ml +++ b/src/stages/1-ast_imperative/combinators.ml @@ -19,14 +19,9 @@ module Errors = struct end open Errors -let make_t type_content = {type_content; type_meta = ()} +let make_t type_content = {type_content} -let tuple_to_record lst = - let aux (i,acc) el = (i+1,(string_of_int i, el)::acc) in - let (_, lst ) = List.fold_left aux (0,[]) lst in - lst - let t_bool : type_expression = make_t @@ T_constant (TC_bool) let t_string : type_expression = make_t @@ T_constant (TC_string) let t_bytes : type_expression = make_t @@ T_constant (TC_bytes) @@ -51,8 +46,8 @@ let t_record m : type_expression = let lst = Map.String.to_kv_list m in t_record_ez lst -let t_pair (a , b) : type_expression = t_record_ez [("0",a) ; ("1",b)] -let t_tuple lst : type_expression = t_record_ez (tuple_to_record lst) +let t_tuple lst : type_expression = make_t @@ T_tuple lst +let t_pair (a , b) : type_expression = t_tuple [a; b] let ez_t_sum (lst:(string * type_expression) list) : type_expression = let aux prev (k, v) = CMap.add (Constructor k) v prev in @@ -118,7 +113,8 @@ let e_list ?loc lst : expression = make_expr ?loc @@ E_list lst let e_constructor ?loc s a : expression = make_expr ?loc @@ E_constructor { constructor = Constructor s; element = a} let e_matching ?loc a b : expression = make_expr ?loc @@ E_matching {matchee=a;cases=b} let e_matching_bool ?loc a b c : expression = e_matching ?loc a (Match_bool {match_true = b ; match_false = c}) -let e_accessor ?loc a b = make_expr ?loc @@ E_record_accessor {record = a; label= Label b} +let e_record_accessor ?loc a b = make_expr ?loc @@ E_record_accessor {record = a; path = Label b} +let e_accessor ?loc a b = e_record_accessor ?loc a b let e_accessor_list ?loc a b = List.fold_left (fun a b -> e_accessor ?loc a b) a b let e_variable ?loc v = make_expr ?loc @@ E_variable v let e_skip ?loc () = make_expr ?loc @@ E_skip @@ -151,11 +147,12 @@ let e_record ?loc map = let lst = Map.String.to_kv_list map in e_record_ez ?loc lst -let e_update ?loc record path update = +let e_record_update ?loc record path update = let path = Label path in make_expr ?loc @@ E_record_update {record; path; update} +let e_update ?loc record path update = e_record_update ?loc record path update -let e_tuple ?loc lst : expression = e_record_ez ?loc (tuple_to_record lst) +let e_tuple ?loc lst : expression = make_expr ?loc @@ E_tuple lst let e_pair ?loc a b : expression = e_tuple ?loc [a;b] let make_option_typed ?loc e t_opt = @@ -201,7 +198,7 @@ let e_ez_assign ?loc variable access_path expression = let get_e_accessor = fun t -> match t with - | E_record_accessor {record; label} -> ok (record , label) + | E_record_accessor {record; path} -> ok (record , path) | _ -> simple_fail "not an accessor" let assert_e_accessor = fun t -> @@ -210,14 +207,7 @@ let assert_e_accessor = fun t -> let get_e_pair = fun t -> match t with - | E_record r -> ( - let lst = LMap.to_kv_list r in - match lst with - | [(Label "O",a);(Label "1",b)] - | [(Label "1",b);(Label "0",a)] -> - ok (a , b) - | _ -> simple_fail "not a pair" - ) + | E_tuple [a ; b] -> ok (a , b) | _ -> simple_fail "not a pair" let get_e_list = fun t -> @@ -225,29 +215,15 @@ let get_e_list = fun t -> | E_list lst -> ok lst | _ -> simple_fail "not a list" -let tuple_of_record (m: _ LMap.t) = - let aux i = - let opt = LMap.find_opt (Label (string_of_int i)) m in - Option.bind (fun opt -> Some (opt,i+1)) opt - in - Base.Sequence.to_list @@ Base.Sequence.unfold ~init:0 ~f:aux - let get_e_tuple = fun t -> match t with - | E_record r -> ok @@ tuple_of_record r + | E_tuple t -> ok @@ t | _ -> simple_fail "ast_core: get_e_tuple: not a tuple" (* Same as get_e_pair *) let extract_pair : expression -> (expression * expression) result = fun e -> match e.expression_content with - | E_record r -> ( - let lst = LMap.to_kv_list r in - match lst with - | [(Label "O",a);(Label "1",b)] - | [(Label "1",b);(Label "0",a)] -> - ok (a , b) - | _ -> fail @@ bad_kind "pair" e.location - ) + | E_tuple [a;b] -> ok @@ (a,b) | _ -> fail @@ bad_kind "pair" e.location let extract_list : expression -> (expression list) result = fun e -> diff --git a/src/stages/1-ast_imperative/misc.ml b/src/stages/1-ast_imperative/misc.ml index 7a2615e04..1c37f1744 100644 --- a/src/stages/1-ast_imperative/misc.ml +++ b/src/stages/1-ast_imperative/misc.ml @@ -140,6 +140,26 @@ let rec assert_value_eq (a, b: (expression * expression )) : unit result = | E_record_update _, _ -> simple_fail "comparing record update with other expression" + | E_tuple lsta, E_tuple lstb -> ( + let%bind lst = + generic_try (simple_error "tuples with different number of elements") + (fun () -> List.combine lsta lstb) in + let%bind _all = bind_list @@ List.map assert_value_eq lst in + ok () + ) + | E_tuple _, _ -> + simple_fail "comparing tuple with other expression" + + | E_tuple_update uta, E_tuple_update utb -> + let _ = + generic_try (simple_error "Updating different tuple") @@ + fun () -> assert_value_eq (uta.tuple, utb.tuple) in + let () = assert (uta.path == utb.path) in + let%bind () = assert_value_eq (uta.update,utb.update) in + ok () + | E_tuple_update _, _ -> + simple_fail "comparing tuple update with other expression" + | (E_map lsta, E_map lstb | E_big_map lsta, E_big_map lstb) -> ( let%bind lst = generic_try (simple_error "maps of different lengths") (fun () -> @@ -182,7 +202,8 @@ let rec assert_value_eq (a, b: (expression * expression )) : unit result = | (_a' , E_ascription b) -> assert_value_eq (a , b.anno_expr) | (E_variable _, _) | (E_lambda _, _) | (E_application _, _) | (E_let_in _, _) - | (E_recursive _,_) | (E_record_accessor _, _) + | (E_recursive _,_) + | (E_record_accessor _, _) | (E_tuple_accessor _, _) | (E_look_up _, _) | (E_matching _, _) | (E_sequence _, _) | (E_skip, _) | (E_assign _, _) diff --git a/src/stages/1-ast_imperative/types.ml b/src/stages/1-ast_imperative/types.ml index 480d612fb..ee69248ba 100644 --- a/src/stages/1-ast_imperative/types.ml +++ b/src/stages/1-ast_imperative/types.ml @@ -2,17 +2,31 @@ module Location = Simple_utils.Location -module Ast_imperative_parameter = struct - type type_meta = unit -end - include Stage_common.Types -(*include Ast_generic_type(Ast_core_parameter) -*) -include Ast_generic_type (Ast_imperative_parameter) +type type_content = + | T_sum of type_expression constructor_map + | T_record of type_expression label_map + | T_tuple of type_expression list + | T_arrow of arrow + | T_variable of type_variable + | T_constant of type_constant + | T_operator of type_operator + +and arrow = {type1: type_expression; type2: type_expression} + +and type_operator = + | TC_contract of type_expression + | TC_option of type_expression + | TC_list of type_expression + | TC_set of type_expression + | TC_map of type_expression * type_expression + | TC_big_map of type_expression * type_expression + | TC_arrow of type_expression * type_expression + +and type_expression = {type_content: type_content} + -type inline = bool type program = declaration Location.wrap list and declaration = | Declaration_type of (type_variable * type_expression) @@ -22,7 +36,7 @@ and declaration = * an optional type annotation * a boolean indicating whether it should be inlined * an expression *) - | Declaration_constant of (expression_variable * type_expression option * inline * expression) + | Declaration_constant of (expression_variable * type_expression option * bool * expression) (* | Macro_declaration of macro_declaration *) and expression = {expression_content: expression_content; location: Location.t} @@ -41,13 +55,16 @@ and expression_content = | E_matching of matching (* Record *) | E_record of expression label_map - | E_record_accessor of accessor - | E_record_update of update + | E_record_accessor of record_accessor + | E_record_update of record_update (* Advanced *) | E_ascription of ascription (* Sugar *) | E_sequence of sequence | E_skip + | E_tuple of expression list + | E_tuple_accessor of tuple_accessor + | E_tuple_update of tuple_update (* Data Structures *) | E_map of (expression * expression) list | E_big_map of (expression * expression) list @@ -89,9 +106,10 @@ and let_in = and constructor = {constructor: constructor'; element: expression} -and accessor = {record: expression; label: label} +and record_accessor = {record: expression; path: label} +and record_update = {record: expression; path: label ; update: expression} + -and update = {record: expression; path: label ; update: expression} and matching_expr = (expr,unit) matching_content and matching = @@ -105,6 +123,9 @@ and sequence = { expr2: expression ; } +and tuple_accessor = {tuple: expression; path: int} +and tuple_update = {tuple: expression; path: int ; update: expression} + and assign = { variable : expression_variable; access_path : access list; diff --git a/src/stages/2-ast_sugar/PP.ml b/src/stages/2-ast_sugar/PP.ml index ca4184aff..bb4d4f147 100644 --- a/src/stages/2-ast_sugar/PP.ml +++ b/src/stages/2-ast_sugar/PP.ml @@ -4,11 +4,41 @@ open Format open PP_helpers include Stage_common.PP -include Ast_PP_type(Ast_sugar_parameter) let expression_variable ppf (ev : expression_variable) : unit = fprintf ppf "%a" Var.pp ev +let rec type_expression' : + (formatter -> type_expression -> unit) + -> formatter + -> type_expression + -> unit = + fun f ppf te -> + match te.type_content with + | T_sum m -> fprintf ppf "sum[%a]" (cmap_sep_d f) m + | T_record m -> fprintf ppf "%a" (tuple_or_record_sep_type f) m + | T_tuple t -> fprintf ppf "%a" (list_sep_d f) t + | T_arrow a -> fprintf ppf "%a -> %a" f a.type1 f a.type2 + | T_variable tv -> type_variable ppf tv + | T_constant tc -> type_constant ppf tc + | T_operator to_ -> type_operator f ppf to_ + +and type_expression ppf (te : type_expression) : unit = + type_expression' type_expression ppf te + +and type_operator : (formatter -> type_expression -> unit) -> formatter -> type_operator -> unit = + fun f ppf to_ -> + let s = + match to_ with + | TC_option te -> Format.asprintf "option(%a)" f te + | TC_list te -> Format.asprintf "list(%a)" f te + | TC_set te -> Format.asprintf "set(%a)" f te + | TC_map (k, v) -> Format.asprintf "Map (%a,%a)" f k f v + | TC_big_map (k, v) -> Format.asprintf "Big Map (%a,%a)" f k f v + | TC_arrow (k, v) -> Format.asprintf "arrow (%a,%a)" f k f v + | TC_contract te -> Format.asprintf "Contract (%a)" f te + in + fprintf ppf "(TO_%s)" s let rec expression ppf (e : expression) = expression_content ppf e.expression_content @@ -28,7 +58,7 @@ and expression_content ppf (ec : expression_content) = | E_record m -> fprintf ppf "%a" (tuple_or_record_sep_expr expression) m | E_record_accessor ra -> - fprintf ppf "%a.%a" expression ra.record label ra.label + fprintf ppf "%a.%a" expression ra.record label ra.path | E_record_update {record; path; update} -> fprintf ppf "{ %a with { %a = %a } }" expression record label path expression update | E_map m -> @@ -69,6 +99,12 @@ and expression_content ppf (ec : expression_content) = fprintf ppf "%a : %a" expression anno_expr type_expression type_annotation | E_skip -> fprintf ppf "skip" + | E_tuple t -> + fprintf ppf "%a" (list_sep_d expression) t + | E_tuple_accessor ta -> + fprintf ppf "%a.%d" expression ta.tuple ta.path + | E_tuple_update {tuple; path; update} -> + fprintf ppf "{ %a with { %d = %a } }" expression tuple path expression update and option_type_name ppf ((n, ty_opt) : expression_variable * type_expression option) = diff --git a/src/stages/2-ast_sugar/combinators.ml b/src/stages/2-ast_sugar/combinators.ml index d5099ab28..cc18739c2 100644 --- a/src/stages/2-ast_sugar/combinators.ml +++ b/src/stages/2-ast_sugar/combinators.ml @@ -19,7 +19,7 @@ module Errors = struct end open Errors -let make_t type_content = {type_content; type_meta = ()} +let make_t type_content = {type_content} let tuple_to_record lst = @@ -112,6 +112,8 @@ let e_none ?loc () : expression = make_expr ?loc @@ E_constant {cons_name = C_NO let e_constructor ?loc s a : expression = make_expr ?loc @@ E_constructor { constructor = Constructor s; element = a} let e_matching ?loc a b : expression = make_expr ?loc @@ E_matching {matchee=a;cases=b} let e_matching_bool ?loc a b c : expression = e_matching ?loc a (Match_bool {match_true = b ; match_false = c}) +let e_record_accessor ?loc a b = make_expr ?loc @@ E_record_accessor {record = a; path = Label b} +let e_record_accessor_list ?loc a b = List.fold_left (fun a b -> e_record_accessor ?loc a b) a b let e_variable ?loc v = make_expr ?loc @@ E_variable v let e_let_in ?loc (binder, ascr) mut inline rhs let_result = make_expr ?loc @@ E_let_in { let_binder = (binder, ascr) ; rhs ; let_result; inline; mut } @@ -127,6 +129,7 @@ let e_list ?loc lst : expression = make_expr ?loc @@ E_list lst let e_set ?loc lst : expression = make_expr ?loc @@ E_set lst let e_map ?loc lst : expression = make_expr ?loc @@ E_map lst let e_big_map ?loc lst : expression = make_expr ?loc @@ E_big_map lst +let e_look_up ?loc a b : expression = make_expr ?loc @@ E_look_up (a,b) let ez_match_variant (lst : ((string * string) * 'a) list) = let lst = List.map (fun ((c,n),a) -> ((Constructor c, Var.of_name n), a) ) lst in @@ -139,7 +142,7 @@ let e_record_ez ?loc (lst : (string * expr) list) : expression = let e_record ?loc map = let lst = Map.String.to_kv_list map in e_record_ez ?loc lst -let e_record_accessor ?loc a b = make_expr ?loc @@ E_record_accessor {record = a; label= Label b} +let e_record_accessor ?loc a b = make_expr ?loc @@ E_record_accessor {record = a; path= Label b} let e_record_update ?loc record path update = let path = Label path in @@ -180,25 +183,18 @@ let e_lambda ?loc (binder : expression_variable) } let e_recursive ?loc fun_name fun_type lambda = make_expr ?loc @@ E_recursive {fun_name; fun_type; lambda} -let get_e_accessor = fun t -> +let get_e_record_accessor = fun t -> match t with - | E_record_accessor {record; label} -> ok (record , label) - | _ -> simple_fail "not an accessor" + | E_record_accessor {record; path} -> ok (record, path) + | _ -> simple_fail "not a record accessor" let assert_e_accessor = fun t -> - let%bind _ = get_e_accessor t in + let%bind _ = get_e_record_accessor t in ok () let get_e_pair = fun t -> match t with - | E_record r -> ( - let lst = LMap.to_kv_list r in - match lst with - | [(Label "O",a);(Label "1",b)] - | [(Label "1",b);(Label "0",a)] -> - ok (a , b) - | _ -> simple_fail "not a pair" - ) + | E_tuple [a ; b] -> ok (a , b) | _ -> simple_fail "not a pair" let get_e_list = fun t -> @@ -206,29 +202,15 @@ let get_e_list = fun t -> | E_list lst -> ok lst | _ -> simple_fail "not a list" -let tuple_of_record (m: _ LMap.t) = - let aux i = - let opt = LMap.find_opt (Label (string_of_int i)) m in - Option.bind (fun opt -> Some (opt,i+1)) opt - in - Base.Sequence.to_list @@ Base.Sequence.unfold ~init:0 ~f:aux - let get_e_tuple = fun t -> match t with - | E_record r -> ok @@ tuple_of_record r + | E_tuple t -> ok @@ t | _ -> simple_fail "ast_core: get_e_tuple: not a tuple" (* Same as get_e_pair *) let extract_pair : expression -> (expression * expression) result = fun e -> match e.expression_content with - | E_record r -> ( - let lst = LMap.to_kv_list r in - match lst with - | [(Label "O",a);(Label "1",b)] - | [(Label "1",b);(Label "0",a)] -> - ok (a , b) - | _ -> fail @@ bad_kind "pair" e.location - ) + | E_tuple [a;b] -> ok @@ (a,b) | _ -> fail @@ bad_kind "pair" e.location let extract_list : expression -> (expression list) result = fun e -> diff --git a/src/stages/2-ast_sugar/combinators.mli b/src/stages/2-ast_sugar/combinators.mli index d790512f6..a8d7b5919 100644 --- a/src/stages/2-ast_sugar/combinators.mli +++ b/src/stages/2-ast_sugar/combinators.mli @@ -80,6 +80,7 @@ val e_let_in : ?loc:Location.t -> ( expression_variable * type_expression option val e_record : ?loc:Location.t -> expr Map.String.t -> expression val e_record_update : ?loc:Location.t -> expression -> string -> expression -> expression val e_record_accessor : ?loc:Location.t -> expression -> string -> expression +val e_record_accessor_list : ?loc:Location.t -> expression -> string list -> expression val e_annotation : ?loc:Location.t -> expression -> type_expression -> expression @@ -90,9 +91,11 @@ val e_list : ?loc:Location.t -> expression list -> expression val e_set : ?loc:Location.t -> expression list -> expression val e_map : ?loc:Location.t -> ( expression * expression ) list -> expression val e_big_map : ?loc:Location.t -> ( expr * expr ) list -> expression +val e_look_up : ?loc:Location.t -> expression -> expression -> expression val e_matching : ?loc:Location.t -> expression -> matching_expr -> expression val e_matching_bool : ?loc:Location.t -> expression -> expression -> expression -> expression +val ez_match_variant : ((string * string ) * 'a ) list -> ('a,unit) matching_content val e_matching_variant : ?loc:Location.t -> expression -> ((string * string) * expression) list -> expression val make_option_typed : ?loc:Location.t -> expression -> type_expression option -> expression @@ -109,9 +112,6 @@ val e_record_ez : ?loc:Location.t -> (string * expression) list -> expression val e_cond: ?loc:Location.t -> expression -> expression -> expression -> expression val e_tuple : ?loc:Location.t -> expression list -> expression val e_pair : ?loc:Location.t -> expression -> expression -> expression -(* -val get_e_accessor : expression' -> ( expression * access_path ) result -*) val assert_e_accessor : expression_content -> unit result diff --git a/src/stages/2-ast_sugar/misc.ml b/src/stages/2-ast_sugar/misc.ml index 324529525..2d43bf2a9 100644 --- a/src/stages/2-ast_sugar/misc.ml +++ b/src/stages/2-ast_sugar/misc.ml @@ -140,6 +140,26 @@ let rec assert_value_eq (a, b: (expression * expression )) : unit result = | E_record_update _, _ -> simple_fail "comparing record update with other expression" + | E_tuple lsta, E_tuple lstb -> ( + let%bind lst = + generic_try (simple_error "tuples with different number of elements") + (fun () -> List.combine lsta lstb) in + let%bind _all = bind_list @@ List.map assert_value_eq lst in + ok () + ) + | E_tuple _, _ -> + simple_fail "comparing tuple with other expression" + + | E_tuple_update uta, E_tuple_update utb -> + let _ = + generic_try (simple_error "Updating different tuple") @@ + fun () -> assert_value_eq (uta.tuple, utb.tuple) in + let () = assert (uta.path == utb.path) in + let%bind () = assert_value_eq (uta.update,utb.update) in + ok () + | E_tuple_update _, _ -> + simple_fail "comparing tuple update with other expression" + | (E_map lsta, E_map lstb | E_big_map lsta, E_big_map lstb) -> ( let%bind lst = generic_try (simple_error "maps of different lengths") (fun () -> @@ -182,7 +202,8 @@ let rec assert_value_eq (a, b: (expression * expression )) : unit result = | (_a' , E_ascription b) -> assert_value_eq (a , b.anno_expr) | (E_variable _, _) | (E_lambda _, _) | (E_application _, _) | (E_let_in _, _) - | (E_recursive _,_) | (E_record_accessor _, _) + | (E_recursive _,_) + | (E_record_accessor _, _) | (E_tuple_accessor _, _) | (E_look_up _, _) | (E_matching _, _) | (E_sequence _, _) | (E_skip, _) -> simple_fail "comparing not a value" diff --git a/src/stages/2-ast_sugar/types.ml b/src/stages/2-ast_sugar/types.ml index 5ad052e46..f4650284c 100644 --- a/src/stages/2-ast_sugar/types.ml +++ b/src/stages/2-ast_sugar/types.ml @@ -2,17 +2,31 @@ module Location = Simple_utils.Location -module Ast_sugar_parameter = struct - type type_meta = unit -end - include Stage_common.Types -(*include Ast_generic_type(Ast_core_parameter) -*) -include Ast_generic_type (Ast_sugar_parameter) +type type_content = + | T_sum of type_expression constructor_map + | T_record of type_expression label_map + | T_tuple of type_expression list + | T_arrow of arrow + | T_variable of type_variable + | T_constant of type_constant + | T_operator of type_operator + +and arrow = {type1: type_expression; type2: type_expression} + +and type_operator = + | TC_contract of type_expression + | TC_option of type_expression + | TC_list of type_expression + | TC_set of type_expression + | TC_map of type_expression * type_expression + | TC_big_map of type_expression * type_expression + | TC_arrow of type_expression * type_expression + +and type_expression = {type_content: type_content} + -type inline = bool type program = declaration Location.wrap list and declaration = | Declaration_type of (type_variable * type_expression) @@ -22,7 +36,7 @@ and declaration = * an optional type annotation * a boolean indicating whether it should be inlined * an expression *) - | Declaration_constant of (expression_variable * type_expression option * inline * expression) + | Declaration_constant of (expression_variable * type_expression option * bool * expression) (* | Macro_declaration of macro_declaration *) and expression = {expression_content: expression_content; location: Location.t} @@ -41,13 +55,16 @@ and expression_content = | E_matching of matching (* Record *) | E_record of expression label_map - | E_record_accessor of accessor - | E_record_update of update + | E_record_accessor of record_accessor + | E_record_update of record_update (* Advanced *) | E_ascription of ascription (* Sugar *) | E_sequence of sequence | E_skip + | E_tuple of expression list + | E_tuple_accessor of tuple_accessor + | E_tuple_update of tuple_update (* Data Structures *) | E_map of (expression * expression) list | E_big_map of (expression * expression) list @@ -86,9 +103,8 @@ and let_in = { and constructor = {constructor: constructor'; element: expression} -and accessor = {record: expression; label: label} - -and update = {record: expression; path: label ; update: expression} +and record_accessor = {record: expression; path: label} +and record_update = {record: expression; path: label ; update: expression} and matching_expr = (expr,unit) matching_content and matching = @@ -102,6 +118,9 @@ and sequence = { expr2: expression ; } +and tuple_accessor = {tuple: expression; path: int} +and tuple_update = {tuple: expression; path: int ; update: expression} + and environment_element_definition = | ED_binder | ED_declaration of (expression * free_variables) diff --git a/src/stages/3-ast_core/PP.ml b/src/stages/3-ast_core/PP.ml index 10ab7e9d4..2909d4dc8 100644 --- a/src/stages/3-ast_core/PP.ml +++ b/src/stages/3-ast_core/PP.ml @@ -28,7 +28,7 @@ and expression_content ppf (ec : expression_content) = | E_record m -> fprintf ppf "%a" (tuple_or_record_sep_expr expression) m | E_record_accessor ra -> - fprintf ppf "%a.%a" expression ra.record label ra.label + fprintf ppf "%a.%a" expression ra.record label ra.path | E_record_update {record; path; update} -> fprintf ppf "{ %a with { %a = %a } }" expression record label path expression update | E_lambda {binder; input_type; output_type; result} -> diff --git a/src/stages/3-ast_core/combinators.ml b/src/stages/3-ast_core/combinators.ml index 787b61438..f5f5f103b 100644 --- a/src/stages/3-ast_core/combinators.ml +++ b/src/stages/3-ast_core/combinators.ml @@ -114,8 +114,8 @@ let e_map_add ?loc k v old : expression = make_expr ?loc @@ E_constant {cons_na let e_constructor ?loc s a : expression = make_expr ?loc @@ E_constructor { constructor = Constructor s; element = a} let e_matching ?loc a b : expression = make_expr ?loc @@ E_matching {matchee=a;cases=b} let e_matching_bool ?loc a b c : expression = e_matching ?loc a (Match_bool {match_true = b ; match_false = c}) -let e_accessor ?loc a b = make_expr ?loc @@ E_record_accessor {record = a; label= Label b} -let e_accessor_list ?loc a b = List.fold_left (fun a b -> e_accessor ?loc a b) a b +let e_record_accessor ?loc a b = make_expr ?loc @@ E_record_accessor {record = a; path = Label b} +let e_record_accessor_list ?loc a b = List.fold_left (fun a b -> e_record_accessor ?loc a b) a b let e_variable ?loc v = make_expr ?loc @@ E_variable v let e_let_in ?loc (binder, ascr) inline rhs let_result = make_expr ?loc @@ E_let_in { let_binder = (binder,ascr) ; rhs ; let_result; inline } @@ -139,7 +139,7 @@ let e_record ?loc map = let lst = Map.String.to_kv_list map in e_record_ez ?loc lst -let e_update ?loc record path update = +let e_record_update ?loc record path update = let path = Label path in make_expr ?loc @@ E_record_update {record; path; update} @@ -178,20 +178,20 @@ let e_assign_with_let ?loc var access_path expr = | lst -> let rec aux path record= match path with | [] -> failwith "acces_path cannot be empty" - | [e] -> e_update ?loc record e expr + | [e] -> e_record_update ?loc record e expr | elem::tail -> - let next_record = e_accessor record elem in - e_update ?loc record elem (aux tail next_record ) + let next_record = e_record_accessor record elem in + e_record_update ?loc record elem (aux tail next_record ) in (var, None), true, (aux lst (e_variable var)), false -let get_e_accessor = fun t -> +let get_e_record_accessor = fun t -> match t with - | E_record_accessor {record; label} -> ok (record , label) + | E_record_accessor {record; path} -> ok (record, path) | _ -> simple_fail "not an accessor" -let assert_e_accessor = fun t -> - let%bind _ = get_e_accessor t in +let assert_e_record_accessor = fun t -> + let%bind _ = get_e_record_accessor t in ok () let get_e_pair = fun t -> @@ -218,16 +218,9 @@ let get_e_list = fun t -> in aux t -let tuple_of_record (m: _ LMap.t) = - let aux i = - let opt = LMap.find_opt (Label (string_of_int i)) m in - Option.bind (fun opt -> Some (opt,i+1)) opt - in - Base.Sequence.to_list @@ Base.Sequence.unfold ~init:0 ~f:aux - let get_e_tuple = fun t -> match t with - | E_record r -> ok @@ tuple_of_record r + | E_record r -> ok @@ List.map snd @@ Stage_common.Helpers.tuple_of_record r | _ -> simple_fail "ast_core: get_e_tuple: not a tuple" (* Same as get_e_pair *) diff --git a/src/stages/3-ast_core/combinators.mli b/src/stages/3-ast_core/combinators.mli index 40a2a8496..e09ede186 100644 --- a/src/stages/3-ast_core/combinators.mli +++ b/src/stages/3-ast_core/combinators.mli @@ -76,8 +76,8 @@ val e_pair : ?loc:Location.t -> expression -> expression -> expression val e_constructor : ?loc:Location.t -> string -> expression -> expression val e_matching : ?loc:Location.t -> expression -> matching_expr -> expression val e_matching_bool : ?loc:Location.t -> expression -> expression -> expression -> expression -val e_accessor : ?loc:Location.t -> expression -> string -> expression -val e_accessor_list : ?loc:Location.t -> expression -> string list -> expression +val e_record_accessor : ?loc:Location.t -> expression -> string -> expression +val e_record_accessor_list : ?loc:Location.t -> expression -> string list -> expression val e_variable : ?loc:Location.t -> expression_variable -> expression val e_cond: ?loc:Location.t -> expression -> expression -> expression -> expression val e_let_in : ?loc:Location.t -> ( expression_variable * type_expression option ) -> bool -> expression -> expression -> expression @@ -95,14 +95,14 @@ val e_typed_none : ?loc:Location.t -> type_expression -> expression val e_lambda : ?loc:Location.t -> expression_variable -> type_expression option -> type_expression option -> expression -> expression val e_recursive : ?loc:Location.t -> expression_variable -> type_expression -> lambda -> expression val e_record : ?loc:Location.t -> expr Map.String.t -> expression -val e_update : ?loc:Location.t -> expression -> string -> expression -> expression +val e_record_update : ?loc:Location.t -> expression -> string -> expression -> expression val e_assign_with_let : ?loc:Location.t -> string -> string list -> expression -> ((expression_variable*type_expression option)*bool*expression*bool) (* val get_e_accessor : expression' -> ( expression * access_path ) result *) -val assert_e_accessor : expression_content -> unit result +val assert_e_record_accessor : expression_content -> unit result val get_e_pair : expression_content -> ( expression * expression ) result diff --git a/src/stages/3-ast_core/types.ml b/src/stages/3-ast_core/types.ml index ac59228c7..6c9637ac4 100644 --- a/src/stages/3-ast_core/types.ml +++ b/src/stages/3-ast_core/types.ml @@ -41,8 +41,8 @@ and expression_content = | E_matching of matching (* Record *) | E_record of expression label_map - | E_record_accessor of accessor - | E_record_update of update + | E_record_accessor of record_accessor + | E_record_update of record_update (* Advanced *) | E_ascription of ascription @@ -75,9 +75,8 @@ and let_in = and constructor = {constructor: constructor'; element: expression} -and accessor = {record: expression; label: label} - -and update = {record: expression; path: label ; update: expression} +and record_accessor = {record: expression; path: label} +and record_update = {record: expression; path: label ; update: expression} and matching_expr = (expr,unit) matching_content and matching = diff --git a/src/stages/4-ast_typed/PP.ml b/src/stages/4-ast_typed/PP.ml index 7461dd7de..0f1722641 100644 --- a/src/stages/4-ast_typed/PP.ml +++ b/src/stages/4-ast_typed/PP.ml @@ -29,7 +29,7 @@ and expression_content ppf (ec: expression_content) = | E_record m -> fprintf ppf "%a" (tuple_or_record_sep_expr expression) m | E_record_accessor ra -> - fprintf ppf "%a.%a" expression ra.record label ra.label + fprintf ppf "%a.%a" expression ra.record label ra.path | E_record_update {record; path; update} -> fprintf ppf "{ %a with { %a = %a } }" expression record label path expression update | E_lambda {binder; result} -> diff --git a/src/stages/4-ast_typed/combinators.ml b/src/stages/4-ast_typed/combinators.ml index 8a399bb35..8f4aaac2a 100644 --- a/src/stages/4-ast_typed/combinators.ml +++ b/src/stages/4-ast_typed/combinators.ml @@ -335,7 +335,7 @@ let get_a_bool (t:expression) = let get_a_record_accessor = fun t -> match t.expression_content with - | E_record_accessor {record ; label} -> ok (record , label) + | E_record_accessor {record; path} -> ok (record, path) | _ -> simple_fail "not an accessor" let get_declaration_by_name : program -> string -> declaration result = fun p name -> diff --git a/src/stages/4-ast_typed/types.ml b/src/stages/4-ast_typed/types.ml index e5ef3dd36..b406c46b7 100644 --- a/src/stages/4-ast_typed/types.ml +++ b/src/stages/4-ast_typed/types.ml @@ -47,8 +47,8 @@ and expression_content = | E_matching of matching (* Record *) | E_record of expression label_map - | E_record_accessor of accessor - | E_record_update of update + | E_record_accessor of record_accessor + | E_record_update of record_update and constant = { cons_name: constant' @@ -84,12 +84,12 @@ and constructor = { element: expression ; } -and accessor = { +and record_accessor = { record: expression ; - label: label ; + path: label ; } -and update = { +and record_update = { record: expression ; path: label ; update: expression ; diff --git a/src/stages/common/PP.ml b/src/stages/common/PP.ml index 559446668..832609b47 100644 --- a/src/stages/common/PP.ml +++ b/src/stages/common/PP.ml @@ -16,15 +16,14 @@ let cmap_sep value sep ppf m = let record_sep value sep ppf (m : 'a label_map) = let lst = LMap.to_kv_list m in - let lst = List.sort (fun (Label a,_) (Label b,_) -> String.compare a b) lst in + let lst = List.sort_uniq (fun (Label a,_) (Label b,_) -> String.compare a b) lst in let new_pp ppf (k, v) = fprintf ppf "%a -> %a" label k value v in fprintf ppf "%a" (list_sep new_pp sep) lst let tuple_sep value sep ppf m = assert (Helpers.is_tuple_lmap m); - let lst = LMap.to_kv_list m in - let lst = List.sort (fun (Label a,_) (Label b,_) -> String.compare a b) lst in - let new_pp ppf (_k, v) = fprintf ppf "%a" value v in + let lst = Helpers.tuple_of_record m in + let new_pp ppf (_, v) = fprintf ppf "%a" value v in fprintf ppf "%a" (list_sep new_pp sep) lst (* Prints records which only contain the consecutive fields @@ -156,43 +155,50 @@ let constant ppf : constant' -> unit = function let literal ppf (l : literal) = match l with - | Literal_unit -> - fprintf ppf "unit" - | Literal_void -> - fprintf ppf "void" - | Literal_bool b -> - fprintf ppf "%b" b - | Literal_int n -> - fprintf ppf "%d" n - | Literal_nat n -> - fprintf ppf "+%d" n - | Literal_timestamp n -> - fprintf ppf "+%d" n - | Literal_mutez n -> - fprintf ppf "%dmutez" n - | Literal_string s -> - fprintf ppf "%S" s - | Literal_bytes b -> - fprintf ppf "0x%a" Hex.pp (Hex.of_bytes b) - | Literal_address s -> - fprintf ppf "@%S" s - | Literal_operation _ -> - fprintf ppf "Operation(...bytes)" - | Literal_key s -> - fprintf ppf "key %s" s - | Literal_key_hash s -> - fprintf ppf "key_hash %s" s - | Literal_signature s -> - fprintf ppf "Signature %s" s - | Literal_chain_id s -> - fprintf ppf "Chain_id %s" s + | Literal_unit -> fprintf ppf "unit" + | Literal_void -> fprintf ppf "void" + | Literal_bool b -> fprintf ppf "%b" b + | Literal_int n -> fprintf ppf "%d" n + | Literal_nat n -> fprintf ppf "+%d" n + | Literal_timestamp n -> fprintf ppf "+%d" n + | Literal_mutez n -> fprintf ppf "%dmutez" n + | Literal_string s -> fprintf ppf "%S" s + | Literal_bytes b -> fprintf ppf "0x%a" Hex.pp (Hex.of_bytes b) + | Literal_address s -> fprintf ppf "@%S" s + | Literal_operation _ -> fprintf ppf "Operation(...bytes)" + | Literal_key s -> fprintf ppf "key %s" s + | Literal_key_hash s -> fprintf ppf "key_hash %s" s + | Literal_signature s -> fprintf ppf "Signature %s" s + | Literal_chain_id s -> fprintf ppf "Chain_id %s" s + +let type_variable ppf (t : type_variable) : unit = fprintf ppf "%a" Var.pp t + +and type_constant ppf (tc : type_constant) : unit = +let s = + match tc with + | TC_unit -> "unit" + | TC_string -> "string" + | TC_bytes -> "bytes" + | TC_nat -> "nat" + | TC_int -> "int" + | TC_mutez -> "mutez" + | TC_bool -> "bool" + | TC_operation -> "operation" + | TC_address -> "address" + | TC_key -> "key" + | TC_key_hash -> "key_hash" + | TC_signature -> "signature" + | TC_timestamp -> "timestamp" + | TC_chain_id -> "chain_id" + | TC_void -> "void" +in +fprintf ppf "%s" s + module Ast_PP_type (PARAMETER : AST_PARAMETER_TYPE) = struct module Agt=Ast_generic_type(PARAMETER) open Agt open Format - let type_variable ppf (t : type_variable) : unit = fprintf ppf "%a" Var.pp t - let rec type_expression' : (formatter -> type_expression -> unit) -> formatter @@ -200,58 +206,16 @@ module Ast_PP_type (PARAMETER : AST_PARAMETER_TYPE) = struct -> unit = fun f ppf te -> match te.type_content with - | T_sum m -> - fprintf ppf "sum[%a]" (cmap_sep_d f) m - | T_record m -> - fprintf ppf "%a" (tuple_or_record_sep_type f) m - | T_arrow a -> - fprintf ppf "%a -> %a" f a.type1 f a.type2 - | T_variable tv -> - type_variable ppf tv - | T_constant tc -> - type_constant ppf tc - | T_operator to_ -> - type_operator f ppf to_ + | T_sum m -> fprintf ppf "sum[%a]" (cmap_sep_d f) m + | T_record m -> fprintf ppf "%a" (tuple_or_record_sep_type f) m + | T_arrow a -> fprintf ppf "%a -> %a" f a.type1 f a.type2 + | T_variable tv -> type_variable ppf tv + | T_constant tc -> type_constant ppf tc + | T_operator to_ -> type_operator f ppf to_ and type_expression ppf (te : type_expression) : unit = type_expression' type_expression ppf te - and type_constant ppf (tc : type_constant) : unit = - let s = - match tc with - | TC_unit -> - "unit" - | TC_string -> - "string" - | TC_bytes -> - "bytes" - | TC_nat -> - "nat" - | TC_int -> - "int" - | TC_mutez -> - "mutez" - | TC_bool -> - "bool" - | TC_operation -> - "operation" - | TC_address -> - "address" - | TC_key -> - "key" - | TC_key_hash -> - "key_hash" - | TC_signature -> - "signature" - | TC_timestamp -> - "timestamp" - | TC_chain_id -> - "chain_id" - | TC_void -> - "void" - in - fprintf ppf "%s" s - and type_operator : (formatter -> type_expression -> unit) -> formatter diff --git a/src/stages/common/helpers.ml b/src/stages/common/helpers.ml index 5dba263b8..91cd0eae1 100644 --- a/src/stages/common/helpers.ml +++ b/src/stages/common/helpers.ml @@ -46,3 +46,23 @@ let get_pair m = match (LMap.find_opt (Label "0") m , LMap.find_opt (Label "1") m) with | Some e1, Some e2 -> ok (e1,e2) | _ -> simple_fail "not a pair" + +let tuple_of_record (m: _ LMap.t) = + let aux i = + let label = Label (string_of_int i) in + let opt = LMap.find_opt (label) m in + Option.bind (fun opt -> Some ((label,opt),i+1)) opt + in + Base.Sequence.to_list @@ Base.Sequence.unfold ~init:0 ~f:aux + +let list_of_record_or_tuple (m: _ LMap.t) = + if (is_tuple_lmap m) then + List.map snd @@ tuple_of_record m + else + List.rev @@ LMap.to_list m + +let kv_list_of_record_or_tuple (m: _ LMap.t) = + if (is_tuple_lmap m) then + tuple_of_record m + else + List.rev @@ LMap.to_kv_list m diff --git a/src/stages/common/helpers.mli b/src/stages/common/helpers.mli index d5e388e36..1292bb807 100644 --- a/src/stages/common/helpers.mli +++ b/src/stages/common/helpers.mli @@ -1,3 +1,5 @@ +open Types + val bind_lmap : ('a * 'b list, 'c) result Types.label_map -> ('a Types.label_map * 'b list, 'c) result @@ -19,6 +21,9 @@ val is_tuple_lmap : 'a Types.label_map -> bool val get_pair : 'a Types.label_map -> (('a * 'a) * 'b list, unit -> Trace.error) result +val tuple_of_record : 'a LMap.t -> (label * 'a) list +val list_of_record_or_tuple : 'a LMap.t -> 'a list +val kv_list_of_record_or_tuple : 'a LMap.t -> (label * 'a) list diff --git a/src/stages/typesystem/misc.ml b/src/stages/typesystem/misc.ml index 545534b77..6b950eccd 100644 --- a/src/stages/typesystem/misc.ml +++ b/src/stages/typesystem/misc.ml @@ -136,7 +136,7 @@ module Substitution = struct and s_matching_expr : T.matching_expr w = fun ~substs _ -> let _TODO = substs in failwith "TODO: subst: unimplemented case s_matching" - and s_accessor : T.accessor w = fun ~substs _ -> + and s_accessor : T.record_accessor w = fun ~substs _ -> let _TODO = substs in failwith "TODO: subst: unimplemented case s_access_path" @@ -182,10 +182,10 @@ module Substitution = struct * let val_ = s_expression ~v ~expr val_ in * ok @@ (key , val_)) aemap in * ok @@ T.E_record aemap *) - | T.E_record_accessor {record=e;label} -> + | T.E_record_accessor {record=e;path} -> let%bind record = s_expression ~substs e in - let%bind label = s_label ~substs label in - ok @@ T.E_record_accessor {record;label} + let%bind path = s_label ~substs path in + ok @@ T.E_record_accessor {record;path} | T.E_record_update {record;path;update}-> let%bind record = s_expression ~substs record in let%bind update = s_expression ~substs update in diff --git a/src/test/contracts/tuple.ligo b/src/test/contracts/tuple.ligo index afe953345..963d1aa51 100644 --- a/src/test/contracts/tuple.ligo +++ b/src/test/contracts/tuple.ligo @@ -13,6 +13,11 @@ const fb : foobar = (0,0) function projection (const tpl : foobar) : int is tpl.0 + tpl.1 -type big_tuple is int * int * int * int * int +type big_tuple is int * int * int * int * int * int * int * int * int * int * int * int -const br : big_tuple = (23, 23, 23, 23, 23) +const br : big_tuple = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) + +function update (const tpl : big_tuple) : big_tuple is + block { + tpl.11 := 2048 + } with tpl diff --git a/src/test/integration_tests.ml b/src/test/integration_tests.ml index 4006d6b79..c1929f55b 100644 --- a/src/test/integration_tests.ml +++ b/src/test/integration_tests.ml @@ -876,9 +876,14 @@ let tuple () : unit result = expect_eq_n program "modify_abc" make_input make_expected in let%bind () = - let expected = ez [23 ; 23 ; 23 ; 23 ; 23] in + let expected = ez [0 ; 1 ; 2 ; 3 ; 4; 5; 6; 7; 8; 9; 10; 11] in expect_eq_evaluate program "br" expected in + let%bind () = + let make_input = fun n -> ez [n; n; n; n; n; n; n; n; n; n; n; n] in + let make_expected = fun n -> ez [n; n; n; n; n; n; n; n; n; n; n; 2048] in + expect_eq_n program "update" make_input make_expected + in ok () let tuple_mligo () : unit result = From c9e509f636e94cd469bc29f825e5f6af75ccd170 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Wulfman Date: Tue, 31 Mar 2020 16:41:56 +0200 Subject: [PATCH 12/13] review 1 --- dexter.ligo | 585 ++++++++++++++++++++++++++++++ src/stages/1-ast_imperative/PP.ml | 12 +- src/stages/2-ast_sugar/PP.ml | 14 +- 3 files changed, 598 insertions(+), 13 deletions(-) create mode 100644 dexter.ligo diff --git a/dexter.ligo b/dexter.ligo new file mode 100644 index 000000000..0af2bb11d --- /dev/null +++ b/dexter.ligo @@ -0,0 +1,585 @@ +// Dexter +// a decentralized Tezos exchange for XTZ and FA1.2 +// copyright: camlCase 2019-2020 +// version: 0.1.5.0 + +// ============================================================================= +// Entrypoints +// ============================================================================= + +type entrypoint is +| Approve of (address * nat * nat) +| AddLiquidity of (address * nat * nat * timestamp) +| RemoveLiquidity of (address * address * nat * tez * nat * timestamp) +| XtzToToken of (address * nat * timestamp) +| TokenToXtz of (address * address * nat * tez * timestamp) +| BetForBakingRights of (key_hash * address * nat) +| EndAuctionRound +| UpdateTokenBalance of (nat) + +// the transfer entrypoint of the FA1.2 contract +type token_contract_transfer is (address * address * nat); + +// ============================================================================= +// Storage +// ============================================================================= + +type baker_address is key_hash; + +type account is record + balance : nat; + allowances: map(address, nat); +end + +// this is just to force big_maps as the first item of a pair on the top +// so we can still use the old big map route without big map id for +// convenience +type s is record + current_baker: option(baker_address); + current_baker_candidate: option(baker_address * address * tez * nat); + last_auction: timestamp; + lqt_total: nat; + token_address: address; + token_balance: nat; + rewards: (tez * nat); +end + +type storage is record + s: s; + accounts: big_map(address, account); +end + +// ============================================================================= +// Constants +// ============================================================================= + +const empty_allowances : map(address,nat) = map end; + +const empty_ops : list(operation) = list end; + +const no_baker_candidate: option(baker_address * address * tez * nat) = None; + +const no_baker: option(key_hash) = None; + +// 21 days +// 86400 seconds * 21 +const dexter_cycle: int = 1814400; + +// ============================================================================= +// Helper Functions +// ============================================================================= + +function mutez_to_natural(const a: tez): nat is + block {skip} with a / 1mutez + +function natural_to_mutez(const a: nat): tez is + block {skip} with a * 1mutez + +// this will fail if provided a negative number +function int_to_nat(const error: string ; const a: int): nat is + block { + var result : nat := 0n; + if (a >= 0) then block { + result := abs(a); + } else block { + failwith(error) + }; + } with result; + +// get an account from the big_map, if one does not exist for a particular +// address then create one. +function get_account(const a: address ; const m: big_map(address, account)): account is + block { skip } with ( + case (m[a]) of + | None -> record balance = 0n; allowances = empty_allowances; end + | Some(account) -> account + end + ); + +function update_allowance(const owner : address; + const spender : address; + const updated_allowance: nat; + var storage : storage): + storage is + block { + if (spender =/= owner) then block { + var account: account := get_account(owner, storage.accounts); + account.allowances[spender] := updated_allowance; + storage.accounts[owner] := record balance = account.balance; allowances = account.allowances; end; + } else { + skip; + } + } with storage; + +// if sender is owner, return amount, otherwise check if sender has permission +// if true then return amount, otherwise fail +function get_sender_allowance(const owner: address ; const storage: storage): nat is + block { + var result: nat := 0n; + case storage.accounts[owner] of + | None -> failwith("2") + | Some(account) -> block { + if sender =/= owner then block { + case account.allowances[sender] of + | None -> failwith("3") + | Some(allowance) -> result := allowance + end; + } else block { + result := account.balance + } + } + end; + } with result; + +function get_current_candidate_bet (const storage: storage): (tez * nat) is + block { + var current_candidate_bet: (tez * nat) := (0mutez, 0n); + case (storage.s.current_baker_candidate) of + | None -> skip + | Some(current_baker_candidate) -> current_candidate_bet := (current_baker_candidate.2, current_baker_candidate.3) + end + } with current_candidate_bet; + +// there might be some zero division edge cases +function get_xtz_pool (const current_candidate_bet_xtz: tez ; const storage: storage): (tez) is + block { + const time_since_last_auction: int = now - storage.s.last_auction; + const days_since_last_auction: int = time_since_last_auction / 86400; + var xtz_pool : tez := 0mutez; + + if (days_since_last_auction < dexter_cycle) then block { + const released_rewards : tez = (storage.s.rewards.0 / abs((days_since_last_auction * 1000 / dexter_cycle))) / 1000n; + const unreleased_rewards: tez = storage.s.rewards.0 - released_rewards; + + xtz_pool := balance - current_candidate_bet_xtz - unreleased_rewards - amount; + } else block { + // the slow reward wait has passed, all the rewards are released + + xtz_pool := balance - current_candidate_bet_xtz - amount; + }; + } with xtz_pool; + +// there might be some zero division edge cases +function get_token_pool (const current_candidate_bet_token: nat ; const storage: storage): (nat) is + block { + const time_since_last_auction: int = now - storage.s.last_auction; + const days_since_last_auction: int = time_since_last_auction / 86400; + var token_pool : nat := 0n; + + if (days_since_last_auction < dexter_cycle) then block { + const reward_days : int = days_since_last_auction; + const released_rewards : nat = (storage.s.rewards.1 / abs((reward_days * 1000 / dexter_cycle))) / 1000n; + const unreleased_rewards: nat = abs(storage.s.rewards.1 - released_rewards); + + token_pool := abs(storage.s.token_balance - current_candidate_bet_token - unreleased_rewards); + } else block { + // the slow reward wait has passed, all the rewards are released + token_pool := abs(storage.s.token_balance - current_candidate_bet_token) + }; + } with token_pool; + +// ============================================================================= +// Entrypoint Functions +// ============================================================================= + +function approve(const spender : address; + const allowance: nat; + const current_allowance: nat; + var storage : storage): + (list(operation) * storage) is + block { + if (spender =/= sender) then block { + // get the sender's account + // if the account does not exist, fail, we do not want to create accounts here + // creating accounts should be done in add_liquidity + const account: account = get_account(sender, storage.accounts); + + var sender_allowances: map(address, nat) := account.allowances; + sender_allowances[spender] := allowance; + storage.accounts[sender] := record balance = account.balance; allowances = sender_allowances; end; + } else block { + failwith("1"); + } + } with (empty_ops, storage); + +// it is assumed that the exchange contract has permission from the FA1.2 token +// to manage the assets of the user. It is the responsibility of the dApp +// developer to handle permissions. +function add_liquidity(const owner : address; + const min_lqt_created : nat; + const max_tokens_deposited: nat; + const deadline : timestamp; + var storage : storage): + (list(operation) * storage) is + block { + // add_liquidity performs a transfer to the token contract, we need to + // return the operations + var op_list: list(operation) := nil; + + if (now < deadline) then skip else block { + failwith("4"); + }; + + if (max_tokens_deposited > 0n) then skip else block { + failwith("5"); + }; + + if (amount > 0mutez) then skip else block { + failwith("6"); + }; + + if (storage.s.lqt_total > 0n) then block { + // lqt_total greater than zero + // use the existing exchange rate + + if (min_lqt_created > 0n) then skip else block { + failwith("7"); + }; + + const current_candidate_bet: (tez * nat) = get_current_candidate_bet(storage); + const xtz_pool : nat = mutez_to_natural(get_xtz_pool(current_candidate_bet.0, storage)); + const token_pool : nat = get_token_pool(current_candidate_bet.1, storage); + const nat_amount : nat = mutez_to_natural(amount); + const tokens_deposited : nat = nat_amount * token_pool / xtz_pool; + const lqt_minted : nat = nat_amount * storage.s.lqt_total / xtz_pool; + + if (max_tokens_deposited >= tokens_deposited) then skip else block { + failwith("8"); + }; + + if (lqt_minted >= min_lqt_created) then skip else block { + failwith("9"); + }; + + const account: account = get_account(owner, storage.accounts); + const new_balance: nat = account.balance + lqt_minted; + storage.accounts[owner] := record balance = new_balance; allowances = account.allowances; end; + storage.s.lqt_total := storage.s.lqt_total + lqt_minted; + storage.s.token_balance := storage.s.token_balance + tokens_deposited; + + // send FA1.2 from owner to exchange + const token_contract: contract(token_contract_transfer) = get_entrypoint("%transfer", storage.s.token_address); + const op1: operation = transaction((owner, self_address, tokens_deposited), 0mutez, token_contract); + op_list := list op1; end; + + } else block { + // initial add liquidity + if (amount >= 1tz) then skip else block { + failwith("10"); + }; + + const tokens_deposited : nat = max_tokens_deposited; + const current_candidate_bet: (tez * nat) = get_current_candidate_bet(storage); + const initial_liquidity : nat = mutez_to_natural(balance - current_candidate_bet.0); + + storage.s.lqt_total := initial_liquidity; + storage.accounts[owner] := record balance = initial_liquidity; allowances = empty_allowances; end; + storage.s.token_balance := tokens_deposited; + + // send FA1.2 tokens from owner to exchange + const token_contract: contract(token_contract_transfer) = get_entrypoint("%transfer", storage.s.token_address); + const op1: operation = transaction((owner, self_address, tokens_deposited), 0mutez, token_contract); + op_list := list op1; end; + } + } with (op_list, storage); + +function remove_liquidity(const owner : address; + const to_ : address; + const lqt_burned : nat; + const min_xtz_withdrawn : tez; + const min_tokens_withdrawn : nat; + const deadline : timestamp; + var storage : storage): + (list(operation) * storage) is + block { + var op_list: list(operation) := nil; + if (now < deadline) then skip else block { + failwith("11"); + }; + + if (min_xtz_withdrawn > 0mutez) then skip else block { + failwith("12"); + }; + + if (min_tokens_withdrawn > 0n) then skip else block { + failwith("13"); + }; + + if (lqt_burned > 0n) then skip else block { + failwith("14"); + }; + + // returns total if sender is owner, otherwise looks it up + const lqt: nat = get_sender_allowance(owner, storage); + + if (lqt >= lqt_burned) then skip else block { + failwith("15"); + }; + + if (storage.s.lqt_total > 0n) then skip else block { + failwith("16"); + }; + + const current_candidate_bet: (tez * nat) = get_current_candidate_bet(storage); + const xtz_withdrawn : tez = natural_to_mutez(lqt_burned * mutez_to_natural(balance - current_candidate_bet.0) / storage.s.lqt_total); + + if (xtz_withdrawn >= min_xtz_withdrawn) then skip else block { + failwith("17"); + }; + + const token_pool : nat = get_token_pool(current_candidate_bet.1, storage); + const tokens_withdrawn: nat = lqt_burned * token_pool / storage.s.lqt_total; + + if (tokens_withdrawn >= min_tokens_withdrawn) then skip else block { + failwith("18"); + }; + + const account: account = get_account(owner, storage.accounts); + + if (account.balance >= lqt_burned) then skip else block { + failwith("19"); + }; + + const new_balance: nat = int_to_nat("33", account.balance - lqt_burned); + storage.accounts[owner] := record balance = new_balance; allowances = account.allowances; end; + + storage.s.lqt_total := int_to_nat("34", storage.s.lqt_total - lqt_burned); + storage.s.token_balance := int_to_nat("35", storage.s.token_balance - tokens_withdrawn); + + // update allowance + // lqt - lqt_burned is safe, we have already checed that lqt >= lqt_burned + storage := update_allowance(owner, sender, int_to_nat("36", lqt - lqt_burned), storage); + + // send xtz_withdrawn to to_ address + const to_contract: contract(unit) = get_contract(to_); + const op1: operation = transaction(unit, xtz_withdrawn, to_contract); + + // send tokens_withdrawn to to address + // if tokens_withdrawn if greater than storage.s.token_balance, this will fail + const token_contract: contract(token_contract_transfer) = get_entrypoint("%transfer", storage.s.token_address); + const op2: operation = transaction((self_address, to_, tokens_withdrawn), 0mutez, token_contract); + op_list := list op1; op2; end + } with (op_list, storage); + +function xtz_to_token(const to_ : address; + const min_tokens_bought: nat; + const deadline : timestamp; + var storage : storage): + (list(operation) * storage) is + block { + var op_list: list(operation) := nil; + if (now < deadline) then skip else block { + failwith("20"); + }; + + const current_candidate_bet: (tez * nat) = get_current_candidate_bet(storage); + const xtz_pool : nat = mutez_to_natural(get_xtz_pool(current_candidate_bet.0, storage)); + const nat_amount : nat = mutez_to_natural(amount); + const token_pool : nat = get_token_pool(current_candidate_bet.1, storage); + const tokens_bought : nat = (nat_amount * 997n * token_pool) / (xtz_pool * 1000n + (nat_amount * 997n)); + + if (tokens_bought >= min_tokens_bought) then skip else block { + failwith("21"); + }; + + storage.s.token_balance := int_to_nat("32", storage.s.token_balance - tokens_bought); + + // send tokens_withdrawn to to address + // if tokens_bought is greater than storage.s.token_balance, this will fail + const token_contract: contract(token_contract_transfer) = get_entrypoint("%transfer", storage.s.token_address); + const op: operation = transaction((self_address, to_, tokens_bought), 0mutez, token_contract); + + // append internal operations + op_list := list op; end; + } with (op_list, storage); + +function token_to_xtz(const owner : address; // the address of the owner of FA1.2 + const to_ : address; + const tokens_sold : nat; + const min_xtz_bought: tez; + const deadline : timestamp; + var storage : storage): + (list(operation) * storage) is + block { + var op_list: list(operation) := nil; + if (now < deadline) then skip else block { + failwith("22"); + }; + + const current_candidate_bet: (tez * nat) = get_current_candidate_bet(storage); + const xtz_pool : tez = get_xtz_pool(current_candidate_bet.0, storage); + const token_pool : nat = get_token_pool(current_candidate_bet.1, storage); + const xtz_bought : tez = natural_to_mutez((tokens_sold * 997n * mutez_to_natural(xtz_pool)) / (token_pool * 1000n + (tokens_sold * 997n))); + + if (xtz_bought >= min_xtz_bought) then skip else block { + failwith("23"); + }; + + storage.s.token_balance := storage.s.token_balance + tokens_sold; + + // send xtz_bought to to_ address + const to_contract: contract(unit) = get_contract(to_); + const op1: operation = transaction(unit, xtz_bought, to_contract); + + // send tokens_sold to the exchange address + // this assumes that the exchange has an allowance for the token and owner in FA1.2 + const token_contract: contract(token_contract_transfer) = get_entrypoint("%transfer", storage.s.token_address); + const op2: operation = transaction((owner, self_address, tokens_sold), 0mutez, token_contract); + + // append internal operations + op_list := list op1; op2; end; + } with (op_list, storage); + +function assert_valid_baker (const current_baker: option(key_hash); + const candidate: key_hash): (operation * operation) is + block { + // test the candidate baker, if it is valid this will not fail + const test_set_delegate_operation: operation = set_delegate(Some(candidate)); + + // reset to the current baker + const reset_set_delegate_operation: operation = set_delegate(current_baker); + } with (test_set_delegate_operation, reset_set_delegate_operation); + +function bet_for_baking_rights (const candidate : key_hash; + const token_source : address; + const max_tokens_bet : nat; + var storage : storage): + (list(operation) * storage) is + block { + var op_list: list(operation) := nil; + + // this is a trick to assert that the provided baker address is valid + case (storage.s.current_baker_candidate) of + | None -> block { + const op_pair: (operation * operation) = assert_valid_baker(storage.s.current_baker, candidate); + op_list := op_pair.0 # op_list; + op_list := op_pair.1 # op_list; + } + | Some(current_baker_candidate) -> block { + if (current_baker_candidate.0 = candidate) then skip else block { + const op_pair: (operation * operation) = assert_valid_baker(storage.s.current_baker, candidate); + op_list := op_pair.0 # op_list; + op_list := op_pair.1 # op_list; + }; + } + end; + + // now we are sure it is a valid baker + + // set a minimum bet + if (max_tokens_bet > 0n) then skip else block { + failwith("24"); + }; + + if (amount > 0mutez) then skip else block { + failwith("25"); + }; + + const current_candidate_bet : (tez * nat) = get_current_candidate_bet(storage); + + if (amount > current_candidate_bet.0) then skip else { failwith("26") }; + + const xtz_pool : nat = mutez_to_natural(get_xtz_pool(current_candidate_bet.0, storage)); + const token_pool : nat = get_token_pool(current_candidate_bet.1, storage); + const nat_amount : nat = mutez_to_natural(amount); + const tokens_deposited : nat = nat_amount * token_pool / xtz_pool; + + if (tokens_deposited > current_candidate_bet.1) then skip else { failwith("27") }; + if (tokens_deposited > max_tokens_bet) then skip else { failwith("28") }; + + case (storage.s.current_baker_candidate) of + | None -> block { + // add the tokens_deposited to the token_balance + storage.s.token_balance := storage.s.token_balance + tokens_deposited; + } + | Some(current_baker_candidate) -> block { + // return rejected candidates tez and tokens to previous candidate + const token_contract: contract(token_contract_transfer) = get_entrypoint("%transfer", storage.s.token_address); + const return_token_op: operation = transaction((self_address, current_baker_candidate.1, current_baker_candidate.3), 0mutez, token_contract); + op_list := return_token_op # op_list; + + const to_contract: contract(unit) = get_contract(current_baker_candidate.1); + const return_xtz_op: operation = transaction(unit, current_baker_candidate.2, to_contract); + op_list := return_xtz_op # op_list; + + // remove the tokens from the current_baker_candidate + // add the tokens_deposited to the token_balance + storage.s.token_balance := abs(storage.s.token_balance - current_baker_candidate.3) + tokens_deposited; + } + end; + + storage.s.current_baker_candidate := Some((candidate,token_source,amount,tokens_deposited)); + + // send FA1.2 from owner to exchange, dexter needs permission to transfer these tokens + const token_contract: contract(token_contract_transfer) = get_entrypoint("%transfer", storage.s.token_address); + const xtz_to_dexter_op: operation = transaction((token_source, self_address, tokens_deposited), 0mutez, token_contract); + op_list := xtz_to_dexter_op # op_list; + + } with (op_list, storage); + +function end_auction_round(var storage : storage) : (list(operation) * storage) is + block { + var op_list: list(operation) := nil; + // 604800 seconds is one week + if (now > storage.s.last_auction + 604800) then skip else {failwith("29")}; + + case (storage.s.current_baker_candidate) of + | None -> block { + const set_delegate_op: operation = set_delegate(no_baker); + op_list := set_delegate_op # op_list; + } + | Some(current_baker_candidate) -> block { + case (storage.s.current_baker) of + | None -> block { + storage.s.current_baker := Some(current_baker_candidate.0); + storage.s.current_baker_candidate := no_baker_candidate; + storage.s.token_balance := storage.s.token_balance + current_baker_candidate.3; + storage.s.last_auction := now; + const set_delegate_op: operation = set_delegate(storage.s.current_baker); + op_list := set_delegate_op # op_list; + } + | Some(current_baker) -> block { + if (current_baker = current_baker_candidate.0) then block { + storage.s.current_baker_candidate := no_baker_candidate; + storage.s.token_balance := storage.s.token_balance + current_baker_candidate.3; + storage.s.last_auction := now; + } else { + storage.s.current_baker := Some(current_baker_candidate.0); + storage.s.current_baker_candidate := no_baker_candidate; + storage.s.token_balance := storage.s.token_balance + current_baker_candidate.3; + storage.s.last_auction := now; + const set_delegate_op: operation = set_delegate(storage.s.current_baker); + op_list := set_delegate_op # op_list; + } + } + end; + } + end; + + } with (op_list, storage); + +function update_token_balance(const token_balance: nat ; var storage : storage) : (list(operation) * storage) is + block { + var op_list: list(operation) := nil; + if (sender =/= storage.s.token_address) then { + failwith("31"); + } else { + storage.s.token_balance := token_balance; + } + } with (op_list, storage); + +// ============================================================================= +// Main +// ============================================================================= + +function main (const entrypoint : entrypoint ; const storage : storage) : (list(operation) * storage) is + (case entrypoint of + | Approve(xs) -> approve(xs.0,xs.1,xs.2,storage) + | AddLiquidity(xs) -> add_liquidity(xs.0,xs.1,xs.2,xs.3,storage) + | RemoveLiquidity(xs) -> remove_liquidity(xs.0,xs.1,xs.2,xs.3,xs.4,xs.5,storage) + | XtzToToken(xs) -> xtz_to_token(xs.0,xs.1,xs.2,storage) + | TokenToXtz(xs) -> token_to_xtz(xs.0,xs.1,xs.2,xs.3,xs.4,storage) + | BetForBakingRights(xs) -> bet_for_baking_rights(xs.0,xs.1,xs.2,storage) + | EndAuctionRound -> end_auction_round(storage) + | UpdateTokenBalance(xs) -> update_token_balance(xs, storage) + end); diff --git a/src/stages/1-ast_imperative/PP.ml b/src/stages/1-ast_imperative/PP.ml index 667da637d..2e8ff28a1 100644 --- a/src/stages/1-ast_imperative/PP.ml +++ b/src/stages/1-ast_imperative/PP.ml @@ -16,8 +16,8 @@ let rec type_expression' : fun f ppf te -> match te.type_content with | T_sum m -> fprintf ppf "sum[%a]" (cmap_sep_d f) m - | T_record m -> fprintf ppf "%a" (tuple_or_record_sep_type f) m - | T_tuple t -> fprintf ppf "%a" (list_sep_d f) t + | T_record m -> fprintf ppf "{%a}" (record_sep f (const ";")) m + | T_tuple t -> fprintf ppf "(%a)" (list_sep_d f) t | T_arrow a -> fprintf ppf "%a -> %a" f a.type1 f a.type2 | T_variable tv -> type_variable ppf tv | T_constant tc -> type_constant ppf tc @@ -60,11 +60,11 @@ and expression_content ppf (ec : expression_content) = fprintf ppf "%a(%a)" constant c.cons_name (list_sep_d expression) c.arguments | E_record m -> - fprintf ppf "%a" (tuple_or_record_sep_expr expression) m + fprintf ppf "{%a}" (record_sep expression (const ";")) m | E_record_accessor ra -> fprintf ppf "%a.%a" expression ra.record label ra.path | E_record_update {record; path; update} -> - fprintf ppf "{ %a with { %a = %a } }" expression record label path expression update + fprintf ppf "{ %a with %a = %a }" expression record label path expression update | E_map m -> fprintf ppf "map[%a]" (list_sep_d assoc_expression) m | E_big_map m -> @@ -101,11 +101,11 @@ and expression_content ppf (ec : expression_content) = | E_skip -> fprintf ppf "skip" | E_tuple t -> - fprintf ppf "%a" (list_sep_d expression) t + fprintf ppf "(%a)" (list_sep_d expression) t | E_tuple_accessor ta -> fprintf ppf "%a.%d" expression ta.tuple ta.path | E_tuple_update {tuple; path; update} -> - fprintf ppf "{ %a with { %d = %a } }" expression tuple path expression update + fprintf ppf "{ %a with %d = %a }" expression tuple path expression update | E_assign {variable; access_path; expression=e} -> fprintf ppf "%a%a := %a" expression_variable variable diff --git a/src/stages/2-ast_sugar/PP.ml b/src/stages/2-ast_sugar/PP.ml index bb4d4f147..e2f86622e 100644 --- a/src/stages/2-ast_sugar/PP.ml +++ b/src/stages/2-ast_sugar/PP.ml @@ -16,9 +16,9 @@ let rec type_expression' : fun f ppf te -> match te.type_content with | T_sum m -> fprintf ppf "sum[%a]" (cmap_sep_d f) m - | T_record m -> fprintf ppf "%a" (tuple_or_record_sep_type f) m - | T_tuple t -> fprintf ppf "%a" (list_sep_d f) t - | T_arrow a -> fprintf ppf "%a -> %a" f a.type1 f a.type2 + | T_record m -> fprintf ppf "{%a}" (record_sep f (const ";")) m + | T_tuple t -> fprintf ppf "(%a)" (list_sep_d f) t + | T_arrow a -> fprintf ppf "%a -> %a" f a.type1 f a.type2 | T_variable tv -> type_variable ppf tv | T_constant tc -> type_constant ppf tc | T_operator to_ -> type_operator f ppf to_ @@ -56,11 +56,11 @@ and expression_content ppf (ec : expression_content) = fprintf ppf "%a(%a)" constant c.cons_name (list_sep_d expression) c.arguments | E_record m -> - fprintf ppf "%a" (tuple_or_record_sep_expr expression) m + fprintf ppf "{%a}" (record_sep expression (const ";")) m | E_record_accessor ra -> fprintf ppf "%a.%a" expression ra.record label ra.path | E_record_update {record; path; update} -> - fprintf ppf "{ %a with { %a = %a } }" expression record label path expression update + fprintf ppf "{ %a with %a = %a }" expression record label path expression update | E_map m -> fprintf ppf "map[%a]" (list_sep_d assoc_expression) m | E_big_map m -> @@ -100,11 +100,11 @@ and expression_content ppf (ec : expression_content) = | E_skip -> fprintf ppf "skip" | E_tuple t -> - fprintf ppf "%a" (list_sep_d expression) t + fprintf ppf "(%a)" (list_sep_d expression) t | E_tuple_accessor ta -> fprintf ppf "%a.%d" expression ta.tuple ta.path | E_tuple_update {tuple; path; update} -> - fprintf ppf "{ %a with { %d = %a } }" expression tuple path expression update + fprintf ppf "{ %a with %d = %a }" expression tuple path expression update and option_type_name ppf ((n, ty_opt) : expression_variable * type_expression option) = From 9dc7e7fcb9a2469c0dba28d6b71e7ee8d437ff0b Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Wulfman Date: Mon, 30 Mar 2020 14:38:18 +0200 Subject: [PATCH 13/13] Add E_cond as sugar (if .. then .. else ..) for match_bool --- src/passes/3-self_ast_imperative/helpers.ml | 15 +++++++++++ .../imperative_to_sugar.ml | 27 +++++++++++++++++++ src/passes/5-self_ast_sugar/helpers.ml | 15 +++++++++++ src/passes/6-sugar_to_core/sugar_to_core.ml | 5 ++++ src/stages/1-ast_imperative/PP.ml | 5 ++++ src/stages/1-ast_imperative/combinators.ml | 2 +- src/stages/1-ast_imperative/misc.ml | 3 ++- src/stages/1-ast_imperative/types.ml | 8 ++++++ src/stages/2-ast_sugar/PP.ml | 9 +++++-- src/stages/2-ast_sugar/combinators.ml | 2 +- src/stages/2-ast_sugar/combinators.mli | 2 +- src/stages/2-ast_sugar/misc.ml | 3 ++- src/stages/2-ast_sugar/types.ml | 7 +++++ 13 files changed, 96 insertions(+), 7 deletions(-) diff --git a/src/passes/3-self_ast_imperative/helpers.ml b/src/passes/3-self_ast_imperative/helpers.ml index c0af289dc..47626335f 100644 --- a/src/passes/3-self_ast_imperative/helpers.ml +++ b/src/passes/3-self_ast_imperative/helpers.ml @@ -76,6 +76,11 @@ let rec fold_expression : 'a folder -> 'a -> expression -> 'a result = fun f ini | E_recursive { lambda={result=e;_}; _} -> let%bind res = self init' e in ok res + | E_cond {condition; then_clause; else_clause} -> + let%bind res = self init' condition in + let%bind res = self res then_clause in + let%bind res = self res else_clause in + ok res | E_sequence {expr1;expr2} -> let ab = (expr1,expr2) in let%bind res = bind_fold_pair self init' ab in @@ -217,6 +222,11 @@ let rec map_expression : exp_mapper -> expression -> expression result = fun f e let%bind args = bind_map_list self c.arguments in return @@ E_constant {c with arguments=args} ) + | E_cond {condition; then_clause; else_clause} -> + let%bind condition = self condition in + let%bind then_clause = self then_clause in + let%bind else_clause = self else_clause in + return @@ E_cond {condition;then_clause;else_clause} | E_sequence {expr1;expr2} -> ( let%bind (expr1,expr2) = bind_map_pair self (expr1,expr2) in return @@ E_sequence {expr1;expr2} @@ -396,6 +406,11 @@ let rec fold_map_expression : 'a fold_mapper -> 'a -> expression -> ('a * expres let%bind (res,args) = bind_fold_map_list self init' c.arguments in ok (res, return @@ E_constant {c with arguments=args}) ) + | E_cond {condition; then_clause; else_clause} -> + let%bind res,condition = self init' condition in + let%bind res,then_clause = self res then_clause in + let%bind res,else_clause = self res else_clause in + ok (res, return @@ E_cond {condition;then_clause;else_clause}) | E_sequence {expr1;expr2} -> ( let%bind (res,(expr1,expr2)) = bind_fold_map_pair self init' (expr1,expr2) in ok (res, return @@ E_sequence {expr1;expr2}) diff --git a/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml b/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml index 89e5d99fb..4a69e6c67 100644 --- a/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml +++ b/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml @@ -234,6 +234,28 @@ let rec compile_expression : I.expression -> O.expression result = let%bind anno_expr = compile_expression anno_expr in let%bind type_annotation = compile_type_expression type_annotation in return @@ O.E_ascription {anno_expr; type_annotation} + | I.E_cond {condition;then_clause;else_clause} -> + let%bind condition = compile_expression condition in + let%bind then_clause' = compile_expression then_clause in + let%bind else_clause' = compile_expression else_clause in + let env = Var.fresh () in + let%bind ((_,free_vars_true), then_clause) = repair_mutable_variable_in_matching then_clause' [] env in + let%bind ((_,free_vars_false), else_clause) = repair_mutable_variable_in_matching else_clause' [] env in + let then_clause = add_to_end then_clause (O.e_variable env) in + let else_clause = add_to_end else_clause (O.e_variable env) in + + let free_vars = List.sort_uniq Var.compare @@ free_vars_true @ free_vars_false in + if (List.length free_vars != 0) then + let cond_expr = O.e_cond condition then_clause else_clause in + let return_expr = fun expr -> + O.E_let_in {let_binder=(env,None); mut=false; inline=false;rhs=(store_mutable_variable free_vars); + let_result=O.e_let_in (env,None) false false cond_expr @@ + expr + } + in + return @@ restore_mutable_variable return_expr free_vars env + else + return @@ O.E_cond {condition; then_clause=then_clause'; else_clause=else_clause'} | I.E_sequence {expr1; expr2} -> let%bind expr1 = compile_expression expr1 in let%bind expr2 = compile_expression expr2 in @@ -672,6 +694,11 @@ let rec uncompile_expression : O.expression -> I.expression result = let%bind anno_expr = uncompile_expression anno_expr in let%bind type_annotation = uncompile_type_expression type_annotation in return @@ I.E_ascription {anno_expr; type_annotation} + | O.E_cond {condition;then_clause;else_clause} -> + let%bind condition = uncompile_expression condition in + let%bind then_clause = uncompile_expression then_clause in + let%bind else_clause = uncompile_expression else_clause in + return @@ I.E_cond {condition; then_clause; else_clause} | O.E_sequence {expr1; expr2} -> let%bind expr1 = uncompile_expression expr1 in let%bind expr2 = uncompile_expression expr2 in diff --git a/src/passes/5-self_ast_sugar/helpers.ml b/src/passes/5-self_ast_sugar/helpers.ml index 46b9e4cfd..7ac208768 100644 --- a/src/passes/5-self_ast_sugar/helpers.ml +++ b/src/passes/5-self_ast_sugar/helpers.ml @@ -56,6 +56,11 @@ let rec fold_expression : 'a folder -> 'a -> expression -> 'a result = fun f ini let%bind res = self res let_result in ok res ) + | E_cond {condition; then_clause; else_clause} -> + let%bind res = self init' condition in + let%bind res = self res then_clause in + let%bind res = self res else_clause in + ok res | E_recursive { lambda={result=e;_}; _} -> let%bind res = self init' e in ok res @@ -189,6 +194,11 @@ let rec map_expression : exp_mapper -> expression -> expression result = fun f e let%bind args = bind_map_list self c.arguments in return @@ E_constant {c with arguments=args} ) + | E_cond {condition; then_clause; else_clause} -> + let%bind condition = self condition in + let%bind then_clause = self then_clause in + let%bind else_clause = self else_clause in + return @@ E_cond {condition;then_clause;else_clause} | E_sequence {expr1;expr2} -> ( let%bind (expr1,expr2) = bind_map_pair self (expr1,expr2) in return @@ E_sequence {expr1;expr2} @@ -365,6 +375,11 @@ let rec fold_map_expression : 'a fold_mapper -> 'a -> expression -> ('a * expres let%bind (res,args) = bind_fold_map_list self init' c.arguments in ok (res, return @@ E_constant {c with arguments=args}) ) + | E_cond {condition; then_clause; else_clause} -> + let%bind res,condition = self init' condition in + let%bind res,then_clause = self res then_clause in + let%bind res,else_clause = self res else_clause in + ok (res, return @@ E_cond {condition;then_clause;else_clause}) | E_sequence {expr1;expr2} -> ( let%bind (res,(expr1,expr2)) = bind_fold_map_pair self init' (expr1,expr2) in ok (res, return @@ E_sequence {expr1;expr2}) diff --git a/src/passes/6-sugar_to_core/sugar_to_core.ml b/src/passes/6-sugar_to_core/sugar_to_core.ml index 1d6270dcb..b175f8eb3 100644 --- a/src/passes/6-sugar_to_core/sugar_to_core.ml +++ b/src/passes/6-sugar_to_core/sugar_to_core.ml @@ -154,6 +154,11 @@ let rec compile_expression : I.expression -> O.expression result = let%bind anno_expr = compile_expression anno_expr in let%bind type_annotation = idle_type_expression type_annotation in return @@ O.E_ascription {anno_expr; type_annotation} + | I.E_cond {condition; then_clause; else_clause} -> + let%bind matchee = compile_expression condition in + let%bind match_true = compile_expression then_clause in + let%bind match_false = compile_expression else_clause in + return @@ O.E_matching {matchee; cases=Match_bool{match_true;match_false}} | I.E_sequence {expr1; expr2} -> let%bind expr1 = compile_expression expr1 in let%bind expr2 = compile_expression expr2 in diff --git a/src/stages/1-ast_imperative/PP.ml b/src/stages/1-ast_imperative/PP.ml index 2e8ff28a1..a9ca9fe03 100644 --- a/src/stages/1-ast_imperative/PP.ml +++ b/src/stages/1-ast_imperative/PP.ml @@ -96,6 +96,11 @@ and expression_content ppf (ec : expression_content) = | E_ascription {anno_expr; type_annotation} -> fprintf ppf "%a : %a" expression anno_expr type_expression type_annotation + | E_cond {condition; then_clause; else_clause} -> + fprintf ppf "if %a then %a else %a" + expression condition + expression then_clause + expression else_clause | E_sequence {expr1;expr2} -> fprintf ppf "{ %a; @. %a}" expression expr1 expression expr2 | E_skip -> diff --git a/src/stages/1-ast_imperative/combinators.ml b/src/stages/1-ast_imperative/combinators.ml index 2dd149b8a..a71f5268e 100644 --- a/src/stages/1-ast_imperative/combinators.ml +++ b/src/stages/1-ast_imperative/combinators.ml @@ -131,7 +131,7 @@ let e_while ?loc condition body = make_expr ?loc @@ E_while {condition; body} let e_for ?loc binder start final increment body = make_expr ?loc @@ E_for {binder;start;final;increment;body} let e_for_each ?loc binder collection collection_type body = make_expr ?loc @@ E_for_each {binder;collection;collection_type;body} -let e_cond ?loc expr match_true match_false = e_matching expr ?loc (Match_bool {match_true; match_false}) +let e_cond ?loc condition then_clause else_clause = make_expr ?loc @@ E_cond {condition;then_clause;else_clause} (* let e_assign ?loc a b c = location_wrap ?loc @@ E_assign (Var.of_name a , b , c) (* TODO handlethat*) *) diff --git a/src/stages/1-ast_imperative/misc.ml b/src/stages/1-ast_imperative/misc.ml index 1c37f1744..884822fff 100644 --- a/src/stages/1-ast_imperative/misc.ml +++ b/src/stages/1-ast_imperative/misc.ml @@ -204,7 +204,8 @@ let rec assert_value_eq (a, b: (expression * expression )) : unit result = | (E_application _, _) | (E_let_in _, _) | (E_recursive _,_) | (E_record_accessor _, _) | (E_tuple_accessor _, _) - | (E_look_up _, _) | (E_matching _, _) + | (E_look_up _, _) + | (E_matching _, _) | (E_cond _, _) | (E_sequence _, _) | (E_skip, _) | (E_assign _, _) | (E_for _, _) | (E_for_each _, _) diff --git a/src/stages/1-ast_imperative/types.ml b/src/stages/1-ast_imperative/types.ml index ee69248ba..6c396fb08 100644 --- a/src/stages/1-ast_imperative/types.ml +++ b/src/stages/1-ast_imperative/types.ml @@ -60,6 +60,7 @@ and expression_content = (* Advanced *) | E_ascription of ascription (* Sugar *) + | E_cond of conditional | E_sequence of sequence | E_skip | E_tuple of expression list @@ -118,6 +119,13 @@ and matching = } and ascription = {anno_expr: expression; type_annotation: type_expression} + +and conditional = { + condition : expression ; + then_clause : expression ; + else_clause : expression ; +} + and sequence = { expr1: expression ; expr2: expression ; diff --git a/src/stages/2-ast_sugar/PP.ml b/src/stages/2-ast_sugar/PP.ml index e2f86622e..b57e65bcb 100644 --- a/src/stages/2-ast_sugar/PP.ml +++ b/src/stages/2-ast_sugar/PP.ml @@ -93,10 +93,15 @@ and expression_content ppf (ec : expression_content) = expression rhs option_inline inline expression let_result - | E_sequence {expr1;expr2} -> - fprintf ppf "{ %a; @. %a}" expression expr1 expression expr2 | E_ascription {anno_expr; type_annotation} -> fprintf ppf "%a : %a" expression anno_expr type_expression type_annotation + | E_cond {condition; then_clause; else_clause} -> + fprintf ppf "if %a then %a else %a" + expression condition + expression then_clause + expression else_clause + | E_sequence {expr1;expr2} -> + fprintf ppf "{ %a; @. %a}" expression expr1 expression expr2 | E_skip -> fprintf ppf "skip" | E_tuple t -> diff --git a/src/stages/2-ast_sugar/combinators.ml b/src/stages/2-ast_sugar/combinators.ml index cc18739c2..4edfc377f 100644 --- a/src/stages/2-ast_sugar/combinators.ml +++ b/src/stages/2-ast_sugar/combinators.ml @@ -122,6 +122,7 @@ let e_constant ?loc name lst = make_expr ?loc @@ E_constant {cons_name=name ; ar let e_annotation ?loc anno_expr ty = make_expr ?loc @@ E_ascription {anno_expr; type_annotation = ty} +let e_cond ?loc condition then_clause else_clause = make_expr ?loc @@ E_cond {condition;then_clause;else_clause} let e_sequence ?loc expr1 expr2 = make_expr ?loc @@ E_sequence {expr1; expr2} let e_skip ?loc () = make_expr ?loc @@ E_skip @@ -153,7 +154,6 @@ let make_option_typed ?loc e t_opt = | None -> e | Some t -> e_annotation ?loc e t -let e_cond ?loc expr match_true match_false = e_matching expr ?loc (Match_bool {match_true; match_false}) let e_tuple ?loc lst : expression = e_record_ez ?loc (tuple_to_record lst) let e_pair ?loc a b : expression = e_tuple ?loc [a;b] diff --git a/src/stages/2-ast_sugar/combinators.mli b/src/stages/2-ast_sugar/combinators.mli index a8d7b5919..1fa10df89 100644 --- a/src/stages/2-ast_sugar/combinators.mli +++ b/src/stages/2-ast_sugar/combinators.mli @@ -84,6 +84,7 @@ val e_record_accessor_list : ?loc:Location.t -> expression -> string list -> exp val e_annotation : ?loc:Location.t -> expression -> type_expression -> expression +val e_cond: ?loc:Location.t -> expression -> expression -> expression -> expression val e_sequence : ?loc:Location.t -> expression -> expression -> expression val e_skip : ?loc:Location.t -> unit -> expression @@ -109,7 +110,6 @@ val e_typed_big_map : ?loc:Location.t -> ( expression * expression ) list -> ty val e_typed_set : ?loc:Location.t -> expression list -> type_expression -> expression val e_record_ez : ?loc:Location.t -> (string * expression) list -> expression -val e_cond: ?loc:Location.t -> expression -> expression -> expression -> expression val e_tuple : ?loc:Location.t -> expression list -> expression val e_pair : ?loc:Location.t -> expression -> expression -> expression diff --git a/src/stages/2-ast_sugar/misc.ml b/src/stages/2-ast_sugar/misc.ml index 2d43bf2a9..508ae70d9 100644 --- a/src/stages/2-ast_sugar/misc.ml +++ b/src/stages/2-ast_sugar/misc.ml @@ -204,7 +204,8 @@ let rec assert_value_eq (a, b: (expression * expression )) : unit result = | (E_application _, _) | (E_let_in _, _) | (E_recursive _,_) | (E_record_accessor _, _) | (E_tuple_accessor _, _) - | (E_look_up _, _) | (E_matching _, _) + | (E_look_up _, _) + | (E_matching _, _) | (E_cond _, _) | (E_sequence _, _) | (E_skip, _) -> simple_fail "comparing not a value" let is_value_eq (a , b) = to_bool @@ assert_value_eq (a , b) diff --git a/src/stages/2-ast_sugar/types.ml b/src/stages/2-ast_sugar/types.ml index f4650284c..cd648c754 100644 --- a/src/stages/2-ast_sugar/types.ml +++ b/src/stages/2-ast_sugar/types.ml @@ -60,6 +60,7 @@ and expression_content = (* Advanced *) | E_ascription of ascription (* Sugar *) + | E_cond of conditional | E_sequence of sequence | E_skip | E_tuple of expression list @@ -113,6 +114,12 @@ and matching = } and ascription = {anno_expr: expression; type_annotation: type_expression} + +and conditional = { + condition : expression ; + then_clause : expression ; + else_clause : expression ; +} and sequence = { expr1: expression ; expr2: expression ;