Skip to content

Commit e5660f7

Browse files
committed
rename coretraits to CoreX
1 parent 5c42c9c commit e5660f7

File tree

10 files changed

+264
-256
lines changed

10 files changed

+264
-256
lines changed

crates/formality-core/src/binder.rs

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,35 @@ use lazy_static::lazy_static;
77

88
use crate::{
99
cast::{Downcast, DowncastFrom, DowncastTo, To, Upcast, UpcastFrom},
10-
fold::Fold,
10+
fold::CoreFold,
1111
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,
1616
Fallible,
1717
};
1818

1919
#[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>>,
2222
term: T,
2323
}
2424

25-
impl<L: Language, T: Fold<L>> Binder<L, T> {
25+
impl<L: Language, T: CoreFold<L>> CoreBinder<L, T> {
2626
/// Accesses the contents of the binder.
2727
///
2828
/// The variables inside will be renamed to fresh var indices
2929
/// that do not alias any other indices seen during this computation.
3030
///
3131
/// 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
3434
.kinds
3535
.iter()
3636
.zip(0..)
3737
.map(|(kind, index)| {
38-
let old_bound_var = BoundVar {
38+
let old_bound_var = CoreBoundVar {
3939
debruijn: Some(DebruijnIndex::INNERMOST),
4040
var_index: VarIndex { index },
4141
kind: *kind,
@@ -49,21 +49,21 @@ impl<L: Language, T: Fold<L>> Binder<L, T> {
4949
}
5050

5151
pub fn dummy(term: T) -> Self {
52-
let v: Vec<Variable<L>> = vec![];
52+
let v: Vec<CoreVariable<L>> = vec![];
5353
Self::new(v, term)
5454
}
5555

5656
/// Given a set of variables (X, Y, Z) and a term referecing some subset of them,
5757
/// 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
6161
.iter()
6262
.zip(0..)
6363
.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();
6565
assert!(old_bound_var.is_free());
66-
let new_bound_var: Parameter<L> = BoundVar {
66+
let new_bound_var: CoreParameter<L> = CoreBoundVar {
6767
debruijn: Some(DebruijnIndex::INNERMOST),
6868
var_index: VarIndex { index },
6969
kind: old_bound_var.kind(),
@@ -74,24 +74,24 @@ impl<L: Language, T: Fold<L>> Binder<L, T> {
7474
.unzip();
7575

7676
let term = substitution.apply(&term);
77-
Binder { kinds, term }
77+
CoreBinder { kinds, term }
7878
}
7979

8080
/// Given a set of variables (X, Y, Z) and a term referecing some subset of them,
8181
/// 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();
8484
let fv = term.free_variables();
8585
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)
8888
}
8989

90-
pub fn into<U>(self) -> Binder<L, U>
90+
pub fn into<U>(self) -> CoreBinder<L, U>
9191
where
9292
T: Into<U>,
9393
{
94-
Binder {
94+
CoreBinder {
9595
kinds: self.kinds,
9696
term: self.term.into(),
9797
}
@@ -108,13 +108,13 @@ impl<L: Language, T: Fold<L>> Binder<L, T> {
108108

109109
/// Instantiate the binder with the given parameters, returning an err if the parameters
110110
/// 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> {
112112
if parameters.len() != self.kinds.len() {
113113
bail!("wrong number of parameters");
114114
}
115115

116116
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();
118118
if p.kind() != *k {
119119
bail!(
120120
"parameter {i} has kind {:?} but should have kind {:?}",
@@ -128,16 +128,16 @@ impl<L: Language, T: Fold<L>> Binder<L, T> {
128128
}
129129

130130
/// 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
133133
.kinds
134134
.iter()
135135
.zip(0..)
136136
.map(|(&kind, index)| op(kind, VarIndex { index }))
137137
.collect();
138138

139139
self.term.substitute(&mut |var| match var {
140-
Variable::BoundVar(BoundVar {
140+
CoreVariable::BoundVar(CoreBoundVar {
141141
debruijn: Some(DebruijnIndex::INNERMOST),
142142
var_index,
143143
kind: _,
@@ -154,35 +154,35 @@ impl<L: Language, T: Fold<L>> Binder<L, T> {
154154
}
155155

156156
/// Returns the kinds of each variable bound by this binder
157-
pub fn kinds(&self) -> &[Kind<L>] {
157+
pub fn kinds(&self) -> &[CoreKind<L>] {
158158
&self.kinds
159159
}
160160

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> {
162162
let (vars, t) = self.open();
163163
let u = op(t);
164-
Binder::new(vars, u)
164+
CoreBinder::new(vars, u)
165165
}
166166
}
167167

168168
/// Creates a fresh bound var of the given kind that is not yet part of a binder.
169169
/// 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> {
171171
lazy_static! {
172172
static ref COUNTER: AtomicUsize = AtomicUsize::new(0);
173173
}
174174

175175
let index = COUNTER.fetch_add(1, Ordering::SeqCst);
176176
let var_index = VarIndex { index };
177-
BoundVar {
177+
CoreBoundVar {
178178
debruijn: None,
179179
var_index,
180180
kind,
181181
}
182182
}
183183

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>> {
186186
self.term.free_variables()
187187
}
188188

@@ -195,7 +195,7 @@ impl<L: Language, T: Visit<L>> Visit<L> for Binder<L, T> {
195195
}
196196
}
197197

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> {
199199
fn substitute(&self, substitution_fn: SubstitutionFn<'_, L>) -> Self {
200200
let term = self.term.substitute(&mut |v| {
201201
// 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> {
210210
Some(parameter.shift_in())
211211
});
212212

213-
Binder {
213+
CoreBinder {
214214
kinds: self.kinds.clone(),
215215
term,
216216
}
217217
}
218218

219219
fn shift_in(&self) -> Self {
220220
let term = self.term.shift_in();
221-
Binder {
221+
CoreBinder {
222222
kinds: self.kinds.clone(),
223223
term,
224224
}
225225
}
226226
}
227227

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>
229229
where
230230
T: Clone,
231231
U: Clone,
232232
T: Upcast<U>,
233233
{
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 {
237237
kinds,
238238
term: term.upcast(),
239239
}
240240
}
241241
}
242242

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>
244244
where
245245
T: DowncastFrom<U>,
246246
{
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;
249249
let term = term.downcast()?;
250-
Some(Binder {
250+
Some(CoreBinder {
251251
kinds: kinds.clone(),
252252
term,
253253
})
254254
}
255255
}
256256

257-
impl<L: Language, T> std::fmt::Debug for Binder<L, T>
257+
impl<L: Language, T> std::fmt::Debug for CoreBinder<L, T>
258258
where
259259
T: std::fmt::Debug,
260260
{

crates/formality-core/src/fold.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ use std::sync::Arc;
33
use crate::{
44
cast::Upcast,
55
collections::Set,
6-
language::{HasKind, Language, Parameter},
7-
variable::Variable,
8-
visit::Visit,
6+
language::{CoreParameter, HasKind, Language},
7+
variable::CoreVariable,
8+
visit::CoreVisit,
99
};
1010

1111
/// Invoked for each variable that we find when folding, ignoring variables bound by binders
1212
/// that we traverse. The arguments are as follows:
1313
///
1414
/// * ParameterKind -- the kind of term in which the variable appeared (type vs lifetime, etc)
1515
/// * Variable -- the variable we encountered
16-
#[allow(type_alias_bounds)]
17-
pub type SubstitutionFn<'a, L: Language> = &'a mut dyn FnMut(Variable<L>) -> Option<Parameter<L>>;
16+
pub type SubstitutionFn<'a, L: Language> =
17+
&'a mut dyn FnMut(CoreVariable<L>) -> Option<CoreParameter<L>>;
1818

19-
pub trait Fold<L: Language>: Sized + Visit<L> {
19+
pub trait CoreFold<L: Language>: Sized + CoreVisit<L> {
2020
/// Replace uses of variables with values from the substitution.
2121
fn substitute(&self, substitution_fn: SubstitutionFn<'_, L>) -> Self;
2222

@@ -26,64 +26,68 @@ pub trait Fold<L: Language>: Sized + Visit<L> {
2626
}
2727

2828
/// Replace all appearances of free variable `v` with `p`.
29-
fn replace_free_var(&self, v: impl Upcast<Variable<L>>, p: impl Upcast<Parameter<L>>) -> Self {
30-
let v: Variable<L> = v.upcast();
31-
let p: Parameter<L> = p.upcast();
29+
fn replace_free_var(
30+
&self,
31+
v: impl Upcast<CoreVariable<L>>,
32+
p: impl Upcast<CoreParameter<L>>,
33+
) -> Self {
34+
let v: CoreVariable<L> = v.upcast();
35+
let p: CoreParameter<L> = p.upcast();
3236
assert!(v.is_free());
3337
assert!(v.kind() == p.kind());
3438
self.substitute(&mut |v1| if v == v1 { Some(p.clone()) } else { None })
3539
}
3640
}
3741

38-
impl<L: Language, T: Fold<L>> Fold<L> for Vec<T> {
42+
impl<L: Language, T: CoreFold<L>> CoreFold<L> for Vec<T> {
3943
fn substitute(&self, substitution_fn: SubstitutionFn<'_, L>) -> Self {
4044
self.iter().map(|e| e.substitute(substitution_fn)).collect()
4145
}
4246
}
4347

44-
impl<L: Language, T: Fold<L> + Ord> Fold<L> for Set<T> {
48+
impl<L: Language, T: CoreFold<L> + Ord> CoreFold<L> for Set<T> {
4549
fn substitute(&self, substitution_fn: SubstitutionFn<'_, L>) -> Self {
4650
self.iter().map(|e| e.substitute(substitution_fn)).collect()
4751
}
4852
}
4953

50-
impl<L: Language, T: Fold<L>> Fold<L> for Option<T> {
54+
impl<L: Language, T: CoreFold<L>> CoreFold<L> for Option<T> {
5155
fn substitute(&self, substitution_fn: SubstitutionFn<'_, L>) -> Self {
5256
self.as_ref().map(|e| e.substitute(substitution_fn))
5357
}
5458
}
5559

56-
impl<L: Language, T: Fold<L>> Fold<L> for Arc<T> {
60+
impl<L: Language, T: CoreFold<L>> CoreFold<L> for Arc<T> {
5761
fn substitute(&self, substitution_fn: SubstitutionFn<'_, L>) -> Self {
5862
let data = T::substitute(self, substitution_fn);
5963
Arc::new(data)
6064
}
6165
}
6266

63-
impl<L: Language> Fold<L> for usize {
67+
impl<L: Language> CoreFold<L> for usize {
6468
fn substitute(&self, _substitution_fn: SubstitutionFn<'_, L>) -> Self {
6569
*self
6670
}
6771
}
6872

69-
impl<L: Language> Fold<L> for u32 {
73+
impl<L: Language> CoreFold<L> for u32 {
7074
fn substitute(&self, _substitution_fn: SubstitutionFn<'_, L>) -> Self {
7175
*self
7276
}
7377
}
7478

75-
impl<L: Language> Fold<L> for () {
79+
impl<L: Language> CoreFold<L> for () {
7680
fn substitute(&self, _substitution_fn: SubstitutionFn<'_, L>) -> Self {}
7781
}
7882

79-
impl<L: Language, A: Fold<L>, B: Fold<L>> Fold<L> for (A, B) {
83+
impl<L: Language, A: CoreFold<L>, B: CoreFold<L>> CoreFold<L> for (A, B) {
8084
fn substitute(&self, substitution_fn: SubstitutionFn<'_, L>) -> Self {
8185
let (a, b) = self;
8286
(a.substitute(substitution_fn), b.substitute(substitution_fn))
8387
}
8488
}
8589

86-
impl<L: Language, A: Fold<L>, B: Fold<L>, C: Fold<L>> Fold<L> for (A, B, C) {
90+
impl<L: Language, A: CoreFold<L>, B: CoreFold<L>, C: CoreFold<L>> CoreFold<L> for (A, B, C) {
8791
fn substitute(&self, substitution_fn: SubstitutionFn<'_, L>) -> Self {
8892
let (a, b, c) = self;
8993
(

0 commit comments

Comments
 (0)