diff --git a/src/bin/expect_tests/get_scope.ml b/src/bin/expect_tests/get_scope.ml
index 2c4e3050eae789c7637dc8b159b0ed0fb2d7d9d3..0c5cca7602dc24d250c402df7de3f25635965e71 100644
--- a/src/bin/expect_tests/get_scope.ml
+++ b/src/bin/expect_tests/get_scope.ml
@@ -1,7 +1,6 @@
 open Cli_expect
 
 let gs s = "../../test/contracts/get_scope_tests/" ^ s
-let () = Ligo_unix.putenv ~key:"LIGO_GET_SCOPE_USE_NEW_IMP" ~data:""
 
 let%expect_test _ =
   run_ligo_good
@@ -17,6 +16,7 @@ let%expect_test _ =
     {|
     Scopes:
     [  ] File "../../test/contracts/get_scope_tests/lambda_letin.mligo", line 1, characters 8-9
+    [ a#1:4-5  ] File "../../test/contracts/get_scope_tests/lambda_letin.mligo", line 4, characters 11-28
     [ j#4:47-48 i#4:37-38 a#1:4-5  ] File "../../test/contracts/get_scope_tests/lambda_letin.mligo", line 5, characters 12-21
     [ g#5:8-9 j#4:47-48 i#4:37-38 a#1:4-5  ] File "../../test/contracts/get_scope_tests/lambda_letin.mligo", line 6, characters 12-25
     [ k#6:8-9 g#5:8-9 j#4:47-48 i#4:37-38 a#1:4-5  ] File "../../test/contracts/get_scope_tests/lambda_letin.mligo", line 7, characters 4-21
@@ -156,6 +156,7 @@ let%expect_test _ =
     {|
     Scopes:
     [  ] File "../../test/contracts/get_scope_tests/lambda.mligo", line 1, characters 8-9
+    [ a#1:4-5  ] File "../../test/contracts/get_scope_tests/lambda.mligo", line 4, characters 11-27
     [ j#4:46-47 i#4:36-37 a#1:4-5  ] File "../../test/contracts/get_scope_tests/lambda.mligo", line 4, characters 58-63
     [ f#4:6-7 a#1:4-5  ] File "../../test/contracts/get_scope_tests/lambda.mligo", line 5, characters 2-7
 
@@ -204,12 +205,13 @@ let%expect_test _ =
   [%expect
     {|
     Scopes:
+    [  ] File "../../test/contracts/get_scope_tests/match.mligo", line 1, characters 14-40
     [ mytype#1:5-11  ] File "../../test/contracts/get_scope_tests/match.mligo", line 3, characters 8-9
     [ a#3:4-5 mytype#1:5-11  ] File "../../test/contracts/get_scope_tests/match.mligo", line 6, characters 17-18
     [ c#6:13-14 a#3:4-5 mytype#1:5-11  ] File "../../test/contracts/get_scope_tests/match.mligo", line 6, characters 26-27
     [ x#7:8-9 a#3:4-5 mytype#1:5-11  ] File "../../test/contracts/get_scope_tests/match.mligo", line 7, characters 13-18
     [ y#8:8-9 a#3:4-5 mytype#1:5-11  ] File "../../test/contracts/get_scope_tests/match.mligo", line 8, characters 13-18
-    [ b#5:4-5 a#3:4-5 mytype#1:5-11  ] File "../../test/contracts/get_scope_tests/match.mligo", line 11, characters 11-19
+    [ b#5:4-5 a#3:4-5 mytype#1:5-11  ] File "../../test/contracts/get_scope_tests/match.mligo", line 11, characters 9-21
     [ b#5:4-5 a#3:4-5 mytype#1:5-11  ] File "../../test/contracts/get_scope_tests/match.mligo", line 13, characters 12-13
     [ c#13:8-9 b#5:4-5 a#3:4-5 mytype#1:5-11  ] File "../../test/contracts/get_scope_tests/match.mligo", line 14, characters 4-5
     [ hd#15:4-6 tl#15:8-10 b#5:4-5 a#3:4-5 mytype#1:5-11  ] File "../../test/contracts/get_scope_tests/match.mligo", line 15, characters 14-15
@@ -386,12 +388,16 @@ let%expect_test _ =
     [  ] File "../../test/contracts/get_scope_tests/rec.mligo", line 1, characters 8-9
     [ c#4:10-11 a#1:4-5  ] File "../../test/contracts/get_scope_tests/rec.mligo", line 4, characters 37-40
     [ j#4:39-40 i#4:37-38 c#4:10-11 a#1:4-5  ] File "../../test/contracts/get_scope_tests/rec.mligo", line 5, characters 12-21
-    [ k#5:8-9 j#4:39-40 i#4:37-38 c#4:10-11 a#1:4-5  ] File "../../test/contracts/get_scope_tests/rec.mligo", line 6, characters 4-10
-    [ m#8:23-24 n#8:13-14 z#8:10-11 c#4:10-11 a#1:4-5  ] File "../../test/contracts/get_scope_tests/rec.mligo", line 9, characters 7-36
+    [ k#5:8-9 j#4:39-40 i#4:37-38 c#4:10-11 a#1:4-5  ] File "../../test/contracts/get_scope_tests/rec.mligo", line 6, characters 4-11
+    [ m#8:23-24 n#8:13-14 z#8:10-11 c#4:10-11 a#1:4-5  ] File "../../test/contracts/get_scope_tests/rec.mligo", line 9, characters 7-12
+    [ m#8:23-24 n#8:13-14 z#8:10-11 c#4:10-11 a#1:4-5  ] File "../../test/contracts/get_scope_tests/rec.mligo", line 9, characters 18-19
+    [ m#8:23-24 n#8:13-14 z#8:10-11 c#4:10-11 a#1:4-5  ] File "../../test/contracts/get_scope_tests/rec.mligo", line 9, characters 25-36
     [ z#8:10-11 c#4:10-11 a#1:4-5  ] File "../../test/contracts/get_scope_tests/rec.mligo", line 11, characters 10-18
     [ v#11:6-7 z#8:10-11 c#4:10-11 a#1:4-5  ] File "../../test/contracts/get_scope_tests/rec.mligo", line 12, characters 10-15
-    [ b#12:6-7 v#11:6-7 z#8:10-11 c#4:10-11 a#1:4-5  ] File "../../test/contracts/get_scope_tests/rec.mligo", line 13, characters 2-9
-    [ y#15:11-12 x#15:8-9 b#3:4-5 a#1:4-5  ] File "../../test/contracts/get_scope_tests/rec.mligo", line 16, character 5 to line 17, character 15
+    [ b#12:6-7 v#11:6-7 z#8:10-11 c#4:10-11 a#1:4-5  ] File "../../test/contracts/get_scope_tests/rec.mligo", line 13, characters 2-10
+    [ y#15:11-12 x#15:8-9 b#3:4-5 a#1:4-5  ] File "../../test/contracts/get_scope_tests/rec.mligo", line 16, characters 5-10
+    [ y#15:11-12 x#15:8-9 b#3:4-5 a#1:4-5  ] File "../../test/contracts/get_scope_tests/rec.mligo", line 16, characters 16-18
+    [ y#15:11-12 x#15:8-9 b#3:4-5 a#1:4-5  ] File "../../test/contracts/get_scope_tests/rec.mligo", line 17, characters 7-16
 
     Variable definitions:
     (a#1:4-5 -> a)
@@ -560,6 +566,7 @@ let%expect_test _ =
   [%expect
     {|
     Scopes:
+    [  ] File "../../test/contracts/get_scope_tests/records.mligo", line 1, characters 13-36
     [ myrec#1:5-10  ] File "../../test/contracts/get_scope_tests/records.mligo", line 3, characters 8-9
     [ a#3:4-5 myrec#1:5-10  ] File "../../test/contracts/get_scope_tests/records.mligo", line 6, characters 22-23
     [ i#6:18-19 a#3:4-5 myrec#1:5-10  ] File "../../test/contracts/get_scope_tests/records.mligo", line 6, characters 35-36
@@ -628,10 +635,10 @@ let%expect_test _ =
     {|
     Scopes:
     [  ] File "../../test/contracts/get_scope_tests/constant.mligo", line 1, characters 8-9
-    [ a#1:4-5  ] File "../../test/contracts/get_scope_tests/constant.mligo", line 4, character 2 to line 6, character 14
     [ c#5:10-11 a#1:4-5  ] File "../../test/contracts/get_scope_tests/constant.mligo", line 5, characters 30-31
     [ d#5:26-27 c#5:10-11 a#1:4-5  ] File "../../test/contracts/get_scope_tests/constant.mligo", line 5, characters 35-44
-    [ e#6:9-10 a#1:4-5  ] File "../../test/contracts/get_scope_tests/constant.mligo", line 6, characters 20-30
+    [ e#6:9-10 a#1:4-5  ] File "../../test/contracts/get_scope_tests/constant.mligo", line 6, characters 18-32
+    [ a#1:4-5  ] File "../../test/contracts/get_scope_tests/constant.mligo", line 4, character 2 to line 6, character 33
 
     Variable definitions:
     (a#1:4-5 -> a)
@@ -682,10 +689,11 @@ let%expect_test _ =
   [%expect
     {|
     Scopes:
+    [  ] File "../../test/contracts/get_scope_tests/application.mligo", line 2, characters 11-27
     [ j#2:46-47 i#2:36-37  ] File "../../test/contracts/get_scope_tests/application.mligo", line 2, characters 58-63
     [ b#3:7-8 f#2:6-7  ] File "../../test/contracts/get_scope_tests/application.mligo", line 3, characters 16-19
-    [ f#2:6-7  ] File "../../test/contracts/get_scope_tests/application.mligo", line 3, characters 11-31
     [ c#3:26-27 f#2:6-7  ] File "../../test/contracts/get_scope_tests/application.mligo", line 3, characters 35-36
+    [ f#2:6-7  ] File "../../test/contracts/get_scope_tests/application.mligo", line 3, characters 2-37
 
     Variable definitions:
     (a#1:4-5 -> a)
@@ -826,6 +834,8 @@ let%expect_test _ =
   [%expect
     {|
     Scopes:
+    [  ] File "../../test/contracts/get_scope_tests/nominal_types.mligo", line 1, characters 19-45
+    [ foo_variant#1:5-16  ] File "../../test/contracts/get_scope_tests/nominal_types.mligo", line 2, characters 18-58
     [ foo_record#2:5-15 foo_variant#1:5-16  ] File "../../test/contracts/get_scope_tests/nominal_types.mligo", line 4, characters 12-13
     [ a#4:4-5 foo_record#2:5-15 foo_variant#1:5-16  ] File "../../test/contracts/get_scope_tests/nominal_types.mligo", line 6, characters 12-14
     [ b#6:4-5 a#4:4-5 foo_record#2:5-15 foo_variant#1:5-16  ] File "../../test/contracts/get_scope_tests/nominal_types.mligo", line 9, characters 8-9
@@ -1595,21 +1605,39 @@ let%expect_test _ =
   [%expect
     {|
       Scopes:
+      [  ] File "../../test/contracts/get_scope_tests/types.mligo", line 2, characters 13-16
+      [ t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 3, characters 4-28
+      [ t#2:9-10 x#3:12-13  ] File "../../test/contracts/get_scope_tests/types.mligo", line 5, characters 12-13
       [ x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 5, characters 16-17
+      [ t#2:9-10 x#3:12-13 a#5:8-9  ] File "../../test/contracts/get_scope_tests/types.mligo", line 6, characters 12-20
       [ a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 6, characters 31-36
+      [ bar#10:16-19 foo#9:13-16 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 12, characters 16-19
       [ bar#10:16-19 foo#9:13-16 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 12, characters 22-29
+      [ g#12:12-13 bar#10:16-19 foo#9:13-16 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 14, characters 16-26
       [ g#12:12-13 bar#10:16-19 foo#9:13-16 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 14, characters 37-44
       [ h#14:12-13 g#12:12-13 bar#10:16-19 foo#9:13-16 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 15, characters 8-10
-      [ B#18:7-8 A#1:7-8 foo#9:13-16 bar#10:16-19 h#14:12-13 g#12:12-13 exp1#8:8-12 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 21, characters 14-15
-      [ hmm#23:5-8 c#21:4-5 B#18:7-8 A#1:7-8 foo#9:13-16 bar#10:16-19 h#14:12-13 g#12:12-13 exp1#8:8-12 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 24, characters 26-28
-      [ d#24:4-5 hmm#23:5-8 c#21:4-5 B#18:7-8 A#1:7-8 foo#9:13-16 bar#10:16-19 h#14:12-13 g#12:12-13 exp1#8:8-12 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 27, characters 14-15
-      [ idk#29:5-8 c#27:4-5 d#24:4-5 hmm#23:5-8 B#18:7-8 A#1:7-8 foo#9:13-16 bar#10:16-19 h#14:12-13 g#12:12-13 exp1#8:8-12 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 30, characters 27-31
-      [ q#33:8-9 s#32:5-6 d#30:4-5 idk#29:5-8 c#27:4-5 hmm#23:5-8 B#18:7-8 A#1:7-8 foo#9:13-16 bar#10:16-19 h#14:12-13 g#12:12-13 exp1#8:8-12 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 36, characters 12-14
-      [ e#36:4-5 q#33:8-9 s#32:5-6 d#30:4-5 idk#29:5-8 c#27:4-5 hmm#23:5-8 B#18:7-8 A#1:7-8 foo#9:13-16 bar#10:16-19 h#14:12-13 g#12:12-13 exp1#8:8-12 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 38, characters 21-26
-      [ qux#41:9-12 f#38:4-5 e#36:4-5 q#33:8-9 s#32:5-6 d#30:4-5 idk#29:5-8 c#27:4-5 hmm#23:5-8 B#18:7-8 A#1:7-8 foo#9:13-16 bar#10:16-19 h#14:12-13 g#12:12-13 exp1#8:8-12 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 43, characters 18-23
-      [ boo#44:12-15 i#43:8-9 qux#41:9-12 f#38:4-5 e#36:4-5 q#33:8-9 s#32:5-6 d#30:4-5 idk#29:5-8 c#27:4-5 hmm#23:5-8 B#18:7-8 A#1:7-8 foo#9:13-16 bar#10:16-19 h#14:12-13 g#12:12-13 exp1#8:8-12 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 46, characters 27-29
-      [ j#46:8-9 boo#44:12-15 i#43:8-9 qux#41:9-12 f#38:4-5 e#36:4-5 q#33:8-9 s#32:5-6 d#30:4-5 idk#29:5-8 c#27:4-5 hmm#23:5-8 B#18:7-8 A#1:7-8 foo#9:13-16 bar#10:16-19 h#14:12-13 g#12:12-13 exp1#8:8-12 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 47, characters 4-6
-      [ x#49:14-15 exp2#40:4-8 f#38:4-5 e#36:4-5 q#33:8-9 s#32:5-6 d#30:4-5 idk#29:5-8 c#27:4-5 hmm#23:5-8 B#18:7-8 A#1:7-8 foo#9:13-16 bar#10:16-19 h#14:12-13 g#12:12-13 exp1#8:8-12 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 49, characters 30-35
+      [ A#1:7-8 exp1#8:8-12 foo#9:13-16 bar#10:16-19 g#12:12-13 h#14:12-13 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10 B#18:7-8  ] File "../../test/contracts/get_scope_tests/types.mligo", line 21, characters 8-11
+      [ B#18:7-8 A#1:7-8 exp1#8:8-12 foo#9:13-16 bar#10:16-19 g#12:12-13 h#14:12-13 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 21, characters 14-15
+      [ c#21:4-5 B#18:7-8 A#1:7-8 exp1#8:8-12 foo#9:13-16 bar#10:16-19 g#12:12-13 h#14:12-13 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 23, characters 11-14
+      [ A#1:7-8 exp1#8:8-12 foo#9:13-16 bar#10:16-19 g#12:12-13 h#14:12-13 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10 B#18:7-8 c#21:4-5 hmm#23:5-8  ] File "../../test/contracts/get_scope_tests/types.mligo", line 24, characters 8-15
+      [ hmm#23:5-8 c#21:4-5 B#18:7-8 A#1:7-8 exp1#8:8-12 foo#9:13-16 bar#10:16-19 g#12:12-13 h#14:12-13 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 24, characters 26-28
+      [ A#1:7-8 exp1#8:8-12 foo#9:13-16 bar#10:16-19 g#12:12-13 h#14:12-13 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10 B#18:7-8 c#21:4-5 hmm#23:5-8 d#24:4-5  ] File "../../test/contracts/get_scope_tests/types.mligo", line 27, characters 8-11
+      [ d#24:4-5 hmm#23:5-8 c#21:4-5 B#18:7-8 A#1:7-8 exp1#8:8-12 foo#9:13-16 bar#10:16-19 g#12:12-13 h#14:12-13 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 27, characters 14-15
+      [ c#27:4-5 d#24:4-5 hmm#23:5-8 B#18:7-8 A#1:7-8 exp1#8:8-12 foo#9:13-16 bar#10:16-19 g#12:12-13 h#14:12-13 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 29, characters 11-14
+      [ A#1:7-8 exp1#8:8-12 foo#9:13-16 bar#10:16-19 g#12:12-13 h#14:12-13 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10 B#18:7-8 c#21:4-5 hmm#23:5-8 d#24:4-5 idk#29:5-8  ] File "../../test/contracts/get_scope_tests/types.mligo", line 30, characters 8-16
+      [ idk#29:5-8 c#27:4-5 d#24:4-5 hmm#23:5-8 B#18:7-8 A#1:7-8 exp1#8:8-12 foo#9:13-16 bar#10:16-19 g#12:12-13 h#14:12-13 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 30, characters 27-31
+      [ d#30:4-5 idk#29:5-8 c#27:4-5 hmm#23:5-8 B#18:7-8 A#1:7-8 exp1#8:8-12 foo#9:13-16 bar#10:16-19 g#12:12-13 h#14:12-13 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 32, characters 9-12
+      [ s#32:5-6 d#30:4-5 idk#29:5-8 c#27:4-5 hmm#23:5-8 B#18:7-8 A#1:7-8 exp1#8:8-12 foo#9:13-16 bar#10:16-19 g#12:12-13 h#14:12-13 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 33, characters 0-21
+      [ A#1:7-8 exp1#8:8-12 foo#9:13-16 bar#10:16-19 g#12:12-13 h#14:12-13 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10 B#18:7-8 c#21:4-5 hmm#23:5-8 d#24:4-5 idk#29:5-8 s#32:5-6 q#33:8-9  ] File "../../test/contracts/get_scope_tests/types.mligo", line 36, characters 8-9
+      [ q#33:8-9 s#32:5-6 d#30:4-5 idk#29:5-8 c#27:4-5 hmm#23:5-8 B#18:7-8 A#1:7-8 exp1#8:8-12 foo#9:13-16 bar#10:16-19 g#12:12-13 h#14:12-13 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 36, characters 12-14
+      [ A#1:7-8 exp1#8:8-12 foo#9:13-16 bar#10:16-19 g#12:12-13 h#14:12-13 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10 B#18:7-8 c#21:4-5 hmm#23:5-8 d#24:4-5 idk#29:5-8 s#32:5-6 q#33:8-9 e#36:4-5  ] File "../../test/contracts/get_scope_tests/types.mligo", line 38, characters 8-14
+      [ e#36:4-5 q#33:8-9 s#32:5-6 d#30:4-5 idk#29:5-8 c#27:4-5 hmm#23:5-8 B#18:7-8 A#1:7-8 exp1#8:8-12 foo#9:13-16 bar#10:16-19 g#12:12-13 h#14:12-13 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 38, characters 21-26
+      [ qux#41:9-12 f#38:4-5 e#36:4-5 q#33:8-9 s#32:5-6 d#30:4-5 idk#29:5-8 c#27:4-5 hmm#23:5-8 B#18:7-8 A#1:7-8 exp1#8:8-12 foo#9:13-16 bar#10:16-19 g#12:12-13 h#14:12-13 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 43, characters 12-15
+      [ qux#41:9-12 f#38:4-5 e#36:4-5 q#33:8-9 s#32:5-6 d#30:4-5 idk#29:5-8 c#27:4-5 hmm#23:5-8 B#18:7-8 A#1:7-8 exp1#8:8-12 foo#9:13-16 bar#10:16-19 g#12:12-13 h#14:12-13 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 43, characters 18-23
+      [ boo#44:12-15 i#43:8-9 qux#41:9-12 f#38:4-5 e#36:4-5 q#33:8-9 s#32:5-6 d#30:4-5 idk#29:5-8 c#27:4-5 hmm#23:5-8 B#18:7-8 A#1:7-8 exp1#8:8-12 foo#9:13-16 bar#10:16-19 g#12:12-13 h#14:12-13 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 46, characters 12-19
+      [ boo#44:12-15 i#43:8-9 qux#41:9-12 f#38:4-5 e#36:4-5 q#33:8-9 s#32:5-6 d#30:4-5 idk#29:5-8 c#27:4-5 hmm#23:5-8 B#18:7-8 A#1:7-8 exp1#8:8-12 foo#9:13-16 bar#10:16-19 g#12:12-13 h#14:12-13 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 46, characters 27-29
+      [ j#46:8-9 boo#44:12-15 i#43:8-9 qux#41:9-12 f#38:4-5 e#36:4-5 q#33:8-9 s#32:5-6 d#30:4-5 idk#29:5-8 c#27:4-5 hmm#23:5-8 B#18:7-8 A#1:7-8 exp1#8:8-12 foo#9:13-16 bar#10:16-19 g#12:12-13 h#14:12-13 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 47, characters 4-6
+      [ x#49:14-15 exp2#40:4-8 f#38:4-5 e#36:4-5 q#33:8-9 s#32:5-6 d#30:4-5 idk#29:5-8 c#27:4-5 hmm#23:5-8 B#18:7-8 A#1:7-8 exp1#8:8-12 foo#9:13-16 bar#10:16-19 g#12:12-13 h#14:12-13 b#6:8-9 a#5:8-9 x#3:12-13 t#2:9-10  ] File "../../test/contracts/get_scope_tests/types.mligo", line 49, characters 30-35
 
       Variable definitions:
       (c#21:4-5 -> c)
@@ -2051,6 +2079,7 @@ let%expect_test _ =
     {|
     Scopes:
     [ X#1:7-8 Mangled_module_____________________test__contracts__get_scope_tests__x____mligo#env x#4:4-5 y#3:4-5 x#1:5-6  ] File "../../test/contracts/get_scope_tests/import_x.mligo", line 3, characters 8-13
+    [  ] File "../../test/contracts/get_scope_tests/x.mligo", line 1, characters 9-33
     [ x#1:5-6  ] File "../../test/contracts/get_scope_tests/x.mligo", line 3, characters 8-9
     [ y#3:4-5 x#1:5-6  ] File "../../test/contracts/get_scope_tests/x.mligo", line 4, characters 14-15
     [ y#3:4-5 x#1:5-6  ] File "../../test/contracts/get_scope_tests/x.mligo", line 4, characters 22-24
@@ -2189,5 +2218,3 @@ let%expect_test _ =
 
     references:
       File "../../test/contracts/get_scope_tests/local_module_using_local_binding.mligo", line 18, characters 4-5 |}]
-
-let () = Ligo_unix.putenv ~key:"LIGO_GET_SCOPE_USE_NEW_IMP" ~data:""
diff --git a/src/bin/expect_tests/get_scope_errors_warns.ml b/src/bin/expect_tests/get_scope_errors_warns.ml
index e2af962d68bb3438448ccdfba6e90436e9e09662..b3deac8dec35e9cc21315b74f7f66e7b0d07d2db 100644
--- a/src/bin/expect_tests/get_scope_errors_warns.ml
+++ b/src/bin/expect_tests/get_scope_errors_warns.ml
@@ -1,7 +1,6 @@
 open Cli_expect
 
 let gs s = "../../test/contracts/get_scope_tests/" ^ s
-let () = Ligo_unix.putenv ~key:"LIGO_GET_SCOPE_USE_NEW_IMP" ~data:""
 
 let%expect_test _ =
   run_ligo_good
@@ -16,6 +15,7 @@ let%expect_test _ =
   [%expect
     {|
     Scopes:
+    [  ] File "../../test/contracts/get_scope_tests/bad_field_record.mligo", line 1, characters 18-43
     [ foo_record#1:5-15  ] File "../../test/contracts/get_scope_tests/bad_field_record.mligo", line 4, characters 8-9
     [ foo_record#1:5-15  ] File "../../test/contracts/get_scope_tests/bad_field_record.mligo", line 5, characters 8-9
     [ c#3:4-5 foo_record#1:5-15  ] File "../../test/contracts/get_scope_tests/bad_field_record.mligo", line 9, characters 10-11
@@ -171,6 +171,7 @@ let%expect_test _ =
   [%expect
     {|
     Scopes:
+    [  ] File "../../test/contracts/get_scope_tests/errors/type_error.mligo", line 1, characters 8-11
     [  ] File "../../test/contracts/get_scope_tests/errors/type_error.mligo", line 1, characters 14-21
 
     Variable definitions:
@@ -199,13 +200,17 @@ let%expect_test _ =
   [%expect
     {|
     Scopes:
+    [  ] File "../../test/contracts/warning_unused.mligo", line 1, character 15 to line 4, character 1
     [ x#6:9-10 storage#1:5-12  ] File "../../test/contracts/warning_unused.mligo", line 6, characters 20-21
     [ x#7:9-10 foo#6:4-7 storage#1:5-12  ] File "../../test/contracts/warning_unused.mligo", line 7, characters 20-29
     [ bar#7:4-7 foo#6:4-7 storage#1:5-12  ] File "../../test/contracts/warning_unused.mligo", line 9, characters 10-13
     [ s#9:12-13 bar#7:4-7 foo#6:4-7 storage#1:5-12  ] File "../../test/contracts/warning_unused.mligo", line 10, characters 10-17
     [ x#10:6-7 s#9:12-13 bar#7:4-7 foo#6:4-7 storage#1:5-12  ] File "../../test/contracts/warning_unused.mligo", line 11, characters 10-15
-    [ x#11:6-7 s#9:12-13 bar#7:4-7 foo#6:4-7 storage#1:5-12  ] File "../../test/contracts/warning_unused.mligo", line 12, characters 10-15
-    [ x#12:6-7 s#9:12-13 bar#7:4-7 foo#6:4-7 storage#1:5-12  ] File "../../test/contracts/warning_unused.mligo", line 13, characters 26-38
+    [ x#11:6-7 s#9:12-13 bar#7:4-7 foo#6:4-7 storage#1:5-12  ] File "../../test/contracts/warning_unused.mligo", line 12, characters 10-17
+    [ x#12:6-7 s#9:12-13 bar#7:4-7 foo#6:4-7 storage#1:5-12  ] File "../../test/contracts/warning_unused.mligo", line 13, characters 3-5
+    [ x#12:6-7 s#9:12-13 bar#7:4-7 foo#6:4-7 storage#1:5-12  ] File "../../test/contracts/warning_unused.mligo", line 13, characters 8-22
+    [ x#12:6-7 s#9:12-13 bar#7:4-7 foo#6:4-7 storage#1:5-12  ] File "../../test/contracts/warning_unused.mligo", line 13, characters 26-27
+    [ x#12:6-7 s#9:12-13 bar#7:4-7 foo#6:4-7 storage#1:5-12  ] File "../../test/contracts/warning_unused.mligo", line 13, characters 37-38
 
     Variable definitions:
     (bar#7:4-7 -> bar)
@@ -308,7 +313,7 @@ let%expect_test _ =
     (x#2:4-5 -> x)
     Range: File "../../test/contracts/warning_duplicate2.mligo", line 2, characters 4-5
     Body Range: File "../../test/contracts/warning_duplicate2.mligo", line 2, characters 9-13
-    Content: |resolved: option (ticket (nat))|
+    Content: |resolved: ( option (ticket (nat)) * option (ticket (nat)) )|
     references: []
     Type definitions:
     Module definitions:
@@ -330,7 +335,8 @@ let%expect_test _ =
   [%expect
     {|
     Scopes:
-    [  ] File "../../test/contracts/warning_duplicate.mligo", line 2, characters 23-64
+    [  ] File "../../test/contracts/warning_duplicate.mligo", line 2, characters 10-20
+    [  ] File "../../test/contracts/warning_duplicate.mligo", line 2, characters 23-65
     [ Foo#1:7-10 x#2:6-7  ] File "../../test/contracts/warning_duplicate.mligo", line 5, characters 9-14
     [ Foo#1:7-10 x#2:6-7  ] File "../../test/contracts/warning_duplicate.mligo", line 5, characters 16-21
 
@@ -338,7 +344,7 @@ let%expect_test _ =
     (x#5:4-5 -> x)
     Range: File "../../test/contracts/warning_duplicate.mligo", line 5, characters 4-5
     Body Range: File "../../test/contracts/warning_duplicate.mligo", line 5, characters 9-21
-    Content: |resolved: ticket (nat)|
+    Content: |resolved: ( ticket (nat) * ticket (nat) )|
     references: []
     Type definitions:
     Module definitions:
@@ -367,5 +373,3 @@ let%expect_test _ =
       3 | end
     :
     Warning: variable "Foo.x" cannot be used more than once. |}]
-
-let () = Ligo_unix.putenv ~key:"LIGO_GET_SCOPE_USE_NEW_IMP" ~data:""
diff --git a/src/bin/expect_tests/get_scope_full_stdlib.ml b/src/bin/expect_tests/get_scope_full_stdlib.ml
index 308c05c5502d3a2e4bde4e9d133f5115b250829d..c362236cbbdb66b99a535f9d8abf30ed5ba510a4 100644
--- a/src/bin/expect_tests/get_scope_full_stdlib.ml
+++ b/src/bin/expect_tests/get_scope_full_stdlib.ml
@@ -1,7 +1,6 @@
 open Cli_expect
 
 let gs s = "../../test/contracts/get_scope_tests/" ^ s
-let () = Ligo_unix.putenv ~key:"LIGO_GET_SCOPE_USE_NEW_IMP" ~data:""
 
 let%expect_test _ =
   run_ligo_good
@@ -10,10 +9,10 @@ let%expect_test _ =
     {|
     Scopes:
     [ Test#239:7-11 assert_none_with_error#475:6-28 assert_some_with_error#474:6-28 assert_with_error#473:6-23 assert_none#471:6-17 assert_some#470:6-17 assert#469:6-12 originate_from_file_and_mutate_all#448:6-40 originate_from_file_and_mutate#428:6-36 mutation_test_all#416:6-23 mutation_test#404:6-19 originate_from_file#399:6-25 compile_contract_from_file#396:6-32 originate_module#389:6-22 originate_uncurried#382:6-25 compile_contract_with_views#379:8-35 originate#372:6-15 originate_contract#371:6-24 to_entrypoint#363:6-19 michelson_equal#362:6-21 transfer_to_contract_exn#357:6-30 transfer_to_contract#352:6-26 create_chest_key#351:6-22 create_chest#350:6-18 set_big_map#349:6-17 baker_account#348:6-19 add_account#347:6-17 sign#346:6-10 save_mutation#345:6-19 mutate_value#344:6-18 bootstrap_contract#343:6-24 reset_state_at#342:6-20 reset_state#341:6-17 log#337:6-9 transfer_exn#336:6-18 transfer#335:6-14 get_last_events_from#327:6-26 PBT#309:9-12 run#313:8-11 make_test#312:8-17 gen_small#311:8-17 gen#310:8-11 unset_print_values#307:6-24 set_print_values#306:6-22 println#303:6-13 nl#302:6-8 chr#292:6-9 read_contract_from_file#291:6-29 compile_contract#287:6-22 size#286:6-10 set_baker#285:6-15 set_baker_policy#284:6-22 get_storage#279:6-17 to_json#278:6-13 to_string#277:6-15 drop_context#276:6-18 save_context#275:6-18 restore_context#274:6-21 parse_michelson#273:6-21 constant_to_michelson_program#272:6-35 to_typed_address#271:6-22 register_constant#270:6-23 register_delegate#269:6-23 cast_address#268:6-18 get_time#267:6-14 bake_until_n_cycle_end#266:6-28 decompile#265:6-15 new_account#264:6-17 random#261:6-12 last_originations#260:6-23 nth_bootstrap_typed_address#259:6-33 get_bootstrap_account#258:6-27 nth_bootstrap_account#255:6-27 nth_bootstrap_contract#254:6-28 get_voting_power#253:6-22 eprint#252:6-12 print#251:6-11 get_balance#250:6-17 get_storage_of_address#249:6-28 set_source#248:6-16 to_contract#247:6-17 failwith#246:6-14 get_total_voting_power#245:6-28 compile_value#244:6-19 eval#242:6-10 run#241:6-9 unforged_ticket#237:8-23 pbt_result#235:8-18 pbt_test#234:8-16 test_baker_policy#229:5-22 test_exec_result#227:5-21 test_exec_error#222:5-20 test_exec_error_balance_too_low#219:5-36 ediv#216:4-8 assert_none_with_error#215:4-26 assert_some_with_error#214:4-26 assert_with_error#213:4-21 uncurry#211:4-11 curry#210:4-9 ignore#209:4-10 int#208:4-7 unit#207:4-8 false#206:4-9 true#205:4-8 is_nat#204:4-10 abs#203:4-7 assert_none#202:4-15 assert_some#201:4-15 assert#200:4-10 Crypto#189:7-13 check#197:6-11 hash_key#195:6-14 keccak#194:6-12 sha3#193:6-10 sha512#192:6-12 sha256#191:6-12 blake2b#190:6-13 Bytes#179:7-12 sub#186:6-9 concat#185:6-12 length#183:6-12 unpack#182:6-12 pack#181:6-10 concats#180:6-13 Option#168:7-13 is_some#176:6-13 is_none#175:6-13 value_exn#174:6-15 value#173:6-11 map#172:15-18 unopt_with_error#171:6-22 unopt#169:6-11 String#160:7-13 sub#165:6-9 concat#164:6-12 concats#162:6-13 length#161:6-12 List#138:7-11 update_with#156:6-17 update#154:6-12 filter_map#152:6-16 find_opt#150:6-14 cons#149:6-10 fold_right#148:6-16 fold_left#147:6-15 fold#146:6-10 iter#145:6-10 map#144:6-9 tail_opt#142:6-14 head_opt#141:6-14 size#140:6-10 length#139:6-12 Set#123:7-10 fold_desc#135:6-15 fold#134:6-10 iter#133:6-10 update#132:6-12 remove#131:6-12 add#130:6-9 mem#129:6-9 literal#127:25-32 cardinal#126:6-14 size#125:6-10 empty#124:6-11 Transpiled#117:7-17 map_remove#120:6-16 map_add#119:6-13 map_find_opt#118:6-18 Map#99:7-10 fold#113:6-10 map#112:6-9 iter#111:6-10 find_opt#110:6-14 find#109:6-10 get_and_update#108:6-20 update#107:6-12 remove#106:6-12 add#105:6-9 mem#104:6-9 literal#102:25-32 size#101:6-10 empty#100:6-11 Big_map#85:7-14 find#95:6-10 find_opt#94:6-14 get_and_update#93:6-20 update#92:6-12 remove#91:6-12 add#90:6-9 mem#89:6-9 literal#87:25-32 empty#86:16-21 Bitwise#77:7-14 shift_right#82:6-17 shift_left#81:6-16 or#80:6-9 xor#79:6-9 and#78:6-10 Tezos#7:7-12 sapling_verify_update#73:25-46 emit#70:25-29 get_entrypoint#67:25-39 get_entrypoint_opt#64:25-43 create_contract_uncurried#62:25-50 create_contract#59:25-40 split_ticket#57:6-18 call_view#55:25-34 transaction#49:6-17 create_ticket#47:6-19 get_contract_with_error#40:6-29 get_contract#36:25-37 get_contract_opt#34:25-41 sapling_empty_state#32:25-44 constant#31:25-33 self#28:25-29 set_delegate#27:6-18 pairing_check#26:6-19 never#25:6-11 read_ticket#23:6-17 join_tickets#22:6-18 implicit_account#21:6-22 address#20:6-13 voting_power#19:6-18 get_min_block_time#18:6-24 get_total_voting_power#17:6-28 get_chain_id#16:6-18 get_self_address#15:6-22 get_level#14:6-15 get_source#13:6-16 get_sender#12:6-16 get_now#11:6-13 get_amount#10:6-16 get_balance#9:6-17 option#5:8-14 bool#4:5-9 failwith#2:4-12  ] File "../../test/contracts/get_scope_tests/constant.mligo", line 1, characters 8-9
-    [ a#1:4-5 Test#239:7-11 assert_none_with_error#475:6-28 assert_some_with_error#474:6-28 assert_with_error#473:6-23 assert_none#471:6-17 assert_some#470:6-17 assert#469:6-12 originate_from_file_and_mutate_all#448:6-40 originate_from_file_and_mutate#428:6-36 mutation_test_all#416:6-23 mutation_test#404:6-19 originate_from_file#399:6-25 compile_contract_from_file#396:6-32 originate_module#389:6-22 originate_uncurried#382:6-25 compile_contract_with_views#379:8-35 originate#372:6-15 originate_contract#371:6-24 to_entrypoint#363:6-19 michelson_equal#362:6-21 transfer_to_contract_exn#357:6-30 transfer_to_contract#352:6-26 create_chest_key#351:6-22 create_chest#350:6-18 set_big_map#349:6-17 baker_account#348:6-19 add_account#347:6-17 sign#346:6-10 save_mutation#345:6-19 mutate_value#344:6-18 bootstrap_contract#343:6-24 reset_state_at#342:6-20 reset_state#341:6-17 log#337:6-9 transfer_exn#336:6-18 transfer#335:6-14 get_last_events_from#327:6-26 PBT#309:9-12 run#313:8-11 make_test#312:8-17 gen_small#311:8-17 gen#310:8-11 unset_print_values#307:6-24 set_print_values#306:6-22 println#303:6-13 nl#302:6-8 chr#292:6-9 read_contract_from_file#291:6-29 compile_contract#287:6-22 size#286:6-10 set_baker#285:6-15 set_baker_policy#284:6-22 get_storage#279:6-17 to_json#278:6-13 to_string#277:6-15 drop_context#276:6-18 save_context#275:6-18 restore_context#274:6-21 parse_michelson#273:6-21 constant_to_michelson_program#272:6-35 to_typed_address#271:6-22 register_constant#270:6-23 register_delegate#269:6-23 cast_address#268:6-18 get_time#267:6-14 bake_until_n_cycle_end#266:6-28 decompile#265:6-15 new_account#264:6-17 random#261:6-12 last_originations#260:6-23 nth_bootstrap_typed_address#259:6-33 get_bootstrap_account#258:6-27 nth_bootstrap_account#255:6-27 nth_bootstrap_contract#254:6-28 get_voting_power#253:6-22 eprint#252:6-12 print#251:6-11 get_balance#250:6-17 get_storage_of_address#249:6-28 set_source#248:6-16 to_contract#247:6-17 failwith#246:6-14 get_total_voting_power#245:6-28 compile_value#244:6-19 eval#242:6-10 run#241:6-9 unforged_ticket#237:8-23 pbt_result#235:8-18 pbt_test#234:8-16 test_baker_policy#229:5-22 test_exec_result#227:5-21 test_exec_error#222:5-20 test_exec_error_balance_too_low#219:5-36 ediv#216:4-8 assert_none_with_error#215:4-26 assert_some_with_error#214:4-26 assert_with_error#213:4-21 uncurry#211:4-11 curry#210:4-9 ignore#209:4-10 int#208:4-7 unit#207:4-8 false#206:4-9 true#205:4-8 is_nat#204:4-10 abs#203:4-7 assert_none#202:4-15 assert_some#201:4-15 assert#200:4-10 Crypto#189:7-13 check#197:6-11 hash_key#195:6-14 keccak#194:6-12 sha3#193:6-10 sha512#192:6-12 sha256#191:6-12 blake2b#190:6-13 Bytes#179:7-12 sub#186:6-9 concat#185:6-12 length#183:6-12 unpack#182:6-12 pack#181:6-10 concats#180:6-13 Option#168:7-13 is_some#176:6-13 is_none#175:6-13 value_exn#174:6-15 value#173:6-11 map#172:15-18 unopt_with_error#171:6-22 unopt#169:6-11 String#160:7-13 sub#165:6-9 concat#164:6-12 concats#162:6-13 length#161:6-12 List#138:7-11 update_with#156:6-17 update#154:6-12 filter_map#152:6-16 find_opt#150:6-14 cons#149:6-10 fold_right#148:6-16 fold_left#147:6-15 fold#146:6-10 iter#145:6-10 map#144:6-9 tail_opt#142:6-14 head_opt#141:6-14 size#140:6-10 length#139:6-12 Set#123:7-10 fold_desc#135:6-15 fold#134:6-10 iter#133:6-10 update#132:6-12 remove#131:6-12 add#130:6-9 mem#129:6-9 literal#127:25-32 cardinal#126:6-14 size#125:6-10 empty#124:6-11 Transpiled#117:7-17 map_remove#120:6-16 map_add#119:6-13 map_find_opt#118:6-18 Map#99:7-10 fold#113:6-10 map#112:6-9 iter#111:6-10 find_opt#110:6-14 find#109:6-10 get_and_update#108:6-20 update#107:6-12 remove#106:6-12 add#105:6-9 mem#104:6-9 literal#102:25-32 size#101:6-10 empty#100:6-11 Big_map#85:7-14 find#95:6-10 find_opt#94:6-14 get_and_update#93:6-20 update#92:6-12 remove#91:6-12 add#90:6-9 mem#89:6-9 literal#87:25-32 empty#86:16-21 Bitwise#77:7-14 shift_right#82:6-17 shift_left#81:6-16 or#80:6-9 xor#79:6-9 and#78:6-10 Tezos#7:7-12 sapling_verify_update#73:25-46 emit#70:25-29 get_entrypoint#67:25-39 get_entrypoint_opt#64:25-43 create_contract_uncurried#62:25-50 create_contract#59:25-40 split_ticket#57:6-18 call_view#55:25-34 transaction#49:6-17 create_ticket#47:6-19 get_contract_with_error#40:6-29 get_contract#36:25-37 get_contract_opt#34:25-41 sapling_empty_state#32:25-44 constant#31:25-33 self#28:25-29 set_delegate#27:6-18 pairing_check#26:6-19 never#25:6-11 read_ticket#23:6-17 join_tickets#22:6-18 implicit_account#21:6-22 address#20:6-13 voting_power#19:6-18 get_min_block_time#18:6-24 get_total_voting_power#17:6-28 get_chain_id#16:6-18 get_self_address#15:6-22 get_level#14:6-15 get_source#13:6-16 get_sender#12:6-16 get_now#11:6-13 get_amount#10:6-16 get_balance#9:6-17 option#5:8-14 bool#4:5-9 failwith#2:4-12  ] File "../../test/contracts/get_scope_tests/constant.mligo", line 4, character 2 to line 6, character 14
     [ c#5:10-11 a#1:4-5 Test#239:7-11 assert_none_with_error#475:6-28 assert_some_with_error#474:6-28 assert_with_error#473:6-23 assert_none#471:6-17 assert_some#470:6-17 assert#469:6-12 originate_from_file_and_mutate_all#448:6-40 originate_from_file_and_mutate#428:6-36 mutation_test_all#416:6-23 mutation_test#404:6-19 originate_from_file#399:6-25 compile_contract_from_file#396:6-32 originate_module#389:6-22 originate_uncurried#382:6-25 compile_contract_with_views#379:8-35 originate#372:6-15 originate_contract#371:6-24 to_entrypoint#363:6-19 michelson_equal#362:6-21 transfer_to_contract_exn#357:6-30 transfer_to_contract#352:6-26 create_chest_key#351:6-22 create_chest#350:6-18 set_big_map#349:6-17 baker_account#348:6-19 add_account#347:6-17 sign#346:6-10 save_mutation#345:6-19 mutate_value#344:6-18 bootstrap_contract#343:6-24 reset_state_at#342:6-20 reset_state#341:6-17 log#337:6-9 transfer_exn#336:6-18 transfer#335:6-14 get_last_events_from#327:6-26 PBT#309:9-12 run#313:8-11 make_test#312:8-17 gen_small#311:8-17 gen#310:8-11 unset_print_values#307:6-24 set_print_values#306:6-22 println#303:6-13 nl#302:6-8 chr#292:6-9 read_contract_from_file#291:6-29 compile_contract#287:6-22 size#286:6-10 set_baker#285:6-15 set_baker_policy#284:6-22 get_storage#279:6-17 to_json#278:6-13 to_string#277:6-15 drop_context#276:6-18 save_context#275:6-18 restore_context#274:6-21 parse_michelson#273:6-21 constant_to_michelson_program#272:6-35 to_typed_address#271:6-22 register_constant#270:6-23 register_delegate#269:6-23 cast_address#268:6-18 get_time#267:6-14 bake_until_n_cycle_end#266:6-28 decompile#265:6-15 new_account#264:6-17 random#261:6-12 last_originations#260:6-23 nth_bootstrap_typed_address#259:6-33 get_bootstrap_account#258:6-27 nth_bootstrap_account#255:6-27 nth_bootstrap_contract#254:6-28 get_voting_power#253:6-22 eprint#252:6-12 print#251:6-11 get_balance#250:6-17 get_storage_of_address#249:6-28 set_source#248:6-16 to_contract#247:6-17 failwith#246:6-14 get_total_voting_power#245:6-28 compile_value#244:6-19 eval#242:6-10 run#241:6-9 unforged_ticket#237:8-23 pbt_result#235:8-18 pbt_test#234:8-16 test_baker_policy#229:5-22 test_exec_result#227:5-21 test_exec_error#222:5-20 test_exec_error_balance_too_low#219:5-36 ediv#216:4-8 assert_none_with_error#215:4-26 assert_some_with_error#214:4-26 assert_with_error#213:4-21 uncurry#211:4-11 curry#210:4-9 ignore#209:4-10 int#208:4-7 unit#207:4-8 false#206:4-9 true#205:4-8 is_nat#204:4-10 abs#203:4-7 assert_none#202:4-15 assert_some#201:4-15 assert#200:4-10 Crypto#189:7-13 check#197:6-11 hash_key#195:6-14 keccak#194:6-12 sha3#193:6-10 sha512#192:6-12 sha256#191:6-12 blake2b#190:6-13 Bytes#179:7-12 sub#186:6-9 concat#185:6-12 length#183:6-12 unpack#182:6-12 pack#181:6-10 concats#180:6-13 Option#168:7-13 is_some#176:6-13 is_none#175:6-13 value_exn#174:6-15 value#173:6-11 map#172:15-18 unopt_with_error#171:6-22 unopt#169:6-11 String#160:7-13 sub#165:6-9 concat#164:6-12 concats#162:6-13 length#161:6-12 List#138:7-11 update_with#156:6-17 update#154:6-12 filter_map#152:6-16 find_opt#150:6-14 cons#149:6-10 fold_right#148:6-16 fold_left#147:6-15 fold#146:6-10 iter#145:6-10 map#144:6-9 tail_opt#142:6-14 head_opt#141:6-14 size#140:6-10 length#139:6-12 Set#123:7-10 fold_desc#135:6-15 fold#134:6-10 iter#133:6-10 update#132:6-12 remove#131:6-12 add#130:6-9 mem#129:6-9 literal#127:25-32 cardinal#126:6-14 size#125:6-10 empty#124:6-11 Transpiled#117:7-17 map_remove#120:6-16 map_add#119:6-13 map_find_opt#118:6-18 Map#99:7-10 fold#113:6-10 map#112:6-9 iter#111:6-10 find_opt#110:6-14 find#109:6-10 get_and_update#108:6-20 update#107:6-12 remove#106:6-12 add#105:6-9 mem#104:6-9 literal#102:25-32 size#101:6-10 empty#100:6-11 Big_map#85:7-14 find#95:6-10 find_opt#94:6-14 get_and_update#93:6-20 update#92:6-12 remove#91:6-12 add#90:6-9 mem#89:6-9 literal#87:25-32 empty#86:16-21 Bitwise#77:7-14 shift_right#82:6-17 shift_left#81:6-16 or#80:6-9 xor#79:6-9 and#78:6-10 Tezos#7:7-12 sapling_verify_update#73:25-46 emit#70:25-29 get_entrypoint#67:25-39 get_entrypoint_opt#64:25-43 create_contract_uncurried#62:25-50 create_contract#59:25-40 split_ticket#57:6-18 call_view#55:25-34 transaction#49:6-17 create_ticket#47:6-19 get_contract_with_error#40:6-29 get_contract#36:25-37 get_contract_opt#34:25-41 sapling_empty_state#32:25-44 constant#31:25-33 self#28:25-29 set_delegate#27:6-18 pairing_check#26:6-19 never#25:6-11 read_ticket#23:6-17 join_tickets#22:6-18 implicit_account#21:6-22 address#20:6-13 voting_power#19:6-18 get_min_block_time#18:6-24 get_total_voting_power#17:6-28 get_chain_id#16:6-18 get_self_address#15:6-22 get_level#14:6-15 get_source#13:6-16 get_sender#12:6-16 get_now#11:6-13 get_amount#10:6-16 get_balance#9:6-17 option#5:8-14 bool#4:5-9 failwith#2:4-12  ] File "../../test/contracts/get_scope_tests/constant.mligo", line 5, characters 30-31
     [ d#5:26-27 c#5:10-11 a#1:4-5 Test#239:7-11 assert_none_with_error#475:6-28 assert_some_with_error#474:6-28 assert_with_error#473:6-23 assert_none#471:6-17 assert_some#470:6-17 assert#469:6-12 originate_from_file_and_mutate_all#448:6-40 originate_from_file_and_mutate#428:6-36 mutation_test_all#416:6-23 mutation_test#404:6-19 originate_from_file#399:6-25 compile_contract_from_file#396:6-32 originate_module#389:6-22 originate_uncurried#382:6-25 compile_contract_with_views#379:8-35 originate#372:6-15 originate_contract#371:6-24 to_entrypoint#363:6-19 michelson_equal#362:6-21 transfer_to_contract_exn#357:6-30 transfer_to_contract#352:6-26 create_chest_key#351:6-22 create_chest#350:6-18 set_big_map#349:6-17 baker_account#348:6-19 add_account#347:6-17 sign#346:6-10 save_mutation#345:6-19 mutate_value#344:6-18 bootstrap_contract#343:6-24 reset_state_at#342:6-20 reset_state#341:6-17 log#337:6-9 transfer_exn#336:6-18 transfer#335:6-14 get_last_events_from#327:6-26 PBT#309:9-12 run#313:8-11 make_test#312:8-17 gen_small#311:8-17 gen#310:8-11 unset_print_values#307:6-24 set_print_values#306:6-22 println#303:6-13 nl#302:6-8 chr#292:6-9 read_contract_from_file#291:6-29 compile_contract#287:6-22 size#286:6-10 set_baker#285:6-15 set_baker_policy#284:6-22 get_storage#279:6-17 to_json#278:6-13 to_string#277:6-15 drop_context#276:6-18 save_context#275:6-18 restore_context#274:6-21 parse_michelson#273:6-21 constant_to_michelson_program#272:6-35 to_typed_address#271:6-22 register_constant#270:6-23 register_delegate#269:6-23 cast_address#268:6-18 get_time#267:6-14 bake_until_n_cycle_end#266:6-28 decompile#265:6-15 new_account#264:6-17 random#261:6-12 last_originations#260:6-23 nth_bootstrap_typed_address#259:6-33 get_bootstrap_account#258:6-27 nth_bootstrap_account#255:6-27 nth_bootstrap_contract#254:6-28 get_voting_power#253:6-22 eprint#252:6-12 print#251:6-11 get_balance#250:6-17 get_storage_of_address#249:6-28 set_source#248:6-16 to_contract#247:6-17 failwith#246:6-14 get_total_voting_power#245:6-28 compile_value#244:6-19 eval#242:6-10 run#241:6-9 unforged_ticket#237:8-23 pbt_result#235:8-18 pbt_test#234:8-16 test_baker_policy#229:5-22 test_exec_result#227:5-21 test_exec_error#222:5-20 test_exec_error_balance_too_low#219:5-36 ediv#216:4-8 assert_none_with_error#215:4-26 assert_some_with_error#214:4-26 assert_with_error#213:4-21 uncurry#211:4-11 curry#210:4-9 ignore#209:4-10 int#208:4-7 unit#207:4-8 false#206:4-9 true#205:4-8 is_nat#204:4-10 abs#203:4-7 assert_none#202:4-15 assert_some#201:4-15 assert#200:4-10 Crypto#189:7-13 check#197:6-11 hash_key#195:6-14 keccak#194:6-12 sha3#193:6-10 sha512#192:6-12 sha256#191:6-12 blake2b#190:6-13 Bytes#179:7-12 sub#186:6-9 concat#185:6-12 length#183:6-12 unpack#182:6-12 pack#181:6-10 concats#180:6-13 Option#168:7-13 is_some#176:6-13 is_none#175:6-13 value_exn#174:6-15 value#173:6-11 map#172:15-18 unopt_with_error#171:6-22 unopt#169:6-11 String#160:7-13 sub#165:6-9 concat#164:6-12 concats#162:6-13 length#161:6-12 List#138:7-11 update_with#156:6-17 update#154:6-12 filter_map#152:6-16 find_opt#150:6-14 cons#149:6-10 fold_right#148:6-16 fold_left#147:6-15 fold#146:6-10 iter#145:6-10 map#144:6-9 tail_opt#142:6-14 head_opt#141:6-14 size#140:6-10 length#139:6-12 Set#123:7-10 fold_desc#135:6-15 fold#134:6-10 iter#133:6-10 update#132:6-12 remove#131:6-12 add#130:6-9 mem#129:6-9 literal#127:25-32 cardinal#126:6-14 size#125:6-10 empty#124:6-11 Transpiled#117:7-17 map_remove#120:6-16 map_add#119:6-13 map_find_opt#118:6-18 Map#99:7-10 fold#113:6-10 map#112:6-9 iter#111:6-10 find_opt#110:6-14 find#109:6-10 get_and_update#108:6-20 update#107:6-12 remove#106:6-12 add#105:6-9 mem#104:6-9 literal#102:25-32 size#101:6-10 empty#100:6-11 Big_map#85:7-14 find#95:6-10 find_opt#94:6-14 get_and_update#93:6-20 update#92:6-12 remove#91:6-12 add#90:6-9 mem#89:6-9 literal#87:25-32 empty#86:16-21 Bitwise#77:7-14 shift_right#82:6-17 shift_left#81:6-16 or#80:6-9 xor#79:6-9 and#78:6-10 Tezos#7:7-12 sapling_verify_update#73:25-46 emit#70:25-29 get_entrypoint#67:25-39 get_entrypoint_opt#64:25-43 create_contract_uncurried#62:25-50 create_contract#59:25-40 split_ticket#57:6-18 call_view#55:25-34 transaction#49:6-17 create_ticket#47:6-19 get_contract_with_error#40:6-29 get_contract#36:25-37 get_contract_opt#34:25-41 sapling_empty_state#32:25-44 constant#31:25-33 self#28:25-29 set_delegate#27:6-18 pairing_check#26:6-19 never#25:6-11 read_ticket#23:6-17 join_tickets#22:6-18 implicit_account#21:6-22 address#20:6-13 voting_power#19:6-18 get_min_block_time#18:6-24 get_total_voting_power#17:6-28 get_chain_id#16:6-18 get_self_address#15:6-22 get_level#14:6-15 get_source#13:6-16 get_sender#12:6-16 get_now#11:6-13 get_amount#10:6-16 get_balance#9:6-17 option#5:8-14 bool#4:5-9 failwith#2:4-12  ] File "../../test/contracts/get_scope_tests/constant.mligo", line 5, characters 35-44
-    [ e#6:9-10 a#1:4-5 Test#239:7-11 assert_none_with_error#475:6-28 assert_some_with_error#474:6-28 assert_with_error#473:6-23 assert_none#471:6-17 assert_some#470:6-17 assert#469:6-12 originate_from_file_and_mutate_all#448:6-40 originate_from_file_and_mutate#428:6-36 mutation_test_all#416:6-23 mutation_test#404:6-19 originate_from_file#399:6-25 compile_contract_from_file#396:6-32 originate_module#389:6-22 originate_uncurried#382:6-25 compile_contract_with_views#379:8-35 originate#372:6-15 originate_contract#371:6-24 to_entrypoint#363:6-19 michelson_equal#362:6-21 transfer_to_contract_exn#357:6-30 transfer_to_contract#352:6-26 create_chest_key#351:6-22 create_chest#350:6-18 set_big_map#349:6-17 baker_account#348:6-19 add_account#347:6-17 sign#346:6-10 save_mutation#345:6-19 mutate_value#344:6-18 bootstrap_contract#343:6-24 reset_state_at#342:6-20 reset_state#341:6-17 log#337:6-9 transfer_exn#336:6-18 transfer#335:6-14 get_last_events_from#327:6-26 PBT#309:9-12 run#313:8-11 make_test#312:8-17 gen_small#311:8-17 gen#310:8-11 unset_print_values#307:6-24 set_print_values#306:6-22 println#303:6-13 nl#302:6-8 chr#292:6-9 read_contract_from_file#291:6-29 compile_contract#287:6-22 size#286:6-10 set_baker#285:6-15 set_baker_policy#284:6-22 get_storage#279:6-17 to_json#278:6-13 to_string#277:6-15 drop_context#276:6-18 save_context#275:6-18 restore_context#274:6-21 parse_michelson#273:6-21 constant_to_michelson_program#272:6-35 to_typed_address#271:6-22 register_constant#270:6-23 register_delegate#269:6-23 cast_address#268:6-18 get_time#267:6-14 bake_until_n_cycle_end#266:6-28 decompile#265:6-15 new_account#264:6-17 random#261:6-12 last_originations#260:6-23 nth_bootstrap_typed_address#259:6-33 get_bootstrap_account#258:6-27 nth_bootstrap_account#255:6-27 nth_bootstrap_contract#254:6-28 get_voting_power#253:6-22 eprint#252:6-12 print#251:6-11 get_balance#250:6-17 get_storage_of_address#249:6-28 set_source#248:6-16 to_contract#247:6-17 failwith#246:6-14 get_total_voting_power#245:6-28 compile_value#244:6-19 eval#242:6-10 run#241:6-9 unforged_ticket#237:8-23 pbt_result#235:8-18 pbt_test#234:8-16 test_baker_policy#229:5-22 test_exec_result#227:5-21 test_exec_error#222:5-20 test_exec_error_balance_too_low#219:5-36 ediv#216:4-8 assert_none_with_error#215:4-26 assert_some_with_error#214:4-26 assert_with_error#213:4-21 uncurry#211:4-11 curry#210:4-9 ignore#209:4-10 int#208:4-7 unit#207:4-8 false#206:4-9 true#205:4-8 is_nat#204:4-10 abs#203:4-7 assert_none#202:4-15 assert_some#201:4-15 assert#200:4-10 Crypto#189:7-13 check#197:6-11 hash_key#195:6-14 keccak#194:6-12 sha3#193:6-10 sha512#192:6-12 sha256#191:6-12 blake2b#190:6-13 Bytes#179:7-12 sub#186:6-9 concat#185:6-12 length#183:6-12 unpack#182:6-12 pack#181:6-10 concats#180:6-13 Option#168:7-13 is_some#176:6-13 is_none#175:6-13 value_exn#174:6-15 value#173:6-11 map#172:15-18 unopt_with_error#171:6-22 unopt#169:6-11 String#160:7-13 sub#165:6-9 concat#164:6-12 concats#162:6-13 length#161:6-12 List#138:7-11 update_with#156:6-17 update#154:6-12 filter_map#152:6-16 find_opt#150:6-14 cons#149:6-10 fold_right#148:6-16 fold_left#147:6-15 fold#146:6-10 iter#145:6-10 map#144:6-9 tail_opt#142:6-14 head_opt#141:6-14 size#140:6-10 length#139:6-12 Set#123:7-10 fold_desc#135:6-15 fold#134:6-10 iter#133:6-10 update#132:6-12 remove#131:6-12 add#130:6-9 mem#129:6-9 literal#127:25-32 cardinal#126:6-14 size#125:6-10 empty#124:6-11 Transpiled#117:7-17 map_remove#120:6-16 map_add#119:6-13 map_find_opt#118:6-18 Map#99:7-10 fold#113:6-10 map#112:6-9 iter#111:6-10 find_opt#110:6-14 find#109:6-10 get_and_update#108:6-20 update#107:6-12 remove#106:6-12 add#105:6-9 mem#104:6-9 literal#102:25-32 size#101:6-10 empty#100:6-11 Big_map#85:7-14 find#95:6-10 find_opt#94:6-14 get_and_update#93:6-20 update#92:6-12 remove#91:6-12 add#90:6-9 mem#89:6-9 literal#87:25-32 empty#86:16-21 Bitwise#77:7-14 shift_right#82:6-17 shift_left#81:6-16 or#80:6-9 xor#79:6-9 and#78:6-10 Tezos#7:7-12 sapling_verify_update#73:25-46 emit#70:25-29 get_entrypoint#67:25-39 get_entrypoint_opt#64:25-43 create_contract_uncurried#62:25-50 create_contract#59:25-40 split_ticket#57:6-18 call_view#55:25-34 transaction#49:6-17 create_ticket#47:6-19 get_contract_with_error#40:6-29 get_contract#36:25-37 get_contract_opt#34:25-41 sapling_empty_state#32:25-44 constant#31:25-33 self#28:25-29 set_delegate#27:6-18 pairing_check#26:6-19 never#25:6-11 read_ticket#23:6-17 join_tickets#22:6-18 implicit_account#21:6-22 address#20:6-13 voting_power#19:6-18 get_min_block_time#18:6-24 get_total_voting_power#17:6-28 get_chain_id#16:6-18 get_self_address#15:6-22 get_level#14:6-15 get_source#13:6-16 get_sender#12:6-16 get_now#11:6-13 get_amount#10:6-16 get_balance#9:6-17 option#5:8-14 bool#4:5-9 failwith#2:4-12  ] File "../../test/contracts/get_scope_tests/constant.mligo", line 6, characters 20-30
+    [ e#6:9-10 a#1:4-5 Test#239:7-11 assert_none_with_error#475:6-28 assert_some_with_error#474:6-28 assert_with_error#473:6-23 assert_none#471:6-17 assert_some#470:6-17 assert#469:6-12 originate_from_file_and_mutate_all#448:6-40 originate_from_file_and_mutate#428:6-36 mutation_test_all#416:6-23 mutation_test#404:6-19 originate_from_file#399:6-25 compile_contract_from_file#396:6-32 originate_module#389:6-22 originate_uncurried#382:6-25 compile_contract_with_views#379:8-35 originate#372:6-15 originate_contract#371:6-24 to_entrypoint#363:6-19 michelson_equal#362:6-21 transfer_to_contract_exn#357:6-30 transfer_to_contract#352:6-26 create_chest_key#351:6-22 create_chest#350:6-18 set_big_map#349:6-17 baker_account#348:6-19 add_account#347:6-17 sign#346:6-10 save_mutation#345:6-19 mutate_value#344:6-18 bootstrap_contract#343:6-24 reset_state_at#342:6-20 reset_state#341:6-17 log#337:6-9 transfer_exn#336:6-18 transfer#335:6-14 get_last_events_from#327:6-26 PBT#309:9-12 run#313:8-11 make_test#312:8-17 gen_small#311:8-17 gen#310:8-11 unset_print_values#307:6-24 set_print_values#306:6-22 println#303:6-13 nl#302:6-8 chr#292:6-9 read_contract_from_file#291:6-29 compile_contract#287:6-22 size#286:6-10 set_baker#285:6-15 set_baker_policy#284:6-22 get_storage#279:6-17 to_json#278:6-13 to_string#277:6-15 drop_context#276:6-18 save_context#275:6-18 restore_context#274:6-21 parse_michelson#273:6-21 constant_to_michelson_program#272:6-35 to_typed_address#271:6-22 register_constant#270:6-23 register_delegate#269:6-23 cast_address#268:6-18 get_time#267:6-14 bake_until_n_cycle_end#266:6-28 decompile#265:6-15 new_account#264:6-17 random#261:6-12 last_originations#260:6-23 nth_bootstrap_typed_address#259:6-33 get_bootstrap_account#258:6-27 nth_bootstrap_account#255:6-27 nth_bootstrap_contract#254:6-28 get_voting_power#253:6-22 eprint#252:6-12 print#251:6-11 get_balance#250:6-17 get_storage_of_address#249:6-28 set_source#248:6-16 to_contract#247:6-17 failwith#246:6-14 get_total_voting_power#245:6-28 compile_value#244:6-19 eval#242:6-10 run#241:6-9 unforged_ticket#237:8-23 pbt_result#235:8-18 pbt_test#234:8-16 test_baker_policy#229:5-22 test_exec_result#227:5-21 test_exec_error#222:5-20 test_exec_error_balance_too_low#219:5-36 ediv#216:4-8 assert_none_with_error#215:4-26 assert_some_with_error#214:4-26 assert_with_error#213:4-21 uncurry#211:4-11 curry#210:4-9 ignore#209:4-10 int#208:4-7 unit#207:4-8 false#206:4-9 true#205:4-8 is_nat#204:4-10 abs#203:4-7 assert_none#202:4-15 assert_some#201:4-15 assert#200:4-10 Crypto#189:7-13 check#197:6-11 hash_key#195:6-14 keccak#194:6-12 sha3#193:6-10 sha512#192:6-12 sha256#191:6-12 blake2b#190:6-13 Bytes#179:7-12 sub#186:6-9 concat#185:6-12 length#183:6-12 unpack#182:6-12 pack#181:6-10 concats#180:6-13 Option#168:7-13 is_some#176:6-13 is_none#175:6-13 value_exn#174:6-15 value#173:6-11 map#172:15-18 unopt_with_error#171:6-22 unopt#169:6-11 String#160:7-13 sub#165:6-9 concat#164:6-12 concats#162:6-13 length#161:6-12 List#138:7-11 update_with#156:6-17 update#154:6-12 filter_map#152:6-16 find_opt#150:6-14 cons#149:6-10 fold_right#148:6-16 fold_left#147:6-15 fold#146:6-10 iter#145:6-10 map#144:6-9 tail_opt#142:6-14 head_opt#141:6-14 size#140:6-10 length#139:6-12 Set#123:7-10 fold_desc#135:6-15 fold#134:6-10 iter#133:6-10 update#132:6-12 remove#131:6-12 add#130:6-9 mem#129:6-9 literal#127:25-32 cardinal#126:6-14 size#125:6-10 empty#124:6-11 Transpiled#117:7-17 map_remove#120:6-16 map_add#119:6-13 map_find_opt#118:6-18 Map#99:7-10 fold#113:6-10 map#112:6-9 iter#111:6-10 find_opt#110:6-14 find#109:6-10 get_and_update#108:6-20 update#107:6-12 remove#106:6-12 add#105:6-9 mem#104:6-9 literal#102:25-32 size#101:6-10 empty#100:6-11 Big_map#85:7-14 find#95:6-10 find_opt#94:6-14 get_and_update#93:6-20 update#92:6-12 remove#91:6-12 add#90:6-9 mem#89:6-9 literal#87:25-32 empty#86:16-21 Bitwise#77:7-14 shift_right#82:6-17 shift_left#81:6-16 or#80:6-9 xor#79:6-9 and#78:6-10 Tezos#7:7-12 sapling_verify_update#73:25-46 emit#70:25-29 get_entrypoint#67:25-39 get_entrypoint_opt#64:25-43 create_contract_uncurried#62:25-50 create_contract#59:25-40 split_ticket#57:6-18 call_view#55:25-34 transaction#49:6-17 create_ticket#47:6-19 get_contract_with_error#40:6-29 get_contract#36:25-37 get_contract_opt#34:25-41 sapling_empty_state#32:25-44 constant#31:25-33 self#28:25-29 set_delegate#27:6-18 pairing_check#26:6-19 never#25:6-11 read_ticket#23:6-17 join_tickets#22:6-18 implicit_account#21:6-22 address#20:6-13 voting_power#19:6-18 get_min_block_time#18:6-24 get_total_voting_power#17:6-28 get_chain_id#16:6-18 get_self_address#15:6-22 get_level#14:6-15 get_source#13:6-16 get_sender#12:6-16 get_now#11:6-13 get_amount#10:6-16 get_balance#9:6-17 option#5:8-14 bool#4:5-9 failwith#2:4-12  ] File "../../test/contracts/get_scope_tests/constant.mligo", line 6, characters 18-32
+    [ a#1:4-5 Test#239:7-11 assert_none_with_error#475:6-28 assert_some_with_error#474:6-28 assert_with_error#473:6-23 assert_none#471:6-17 assert_some#470:6-17 assert#469:6-12 originate_from_file_and_mutate_all#448:6-40 originate_from_file_and_mutate#428:6-36 mutation_test_all#416:6-23 mutation_test#404:6-19 originate_from_file#399:6-25 compile_contract_from_file#396:6-32 originate_module#389:6-22 originate_uncurried#382:6-25 compile_contract_with_views#379:8-35 originate#372:6-15 originate_contract#371:6-24 to_entrypoint#363:6-19 michelson_equal#362:6-21 transfer_to_contract_exn#357:6-30 transfer_to_contract#352:6-26 create_chest_key#351:6-22 create_chest#350:6-18 set_big_map#349:6-17 baker_account#348:6-19 add_account#347:6-17 sign#346:6-10 save_mutation#345:6-19 mutate_value#344:6-18 bootstrap_contract#343:6-24 reset_state_at#342:6-20 reset_state#341:6-17 log#337:6-9 transfer_exn#336:6-18 transfer#335:6-14 get_last_events_from#327:6-26 PBT#309:9-12 run#313:8-11 make_test#312:8-17 gen_small#311:8-17 gen#310:8-11 unset_print_values#307:6-24 set_print_values#306:6-22 println#303:6-13 nl#302:6-8 chr#292:6-9 read_contract_from_file#291:6-29 compile_contract#287:6-22 size#286:6-10 set_baker#285:6-15 set_baker_policy#284:6-22 get_storage#279:6-17 to_json#278:6-13 to_string#277:6-15 drop_context#276:6-18 save_context#275:6-18 restore_context#274:6-21 parse_michelson#273:6-21 constant_to_michelson_program#272:6-35 to_typed_address#271:6-22 register_constant#270:6-23 register_delegate#269:6-23 cast_address#268:6-18 get_time#267:6-14 bake_until_n_cycle_end#266:6-28 decompile#265:6-15 new_account#264:6-17 random#261:6-12 last_originations#260:6-23 nth_bootstrap_typed_address#259:6-33 get_bootstrap_account#258:6-27 nth_bootstrap_account#255:6-27 nth_bootstrap_contract#254:6-28 get_voting_power#253:6-22 eprint#252:6-12 print#251:6-11 get_balance#250:6-17 get_storage_of_address#249:6-28 set_source#248:6-16 to_contract#247:6-17 failwith#246:6-14 get_total_voting_power#245:6-28 compile_value#244:6-19 eval#242:6-10 run#241:6-9 unforged_ticket#237:8-23 pbt_result#235:8-18 pbt_test#234:8-16 test_baker_policy#229:5-22 test_exec_result#227:5-21 test_exec_error#222:5-20 test_exec_error_balance_too_low#219:5-36 ediv#216:4-8 assert_none_with_error#215:4-26 assert_some_with_error#214:4-26 assert_with_error#213:4-21 uncurry#211:4-11 curry#210:4-9 ignore#209:4-10 int#208:4-7 unit#207:4-8 false#206:4-9 true#205:4-8 is_nat#204:4-10 abs#203:4-7 assert_none#202:4-15 assert_some#201:4-15 assert#200:4-10 Crypto#189:7-13 check#197:6-11 hash_key#195:6-14 keccak#194:6-12 sha3#193:6-10 sha512#192:6-12 sha256#191:6-12 blake2b#190:6-13 Bytes#179:7-12 sub#186:6-9 concat#185:6-12 length#183:6-12 unpack#182:6-12 pack#181:6-10 concats#180:6-13 Option#168:7-13 is_some#176:6-13 is_none#175:6-13 value_exn#174:6-15 value#173:6-11 map#172:15-18 unopt_with_error#171:6-22 unopt#169:6-11 String#160:7-13 sub#165:6-9 concat#164:6-12 concats#162:6-13 length#161:6-12 List#138:7-11 update_with#156:6-17 update#154:6-12 filter_map#152:6-16 find_opt#150:6-14 cons#149:6-10 fold_right#148:6-16 fold_left#147:6-15 fold#146:6-10 iter#145:6-10 map#144:6-9 tail_opt#142:6-14 head_opt#141:6-14 size#140:6-10 length#139:6-12 Set#123:7-10 fold_desc#135:6-15 fold#134:6-10 iter#133:6-10 update#132:6-12 remove#131:6-12 add#130:6-9 mem#129:6-9 literal#127:25-32 cardinal#126:6-14 size#125:6-10 empty#124:6-11 Transpiled#117:7-17 map_remove#120:6-16 map_add#119:6-13 map_find_opt#118:6-18 Map#99:7-10 fold#113:6-10 map#112:6-9 iter#111:6-10 find_opt#110:6-14 find#109:6-10 get_and_update#108:6-20 update#107:6-12 remove#106:6-12 add#105:6-9 mem#104:6-9 literal#102:25-32 size#101:6-10 empty#100:6-11 Big_map#85:7-14 find#95:6-10 find_opt#94:6-14 get_and_update#93:6-20 update#92:6-12 remove#91:6-12 add#90:6-9 mem#89:6-9 literal#87:25-32 empty#86:16-21 Bitwise#77:7-14 shift_right#82:6-17 shift_left#81:6-16 or#80:6-9 xor#79:6-9 and#78:6-10 Tezos#7:7-12 sapling_verify_update#73:25-46 emit#70:25-29 get_entrypoint#67:25-39 get_entrypoint_opt#64:25-43 create_contract_uncurried#62:25-50 create_contract#59:25-40 split_ticket#57:6-18 call_view#55:25-34 transaction#49:6-17 create_ticket#47:6-19 get_contract_with_error#40:6-29 get_contract#36:25-37 get_contract_opt#34:25-41 sapling_empty_state#32:25-44 constant#31:25-33 self#28:25-29 set_delegate#27:6-18 pairing_check#26:6-19 never#25:6-11 read_ticket#23:6-17 join_tickets#22:6-18 implicit_account#21:6-22 address#20:6-13 voting_power#19:6-18 get_min_block_time#18:6-24 get_total_voting_power#17:6-28 get_chain_id#16:6-18 get_self_address#15:6-22 get_level#14:6-15 get_source#13:6-16 get_sender#12:6-16 get_now#11:6-13 get_amount#10:6-16 get_balance#9:6-17 option#5:8-14 bool#4:5-9 failwith#2:4-12  ] File "../../test/contracts/get_scope_tests/constant.mligo", line 4, character 2 to line 6, character 33
 
     Variable definitions:
     (a#1:4-5 -> a)
@@ -162,6 +161,7 @@ let%expect_test _ =
     Content: : |sum[False -> unit , True -> unit]|
     references:
       File "", line 26, characters 63-67 ,
+      File "", line 26, characters 147-151 ,
       File "", line 89, characters 52-56 ,
       File "", line 104, characters 48-52 ,
       File "", line 129, characters 41-45 ,
@@ -171,6 +171,7 @@ let%expect_test _ =
       File "", line 175, characters 40-44 ,
       File "", line 176, characters 40-44 ,
       File "", line 197, characters 52-56 ,
+      File "", line 197, characters 145-149 ,
       File "", line 200, characters 16-20 ,
       File "", line 205, characters 11-15 ,
       File "", line 206, characters 12-16 ,
@@ -186,16 +187,24 @@ let%expect_test _ =
     Content: : |funtype 'a : * . option ('a)|
     references:
       File "", line 22, characters 56-73 ,
+      File "", line 22, characters 137-152 ,
       File "", line 27, characters 24-39 ,
+      File "", line 27, characters 93-108 ,
       File "", line 29, characters 12-20 ,
       File "", line 34, characters 67-86 ,
+      File "", line 35, characters 60-79 ,
       File "", line 47, characters 49-66 ,
+      File "", line 47, characters 121-138 ,
       File "", line 55, characters 84-92 ,
+      File "", line 56, characters 81-89 ,
       File "", line 57, characters 61-89 ,
+      File "", line 58, characters 77-105 ,
       File "", line 59, characters 93-108 ,
       File "", line 62, characters 102-117 ,
       File "", line 64, characters 82-99 ,
+      File "", line 66, characters 71-90 ,
       File "", line 73, characters 120-164 ,
+      File "", line 73, characters 278-322 ,
       File "", line 92, characters 37-45 ,
       File "", line 93, characters 45-53 ,
       File "", line 93, characters 78-86 ,
@@ -219,9 +228,11 @@ let%expect_test _ =
       File "", line 175, characters 28-36 ,
       File "", line 176, characters 28-36 ,
       File "", line 182, characters 36-44 ,
+      File "", line 182, characters 99-107 ,
       File "", line 201, characters 30-38 ,
       File "", line 202, characters 30-38 ,
       File "", line 204, characters 23-33 ,
+      File "", line 204, characters 74-84 ,
       File "", line 214, characters 41-49 ,
       File "", line 215, characters 41-49 ,
       File "", line 292, characters 22-35 ,
@@ -1306,9 +1317,7 @@ let%expect_test _ =
                                         Range: File "", line 310, characters 8-11
                                         Body Range: File "", line 310, characters 12-20
                                         Content: |core: ∀ a : * . pbt_gen (a)|
-                                        references:
-                                          File "", line 313, characters 23-27 ,
-                                          File "", line 314, characters 23-27
+                                        references: []
                                         (gen_small#311:8-17 -> gen_small)
                                         Range: File "", line 311, characters 8-17
                                         Body Range: File "", line 311, characters 18-26
@@ -1554,5 +1563,3 @@ let%expect_test _ =
                       Module definitions:
 
     references: [] |}]
-
-let () = Ligo_unix.putenv ~key:"LIGO_GET_SCOPE_USE_NEW_IMP" ~data:""
diff --git a/src/bin/expect_tests/get_scope_regressions.ml b/src/bin/expect_tests/get_scope_regressions.ml
index 598da68f79771cb57dd60a585e5fd8605606d2fc..f4087108f42d48317efb2baf2fafe043153142d2 100644
--- a/src/bin/expect_tests/get_scope_regressions.ml
+++ b/src/bin/expect_tests/get_scope_regressions.ml
@@ -1,7 +1,6 @@
 open Cli_expect
 
 let gs s = "../../test/contracts/get_scope_tests/regressions/" ^ s
-let () = Ligo_unix.putenv ~key:"LIGO_GET_SCOPE_USE_NEW_IMP" ~data:""
 
 let%expect_test _ =
   run_ligo_good
@@ -16,9 +15,9 @@ let%expect_test _ =
   [%expect
     {|
     Scopes:
-    [ _useless#2:20-28 s#1:17-18  ] File "../../test/contracts/get_scope_tests/regressions/missing_stdlib_and_let_mut_in.jsligo", line 2, characters 44-48
-    [ do_nothing#2:6-16 s#1:17-18  ] File "../../test/contracts/get_scope_tests/regressions/missing_stdlib_and_let_mut_in.jsligo", line 3, characters 2-26
     [ s#1:17-18  ] File "../../test/contracts/get_scope_tests/regressions/missing_stdlib_and_let_mut_in.jsligo", line 1, character 16 to line 4, character 1
+    [ _useless#2:20-28 s#1:17-18  ] File "../../test/contracts/get_scope_tests/regressions/missing_stdlib_and_let_mut_in.jsligo", line 2, characters 44-48
+    [ do_nothing#2:6-16 s#1:17-18  ] File "../../test/contracts/get_scope_tests/regressions/missing_stdlib_and_let_mut_in.jsligo", line 3, characters 2-27
     [ iter_op#1:6-13  ] File "../../test/contracts/get_scope_tests/regressions/missing_stdlib_and_let_mut_in.jsligo", line 6, characters 13-20
 
     Variable definitions:
@@ -66,7 +65,8 @@ let%expect_test _ =
   [%expect
     {|
     Scopes:
-    [ p#1:10-11  ] File "../../test/contracts/get_scope_tests/regressions/missing_stdlib.mligo", line 2, characters 26-51
+    [ p#1:10-11  ] File "../../test/contracts/get_scope_tests/regressions/missing_stdlib.mligo", line 2, characters 10-23
+    [ p#1:10-11  ] File "../../test/contracts/get_scope_tests/regressions/missing_stdlib.mligo", line 2, characters 26-52
     [ c#2:6-7 p#1:10-11  ] File "../../test/contracts/get_scope_tests/regressions/missing_stdlib.mligo", line 3, characters 2-17
 
     Variable definitions:
@@ -103,8 +103,12 @@ let%expect_test _ =
   [%expect
     {|
     Scopes:
-    [ user#1:5-9  ] File "../../test/contracts/get_scope_tests/regressions/buggy_file_with_core_types.jsligo", line 8, character 13 to line 9, character 17
+    [  ] File "../../test/contracts/get_scope_tests/regressions/buggy_file_with_core_types.jsligo", line 1, character 12 to line 5, character 1
+    [ user#1:5-9  ] File "../../test/contracts/get_scope_tests/regressions/buggy_file_with_core_types.jsligo", line 7, characters 14-18
+    [ user#1:5-9  ] File "../../test/contracts/get_scope_tests/regressions/buggy_file_with_core_types.jsligo", line 8, characters 13-14
+    [ user#1:5-9  ] File "../../test/contracts/get_scope_tests/regressions/buggy_file_with_core_types.jsligo", line 9, characters 13-17
     [ user#1:5-9  ] File "../../test/contracts/get_scope_tests/regressions/buggy_file_with_core_types.jsligo", line 10, characters 13-20
+    [ alice#7:6-11 user#1:5-9  ] File "../../test/contracts/get_scope_tests/regressions/buggy_file_with_core_types.jsligo", line 13, characters 18-22
     [ alice#7:6-11 user#1:5-9  ] File "../../test/contracts/get_scope_tests/regressions/buggy_file_with_core_types.jsligo", line 13, characters 25-30
 
     Variable definitions:
@@ -277,7 +281,10 @@ let%expect_test _ =
   [%expect
     {|
     Scopes:
+    [  ] File "../../test/contracts/get_scope_tests/regressions/local_module_alias_def_reference.mligo", line 2, characters 16-19
+    [ titi#2:9-13  ] File "../../test/contracts/get_scope_tests/regressions/local_module_alias_def_reference.mligo", line 4, characters 18-22
     [ titi#2:9-13  ] File "../../test/contracts/get_scope_tests/regressions/local_module_alias_def_reference.mligo", line 4, characters 25-27
+    [ A#1:7-8 C#3:11-12 toto#4:12-16 titi#2:9-13 D#8:7-8  ] File "../../test/contracts/get_scope_tests/regressions/local_module_alias_def_reference.mligo", line 10, characters 11-17
     [ E#11:11-12 D#8:7-8 A#1:7-8 C#3:11-12 toto#4:12-16 titi#2:9-13  ] File "../../test/contracts/get_scope_tests/regressions/local_module_alias_def_reference.mligo", line 12, characters 4-10
 
     Variable definitions:
@@ -349,6 +356,7 @@ let%expect_test _ =
   [%expect
     {|
     Scopes:
+    [  ] File "../../test/contracts/get_scope_tests/regressions/local_module_alias_def_reference2.mligo", line 1, characters 11-14
     [  ] File "../../test/contracts/get_scope_tests/regressions/local_module_alias_def_reference2.mligo", line 4, characters 23-24
     [ F#8:15-16 A#2:11-12 C#3:15-16 toto#4:16-20  ] File "../../test/contracts/get_scope_tests/regressions/local_module_alias_def_reference2.mligo", line 9, characters 19-25
     [ E#7:11-12 toto#9:12-16 F#8:15-16 A#2:11-12 C#3:15-16 toto#4:16-20  ] File "../../test/contracts/get_scope_tests/regressions/local_module_alias_def_reference2.mligo", line 11, characters 4-10
@@ -409,5 +417,3 @@ let%expect_test _ =
 
     references:
       File "../../test/contracts/get_scope_tests/regressions/local_module_alias_def_reference2.mligo", line 11, characters 4-5 |}]
-
-let () = Ligo_unix.putenv ~key:"LIGO_GET_SCOPE_USE_NEW_IMP" ~data:""
diff --git a/src/bin/expect_tests/package_management.ml b/src/bin/expect_tests/package_management.ml
index fa7a146dce353be83b93dbccf67ad1168226468d..b11c45dc90fa31a338b0bde46ef8bdf296759332 100644
--- a/src/bin/expect_tests/package_management.ml
+++ b/src/bin/expect_tests/package_management.ml
@@ -342,7 +342,6 @@ let%expect_test _ =
     |> List.length
     |> fun len -> if len > 0 then "Test passed" else "Test failed"
   in
-  Ligo_unix.putenv ~key:"LIGO_GET_SCOPE_USE_NEW_IMP" ~data:"true";
   run_ligo_good
     [ "info"
     ; "get-scope"
@@ -401,8 +400,7 @@ let%expect_test _ =
   [%expect {|
     include_include/main.mligo declarations:
     main
-    hello |}];
-  Ligo_unix.putenv ~key:"LIGO_GET_SCOPE_USE_NEW_IMP" ~data:""
+    hello |}]
 
 (* main file resolution tests *)
 
diff --git a/src/main/scopes/definitions.ml b/src/main/scopes/definitions.ml
index 6075b0a371323e1e47e382ba5a739370ea1d4bc4..ee1a2a8acc03c51050ae97a9fea460bef61614a7 100644
--- a/src/main/scopes/definitions.ml
+++ b/src/main/scopes/definitions.ml
@@ -13,6 +13,12 @@ module Location = Simple_utils.Location
 module Trace = Simple_utils.Trace
 module Types = Types
 
+let get_location_of_module_path : Module_var.t list -> Location.t =
+ fun mvs ->
+  List.fold mvs ~init:Location.dummy ~f:(fun loc m ->
+      Location.cover loc (Module_var.get_location m))
+
+
 let defs_of_vvar ~(body : AST.expression) : VVar.t -> def list -> def list =
  fun vvar acc ->
   if VVar.is_generated vvar
@@ -78,7 +84,7 @@ let defs_of_mvar ~(bindee : Ast_core.module_expr) ~(mod_case : mod_case)
         match Location.unwrap bindee with
         | M_struct _ -> Location.get_location bindee
         | M_variable mvar -> MVar.get_location mvar
-        | M_module_path mpath -> Misc.get_location_of_module_path @@ List.Ne.to_list mpath
+        | M_module_path mpath -> get_location_of_module_path @@ List.Ne.to_list mpath
       in
       let references : LSet.t = LSet.empty (* Filled in a later pass *) in
       let mod_case : mod_case = mod_case in
@@ -124,7 +130,7 @@ let rec defs_of_expr : AST.expression -> def list -> def list =
   | E_type_in { type_binder; rhs; let_result } ->
     defs_of_tvar ~bindee:rhs type_binder @@ self let_result @@ acc
   | E_mod_in { module_binder; rhs; let_result } ->
-    let mod_case = mod_case_of_mod_expr rhs in
+    let mod_case = mod_case_of_mod_expr ~defs_of_decls rhs in
     defs_of_mvar ~mod_case ~bindee:rhs module_binder @@ self let_result @@ acc
   | E_raw_code { language; code } -> []
   (* Variant *)
@@ -168,8 +174,11 @@ let rec defs_of_expr : AST.expression -> def list -> def list =
   | E_while { cond; body } -> self cond @@ self body @@ acc
 
 
-and mod_case_of_mod_expr : AST.module_expr -> mod_case =
- fun mod_expr ->
+and mod_case_of_mod_expr
+    :  defs_of_decls:(AST.declaration list -> def list -> def list) -> AST.module_expr
+    -> mod_case
+  =
+ fun ~defs_of_decls mod_expr ->
   let alias_of_mvars : Module_var.t list -> mod_case =
    fun mvars ->
     let path = List.map ~f:(fun mvar -> Format.asprintf "%a" MVar.pp mvar) mvars in
@@ -193,7 +202,7 @@ and defs_of_decl : AST.declaration -> def list -> def list =
   | D_module { module_binder; module_; module_attr } ->
     (* Here, the module body's defs are within the lhs_def,
          mod_case_of_mod_expr recursively calls defs_of_decl *)
-    let mod_case : mod_case = mod_case_of_mod_expr module_ in
+    let mod_case : mod_case = mod_case_of_mod_expr ~defs_of_decls module_ in
     defs_of_mvar ~mod_case ~bindee:module_ module_binder @@ acc
 
 
@@ -205,101 +214,25 @@ let definitions : AST.program -> def list -> def list =
  fun prg acc -> defs_of_decls prg acc
 
 
-module Merge_defs_temp = struct
-  let sort_by_range ds =
-    List.sort ds ~compare:(fun d1 d2 -> Location.compare (get_range d1) (get_range d2))
-
+module Of_Stdlib = struct
+  let rec defs_of_decl : AST.declaration -> def list -> def list =
+   fun decl acc ->
+    match Location.unwrap decl with
+    | D_value { binder; expr; attr } -> defs_of_binder ~body:expr binder acc
+    | D_irrefutable_match { pattern; expr; attr } ->
+      defs_of_pattern ~body:expr pattern acc
+    | D_type { type_binder; type_expr; type_attr } ->
+      defs_of_tvar ~bindee:type_expr type_binder acc
+    | D_module { module_binder; module_; module_attr } ->
+      (* Here, the module body's defs are within the lhs_def,
+         mod_case_of_mod_expr recursively calls defs_of_decl *)
+      let mod_case : mod_case = mod_case_of_mod_expr module_ ~defs_of_decls in
+      defs_of_mvar ~mod_case ~bindee:module_ module_binder acc
 
-  (* The old imp computes 4 things : (range, body range, types, refs)
-      The new imp computes 2 things so far : (range', body_range') 
-      types and refs will be computed in later passes, not yet implemented
 
-      We expect range == range' and body_range == body_range' (new impl returns the same as old imp)
-      So, we return (range', body_range', types, refs)
-      and expect the expect_test result to be the same *)
-  let merge_defs_opt (old_defs : def list) (new_defs : def list) : def list option =
-    let rec aux (olds : def list) (news : def list) (acc : def list) : def list option =
-      let olds = sort_by_range olds in
-      let news = sort_by_range news in
-      let merge_mod_cases (o : mod_case) (n : mod_case) : mod_case option =
-        match o, n with
-        | Def o, Def n ->
-          (match aux o n [] with
-          | Some dlist -> Some (Def dlist)
-          | None -> None)
-        | (Alias _ as old), Alias _ -> Some old
-        | _, _ -> None
-      in
-      match olds, news with
-      | [], [] -> Some acc
-      | Variable o :: tail_old, Variable n :: tail_new ->
-        if String.equal o.name n.name && Location.equal o.range n.range
-        then (
-          let merged_def : def =
-            Variable
-              { name = o.name
-              ; uid = o.uid
-              ; range = n.range
-              ; body_range = n.body_range
-              ; t = o.t
-              ; references = o.references
-              ; def_type = o.def_type
-              }
-          in
-          aux tail_old tail_new (merged_def :: acc))
-        else None
-      | Type o :: tail_old, Type n :: tail_new ->
-        if String.equal o.name n.name && Location.equal o.range n.range
-        then (
-          let merged_def : def =
-            Type
-              { name = o.name
-              ; uid = o.uid
-              ; range = n.range
-              ; body_range = n.body_range
-              ; content = o.content
-              ; def_type = o.def_type
-              ; references = o.references
-              }
-          in
-          aux tail_old tail_new (merged_def :: acc))
-        else None
-      | Module o :: tail_old, Module n :: tail_new ->
-        if String.equal o.name o.name && Location.equal o.range n.range
-        then (
-          match merge_mod_cases o.mod_case n.mod_case with
-          | None -> None
-          | Some merged_mod_case ->
-            let merged_def : def =
-              Module
-                { name = o.name
-                ; uid = o.uid
-                ; range = n.range
-                ; body_range = n.body_range
-                ; references = o.references
-                ; mod_case = merged_mod_case
-                ; def_type = o.def_type
-                }
-            in
-            aux tail_old tail_new (merged_def :: acc))
-        else None
-      | _, _ -> None
-    in
-    aux old_defs new_defs []
+  and defs_of_decls : AST.declaration list -> def list -> def list =
+   fun decls acc -> List.fold ~init:acc ~f:(fun accu decl -> defs_of_decl decl accu) decls
 
 
-  let merge_defs old_defs new_defs : def list =
-    let defs =
-      match merge_defs_opt old_defs new_defs with
-      | Some merged_defs -> merged_defs
-      | None ->
-        Format.printf
-          "OLD:\n%a\n\nNEW:\n%a\n"
-          PP.definitions
-          old_defs
-          PP.definitions
-          new_defs;
-        failwith "IMP MISMATCH"
-    in
-    defs
+  let definitions : AST.program -> def list = fun prg -> defs_of_decls prg []
 end
diff --git a/src/main/scopes/definitions.mli b/src/main/scopes/definitions.mli
index 8a436eb2eb6cd5994cbb54007d6d1a1774adaa92..1dabaabf241feede3c02619ed4fcc2ca79ab30e6 100644
--- a/src/main/scopes/definitions.mli
+++ b/src/main/scopes/definitions.mli
@@ -2,6 +2,6 @@ module AST = Ast_core
 
 val definitions : AST.program -> Types.def list -> Types.def list
 
-module Merge_defs_temp : sig
-  val merge_defs : Types.def list -> Types.def list -> Types.def list
+module Of_Stdlib : sig
+  val definitions : AST.program -> Types.def list
 end
diff --git a/src/main/scopes/misc.ml b/src/main/scopes/misc.ml
deleted file mode 100644
index 57fd1d74cd16df401c578745de78a5a1625ee0a0..0000000000000000000000000000000000000000
--- a/src/main/scopes/misc.ml
+++ /dev/null
@@ -1,147 +0,0 @@
-open Ligo_prim
-open Types
-module Bindings_map = Simple_utils.Map.Make (Value_var)
-module Pattern = Ast_typed.Pattern
-
-type bindings_map = Ast_typed.type_expression Bindings_map.t
-
-let rec extract_variable_types
-    : bindings_map -> Ast_typed.declaration_content -> bindings_map
-  =
- fun prev decl ->
-  let add env b =
-    let aux
-        :  Ast_typed.expression_variable * Ast_typed.type_expression
-        -> Ast_typed.expression_variable * Ast_typed.type_expression
-      =
-     fun (v, t) ->
-      let t' =
-        match t.orig_var with
-        | Some t' -> { t with type_content = T_variable t' }
-        | None -> t
-      in
-      v, t'
-    in
-    let b' = List.map ~f:aux b in
-    Bindings_map.add_bindings b' env
-  in
-  let aux : bindings_map -> Ast_typed.expression -> bindings_map =
-   fun env exp ->
-    let return = add env in
-    let loc = exp.location in
-    match exp.expression_content with
-    | E_literal _
-    | E_application _
-    | E_raw_code _
-    | E_constructor _
-    | E_assign _
-    | E_deref _
-    | E_while _
-    | E_type_abstraction _
-    | E_mod_in _
-    | E_record _
-    | E_accessor _
-    | E_update _
-    | E_constant _ -> return []
-    | E_type_inst _ -> return [] (* TODO *)
-    | E_variable v -> return [ v, exp.type_expression ]
-    (* Wait what??? *)
-    | E_lambda { binder; _ } ->
-      let rec in_t t =
-        match t.Ast_typed.type_content with
-        | T_arrow { type1; _ } -> type1
-        | T_for_all { type_; _ } -> in_t type_
-        | _ -> failwith "lambda does not have type arrow"
-      in
-      let in_t = in_t exp.type_expression in
-      return [ Param.get_var binder, in_t ]
-    | E_recursive { fun_name; fun_type; lambda = { binder; _ }; force_lambdarec = _ } ->
-      let in_t =
-        match fun_type.type_content with
-        | T_arrow { type1; _ } -> type1
-        | _ -> failwith "rec fun does not have type arrow"
-      in
-      return [ fun_name, fun_type; Param.get_var binder, in_t ]
-    | E_let_mut_in { let_binder; rhs = _; _ } | E_let_in { let_binder; rhs = _; _ } ->
-      return
-      @@ List.map
-           ~f:(fun binder -> Binder.get_var binder, Binder.get_ascr binder)
-           (Pattern.binders let_binder)
-    | E_matching { matchee = _; cases } ->
-      let bindings =
-        List.concat
-        @@ List.map cases ~f:(fun { pattern; _ } ->
-               let binders = Pattern.binders pattern in
-               List.map binders ~f:(fun b -> Binder.get_var b, Binder.get_ascr b))
-      in
-      return bindings
-    | E_module_accessor { element = e; _ } -> return [ e, exp.type_expression ]
-    | E_for { binder; start; _ } -> return [ binder, start.type_expression ]
-    | E_for_each { fe_binder = binder1, Some binder2; collection; _ } ->
-      let key_type, val_type = Ast_typed.get_t_map_exn collection.type_expression in
-      return [ binder1, key_type; binder2, val_type ]
-    | E_for_each { fe_binder = binder, None; collection; _ } ->
-      let type_ = collection.type_expression in
-      if Ast_typed.is_t_set type_
-      then return [ binder, Ast_typed.get_t_set_exn type_ ]
-      else if Ast_typed.is_t_list type_
-      then return [ binder, Ast_typed.get_t_list_exn type_ ]
-      else if Ast_typed.is_t_map type_
-      then (
-        let k, v = Ast_typed.get_t_map_exn type_ in
-        return [ binder, Ast_typed.t_pair ~loc k v ])
-      else failwith "E_for_each type with 1 binder should have map, set or list type"
-  in
-  match decl with
-  | D_value { attr = { hidden = true; _ }; _ } -> prev
-  | D_irrefutable_match { attr = { hidden = true; _ }; _ } -> prev
-  | D_value { binder; expr; _ } ->
-    let prev = add prev [ Binder.get_var binder, expr.type_expression ] in
-    Self_ast_typed.Helpers.fold_expression aux prev expr
-  | D_irrefutable_match { pattern; expr; _ } ->
-    let prev =
-      let f acc binder = add acc [ Binder.get_var binder, expr.type_expression ] in
-      List.fold (Pattern.binders pattern) ~f ~init:prev
-    in
-    Self_ast_typed.Helpers.fold_expression aux prev expr
-  | D_type _ -> prev
-  | D_module { module_; _ } ->
-    (match module_.module_content with
-    | M_variable _ -> prev
-    | M_module_path _ -> prev
-    | M_struct ds ->
-      List.fold_left ds ~init:prev ~f:(fun prev d ->
-          extract_variable_types prev d.wrap_content))
-
-
-let resolve_if : with_types:bool -> bindings_map -> Value_var.t -> type_case =
- fun ~with_types bindings var ->
-  if with_types
-  then (
-    let t_opt = Bindings_map.find_opt var bindings in
-    match t_opt with
-    | Some t -> Resolved t
-    | None -> Unresolved)
-  else Unresolved
-
-
-let make_v_def
-    :  with_types:bool -> ?core_type:Ast_core.type_expression -> bindings_map -> def_type
-    -> Value_var.t -> Location.t -> Location.t -> def option
-  =
- fun ~with_types ?core_type bindings def_type var range body_range ->
-  if Value_var.is_generated var
-  then None
-  else (
-    let type_case =
-      match core_type with
-      | Some t -> Core t
-      | None -> resolve_if ~with_types bindings var
-    in
-    Some (make_v_def (get_binder_name var) type_case def_type range body_range))
-
-
-let get_location_of_module_path : Module_var.t list -> Location.t =
- fun mvs ->
-  List.fold mvs ~init:Location.dummy ~f:(fun loc m ->
-      Location.cover loc (Module_var.get_location m))
diff --git a/src/main/scopes/module_aliases_pass.ml b/src/main/scopes/module_aliases_pass.ml
new file mode 100644
index 0000000000000000000000000000000000000000..1d9822ec3d978b1d6027a92815aba7bfe8a22c39
--- /dev/null
+++ b/src/main/scopes/module_aliases_pass.ml
@@ -0,0 +1,212 @@
+open Ligo_prim
+open Simple_utils
+module AST = Ast_core
+module LSet = Types.LSet
+module LMap = Map.Make (Location)
+open Env
+
+type t = string list LMap.t
+
+(** [mvar_to_id] takes and [Module_var.t] and gives id of the form
+    [{name}#{line}:{start_col}-{end_col}] *)
+let mvar_to_id m =
+  let name = Format.asprintf "%a" Module_var.pp m in
+  let loc = Module_var.get_location m in
+  Types.make_def_id name loc
+
+
+(** [resolve_mpath] takes a module path [mvs] and tries to resolve in the [env]
+    the final output is of type [(Module_var.t * string list) option] 
+    it is optional because we are not sure if the module alias can be resolved
+    in the [env], in case we are able to resolve the alias the first part 
+    [Module_var.t] is the finally resolved module & the [string list] is the list
+    of module ids as explained in the comments of the [resolve_module_alias]
+    
+    This function uses [Env.fold_resolve_mpath] to resolve the module path,
+    i.e. for [module X = A.B.C.D] it will initially completely resolve module [A]
+    if it is an alias-of-an-alias it will resolve it to the final module definition
+    and then look for the rest of the module path [B.C.D] in [env] of the resolved
+    module *)
+let resolve_mpath : Module_var.t List.Ne.t -> env -> (Module_var.t * string list) option =
+ fun mvs env ->
+  let init = [] in
+  let f : string list -> _ -> string list =
+   fun acc (_input, real, _resolved, _defs_of_that_module) -> mvar_to_id real :: acc
+  in
+  let defs = env.avail_defs @ env.parent in
+  let mmap = env.module_map in
+  let acc, m_opt = Env.fold_resolve_mpath mvs defs mmap ~init ~f in
+  match m_opt with
+  | None -> None
+  | Some (_real, resolved, _defs) -> Some (resolved, List.rev acc)
+
+
+(** [resolve_module_alias] completely resolves the module_path [mv] to a module def
+    using the [env] and add an entry in [m_alias] with key as lhs module name & the
+    value is a list of module ids. 
+    For example.
+    {[
+        module A = struct 
+          module B = struct 
+            let x = 1
+          end
+          module C = B
+        end
+        module D = A
+
+        module E = D.C
+    ]}
+    In case of [module E = D.C], [D.C] will be resolved to module definitions ids
+    [D#8-8:9; C#5-10-11]
+    *)
+let resolve_module_alias
+    : Module_var.t -> Module_var.t List.Ne.t -> env -> t -> t * defs_or_alias option
+  =
+ fun lhs_mv mvs env m_alias ->
+  let ma_res = resolve_mpath mvs env in
+  match ma_res with
+  | Some (ma, resolved_ids) ->
+    let m_alias = LMap.add (Module_var.get_location lhs_mv) resolved_ids m_alias in
+    m_alias, Some (Alias ma)
+  | None -> m_alias, None
+
+
+(** [expression] walks the expression and builds the [env] and looks for local 
+    module [E_mod_in] calls [module_expression] to resolve that module.  *)
+let rec expression : AST.expression -> t -> env -> t =
+ fun e m_alias env ->
+  match e.expression_content with
+  | E_variable _ -> m_alias
+  | E_module_accessor _ -> m_alias
+  | E_literal _ -> m_alias
+  | E_constant { arguments; cons_name = _ } ->
+    List.fold arguments ~init:m_alias ~f:(fun m_alias arg -> expression arg m_alias env)
+  | E_application { lamb; args } ->
+    let m_alias = expression lamb m_alias env in
+    expression args m_alias env
+  | E_lambda { binder = _; output_type = _; result } -> expression result m_alias env
+  | E_recursive
+      { fun_name = _
+      ; fun_type = _
+      ; lambda = { binder = _; output_type = _; result }
+      ; force_lambdarec = _
+      } -> expression result m_alias env
+  | E_type_abstraction { type_binder = _; result } -> expression result m_alias env
+  | E_let_mut_in { let_binder = _; rhs; let_result; attributes = _ }
+  | E_let_in { let_binder = _; rhs; let_result; attributes = _ } ->
+    let m_alias = expression rhs m_alias env in
+    expression let_result m_alias env
+  | E_type_in { type_binder = _; rhs = _; let_result } ->
+    expression let_result m_alias env
+  | E_raw_code { language = _; code } -> expression code m_alias env
+  | E_constructor { constructor = _; element } -> expression element m_alias env
+  | E_matching { matchee; cases } ->
+    let m_alias = expression matchee m_alias env in
+    List.fold cases ~init:m_alias ~f:(fun m_alias { pattern = _; body } ->
+        expression body m_alias env)
+  | E_record e_label_map ->
+    let es = Record.values e_label_map in
+    List.fold es ~init:m_alias ~f:(fun m_alias e -> expression e m_alias env)
+  | E_accessor { struct_; path = _ } -> expression struct_ m_alias env
+  | E_update { struct_; path = _; update } ->
+    let m_alias = expression struct_ m_alias env in
+    expression update m_alias env
+  | E_ascription { anno_expr; type_annotation = _ } -> expression anno_expr m_alias env
+  | E_assign { binder = _; expression = e } -> expression e m_alias env
+  | E_for { binder = _; start; final; incr; f_body } ->
+    let m_alias = expression start m_alias env in
+    let m_alias = expression final m_alias env in
+    let m_alias = expression incr m_alias env in
+    expression f_body m_alias env
+  | E_for_each { fe_binder = _v, _v_opt; collection; collection_type = _; fe_body } ->
+    let m_alias = expression collection m_alias env in
+    expression fe_body m_alias env
+  | E_while { cond; body } ->
+    let m_alias = expression cond m_alias env in
+    expression body m_alias env
+  | E_mod_in { module_binder; rhs; let_result } ->
+    let m_alias, defs_or_alias_opt, module_map =
+      module_expression module_binder rhs m_alias env
+    in
+    let env = Env.add_mvar module_binder defs_or_alias_opt module_map env in
+    expression let_result m_alias env
+
+
+(** [module_expression] walks the module expression and builds the [env] and
+    looks for module aliase and calls [resolve_module_alias] on it. *)
+and module_expression
+    : Module_var.t -> AST.module_expr -> t -> env -> t * defs_or_alias option * module_map
+  =
+ fun lhs_mv me m_alias env ->
+  let env = { env with avail_defs = []; parent = env.avail_defs @ env.parent } in
+  let m_alias, defs_or_alias_opt, env =
+    match me.wrap_content with
+    | M_struct decls ->
+      let m_alias, env = declarations decls m_alias env in
+      m_alias, Some (Defs env.avail_defs), env
+    | M_variable mv ->
+      let m_alias, alias_opt = resolve_module_alias lhs_mv (mv, []) env m_alias in
+      m_alias, alias_opt, env
+    | M_module_path mvs ->
+      let m_alias, alias_opt = resolve_module_alias lhs_mv mvs env m_alias in
+      m_alias, alias_opt, env
+  in
+  m_alias, defs_or_alias_opt, env.module_map
+
+
+(** [declaration] builds the [env] and tries to resolves module aliases *)
+and declaration : AST.declaration -> t -> env -> t * env =
+ fun d m_alias env ->
+  match d.wrap_content with
+  | D_value { binder = _; expr; attr = _ } ->
+    let m_alias = expression expr m_alias env in
+    m_alias, env
+  | D_irrefutable_match { pattern = _; expr; attr = _ } ->
+    let m_alias = expression expr m_alias env in
+    m_alias, env
+  | D_type { type_binder = _; type_expr = _; type_attr = _ } -> m_alias, env
+  | D_module { module_binder; module_; module_attr = _ } ->
+    let m_alias, defs_or_alias_opt, module_map =
+      module_expression module_binder module_ m_alias env
+    in
+    let env = Env.add_mvar module_binder defs_or_alias_opt module_map env in
+    m_alias, env
+
+
+(** [declarations] builds the [env] and tries to resolves module aliases *)
+and declarations : AST.declaration list -> t -> env -> t * env =
+ fun decls m_alias env ->
+  let m_alias, env =
+    List.fold decls ~init:(m_alias, env) ~f:(fun (m_alias, env) decl ->
+        declaration decl m_alias env)
+  in
+  m_alias, env
+
+
+(** [declarations] sets up the initial env and calls [declarations] *)
+let declarations : AST.declaration list -> t =
+ fun decls ->
+  let m_alias = LMap.empty in
+  let env = Env.empty in
+  let m_alias, _ = declarations decls m_alias env in
+  m_alias
+
+
+(** [patch] fixes the module aliases in the [defs], It looks for module aliase
+    definitions & then looks up the range of the module definition in [t] *)
+let rec patch : t -> Types.def list -> Types.def list =
+ fun m_alias defs ->
+  let open Types in
+  List.map defs ~f:(fun def ->
+      match def with
+      | Module m ->
+        let mod_case =
+          match m.mod_case with
+          | Alias a ->
+            (match LMap.find_opt m.range m_alias with
+            | Some a -> Types.Alias a
+            | None -> Types.Alias a)
+          | Def defs -> Def (patch m_alias defs)
+        in
+        Module { m with mod_case }
+      | _ as others -> others)
diff --git a/src/main/scopes/module_aliases_pass.mli b/src/main/scopes/module_aliases_pass.mli
new file mode 100644
index 0000000000000000000000000000000000000000..3f0e3f10e9f1388dd64757736dd18e1f434a26c5
--- /dev/null
+++ b/src/main/scopes/module_aliases_pass.mli
@@ -0,0 +1,9 @@
+open Simple_utils
+module AST = Ast_core
+module LSet = Types.LSet
+module LMap : Map.S with type key = Location.t
+
+type t = string list LMap.t
+
+val declarations : AST.declaration list -> t
+val patch : t -> Types.def list -> Types.def list
diff --git a/src/main/scopes/references.ml b/src/main/scopes/references.ml
index 26514f344edba26907f013775b90fa672b7930f6..dcf3099d2a83ec7c5a763fec1285e7dac36ea499 100644
--- a/src/main/scopes/references.ml
+++ b/src/main/scopes/references.ml
@@ -437,3 +437,29 @@ let declarations : AST.declaration list -> references =
   let env = Env.empty in
   let refs, _ = declarations decls refs env in
   refs
+
+
+let rec patch : references -> Types.def list -> Types.def list =
+ fun refs defs ->
+  List.map defs ~f:(fun def ->
+      match def with
+      | Variable v ->
+        (match LMap.find_opt v.range refs with
+        | None -> Types.Variable v
+        | Some references -> Variable { v with references })
+      | Type t ->
+        (match LMap.find_opt t.range refs with
+        | None -> Types.Type t
+        | Some references -> Type { t with references })
+      | Module m ->
+        let mod_case =
+          match m.mod_case with
+          | Alias a -> Types.Alias a
+          | Def defs -> Def (patch refs defs)
+        in
+        let m =
+          match LMap.find_opt m.range refs with
+          | None -> m
+          | Some references -> { m with references }
+        in
+        Module { m with mod_case })
diff --git a/src/main/scopes/references.mli b/src/main/scopes/references.mli
index f6a52f4d46ba8c73ea4e5de58c8bd2db586f52d7..823fdd58055d7f1f5285087d39a5215ee7c7211c 100644
--- a/src/main/scopes/references.mli
+++ b/src/main/scopes/references.mli
@@ -6,3 +6,4 @@ module LMap : Map.S with type key = Location.t
 type references = LSet.t LMap.t
 
 val declarations : AST.declaration list -> references
+val patch : references -> Types.def list -> Types.def list
diff --git a/src/main/scopes/scopes.ml b/src/main/scopes/scopes.ml
index 3eed5785a72608dd23616a016566cd64fdcf436f..00d92b39bb1ce07e46d4adc57151d69c8f037892 100644
--- a/src/main/scopes/scopes.ml
+++ b/src/main/scopes/scopes.ml
@@ -23,1140 +23,6 @@ module PP = PP
 type def = Types.def
 type scopes = Types.scopes
 
-type typing_env =
-  { type_env : Environment.t
-  ; bindings : Misc.bindings_map
-  ; decls : Ast_typed.declaration list
-  }
-
-let rec drop_last : 'a list -> 'a * 'a list =
- fun xs ->
-  match xs with
-  | [] -> failwith "empty list"
-  | [ x ] -> x, []
-  | x :: xs ->
-    let last, xs = drop_last xs in
-    last, x :: xs
-
-
-let set_core_type_if_possible
-    :  AST.type_expression option Binder.t list -> AST.expression
-    -> AST.type_expression option Binder.t list * AST.expression
-  =
- fun binders expr ->
-  match binders, expr.expression_content with
-  | [ binder ], AST.E_ascription { anno_expr; type_annotation } ->
-    let binder = Binder.set_ascr binder (Some type_annotation) in
-    [ binder ], anno_expr
-  | _ -> binders, expr
-
-
-let collect_warns_and_errs
-    ~(raise : (Main_errors.all, Main_warnings.all) Trace.raise)
-    tracer
-    (e, ws)
-  =
-  let () = List.iter ws ~f:raise.warning in
-  raise.log_error (tracer e)
-
-
-let checking
-    ~(raise : (Main_errors.all, Main_warnings.all) Trace.raise)
-    ~options
-    tenv
-    decl
-  =
-  let typed_prg =
-    Simple_utils.Trace.to_stdlib_result
-    @@ Checking.type_declaration ~options ~env:tenv.type_env decl
-  in
-  Result.(
-    match typed_prg with
-    | Ok (decl, ws) ->
-      let module AST = Ast_typed in
-      let bindings = Misc.extract_variable_types tenv.bindings decl.wrap_content in
-      let type_env = Environment.add_declaration decl tenv.type_env in
-      let decls = tenv.decls @ [ decl ] in
-      let () = List.iter ws ~f:raise.warning in
-      { type_env; bindings; decls }
-    | Error (e, ws) ->
-      collect_warns_and_errs ~raise Main_errors.checking_tracer (e, ws);
-      tenv)
-
-
-let checking_self_pass
-    ~(raise : (Main_errors.all, Main_warnings.all) Trace.raise)
-    ~(options : Compiler_options.middle_end)
-    tenv
-  =
-  match
-    let warn_unused_rec = options.warn_unused_rec in
-    Simple_utils.Trace.to_stdlib_result
-    @@ Self_ast_typed.all_program ~warn_unused_rec tenv.decls
-  with
-  | Ok (_, ws) -> List.iter ws ~f:raise.warning
-  | Error (e, ws) ->
-    collect_warns_and_errs ~raise Main_errors.self_ast_typed_tracer (e, ws)
-
-
-let update_typing_env
-    :  raise:(Main_errors.all, Main_warnings.all) Trace.raise -> with_types:bool
-    -> options:Compiler_options.middle_end -> typing_env -> AST.declaration -> typing_env
-  =
- fun ~raise ~with_types ~options tenv decl ->
-  match with_types with
-  | true ->
-    let tenv = checking ~raise ~options tenv decl in
-    let () = checking_self_pass ~raise ~options tenv in
-    tenv
-  | false -> tenv
-
-
-type reference =
-  | Variable of VVar.t
-  | Type of TVar.t
-  | ModuleAccess of MVar.t list * [ `Variable of VVar.t | `Type of TVar.t ]
-  | ModuleAlias of MVar.t list
-
-let pp_reference : Format.formatter -> reference -> unit =
- fun f r ->
-  let () = Format.fprintf f "\n" in
-  match r with
-  | Variable v -> Format.fprintf f "%a" VVar.pp v
-  | Type t -> Format.fprintf f "%a" TVar.pp t
-  | ModuleAccess (mvs, `Variable v) ->
-    let () = List.iter mvs ~f:(fun mv -> Format.fprintf f "%a." MVar.pp mv) in
-    Format.fprintf f "%a" VVar.pp v
-  | ModuleAccess (mvs, `Type t) ->
-    let () = List.iter mvs ~f:(fun mv -> Format.fprintf f "%a." MVar.pp mv) in
-    Format.fprintf f "%a" TVar.pp t
-  | ModuleAlias mvs -> List.iter mvs ~f:(fun mv -> Format.fprintf f "%a." MVar.pp mv)
-
-
-let[@warning "-32"] pp_references : Format.formatter -> reference list -> unit =
- fun f refs -> List.iter refs ~f:(pp_reference f)
-
-
-let rec update_variable_reference : VVar.t -> def list -> bool * def list =
- fun ev defs ->
-  match defs with
-  | [] -> false, []
-  | Variable v :: defs when VVar.is_name ev v.name ->
-    let loc = VVar.get_location ev in
-    let references = LSet.add loc v.references in
-    true, Variable { v with references } :: defs
-  | def :: defs ->
-    let updated, defs = update_variable_reference ev defs in
-    updated, def :: defs
-
-
-let rec update_type_variable_reference : TVar.t -> def list -> bool * def list =
- fun tv defs ->
-  match defs with
-  | [] -> false, []
-  | Type t :: defs when TVar.is_name tv t.name ->
-    let loc = TVar.get_location tv in
-    let references = LSet.add loc t.references in
-    true, Type { t with references } :: defs
-  | def :: defs ->
-    let updated, defs = update_type_variable_reference tv defs in
-    updated, def :: defs
-
-
-let rec update_module_variable_references
-    :  MVar.t list -> [ `Variable of VVar.t | `Type of TVar.t | `None ] -> def list
-    -> bool * def list
-  =
- fun mvs ev defs ->
-  match mvs, defs with
-  | _, [] -> false, defs
-  | [], defs ->
-    (match ev with
-    | `Variable ev -> update_variable_reference ev defs
-    | `Type tv -> update_type_variable_reference tv defs
-    | `None -> true, defs)
-  | mv :: mvs, Module ({ name; mod_case = Def d; _ } as m) :: defs
-    when MVar.is_name mv name ->
-    let loc = MVar.get_location mv in
-    let references = LSet.add loc m.references in
-    let updated, d = update_module_variable_references mvs ev d in
-    let mod_case = Def d in
-    updated, Module { m with mod_case; references } :: defs
-  | mv :: mvs, Module ({ name; mod_case = Alias a; _ } as m) :: defs
-    when MVar.is_name mv name ->
-    let loc = MVar.get_location mv in
-    let references = LSet.add loc m.references in
-    let updated, defs = resolve_alias a mvs ev defs in
-    updated, Module { m with references } :: defs
-  | mvs, def :: defs ->
-    let updated, defs = update_module_variable_references mvs ev defs in
-    updated, def :: defs
-
-
-and resolve_alias
-    :  string list -> MVar.t list -> [ `Variable of VVar.t | `Type of TVar.t | `None ]
-    -> def list -> bool * def list
-  =
- fun aliases mvs ev defs ->
-  match aliases with
-  | [] -> update_module_variable_references mvs ev defs
-  | alias :: aliases ->
-    let rec aux = function
-      | [] -> false, []
-      | Module ({ name; mod_case = Def d; _ } as m) :: defs when String.(name = alias) ->
-        let updated, d = resolve_alias aliases mvs ev d in
-        let mod_case = Def d in
-        updated, Module { m with mod_case } :: defs
-      | (Module { name; mod_case = Alias a; _ } as def) :: defs when String.(name = alias)
-        ->
-        let updated, defs = resolve_alias (a @ aliases) mvs ev defs in
-        updated, def :: defs
-      | def :: defs ->
-        let updated, defs = aux defs in
-        updated, def :: defs
-    in
-    aux defs
-
-
-let update_reference : reference -> def list -> bool * def list =
- fun r defs ->
-  match r with
-  | Variable ev -> update_variable_reference ev defs
-  | Type tv -> update_type_variable_reference tv defs
-  | ModuleAccess (mvs, `Type tv) -> update_module_variable_references mvs (`Type tv) defs
-  | ModuleAccess (mvs, `Variable ev) ->
-    update_module_variable_references mvs (`Variable ev) defs
-  | ModuleAlias mvs -> update_module_variable_references mvs `None defs
-
-
-let update_references : reference list -> def list -> def list * reference list =
- fun refs defs ->
-  let defs, refs =
-    List.fold_left refs ~init:(defs, []) ~f:(fun (defs, refs) r ->
-        let updated, defs = update_reference r defs in
-        let refs = if updated then refs else r :: refs in
-        defs, refs)
-  in
-  defs, refs
-
-
-let join_defs_and_top_defs
-    : def list -> [ `Toplevel of def list ] -> (def list * string) option
-  =
- fun defs (`Toplevel top_defs) ->
-  let rec join defs top_defs =
-    match defs with
-    | [] -> None
-    | [ def ] -> Some (def :: top_defs, get_def_uid def)
-    | def :: defs ->
-      Option.map ~f:(fun (defs, uid) -> def :: defs, uid) (join defs top_defs)
-  in
-  join defs top_defs
-
-
-let split_defs : def list -> string -> def list * def list =
- fun defs uid ->
-  let rec split defs =
-    match defs with
-    | [] -> [], []
-    | def :: defs ->
-      if String.equal (get_def_uid def) uid
-      then [ def ], defs
-      else (
-        let local, top = split defs in
-        def :: local, top)
-  in
-  split defs
-
-
-let update_references
-    :  reference list -> def list -> [ `Toplevel of def list ]
-    -> [ `Toplevel of def list ] * def list * reference list
-  =
- fun refs defs (`Toplevel top_defs) ->
-  match join_defs_and_top_defs defs (`Toplevel top_defs) with
-  | Some (defs, uid) ->
-    let defs, refs = update_references refs defs in
-    let defs, top_defs = split_defs defs uid in
-    `Toplevel top_defs, defs, refs
-  | None ->
-    let top_defs, refs = update_references refs top_defs in
-    `Toplevel top_defs, defs, refs
-
-
-let rec find_type_references : AST.type_expression -> reference list =
- fun te ->
-  match te.type_content with
-  | T_variable t ->
-    let t = TVar.set_location te.location t in
-    [ Type t ]
-  | T_sum { fields; layout = _ } | T_record { fields; layout = _ } ->
-    Record.fold fields ~init:[] ~f:(fun refs row ->
-        let t_refs = find_type_references row in
-        refs @ t_refs)
-  | T_arrow { type1; type2 } -> find_type_references type1 @ find_type_references type2
-  | T_app { type_operator; arguments } ->
-    (* TODO: unignore the path on `type_operator`, update `ModuleAccessType`? *)
-    let type_operator =
-      TVar.set_location te.location (Module_access.get_el @@ type_operator)
-    in
-    let t_refs = List.concat @@ List.map arguments ~f:find_type_references in
-    Type type_operator :: t_refs
-  | T_module_accessor { module_path; element } ->
-    [ ModuleAccess (module_path, `Type element) ]
-  | T_singleton _ -> []
-  | T_abstraction { ty_binder; kind = _; type_ }
-  | T_for_all { ty_binder; kind = _; type_ } ->
-    let t_refs = find_type_references type_ in
-    List.filter t_refs ~f:(fun r ->
-        match r with
-        | Type tv -> not @@ TVar.equal tv ty_binder
-        | Variable _ | ModuleAccess _ | ModuleAlias _ -> true)
-
-
-let find_binder_type_references : AST.type_expression option Binder.t -> reference list =
- fun b ->
-  let ascr = Binder.get_ascr b in
-  match ascr with
-  | Some t -> find_type_references t
-  | None -> []
-
-
-let find_pattern_type_references
-    : AST.type_expression option AST.Pattern.t -> reference list
-  =
- fun pattern ->
-  pattern |> AST.Pattern.binders |> List.concat_map ~f:find_binder_type_references
-
-
-let find_param_type_references : AST.type_expression option Param.t -> reference list =
- fun p ->
-  let ascr = Param.get_ascr p in
-  match ascr with
-  | Some t -> find_type_references t
-  | None -> []
-
-
-let find_top_level_definion : [ `Toplevel of def list ] -> string -> def option =
- fun (`Toplevel top_defs) uid ->
-  let rec find defs =
-    match defs with
-    | [] -> None
-    | def :: defs ->
-      (match def with
-      | Module mdef when String.equal mdef.uid uid -> Some (Module mdef)
-      | Variable vdef when String.equal vdef.uid uid -> Some (Variable vdef)
-      | Type tdef when String.equal tdef.uid uid -> Some (Type tdef)
-      | Variable _ | Type _ -> find defs
-      | Module { mod_case = Alias _; _ } -> find defs
-      | Module { mod_case = Def inner; _ } ->
-        (match find inner with
-        | Some d -> Some d
-        | None -> find defs))
-  in
-  find top_defs
-
-
-let patch_top_level_references : def list -> [ `Toplevel of def list ] -> def list =
- fun defs (`Toplevel top_defs) ->
-  let add_references def =
-    match find_top_level_definion (`Toplevel top_defs) (get_def_uid def) with
-    | Some d -> Some (add_references_to_def def (get_references d))
-    | None -> None
-  in
-  let rec patch (defs : def list) =
-    match defs with
-    | [] -> []
-    | (Module ({ mod_case = Def inner; _ } as mdef) as def) :: defs ->
-      let def =
-        match add_references def with
-        | Some def -> def
-        | None -> def
-      in
-      let inner = patch inner in
-      Module { mdef with mod_case = Def inner; references = get_references def }
-      :: patch defs
-    | ((Variable _ | Type _ | Module { mod_case = Alias _; _ }) as def) :: defs ->
-      (match add_references def with
-      | Some def -> def :: patch defs
-      | None -> def :: patch defs)
-  in
-  patch defs
-
-
-let rec expression ~raise
-    :  with_types:bool -> options:Compiler_options.middle_end -> typing_env
-    -> AST.expression -> [ `Toplevel of def list ] -> [ `Local of def list ]
-    -> [ `Local of def list ]
-       * [ `Toplevel of def list ]
-       * def list
-       * reference list
-       * typing_env
-       * scopes
-  =
- fun ~with_types ~options tenv e (`Toplevel top_defs) (`Local local_defs) ->
-  let expression = expression ~raise ~with_types ~options in
-  match e.expression_content with
-  | E_literal _ -> `Local local_defs, `Toplevel top_defs, [], [], tenv, [ e.location, [] ]
-  | E_raw_code _ ->
-    `Local local_defs, `Toplevel top_defs, [], [], tenv, [ e.location, [] ]
-  | E_variable ev ->
-    `Local local_defs, `Toplevel top_defs, [], [ Variable ev ], tenv, [ e.location, [] ]
-  | E_module_accessor m ->
-    ( `Local local_defs
-    , `Toplevel top_defs
-    , []
-    , [ ModuleAccess (m.module_path, `Variable m.element) ]
-    , tenv
-    , [ e.location, [] ] )
-  | E_constant { arguments; _ } ->
-    let `Local local_defs, `Toplevel top_defs, defs, refs, tenv, scopes =
-      List.fold_left
-        arguments
-        ~init:(`Local local_defs, `Toplevel top_defs, [], [], tenv, [])
-        ~f:(fun (`Local local_defs, `Toplevel top_defs, defs, refs, tenv, scopes) e ->
-          let `Local local_defs, `Toplevel top_defs, ds, rs, tenv, scopes' =
-            expression tenv e (`Toplevel top_defs) (`Local local_defs)
-          in
-          ( `Local (ds @ local_defs)
-          , `Toplevel top_defs
-          , ds @ defs
-          , rs @ refs
-          , tenv
-          , scopes @ scopes' ))
-    in
-    `Local local_defs, `Toplevel top_defs, defs, refs, tenv, merge_same_scopes scopes
-  | E_application { lamb; args } ->
-    let `Local local_defs, `Toplevel top_defs, defs, refs, tenv, scopes =
-      expression tenv lamb (`Toplevel top_defs) (`Local local_defs)
-    in
-    let scopes = merge_same_scopes scopes in
-    let `Local local_defs, `Toplevel top_defs, defs', refs', tenv, scopes' =
-      expression tenv args (`Toplevel top_defs) (`Local local_defs)
-    in
-    let scopes' = merge_same_scopes scopes' in
-    let scopes_final = merge_same_scopes (scopes @ scopes') in
-    ( `Local (defs' @ defs @ local_defs)
-    , `Toplevel top_defs
-    , defs' @ defs
-    , refs' @ refs
-    , tenv
-    , scopes_final )
-  | E_lambda { binder; result; output_type } ->
-    let binder_type_refs = find_param_type_references binder in
-    let output_type_refs =
-      Option.value ~default:[] @@ Option.map ~f:find_type_references output_type
-    in
-    let t_refs = binder_type_refs @ output_type_refs in
-    let var = Param.get_var binder in
-    let core_type = Param.get_ascr binder in
-    let def =
-      let binder_loc = VVar.get_location var in
-      Misc.make_v_def
-        ~with_types
-        ?core_type
-        tenv.bindings
-        Local
-        var
-        binder_loc
-        result.location
-      |> Option.to_list
-    in
-    let `Local local_defs, `Toplevel top_defs, defs_result, refs_result, tenv, scopes =
-      expression tenv result (`Toplevel top_defs) (`Local (def @ local_defs))
-    in
-    let scopes = merge_same_scopes scopes in
-    let `Toplevel top_defs, defs, refs =
-      update_references (refs_result @ t_refs) def (`Toplevel top_defs)
-    in
-    ( `Local (defs_result @ local_defs)
-    , `Toplevel top_defs
-    , defs_result @ defs
-    , refs
-    , tenv
-    , add_defs_to_scopes def scopes )
-  | E_type_abstraction { result; _ } ->
-    expression tenv result (`Toplevel top_defs) (`Local local_defs)
-  | E_constructor { element; _ } ->
-    expression tenv element (`Toplevel top_defs) (`Local local_defs)
-  | E_accessor { struct_; _ } ->
-    expression tenv struct_ (`Toplevel top_defs) (`Local local_defs)
-  | E_ascription { anno_expr; type_annotation } ->
-    let t_refs = find_type_references type_annotation in
-    let `Local local_defs, `Toplevel top_defs, defs, refs, env, scopes =
-      expression tenv anno_expr (`Toplevel top_defs) (`Local local_defs)
-    in
-    `Local local_defs, `Toplevel top_defs, defs, refs @ t_refs, env, scopes
-  | E_update { struct_; update; _ } ->
-    let `Local local_defs, `Toplevel top_defs, defs, refs, tenv, scopes =
-      expression tenv struct_ (`Toplevel top_defs) (`Local local_defs)
-    in
-    let `Local local_defs, `Toplevel top_defs, defs', refs', tenv, scopes' =
-      expression tenv update (`Toplevel top_defs) (`Local local_defs)
-    in
-    ( `Local (defs' @ defs @ local_defs)
-    , `Toplevel top_defs
-    , defs' @ defs
-    , refs' @ refs
-    , tenv
-    , merge_same_scopes scopes @ scopes' )
-  | E_while { cond; body } ->
-    let `Local local_defs, `Toplevel top_defs, defs, refs, tenv, scopes =
-      expression tenv cond (`Toplevel top_defs) (`Local local_defs)
-    in
-    let `Local local_defs, `Toplevel top_defs, defs', refs', tenv, scopes' =
-      expression tenv body (`Toplevel top_defs) (`Local local_defs)
-    in
-    ( `Local (defs' @ defs @ local_defs)
-    , `Toplevel top_defs
-    , defs' @ defs
-    , refs' @ refs
-    , tenv
-    , merge_same_scopes scopes @ scopes' )
-  | E_for { binder; start; incr; final; f_body } ->
-    let def =
-      let binder_loc = VVar.get_location binder in
-      Misc.make_v_def ~with_types tenv.bindings Local binder binder_loc start.location
-      |> Option.to_list
-    in
-    let `Local local_defs, `Toplevel top_defs, defs_start, refs_start, tenv, scopes1 =
-      expression tenv start (`Toplevel top_defs) (`Local (def @ local_defs))
-    in
-    let `Local local_defs, `Toplevel top_defs, defs_incr, refs_incr, tenv, scopes2 =
-      expression tenv incr (`Toplevel top_defs) (`Local local_defs)
-    in
-    let `Local local_defs, `Toplevel top_defs, defs_final, refs_final, tenv, scopes3 =
-      expression tenv final (`Toplevel top_defs) (`Local local_defs)
-    in
-    let `Local local_defs, `Toplevel top_defs, defs_body, refs_body, tenv, scopes4 =
-      expression tenv f_body (`Toplevel top_defs) (`Local local_defs)
-    in
-    let scopes4 = add_defs_to_scopes def scopes4 in
-    let scopes =
-      merge_same_scopes scopes1
-      @ merge_same_scopes scopes2
-      @ merge_same_scopes scopes3
-      @ scopes4
-    in
-    let `Toplevel top_defs, defs, refs_body =
-      update_references refs_body def (`Toplevel top_defs)
-    in
-    ( `Local (defs_start @ defs_incr @ defs_final @ defs_body @ local_defs)
-    , `Toplevel top_defs
-    , defs_start @ defs_incr @ defs_final @ defs_body @ defs
-    , refs_start @ refs_incr @ refs_final @ refs_body
-    , tenv
-    , add_defs_to_scopes def scopes )
-  | E_for_each { fe_binder = binder1, binder2; collection; fe_body; _ } ->
-    let binders = binder1 :: Option.to_list binder2 in
-    let defs =
-      binders
-      |> List.filter ~f:VVar.is_generated
-      |> List.filter_map ~f:(fun binder ->
-             let loc = VVar.get_location binder in
-             Misc.make_v_def
-               ~with_types
-               tenv.bindings
-               Local
-               binder
-               loc
-               collection.location)
-    in
-    let `Local local_defs, `Toplevel top_defs, defs_coll, refs_coll, tenv, scopes_coll =
-      expression tenv collection (`Toplevel top_defs) (`Local local_defs)
-    in
-    let `Local local_defs, `Toplevel top_defs, defs_body, refs_body, tenv, scopes_body =
-      expression tenv fe_body (`Toplevel top_defs) (`Local (defs @ local_defs))
-    in
-    let scopes_body = add_defs_to_scopes defs scopes_body in
-    let scopes = merge_same_scopes scopes_coll @ scopes_body in
-    let `Toplevel top_defs, defs, refs_body =
-      update_references refs_body defs (`Toplevel top_defs)
-    in
-    ( `Local (defs_body @ defs_coll @ local_defs)
-    , `Toplevel top_defs
-    , defs_body @ defs_coll @ defs
-    , refs_body @ refs_coll
-    , tenv
-    , scopes )
-  | E_record e_lable_map ->
-    let `Local local_defs, `Toplevel top_defs, defs, refs, tenv, scopes =
-      Record.fold
-        e_lable_map
-        ~init:(`Local local_defs, `Toplevel top_defs, [], [], tenv, [])
-        ~f:(fun (`Local local_defs, `Toplevel top_defs, defs, refs, tenv, scopes) e ->
-          let `Local local_defs, `Toplevel top_defs, defs', refs', tenv, scopes' =
-            expression tenv e (`Toplevel top_defs) (`Local local_defs)
-          in
-          let scopes' = merge_same_scopes scopes' in
-          ( `Local (defs' @ local_defs)
-          , `Toplevel top_defs
-          , defs' @ defs
-          , refs' @ refs
-          , tenv
-          , merge_same_scopes scopes @ scopes' ))
-    in
-    `Local local_defs, `Toplevel top_defs, defs, refs, tenv, scopes
-  | E_assign { binder; expression = e } ->
-    let t_refs = find_binder_type_references binder in
-    let refs' = [ Variable (Binder.get_var binder) ] in
-    let `Local local_defs, `Toplevel top_defs, defs, refs, tenv, scopes =
-      expression tenv e (`Toplevel top_defs) (`Local local_defs)
-    in
-    let scopes = merge_same_scopes scopes in
-    `Local local_defs, `Toplevel top_defs, defs, refs @ refs' @ t_refs, tenv, scopes
-  | E_let_in
-      { let_binder
-      ; rhs = { expression_content = E_recursive _; _ } as rhs
-      ; let_result
-      ; _
-      } ->
-    let t_refs = find_pattern_type_references let_binder in
-    (* For recursive functions we don't need to add a def for [let_binder]
-        becase it will be added by the [E_recursive] case we just need to extract it
-        out of the [defs_rhs] *)
-    let `Local local_defs, `Toplevel top_defs, defs_rhs, refs_rhs, tenv, scopes =
-      expression tenv rhs (`Toplevel top_defs) (`Local local_defs)
-    in
-    let def, defs_rhs = drop_last defs_rhs in
-    let `Local local_defs, `Toplevel top_defs, defs_result, refs_result, tenv, scopes' =
-      expression tenv let_result (`Toplevel top_defs) (`Local ([ def ] @ local_defs))
-    in
-    let scopes' = add_defs_to_scopes [ def ] scopes' in
-    let scopes = merge_same_scopes scopes @ scopes' in
-    let `Toplevel top_defs, defs, refs_result =
-      update_references (refs_result @ t_refs) [ def ] (`Toplevel top_defs)
-    in
-    ( `Local local_defs
-    , `Toplevel top_defs
-    , defs_result @ defs_rhs @ defs
-    , refs_result @ refs_rhs
-    , tenv
-    , scopes )
-  | E_let_mut_in { let_binder; rhs; let_result; _ }
-  | E_let_in { let_binder; rhs; let_result; _ } ->
-    let binders = AST.Pattern.binders let_binder in
-    let binders, rhs = set_core_type_if_possible binders rhs in
-    let t_refs = List.concat (List.map ~f:find_binder_type_references binders) in
-    let defs_binder =
-      List.filter_map binders ~f:(fun binder ->
-          let var = Binder.get_var binder in
-          let core_type = Binder.get_ascr binder in
-          let binder_loc = VVar.get_location var in
-          Misc.make_v_def
-            ~with_types
-            ?core_type
-            tenv.bindings
-            Local
-            var
-            binder_loc
-            rhs.location)
-    in
-    let `Local local_defs, `Toplevel top_defs, defs_rhs, refs_rhs, tenv, scopes =
-      expression tenv rhs (`Toplevel top_defs) (`Local local_defs)
-    in
-    let `Local local_defs, `Toplevel top_defs, defs_result, refs_result, tenv, scopes' =
-      expression tenv let_result (`Toplevel top_defs) (`Local (defs_binder @ local_defs))
-    in
-    let scopes' = add_defs_to_scopes defs_binder scopes' in
-    let scopes = merge_same_scopes scopes @ scopes' in
-    let `Toplevel top_defs, defs, refs_result =
-      update_references (refs_result @ t_refs) defs_binder (`Toplevel top_defs)
-    in
-    ( `Local local_defs
-    , `Toplevel top_defs
-    , defs_result @ defs_rhs @ defs
-    , refs_result @ refs_rhs
-    , tenv
-    , scopes )
-  | E_recursive
-      { fun_name; fun_type; lambda = { binder; result; _ }; force_lambdarec = _ } ->
-    let t_refs = find_type_references fun_type in
-    let t_refs = t_refs @ find_type_references (Param.get_ascr binder) in
-    let def_fun =
-      let binder_loc = VVar.get_location fun_name in
-      Misc.make_v_def
-        ~with_types
-        ~core_type:fun_type
-        tenv.bindings
-        Local
-        fun_name
-        binder_loc
-        result.location
-      |> Option.to_list
-    in
-    let def_par =
-      let var = Param.get_var binder in
-      let core_type = Param.get_ascr binder in
-      let binder_loc = VVar.get_location var in
-      Misc.make_v_def
-        ~with_types
-        ~core_type
-        tenv.bindings
-        Local
-        var
-        binder_loc
-        result.location
-      |> Option.to_list
-    in
-    let `Local local_defs, `Toplevel top_defs, defs_result, refs_result, tenv, scopes =
-      expression tenv result (`Toplevel top_defs) (`Local (def_par @ local_defs))
-    in
-    let scopes = merge_same_scopes scopes in
-    let defs = def_par @ def_fun in
-    let `Toplevel top_defs, defs, refs =
-      update_references (refs_result @ t_refs) defs (`Toplevel top_defs)
-    in
-    ( `Local (defs_result @ local_defs)
-    , `Toplevel top_defs
-    , defs_result @ defs
-    , refs
-    , tenv
-    , add_defs_to_scopes (def_par @ def_fun) scopes )
-  | E_type_in { type_binder; rhs; let_result } ->
-    let t_refs = find_type_references rhs in
-    let tdef = [ type_expression type_binder Local rhs ] in
-    let `Local local_defs, `Toplevel top_defs, defs, refs, tenv, scopes =
-      expression tenv let_result (`Toplevel top_defs) (`Local local_defs)
-    in
-    let `Toplevel top_defs, tdef, refs =
-      update_references refs tdef (`Toplevel top_defs)
-    in
-    ( `Local (tdef @ local_defs)
-    , `Toplevel top_defs
-    , tdef @ defs
-    , refs @ t_refs
-    , tenv
-    , add_defs_to_scopes tdef scopes )
-  | E_matching { matchee; cases } ->
-    let `Local local_defs, `Toplevel top_defs, defs_matchee, refs_matchee, tenv, scopes =
-      expression tenv matchee (`Toplevel top_defs) (`Local local_defs)
-    in
-    let `Toplevel top_defs, defs_matchee, refs_matchee =
-      update_references refs_matchee defs_matchee (`Toplevel top_defs)
-    in
-    let scopes = merge_same_scopes scopes in
-    let `Local local_defs, `Toplevel top_defs, defs_cases, refs_cases, tenv, scopes' =
-      List.fold_left
-        cases
-        ~init:(`Local local_defs, `Toplevel top_defs, [], [], tenv, [])
-        ~f:
-          (fun (`Local local_defs, `Toplevel top_defs, defs, refs, tenv, scopes)
-               { pattern; body } ->
-          let defs_pat, refs_pat =
-            AST.Pattern.fold_pattern
-              (fun (defs, refs) (p : _ AST.Pattern.t) ->
-                match p.wrap_content with
-                | P_var binder ->
-                  let t_refs = find_binder_type_references binder in
-                  let def =
-                    let var = Binder.get_var binder in
-                    let core_type = Binder.get_ascr binder in
-                    let binder_loc = VVar.get_location var in
-                    Misc.make_v_def
-                      ~with_types
-                      ?core_type
-                      tenv.bindings
-                      Local
-                      var
-                      binder_loc
-                      body.location
-                    |> Option.to_list
-                  in
-                  def @ defs, refs @ t_refs
-                | P_unit
-                | P_list (Cons _)
-                | P_list (List _)
-                | P_variant _ | P_tuple _ | P_record _ -> defs, refs)
-              ([], [])
-              pattern
-          in
-          let `Local local_defs, `Toplevel top_defs, defs_body, refs_body, tenv, scopes' =
-            expression tenv body (`Toplevel top_defs) (`Local local_defs)
-          in
-          let scopes' = merge_same_scopes scopes' in
-          let scopes' = add_defs_to_scopes defs_pat scopes' in
-          let `Toplevel top_defs, defs_pat, refs_body =
-            update_references refs_body defs_pat (`Toplevel top_defs)
-          in
-          ( `Local (defs_body @ defs_pat @ local_defs)
-          , `Toplevel top_defs
-          , defs_body @ defs_pat @ defs
-          , refs_body @ refs @ refs_pat
-          , tenv
-          , merge_same_scopes scopes @ scopes' ))
-    in
-    ( `Local local_defs
-    , `Toplevel top_defs
-    , defs_matchee @ defs_cases
-    , refs_matchee @ refs_cases
-    , tenv
-    , scopes @ scopes' )
-  | E_mod_in { module_binder; rhs; let_result } ->
-    let `Local local_defs, `Toplevel top_defs, defs_module, refs_module, tenv, scopes =
-      match join_defs_and_top_defs local_defs (`Toplevel top_defs) with
-      | Some (top_defs, uid) ->
-        let `Toplevel top_defs, defs_module, refs_module, tenv, scopes =
-          module_expression
-            ~raise
-            ~with_types
-            ~options
-            tenv
-            Local
-            module_binder
-            rhs
-            (`Toplevel top_defs)
-        in
-        let local_defs, top_defs = split_defs top_defs uid in
-        `Local local_defs, `Toplevel top_defs, defs_module, refs_module, tenv, scopes
-      | None ->
-        let `Toplevel top_defs, defs_module, refs_module, tenv, scopes =
-          module_expression
-            ~raise
-            ~with_types
-            ~options
-            tenv
-            Local
-            module_binder
-            rhs
-            (`Toplevel top_defs)
-        in
-        `Local local_defs, `Toplevel top_defs, defs_module, refs_module, tenv, scopes
-    in
-    (* let () =
-      Format.printf
-        "$$$$$$$$$$$$$$$$$$\n %a \n\n %a\n$$$$$$$$$$$$$$$$$$\n"
-        PP.definitions local_defs
-        pp_references
-        refs_module
-    in *)
-    let `Local local_defs, `Toplevel top_defs, defs_result, refs_result, tenv, scopes' =
-      expression tenv let_result (`Toplevel top_defs) (`Local (defs_module @ local_defs))
-    in
-    (* let () =
-      Format.printf
-        "##################\n %a \n\n %a\n##################\n"
-        PP.definitions local_defs
-        pp_references
-        refs_result
-    in *)
-    let `Toplevel top_defs, local_defs, refs_result =
-      update_references refs_result local_defs (`Toplevel top_defs)
-    in
-    let scopes' = merge_same_scopes scopes' in
-    let scopes' = add_defs_to_scopes defs_module scopes' in
-    let `Toplevel top_defs, defs, refs_result =
-      update_references refs_result (defs_result @ defs_module) (`Toplevel top_defs)
-    in
-    ( `Local local_defs
-    , `Toplevel top_defs
-    , defs
-    , refs_result @ refs_module
-    , tenv
-    , merge_same_scopes scopes @ scopes' )
-
-
-and type_expression : TVar.t -> def_type -> AST.type_expression -> def =
- fun tv def_type t ->
-  let def =
-    let binder_name = get_type_binder_name tv in
-    let binder_loc = TVar.get_location tv in
-    make_t_def binder_name def_type binder_loc t
-  in
-  def
-
-
-and module_expression ~raise
-    :  with_types:bool -> options:Compiler_options.middle_end -> typing_env -> def_type
-    -> MVar.t -> AST.module_expr -> [ `Toplevel of def list ]
-    -> [ `Toplevel of def list ] * def list * reference list * typing_env * scopes
-  =
- fun ~with_types ~options tenv def_type top m (`Toplevel top_defs) ->
-  match m.wrap_content with
-  | M_struct decls ->
-    let `Toplevel top_defs, defs, refs, tenv, scopes =
-      (* [update_tenv] is [false] because [update_typing_env] already types
-         nested modules, so we only need to call it at toplevel declarations. *)
-      declarations
-        ~raise
-        ~update_tenv:false
-        ~with_types
-        ~options
-        tenv
-        decls
-        (`Toplevel top_defs)
-    in
-    let range = MVar.get_location top in
-    let body_range = m.location in
-    let name = get_mod_binder_name top in
-    let def = make_m_def ~range ~body_range name def_type defs in
-    `Toplevel top_defs, [ def ], refs, tenv, scopes
-  | M_variable mv ->
-    let def, reference =
-      let name = get_mod_binder_name top in
-      let range = MVar.get_location top in
-      let body_range = MVar.get_location mv in
-      let alias = [ get_mod_binder_name mv ] in
-      let def = make_m_alias_def ~range ~body_range name def_type alias in
-      let reference = ModuleAlias [ mv ] in
-      def, reference
-    in
-    `Toplevel top_defs, [ def ], [ reference ], tenv, []
-  | M_module_path path ->
-    let mvs = List.Ne.to_list path in
-    let def, reference =
-      let name = get_mod_binder_name top in
-      let range = MVar.get_location top in
-      let body_range = Misc.get_location_of_module_path mvs in
-      let alias = List.map mvs ~f:get_mod_binder_name in
-      let def = make_m_alias_def ~range ~body_range name def_type alias in
-      let reference = ModuleAlias mvs in
-      def, reference
-    in
-    `Toplevel top_defs, [ def ], [ reference ], tenv, []
-
-
-and declaration_expression ~raise
-    :  with_types:bool -> options:Compiler_options.middle_end -> typing_env
-    -> AST.type_expression option Binder.t list -> AST.expression
-    -> [ `Toplevel of def list ]
-    -> [ `Toplevel of def list ] * def list * reference list * typing_env * scopes
-  =
- fun ~with_types ~options tenv binders e (`Toplevel top_defs) ->
-  let `Local local_defs, `Toplevel top_defs, defs, refs, tenv, scopes =
-    expression ~raise ~with_types ~options tenv e (`Toplevel top_defs) (`Local [])
-  in
-  let defs = patch_top_level_references defs (`Toplevel local_defs) in
-  let defs' =
-    List.filter_map binders ~f:(fun binder ->
-        let core_type = Binder.get_ascr binder in
-        let ev = Binder.get_var binder in
-        let range = VVar.get_location ev in
-        let body_range = e.location in
-        Misc.make_v_def ~with_types ?core_type tenv.bindings Global ev range body_range)
-  in
-  `Toplevel top_defs, defs' @ defs, refs, tenv, scopes
-
-
-and declaration ~raise
-    :  with_types:bool -> options:Compiler_options.middle_end -> update_tenv:bool
-    -> typing_env -> AST.declaration -> [ `Toplevel of def list ]
-    -> [ `Toplevel of def list ] * def list * reference list * typing_env * scopes
-  =
- fun ~with_types ~options ~update_tenv tenv decl (`Toplevel top_defs) ->
-  let tenv =
-    if update_tenv
-    then (* only at top-level *)
-      update_typing_env ~raise ~with_types ~options tenv decl
-    else tenv
-  in
-  match decl.wrap_content with
-  | D_value { binder; expr = { expression_content = E_recursive _; _ } as expr; attr = _ }
-    ->
-    let t_refs = find_binder_type_references binder in
-    (* For recursive functions we don't need to add a def for [binder]
-        becase it will be added by the [E_recursive] case we just need to extract it
-        out of the [defs_expr] *)
-    let `Local local_defs, `Toplevel top_defs, defs_expr, refs_rhs, tenv, scopes =
-      expression ~raise ~with_types ~options tenv expr (`Toplevel top_defs) (`Local [])
-    in
-    let defs_expr = patch_top_level_references defs_expr (`Toplevel local_defs) in
-    let defs_expr = patch_top_level_references defs_expr (`Toplevel local_defs) in
-    let def, defs_expr = drop_last defs_expr in
-    `Toplevel top_defs, [ def ] @ defs_expr, refs_rhs @ t_refs, tenv, scopes
-  | D_irrefutable_match { pattern; expr; _ } ->
-    let binders = AST.Pattern.binders pattern in
-    let binders, expr = set_core_type_if_possible binders expr in
-    let t_refs = List.concat (List.map ~f:find_binder_type_references binders) in
-    let `Toplevel top_defs, defs, refs, env, scopes =
-      declaration_expression
-        ~raise
-        ~with_types
-        ~options
-        tenv
-        binders
-        expr
-        (`Toplevel top_defs)
-    in
-    `Toplevel top_defs, defs, refs @ t_refs, env, scopes
-  | D_value { binder; expr; attr = _ } ->
-    let t_refs = find_binder_type_references binder in
-    let `Toplevel top_defs, defs, refs, env, scopes =
-      declaration_expression
-        ~raise
-        ~with_types
-        ~options
-        tenv
-        [ binder ]
-        expr
-        (`Toplevel top_defs)
-    in
-    `Toplevel top_defs, defs, refs @ t_refs, env, scopes
-  | D_type { type_binder; type_expr; type_attr = _ } ->
-    let t_refs = find_type_references type_expr in
-    let def = type_expression type_binder Global type_expr in
-    `Toplevel top_defs, [ def ], t_refs, tenv, []
-  | D_module { module_binder; module_; module_attr = _ } ->
-    let `Toplevel top_defs, defs, refs, env, scopes =
-      module_expression
-        ~raise
-        ~with_types
-        ~options
-        tenv
-        Global
-        module_binder
-        module_
-        (`Toplevel top_defs)
-    in
-    let `Toplevel top_defs, defs, refs =
-      update_references refs defs (`Toplevel top_defs)
-    in
-    `Toplevel top_defs, defs, refs, env, scopes
-
-
-and declarations ~raise
-    :  with_types:bool -> options:Compiler_options.middle_end -> ?update_tenv:bool
-    -> ?stdlib_defs:def list -> ?top_level:bool -> typing_env -> AST.declaration list
-    -> [ `Toplevel of def list ]
-    -> [ `Toplevel of def list ] * def list * reference list * typing_env * scopes
-  =
- fun ~with_types
-     ~options
-     ?(update_tenv = true)
-     ?(stdlib_defs = [])
-     ?(top_level = false)
-     tenv
-     decls
-     (`Toplevel top_defs) ->
-  let `Toplevel top_defs, `Global gdefs, `Local ldefs, refs, tenv, scopes =
-    List.fold_left
-      decls
-      ~init:(`Toplevel top_defs, `Global stdlib_defs, `Local [], [], tenv, [])
-      ~f:
-        (fun (`Toplevel top_defs, `Global gdefs, `Local ldefs, refs, tenv, scopes) decl ->
-        let `Toplevel top_defs, defs', refs', tenv, scopes' =
-          declaration
-            ~raise
-            ~with_types
-            ~options
-            ~update_tenv
-            tenv
-            decl
-            (`Toplevel top_defs)
-        in
-        let `Global gdefs', `Local ldefs' = filter_local_defs defs' in
-        let scopes' = add_defs_to_scopes gdefs scopes' in
-        let `Toplevel top_defs, gdefs, refs =
-          update_references (refs' @ refs) gdefs (`Toplevel top_defs)
-        in
-        let top_defs = if top_level then gdefs' @ top_defs else top_defs in
-        ( `Toplevel top_defs
-        , `Global (gdefs' @ gdefs)
-        , `Local (ldefs' @ ldefs)
-        , refs
-        , tenv
-        , scopes @ scopes' ))
-  in
-  let `Toplevel top_defs, gdefs, refs =
-    update_references refs gdefs (`Toplevel top_defs)
-  in
-  `Toplevel top_defs, ldefs @ gdefs, refs, tenv, scopes
-
-
-let resolve_module_aliases_to_module_ids : def list -> def list =
- fun defs ->
-  let find_mod_def_id ids name =
-    match List.find ids ~f:(fun (name', _) -> String.(name = name')) with
-    | Some (_, id) -> id
-    | None -> name
-  in
-  let rec resolve defs mod_def_ids =
-    match defs with
-    | [] -> mod_def_ids, defs
-    | Module ({ uid; name; mod_case = Def d; _ } as m) :: defs ->
-      let d = List.rev d in
-      let mod_def_ids, d = resolve d mod_def_ids in
-      let d = List.rev d in
-      let mod_case = Def d in
-      let mod_def_ids, defs = resolve defs ((name, uid) :: mod_def_ids) in
-      mod_def_ids, Module { m with mod_case } :: defs
-    | Module ({ uid; name; mod_case = Alias a; _ } as m) :: defs ->
-      let a = List.map a ~f:(find_mod_def_id mod_def_ids) in
-      let mod_case = Alias a in
-      let mod_def_ids, defs = resolve defs ((name, uid) :: mod_def_ids) in
-      mod_def_ids, Module { m with mod_case } :: defs
-    | def :: defs ->
-      let mod_def_ids, defs = resolve defs mod_def_ids in
-      mod_def_ids, def :: defs
-  in
-  let defs = List.rev defs in
-  let defs = snd (resolve defs []) in
-  List.rev defs
-
-
-let stdlib_defs ~raise
-    : options:Compiler_options.middle_end -> Ast_core.program -> def list
-  =
- fun ~options stdlib_core ->
-  let no_stdlib = options.no_stdlib in
-  if no_stdlib
-  then []
-  else (
-    let tenv =
-      { type_env = options.init_env; bindings = Misc.Bindings_map.empty; decls = [] }
-    in
-    let `Toplevel top_defs, stdlib_defs, _, _, _ =
-      declarations ~raise ~with_types:false ~options tenv stdlib_core (`Toplevel [])
-    in
-    let stdlib_defs = patch_top_level_references stdlib_defs (`Toplevel top_defs) in
-    ignore_local_defs stdlib_defs)
-
-
-let rec patch_refs : def list -> References.references -> def list =
- fun defs refs ->
-  let open References in
-  List.map defs ~f:(fun def ->
-      match def with
-      | Variable v ->
-        (match LMap.find_opt v.range refs with
-        | None -> Types.Variable v
-        | Some references -> Variable { v with references })
-      | Type t ->
-        (match LMap.find_opt t.range refs with
-        | None -> Types.Type t
-        | Some references -> Type { t with references })
-      | Module m ->
-        let mod_case =
-          match m.mod_case with
-          | Alias a -> Types.Alias a
-          | Def defs -> Def (patch_refs defs refs)
-        in
-        let m =
-          match LMap.find_opt m.range refs with
-          | None -> m
-          | Some references -> { m with references }
-        in
-        Module { m with mod_case })
-
-
-let rec patch_types : def list -> Types_pass.t -> def list =
- fun defs bindings ->
-  List.map defs ~f:(fun def ->
-      match def with
-      | Variable v ->
-        (match v.t, Types_pass.LMap.find_opt v.range bindings with
-        | Unresolved, Some t -> Types.Variable { v with t }
-        | _ -> Variable v)
-      | Type t -> Type t
-      | Module m ->
-        let mod_case =
-          match m.mod_case with
-          | Alias a -> Types.Alias a
-          | Def defs -> Def (patch_types defs bindings)
-        in
-        Module { m with mod_case })
-
-
 let scopes
     :  raise:(Main_errors.all, Main_warnings.all) Trace.raise -> with_types:bool
     -> options:Compiler_options.middle_end -> stdlib:Ast_typed.program * Ast_core.program
@@ -1164,42 +30,31 @@ let scopes
   =
  fun ~raise ~with_types ~options ~stdlib prg ->
   let stdlib_decls, stdlib_core = stdlib in
-  let type_env = Environment.append options.init_env stdlib_decls in
-  let tenv = { type_env; bindings = Misc.Bindings_map.empty; decls = [] } in
-  let stdlib_defs = stdlib_defs ~raise ~options stdlib_core in
-  let defs, scopes =
-    let `Toplevel top_defs, defs, _, _, scopes =
-      declarations
-        ~raise
-        ~with_types
-        ~options
-        ~stdlib_defs
-        ~top_level:true
-        tenv
-        prg
-        (`Toplevel stdlib_defs)
-    in
-    let defs = patch_top_level_references defs (`Toplevel top_defs) in
-    defs, scopes
-  in
-  let scopes = fix_shadowing_in_scopes scopes in
-  let defs = resolve_module_aliases_to_module_ids defs in
-  (* Use WIP new implementation only during expect_tests *)
-  let defs, scopes =
-    match Sys.getenv "LIGO_GET_SCOPE_USE_NEW_IMP" with
-    | Some s when String.(s <> "") ->
-      let types = Types_pass.resolve ~raise ~options ~stdlib_decls prg in
-      let new_defs = Definitions.definitions prg stdlib_defs in
-      let defs = Definitions.Merge_defs_temp.merge_defs defs new_defs in
-      let defs = patch_types defs types in
-      let refs = References.declarations (stdlib_core @ prg) in
-      let env_preload_decls = if options.no_stdlib then [] else stdlib_core in
-      let new_scopes = Scopes_pass.Of_Ast.declarations ~env_preload_decls prg in
-      let scopes =
-        Scopes_pass.to_old_scopes (flatten_defs defs @ stdlib_defs) new_scopes
-      in
-      let scopes = fix_shadowing_in_scopes scopes in
-      patch_refs defs refs, scopes
-    | Some _ | None -> defs, scopes
+  let stdlib_defs, env_preload_decls =
+    if options.no_stdlib
+    then [], []
+    else
+      ( (let stdlib_core_types = Types_pass.(Of_Ast_core.declarations empty stdlib_core) in
+         Definitions.Of_Stdlib.definitions stdlib_core
+         |> Types_pass.patch stdlib_core_types)
+      , stdlib_core )
+  in
+  let defs =
+    let types =
+      if with_types
+      then Types_pass.resolve ~raise ~options ~stdlib_decls prg
+      else Types_pass.empty
+    in
+    let module_aliases = Module_aliases_pass.declarations prg in
+    let refs = References.declarations (stdlib_core @ prg) in
+    Definitions.definitions prg stdlib_defs
+    |> (if with_types then Types_pass.patch types else Fn.id)
+    |> References.patch refs
+    |> Module_aliases_pass.patch module_aliases
+  in
+  let scopes =
+    Scopes_pass.Of_Ast.declarations ~env_preload_decls prg
+    |> Scopes_pass.to_old_scopes (flatten_defs defs @ stdlib_defs)
+    |> fix_shadowing_in_scopes
   in
   defs, scopes
diff --git a/src/main/scopes/types.ml b/src/main/scopes/types.ml
index 9cfa905d20660fe45b3313fef6fbd46f1fb45dc1..a05df1981e6f39fadc25cc29f905d9882406b0fc 100644
--- a/src/main/scopes/types.ml
+++ b/src/main/scopes/types.ml
@@ -202,24 +202,6 @@ let add_defs_to_scopes : def list -> scopes -> scopes =
  fun defs scopes -> List.map scopes ~f:(add_defs_to_scope defs)
 
 
-let merge_same_scopes : scopes -> scopes =
- fun scopes ->
-  let rec aux scopes acc =
-    match scopes with
-    | [] -> acc
-    | (loc, scope) :: scopes ->
-      let same, different =
-        List.partition_tf scopes ~f:(fun (_, s) -> List.equal def_equal s scope)
-      in
-      let merged_scope_loc =
-        List.fold_left same ~init:loc ~f:(fun loc (loc', _) -> Location.cover loc loc')
-      in
-      let merged_scope = merged_scope_loc, scope in
-      aux different (merged_scope :: acc)
-  in
-  aux scopes []
-
-
 let rec flatten_defs defs =
   match defs with
   | [] -> []
@@ -246,12 +228,3 @@ let fix_shadowing_in_scope : scope -> scope =
 
 let fix_shadowing_in_scopes : scopes -> scopes =
  fun scopes -> List.map scopes ~f:fix_shadowing_in_scope
-
-
-module Bindings_map = Simple_utils.Map.Make (struct
-  type t = Ast_typed.expression_variable
-
-  let compare = Value_var.compare
-end)
-
-type bindings_map = Ast_typed.type_expression Bindings_map.t
diff --git a/src/main/scopes/types_pass.ml b/src/main/scopes/types_pass.ml
index e9f47a8a08029e236c3834509664af107d8e04d5..6b7444c75dbea04b7cec865095d4e0d304da7001 100644
--- a/src/main/scopes/types_pass.ml
+++ b/src/main/scopes/types_pass.ml
@@ -6,6 +6,8 @@ module Pattern = Ast_typed.Pattern
 
 type t = type_case LMap.t
 
+let empty = LMap.empty
+
 module Of_Ast_typed = struct
   let add_binding : t -> Ast_typed.expression_variable * Ast_typed.type_expression -> t =
    fun env binding ->
@@ -242,7 +244,8 @@ module Of_Ast_core = struct
    fun bindings decl ->
     match Location.unwrap decl with
     | D_value { binder; expr; _ } ->
-      let bindings = add_binders bindings [ binder ] in
+      let binders, expr = set_core_type_if_possible [ binder ] expr in
+      let bindings = add_binders bindings binders in
       expression bindings expr
     | D_irrefutable_match { pattern; expr; _ } ->
       let binders = Pattern.binders pattern in
@@ -372,3 +375,21 @@ let resolve
   let tenv = List.fold prg ~init:tenv ~f:(Typing_env.update_typing_env ~raise ~options) in
   let bindings = tenv.bindings in
   Of_Ast_core.declarations bindings prg
+
+
+let rec patch : t -> Types.def list -> Types.def list =
+ fun bindings defs ->
+  List.map defs ~f:(fun def ->
+      match def with
+      | Variable v ->
+        (match v.t, LMap.find_opt v.range bindings with
+        | Unresolved, Some t -> Types.Variable { v with t }
+        | _ -> Variable v)
+      | Type t -> Type t
+      | Module m ->
+        let mod_case =
+          match m.mod_case with
+          | Alias a -> Types.Alias a
+          | Def defs -> Def (patch bindings defs)
+        in
+        Module { m with mod_case })
diff --git a/src/main/scopes/types_pass.mli b/src/main/scopes/types_pass.mli
index 26b8c42b4d9dd51d177ca444de8233829fd380bb..e453ab640f72d01d07462366bb3a41ece946a29a 100644
--- a/src/main/scopes/types_pass.mli
+++ b/src/main/scopes/types_pass.mli
@@ -3,9 +3,17 @@ module LMap : Map.S with type key = Location.t
 
 type t = Types.type_case LMap.t
 
+val empty : t
+
 val resolve
   :  raise:(Main_errors.all, Main_warnings.all) Trace.raise
   -> options:Compiler_options.middle_end
   -> stdlib_decls:Ast_typed.program
   -> Ast_core.program
   -> t
+
+module Of_Ast_core : sig
+  val declarations : t -> Ast_core.declaration list -> t
+end
+
+val patch : t -> Types.def list -> Types.def list
diff --git a/src/test/get_scope_tests.ml b/src/test/get_scope_tests.ml
index 6fe4be95f57e73f09d2d4d04f6a1e03409bcd8c1..5776c3dd91d24fe9d706c13178a19792a7f50cf3 100644
--- a/src/test/get_scope_tests.ml
+++ b/src/test/get_scope_tests.ml
@@ -4,7 +4,6 @@ module InfoApi = Ligo_api.Info
 module Raw_options = Compiler_options.Raw_options
 
 let schema = "../main/scopes/schema.json"
-let () = Ligo_unix.putenv ~key:"LIGO_GET_SCOPE_USE_NEW_IMP" ~data:""
 
 let validate_json_file file_name =
   let command_str = Format.sprintf "python3 -m jsonschema -i %s %s" file_name schema in
@@ -148,6 +147,3 @@ let _main =
             schema_test_negative ~with_types:true ~speed:`Slow file ~expected_status:None)
       )
     ]
-
-
-let () = Ligo_unix.putenv ~key:"LIGO_GET_SCOPE_USE_NEW_IMP" ~data:""
diff --git a/src/test/lsp_test.ml b/src/test/lsp_test.ml
index a3f1313dba46364a12653e301ee1b155547eb3d0..c767e69956b4c123e0e269df1f05cefb31db6b91 100644
--- a/src/test/lsp_test.ml
+++ b/src/test/lsp_test.ml
@@ -2,7 +2,6 @@ open Lsp_test_requests
 
 let _main =
   Printexc.record_backtrace true;
-  Ligo_unix.putenv ~key:"LIGO_GET_SCOPE_USE_NEW_IMP" ~data:"";
   Alcotest.run
     "lsp_server_tests"
     [ Definition.tests
@@ -11,5 +10,4 @@ let _main =
     ; Document_link.tests
     ; Folding_range.tests
     ; Formatting.tests
-    ];
-  Ligo_unix.putenv ~key:"LIGO_GET_SCOPE_USE_NEW_IMP" ~data:""
+    ]