@@ -6,6 +6,7 @@ use std::sync::Arc;
6
6
7
7
use scoped_arena:: Scope ;
8
8
9
+ use super :: { Builder , LetDef } ;
9
10
use crate :: alloc:: SliceVec ;
10
11
use crate :: core:: { prim, Const , LocalInfo , Plicity , Prim , Term } ;
11
12
use crate :: env:: { EnvLen , Index , Level , SharedEnv , SliceEnv } ;
@@ -300,8 +301,8 @@ impl<'arena, 'env> EvalEnv<'arena, 'env> {
300
301
self . apply_local_infos ( head_expr, local_infos)
301
302
}
302
303
Term :: Ann ( span, expr, _) => Spanned :: merge ( * span, self . eval ( expr) ) ,
303
- Term :: Let ( span, _ , _ , def_expr , body_expr) => {
304
- let def_expr = self . eval ( def_expr ) ;
304
+ Term :: Let ( span, def , body_expr) => {
305
+ let def_expr = self . eval ( & def . expr ) ;
305
306
self . local_exprs . push ( def_expr) ;
306
307
let body_expr = self . eval ( body_expr) ;
307
308
self . local_exprs . pop ( ) ;
@@ -667,21 +668,17 @@ impl<'in_arena, 'env> QuoteEnv<'in_arena, 'env> {
667
668
// NOTE: this copies more than is necessary when `'in_arena == 'out_arena`:
668
669
// for example when copying label slices.
669
670
671
+ let builder = Builder :: new ( scope) ;
670
672
let value = self . elim_env . force ( value) ;
671
673
let span = value. span ( ) ;
672
674
match value. as_ref ( ) {
673
675
Value :: Stuck ( head, spine) => spine. iter ( ) . fold (
674
676
self . quote_head ( scope, span, head) ,
675
677
|head_expr, elim| match elim {
676
- Elim :: FunApp ( plicity, arg_expr) => Term :: FunApp (
677
- span,
678
- * plicity,
679
- scope. to_scope ( head_expr) ,
680
- scope. to_scope ( self . quote ( scope, arg_expr) ) ,
681
- ) ,
682
- Elim :: RecordProj ( label) => {
683
- Term :: RecordProj ( span, scope. to_scope ( head_expr) , * label)
678
+ Elim :: FunApp ( plicity, arg_expr) => {
679
+ builder. fun_app ( span, * plicity, head_expr, self . quote ( scope, arg_expr) )
684
680
}
681
+ Elim :: RecordProj ( label) => builder. record_proj ( span, head_expr, * label) ,
685
682
Elim :: ConstMatch ( branches) => {
686
683
let mut branches = branches. clone ( ) ;
687
684
let mut pattern_branches = SliceVec :: new ( scope, branches. num_patterns ( ) ) ;
@@ -699,9 +696,9 @@ impl<'in_arena, 'env> QuoteEnv<'in_arena, 'env> {
699
696
}
700
697
} ;
701
698
702
- Term :: ConstMatch (
699
+ builder . const_match (
703
700
span,
704
- scope . to_scope ( head_expr) ,
701
+ head_expr,
705
702
pattern_branches. into ( ) ,
706
703
default_branch
707
704
. map ( |( name, expr) | ( name, self . quote_closure ( scope, & expr) ) ) ,
@@ -712,20 +709,19 @@ impl<'in_arena, 'env> QuoteEnv<'in_arena, 'env> {
712
709
713
710
Value :: Universe => Term :: Universe ( span) ,
714
711
715
- Value :: FunType ( plicity, param_name, param_type, body_type) => Term :: FunType (
712
+ Value :: FunType ( plicity, param_name, param_type, body_type) => builder . fun_type (
716
713
span,
717
714
* plicity,
718
715
* param_name,
719
- scope . to_scope ( self . quote ( scope, param_type) ) ,
716
+ self . quote ( scope, param_type) ,
720
717
self . quote_closure ( scope, body_type) ,
721
718
) ,
722
- Value :: FunLit ( plicity, param_name, body_expr) => Term :: FunLit (
719
+ Value :: FunLit ( plicity, param_name, body_expr) => builder . fun_lit (
723
720
span,
724
721
* plicity,
725
722
* param_name,
726
723
self . quote_closure ( scope, body_expr) ,
727
724
) ,
728
-
729
725
Value :: RecordType ( labels, types) => Term :: RecordType (
730
726
span,
731
727
scope. to_scope_from_iter ( labels. iter ( ) . copied ( ) ) ,
@@ -746,10 +742,10 @@ impl<'in_arena, 'env> QuoteEnv<'in_arena, 'env> {
746
742
scope. to_scope_from_iter ( labels. iter ( ) . copied ( ) ) ,
747
743
self . quote_telescope ( scope, formats) ,
748
744
) ,
749
- Value :: FormatCond ( label, format, cond) => Term :: FormatCond (
745
+ Value :: FormatCond ( label, format, cond) => builder . format_cond (
750
746
span,
751
747
* label,
752
- scope . to_scope ( self . quote ( scope, format) ) ,
748
+ self . quote ( scope, format) ,
753
749
self . quote_closure ( scope, cond) ,
754
750
) ,
755
751
Value :: FormatOverlap ( labels, formats) => Term :: FormatOverlap (
@@ -792,15 +788,15 @@ impl<'in_arena, 'env> QuoteEnv<'in_arena, 'env> {
792
788
& mut self ,
793
789
scope : & ' out_arena Scope < ' out_arena > ,
794
790
closure : & Closure < ' in_arena > ,
795
- ) -> & ' out_arena Term < ' out_arena > {
791
+ ) -> Term < ' out_arena > {
796
792
let var = Arc :: new ( Value :: local_var ( self . local_exprs . next_level ( ) ) ) ;
797
793
let value = self . elim_env . apply_closure ( closure, Spanned :: empty ( var) ) ;
798
794
799
795
self . push_local ( ) ;
800
796
let term = self . quote ( scope, & value) ;
801
797
self . pop_local ( ) ;
802
798
803
- scope . to_scope ( term)
799
+ term
804
800
}
805
801
806
802
/// Quote a [telescope][Telescope] back into a slice of [terms][Term].
@@ -848,6 +844,8 @@ impl<'arena, 'env> EvalEnv<'arena, 'env> {
848
844
scope : & ' out_arena Scope < ' out_arena > ,
849
845
term : & Term < ' arena > ,
850
846
) -> Term < ' out_arena > {
847
+ let builder = Builder :: new ( scope) ;
848
+
851
849
match term {
852
850
Term :: ItemVar ( span, var) => Term :: ItemVar ( * span, * var) ,
853
851
Term :: LocalVar ( span, var) => Term :: LocalVar ( * span, * var) ,
@@ -871,29 +869,31 @@ impl<'arena, 'env> EvalEnv<'arena, 'env> {
871
869
Term :: InsertedMeta ( * span, * var, infos)
872
870
}
873
871
} ,
874
- Term :: Ann ( span, expr, r#type) => Term :: Ann (
872
+ Term :: Ann ( span, expr, r#type) => builder . ann (
875
873
* span,
876
- scope . to_scope ( self . unfold_metas ( scope, expr) ) ,
877
- scope . to_scope ( self . unfold_metas ( scope, r#type) ) ,
874
+ self . unfold_metas ( scope, expr) ,
875
+ self . unfold_metas ( scope, r#type) ,
878
876
) ,
879
- Term :: Let ( span, def_name , def_type , def_expr , body_expr) => Term :: Let (
877
+ Term :: Let ( span, def , body_expr) => builder . r#let (
880
878
* span,
881
- * def_name,
882
- scope. to_scope ( self . unfold_metas ( scope, def_type) ) ,
883
- scope. to_scope ( self . unfold_metas ( scope, def_expr) ) ,
879
+ LetDef {
880
+ name : def. name ,
881
+ r#type : ( self . unfold_metas ( scope, & def. r#type ) ) ,
882
+ expr : ( self . unfold_metas ( scope, & def. expr ) ) ,
883
+ } ,
884
884
self . unfold_bound_metas ( scope, body_expr) ,
885
885
) ,
886
886
887
887
Term :: Universe ( span) => Term :: Universe ( * span) ,
888
888
889
- Term :: FunType ( span, plicity, param_name, param_type, body_type) => Term :: FunType (
889
+ Term :: FunType ( span, plicity, param_name, param_type, body_type) => builder . fun_type (
890
890
* span,
891
891
* plicity,
892
892
* param_name,
893
- scope . to_scope ( self . unfold_metas ( scope, param_type) ) ,
893
+ self . unfold_metas ( scope, param_type) ,
894
894
self . unfold_bound_metas ( scope, body_type) ,
895
895
) ,
896
- Term :: FunLit ( span, plicity, param_name, body_expr) => Term :: FunLit (
896
+ Term :: FunLit ( span, plicity, param_name, body_expr) => builder . fun_lit (
897
897
* span,
898
898
* plicity,
899
899
* param_name,
@@ -921,10 +921,10 @@ impl<'arena, 'env> EvalEnv<'arena, 'env> {
921
921
scope. to_scope_from_iter ( labels. iter ( ) . copied ( ) ) ,
922
922
self . unfold_telescope_metas ( scope, formats) ,
923
923
) ,
924
- Term :: FormatCond ( span, name, format, pred) => Term :: FormatCond (
924
+ Term :: FormatCond ( span, name, format, pred) => builder . format_cond (
925
925
* span,
926
926
* name,
927
- scope . to_scope ( self . unfold_metas ( scope, format) ) ,
927
+ self . unfold_metas ( scope, format) ,
928
928
self . unfold_bound_metas ( scope, pred) ,
929
929
) ,
930
930
Term :: FormatOverlap ( span, labels, formats) => Term :: FormatOverlap (
@@ -945,6 +945,8 @@ impl<'arena, 'env> EvalEnv<'arena, 'env> {
945
945
scope : & ' out_arena Scope < ' out_arena > ,
946
946
term : & Term < ' arena > ,
947
947
) -> TermOrValue < ' arena , ' out_arena > {
948
+ let builder = Builder :: new ( scope) ;
949
+
948
950
// Recurse to find the head of an elimination, checking if it's a
949
951
// metavariable. If so, check if it has a solution, and then apply
950
952
// eliminations to the solution in turn on our way back out.
@@ -971,11 +973,11 @@ impl<'arena, 'env> EvalEnv<'arena, 'env> {
971
973
972
974
Term :: FunApp ( span, plicity, head_expr, arg_expr) => {
973
975
match self . unfold_meta_var_spines ( scope, head_expr) {
974
- TermOrValue :: Term ( head_expr) => TermOrValue :: Term ( Term :: FunApp (
976
+ TermOrValue :: Term ( head_expr) => TermOrValue :: Term ( builder . fun_app (
975
977
* span,
976
978
* plicity,
977
- scope . to_scope ( head_expr) ,
978
- scope . to_scope ( self . unfold_metas ( scope, arg_expr) ) ,
979
+ head_expr,
980
+ self . unfold_metas ( scope, arg_expr) ,
979
981
) ) ,
980
982
TermOrValue :: Value ( head_expr) => {
981
983
let arg_expr = self . eval ( arg_expr) ;
@@ -985,28 +987,29 @@ impl<'arena, 'env> EvalEnv<'arena, 'env> {
985
987
}
986
988
Term :: RecordProj ( span, head_expr, label) => {
987
989
match self . unfold_meta_var_spines ( scope, head_expr) {
988
- TermOrValue :: Term ( head_expr) => TermOrValue :: Term ( Term :: RecordProj (
989
- * span,
990
- scope. to_scope ( head_expr) ,
991
- * label,
992
- ) ) ,
990
+ TermOrValue :: Term ( head_expr) => {
991
+ TermOrValue :: Term ( builder. record_proj ( * span, head_expr, * label) )
992
+ }
993
993
TermOrValue :: Value ( head_expr) => {
994
994
TermOrValue :: Value ( self . elim_env . record_proj ( head_expr, * label) )
995
995
}
996
996
}
997
997
}
998
998
Term :: ConstMatch ( span, head_expr, branches, default_branch) => {
999
999
match self . unfold_meta_var_spines ( scope, head_expr) {
1000
- TermOrValue :: Term ( head_expr) => TermOrValue :: Term ( Term :: ConstMatch (
1001
- * span,
1002
- scope. to_scope ( head_expr) ,
1003
- scope. to_scope_from_iter (
1004
- ( branches. iter ( ) )
1005
- . map ( |( r#const, expr) | ( * r#const, self . unfold_metas ( scope, expr) ) ) ,
1000
+ TermOrValue :: Term ( head_expr) => TermOrValue :: Term (
1001
+ builder. const_match (
1002
+ * span,
1003
+ head_expr,
1004
+ scope. to_scope_from_iter (
1005
+ branches. iter ( ) . map ( |( r#const, expr) | {
1006
+ ( * r#const, self . unfold_metas ( scope, expr) )
1007
+ } ) ,
1008
+ ) ,
1009
+ default_branch
1010
+ . map ( |( name, expr) | ( name, self . unfold_bound_metas ( scope, expr) ) ) ,
1006
1011
) ,
1007
- default_branch
1008
- . map ( |( name, expr) | ( name, self . unfold_bound_metas ( scope, expr) ) ) ,
1009
- ) ) ,
1012
+ ) ,
1010
1013
TermOrValue :: Value ( head_expr) => {
1011
1014
let branches =
1012
1015
Branches :: new ( self . local_exprs . clone ( ) , branches, * default_branch) ;
@@ -1023,14 +1026,14 @@ impl<'arena, 'env> EvalEnv<'arena, 'env> {
1023
1026
& mut self ,
1024
1027
scope : & ' out_arena Scope < ' out_arena > ,
1025
1028
term : & Term < ' arena > ,
1026
- ) -> & ' out_arena Term < ' out_arena > {
1029
+ ) -> Term < ' out_arena > {
1027
1030
let var = Arc :: new ( Value :: local_var ( self . local_exprs . len ( ) . next_level ( ) ) ) ;
1028
1031
1029
1032
self . local_exprs . push ( Spanned :: empty ( var) ) ;
1030
1033
let term = self . unfold_metas ( scope, term) ;
1031
1034
self . local_exprs . pop ( ) ;
1032
1035
1033
- scope . to_scope ( term)
1036
+ term
1034
1037
}
1035
1038
1036
1039
fn unfold_telescope_metas < ' out_arena > (
0 commit comments