@@ -7,35 +7,35 @@ use lazy_static::lazy_static;
7
7
8
8
use crate :: {
9
9
cast:: { Downcast , DowncastFrom , DowncastTo , To , Upcast , UpcastFrom } ,
10
- fold:: Fold ,
10
+ fold:: CoreFold ,
11
11
fold:: SubstitutionFn ,
12
- language:: { HasKind , Kind , Language , Parameter } ,
13
- substitution:: Substitution ,
14
- variable:: { BoundVar , DebruijnIndex , VarIndex , Variable } ,
15
- visit:: Visit ,
12
+ language:: { CoreKind , CoreParameter , HasKind , Language } ,
13
+ substitution:: CoreSubstitution ,
14
+ variable:: { CoreBoundVar , CoreVariable , DebruijnIndex , VarIndex } ,
15
+ visit:: CoreVisit ,
16
16
Fallible ,
17
17
} ;
18
18
19
19
#[ derive( Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Default ) ]
20
- pub struct Binder < L : Language , T > {
21
- kinds : Vec < Kind < L > > ,
20
+ pub struct CoreBinder < L : Language , T > {
21
+ kinds : Vec < CoreKind < L > > ,
22
22
term : T ,
23
23
}
24
24
25
- impl < L : Language , T : Fold < L > > Binder < L , T > {
25
+ impl < L : Language , T : CoreFold < L > > CoreBinder < L , T > {
26
26
/// Accesses the contents of the binder.
27
27
///
28
28
/// The variables inside will be renamed to fresh var indices
29
29
/// that do not alias any other indices seen during this computation.
30
30
///
31
31
/// The expectation is that you will create a term and use `Binder::new`.
32
- pub fn open ( & self ) -> ( Vec < BoundVar < L > > , T ) {
33
- let ( bound_vars, substitution) : ( Vec < BoundVar < L > > , Substitution < L > ) = self
32
+ pub fn open ( & self ) -> ( Vec < CoreBoundVar < L > > , T ) {
33
+ let ( bound_vars, substitution) : ( Vec < CoreBoundVar < L > > , CoreSubstitution < L > ) = self
34
34
. kinds
35
35
. iter ( )
36
36
. zip ( 0 ..)
37
37
. map ( |( kind, index) | {
38
- let old_bound_var = BoundVar {
38
+ let old_bound_var = CoreBoundVar {
39
39
debruijn : Some ( DebruijnIndex :: INNERMOST ) ,
40
40
var_index : VarIndex { index } ,
41
41
kind : * kind,
@@ -49,21 +49,21 @@ impl<L: Language, T: Fold<L>> Binder<L, T> {
49
49
}
50
50
51
51
pub fn dummy ( term : T ) -> Self {
52
- let v: Vec < Variable < L > > = vec ! [ ] ;
52
+ let v: Vec < CoreVariable < L > > = vec ! [ ] ;
53
53
Self :: new ( v, term)
54
54
}
55
55
56
56
/// Given a set of variables (X, Y, Z) and a term referecing some subset of them,
57
57
/// create a binder where exactly those variables are bound (even the ones not used).
58
- pub fn new ( variables : impl Upcast < Vec < Variable < L > > > , term : T ) -> Self {
59
- let variables: Vec < Variable < L > > = variables. upcast ( ) ;
60
- let ( kinds, substitution) : ( Vec < Kind < L > > , Substitution < L > ) = variables
58
+ pub fn new ( variables : impl Upcast < Vec < CoreVariable < L > > > , term : T ) -> Self {
59
+ let variables: Vec < CoreVariable < L > > = variables. upcast ( ) ;
60
+ let ( kinds, substitution) : ( Vec < CoreKind < L > > , CoreSubstitution < L > ) = variables
61
61
. iter ( )
62
62
. zip ( 0 ..)
63
63
. map ( |( old_bound_var, index) | {
64
- let old_bound_var: Variable < L > = old_bound_var. upcast ( ) ;
64
+ let old_bound_var: CoreVariable < L > = old_bound_var. upcast ( ) ;
65
65
assert ! ( old_bound_var. is_free( ) ) ;
66
- let new_bound_var: Parameter < L > = BoundVar {
66
+ let new_bound_var: CoreParameter < L > = CoreBoundVar {
67
67
debruijn : Some ( DebruijnIndex :: INNERMOST ) ,
68
68
var_index : VarIndex { index } ,
69
69
kind : old_bound_var. kind ( ) ,
@@ -74,24 +74,24 @@ impl<L: Language, T: Fold<L>> Binder<L, T> {
74
74
. unzip ( ) ;
75
75
76
76
let term = substitution. apply ( & term) ;
77
- Binder { kinds, term }
77
+ CoreBinder { kinds, term }
78
78
}
79
79
80
80
/// Given a set of variables (X, Y, Z) and a term referecing some subset of them,
81
81
/// create a binder for just those variables that are mentioned.
82
- pub fn mentioned ( variables : impl Upcast < Vec < Variable < L > > > , term : T ) -> Self {
83
- let mut variables: Vec < Variable < L > > = variables. upcast ( ) ;
82
+ pub fn mentioned ( variables : impl Upcast < Vec < CoreVariable < L > > > , term : T ) -> Self {
83
+ let mut variables: Vec < CoreVariable < L > > = variables. upcast ( ) ;
84
84
let fv = term. free_variables ( ) ;
85
85
variables. retain ( |v| fv. contains ( v) ) ;
86
- let variables: Vec < Variable < L > > = variables. into_iter ( ) . collect ( ) ;
87
- Binder :: new ( variables, term)
86
+ let variables: Vec < CoreVariable < L > > = variables. into_iter ( ) . collect ( ) ;
87
+ CoreBinder :: new ( variables, term)
88
88
}
89
89
90
- pub fn into < U > ( self ) -> Binder < L , U >
90
+ pub fn into < U > ( self ) -> CoreBinder < L , U >
91
91
where
92
92
T : Into < U > ,
93
93
{
94
- Binder {
94
+ CoreBinder {
95
95
kinds : self . kinds ,
96
96
term : self . term . into ( ) ,
97
97
}
@@ -108,13 +108,13 @@ impl<L: Language, T: Fold<L>> Binder<L, T> {
108
108
109
109
/// Instantiate the binder with the given parameters, returning an err if the parameters
110
110
/// are the wrong number or ill-kinded.
111
- pub fn instantiate_with ( & self , parameters : & [ impl Upcast < Parameter < L > > ] ) -> Fallible < T > {
111
+ pub fn instantiate_with ( & self , parameters : & [ impl Upcast < CoreParameter < L > > ] ) -> Fallible < T > {
112
112
if parameters. len ( ) != self . kinds . len ( ) {
113
113
bail ! ( "wrong number of parameters" ) ;
114
114
}
115
115
116
116
for ( ( p, k) , i) in parameters. iter ( ) . zip ( & self . kinds ) . zip ( 0 ..) {
117
- let p: Parameter < L > = p. upcast ( ) ;
117
+ let p: CoreParameter < L > = p. upcast ( ) ;
118
118
if p. kind ( ) != * k {
119
119
bail ! (
120
120
"parameter {i} has kind {:?} but should have kind {:?}" ,
@@ -128,16 +128,16 @@ impl<L: Language, T: Fold<L>> Binder<L, T> {
128
128
}
129
129
130
130
/// Instantiate the term, replacing each bound variable with `op(i)`.
131
- pub fn instantiate ( & self , mut op : impl FnMut ( Kind < L > , VarIndex ) -> Parameter < L > ) -> T {
132
- let substitution: Vec < Parameter < L > > = self
131
+ pub fn instantiate ( & self , mut op : impl FnMut ( CoreKind < L > , VarIndex ) -> CoreParameter < L > ) -> T {
132
+ let substitution: Vec < CoreParameter < L > > = self
133
133
. kinds
134
134
. iter ( )
135
135
. zip ( 0 ..)
136
136
. map ( |( & kind, index) | op ( kind, VarIndex { index } ) )
137
137
. collect ( ) ;
138
138
139
139
self . term . substitute ( & mut |var| match var {
140
- Variable :: BoundVar ( BoundVar {
140
+ CoreVariable :: BoundVar ( CoreBoundVar {
141
141
debruijn : Some ( DebruijnIndex :: INNERMOST ) ,
142
142
var_index,
143
143
kind : _,
@@ -154,35 +154,35 @@ impl<L: Language, T: Fold<L>> Binder<L, T> {
154
154
}
155
155
156
156
/// Returns the kinds of each variable bound by this binder
157
- pub fn kinds ( & self ) -> & [ Kind < L > ] {
157
+ pub fn kinds ( & self ) -> & [ CoreKind < L > ] {
158
158
& self . kinds
159
159
}
160
160
161
- pub fn map < U : Fold < L > > ( & self , op : impl FnOnce ( T ) -> U ) -> Binder < L , U > {
161
+ pub fn map < U : CoreFold < L > > ( & self , op : impl FnOnce ( T ) -> U ) -> CoreBinder < L , U > {
162
162
let ( vars, t) = self . open ( ) ;
163
163
let u = op ( t) ;
164
- Binder :: new ( vars, u)
164
+ CoreBinder :: new ( vars, u)
165
165
}
166
166
}
167
167
168
168
/// Creates a fresh bound var of the given kind that is not yet part of a binder.
169
169
/// You can put this into a term and then use `Binder::new`.
170
- pub fn fresh_bound_var < L : Language > ( kind : Kind < L > ) -> BoundVar < L > {
170
+ pub fn fresh_bound_var < L : Language > ( kind : CoreKind < L > ) -> CoreBoundVar < L > {
171
171
lazy_static ! {
172
172
static ref COUNTER : AtomicUsize = AtomicUsize :: new( 0 ) ;
173
173
}
174
174
175
175
let index = COUNTER . fetch_add ( 1 , Ordering :: SeqCst ) ;
176
176
let var_index = VarIndex { index } ;
177
- BoundVar {
177
+ CoreBoundVar {
178
178
debruijn : None ,
179
179
var_index,
180
180
kind,
181
181
}
182
182
}
183
183
184
- impl < L : Language , T : Visit < L > > Visit < L > for Binder < L , T > {
185
- fn free_variables ( & self ) -> Vec < Variable < L > > {
184
+ impl < L : Language , T : CoreVisit < L > > CoreVisit < L > for CoreBinder < L , T > {
185
+ fn free_variables ( & self ) -> Vec < CoreVariable < L > > {
186
186
self . term . free_variables ( )
187
187
}
188
188
@@ -195,7 +195,7 @@ impl<L: Language, T: Visit<L>> Visit<L> for Binder<L, T> {
195
195
}
196
196
}
197
197
198
- impl < L : Language , T : Fold < L > > Fold < L > for Binder < L , T > {
198
+ impl < L : Language , T : CoreFold < L > > CoreFold < L > for CoreBinder < L , T > {
199
199
fn substitute ( & self , substitution_fn : SubstitutionFn < ' _ , L > ) -> Self {
200
200
let term = self . term . substitute ( & mut |v| {
201
201
// Shift this variable out through the binder. If that fails,
@@ -210,51 +210,51 @@ impl<L: Language, T: Fold<L>> Fold<L> for Binder<L, T> {
210
210
Some ( parameter. shift_in ( ) )
211
211
} ) ;
212
212
213
- Binder {
213
+ CoreBinder {
214
214
kinds : self . kinds . clone ( ) ,
215
215
term,
216
216
}
217
217
}
218
218
219
219
fn shift_in ( & self ) -> Self {
220
220
let term = self . term . shift_in ( ) ;
221
- Binder {
221
+ CoreBinder {
222
222
kinds : self . kinds . clone ( ) ,
223
223
term,
224
224
}
225
225
}
226
226
}
227
227
228
- impl < L : Language , T , U > UpcastFrom < Binder < L , T > > for Binder < L , U >
228
+ impl < L : Language , T , U > UpcastFrom < CoreBinder < L , T > > for CoreBinder < L , U >
229
229
where
230
230
T : Clone ,
231
231
U : Clone ,
232
232
T : Upcast < U > ,
233
233
{
234
- fn upcast_from ( term : Binder < L , T > ) -> Self {
235
- let Binder { kinds, term } = term;
236
- Binder {
234
+ fn upcast_from ( term : CoreBinder < L , T > ) -> Self {
235
+ let CoreBinder { kinds, term } = term;
236
+ CoreBinder {
237
237
kinds,
238
238
term : term. upcast ( ) ,
239
239
}
240
240
}
241
241
}
242
242
243
- impl < L : Language , T , U > DowncastTo < Binder < L , T > > for Binder < L , U >
243
+ impl < L : Language , T , U > DowncastTo < CoreBinder < L , T > > for CoreBinder < L , U >
244
244
where
245
245
T : DowncastFrom < U > ,
246
246
{
247
- fn downcast_to ( & self ) -> Option < Binder < L , T > > {
248
- let Binder { kinds, term } = self ;
247
+ fn downcast_to ( & self ) -> Option < CoreBinder < L , T > > {
248
+ let CoreBinder { kinds, term } = self ;
249
249
let term = term. downcast ( ) ?;
250
- Some ( Binder {
250
+ Some ( CoreBinder {
251
251
kinds : kinds. clone ( ) ,
252
252
term,
253
253
} )
254
254
}
255
255
}
256
256
257
- impl < L : Language , T > std:: fmt:: Debug for Binder < L , T >
257
+ impl < L : Language , T > std:: fmt:: Debug for CoreBinder < L , T >
258
258
where
259
259
T : std:: fmt:: Debug ,
260
260
{
0 commit comments