1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use crate::RustIrDatabase;
use cast::CastTo;
use chalk_ir::cast::Cast;
use chalk_ir::cast::Caster;
use chalk_ir::*;
use fold::shift::Shift;
use fold::Fold;
use interner::{HasInterner, Interner};
pub struct GoalBuilder<'i, I: Interner> {
db: &'i dyn RustIrDatabase<I>,
}
impl<'i, I: Interner> GoalBuilder<'i, I> {
pub fn new(db: &'i dyn RustIrDatabase<I>) -> Self {
GoalBuilder { db }
}
pub fn db(&self) -> &'i dyn RustIrDatabase<I> {
self.db
}
pub fn interner(&self) -> &'i I {
self.db.interner()
}
pub fn all<GS, G>(&mut self, goals: GS) -> Goal<I>
where
GS: IntoIterator<Item = G>,
G: CastTo<Goal<I>>,
{
Goal::all(self.interner(), goals.into_iter().casted(self.interner()))
}
pub fn implies<CS, C, G>(&mut self, clauses: CS, goal: impl FnOnce(&mut Self) -> G) -> Goal<I>
where
CS: IntoIterator<Item = C>,
C: CastTo<ProgramClause<I>>,
G: CastTo<Goal<I>>,
{
GoalData::Implies(
ProgramClauses::from_iter(self.interner(), clauses),
goal(self).cast(self.interner()),
)
.intern(self.interner())
}
pub fn forall<G, B, P>(
&mut self,
binders: &Binders<B>,
passthru: P,
body: fn(&mut Self, Substitution<I>, &B, P::Result) -> G,
) -> Goal<I>
where
B: Fold<I> + HasInterner<Interner = I>,
P: Fold<I>,
B::Result: std::fmt::Debug,
G: CastTo<Goal<I>>,
{
self.quantified(QuantifierKind::ForAll, binders, passthru, body)
}
pub fn exists<G, B, P>(
&mut self,
binders: &Binders<B>,
passthru: P,
body: fn(&mut Self, Substitution<I>, &B, P::Result) -> G,
) -> Goal<I>
where
B: Fold<I> + HasInterner<Interner = I>,
P: Fold<I>,
B::Result: std::fmt::Debug,
G: CastTo<Goal<I>>,
{
self.quantified(QuantifierKind::Exists, binders, passthru, body)
}
fn quantified<G, B, P>(
&mut self,
quantifier_kind: QuantifierKind,
binders: &Binders<B>,
passthru: P,
body: fn(&mut Self, Substitution<I>, &B, P::Result) -> G,
) -> Goal<I>
where
B: Fold<I> + HasInterner<Interner = I>,
P: Fold<I>,
B::Result: std::fmt::Debug,
G: CastTo<Goal<I>>,
{
let interner = self.interner();
let substitution = Substitution::from_iter(
interner,
binders
.binders
.iter(interner)
.enumerate()
.map(|p| p.to_generic_arg(interner)),
);
let passthru_shifted = passthru.shifted_in(self.interner());
let bound_goal = binders.map_ref(|bound_value| {
body(self, substitution, bound_value, passthru_shifted).cast(interner)
});
GoalData::Quantified(quantifier_kind, bound_goal).intern(interner)
}
}