Skip to content

Commit 3f7b4c2

Browse files
Don't generate expressions with app(lambda, _) since they are equivalent to the reduced expression
1 parent 0c76cc6 commit 3f7b4c2

2 files changed

Lines changed: 30 additions & 16 deletions

File tree

src/language/enumerator.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl<'src> PossibleExpressions<'src, Expr<'src>> {
142142
let mut stack: Vec<HashedExpr<_>> = self
143143
.terms(
144144
t,
145-
false,
145+
true,
146146
std::iter::empty(),
147147
possible_applications(t, std::iter::empty()),
148148
)
@@ -305,7 +305,9 @@ impl<'src> ExprWrapper<'src, Expr<'src>> {
305305
{
306306
let mut terms = possibles.terms(
307307
typ,
308-
false,
308+
//the function of a application shouldn't have lambdas, since otherwise we could just
309+
//apply the argument there.
310+
i != 0 || !matches!(this.h.expr, LambdaExpr::Application { .. }),
309311
this.variables
310312
.iter()
311313
.rev()
@@ -554,6 +556,8 @@ impl<'src, T: LambdaLanguageOfThought + Clone> From<FinishedExpr<'src, T>>
554556

555557
#[cfg(test)]
556558
mod test {
559+
use ahash::HashSetExt;
560+
557561
use super::*;
558562

559563
#[test]
@@ -582,11 +586,24 @@ mod test {
582586
LambdaType::from_string("<<a,t>,t>").unwrap(),
583587
];
584588
for t in t {
585-
for x in possibles.enumerator(&t, 4) {
589+
println!("{t}");
590+
let mut count = 0;
591+
let mut pool_set = HashSet::new();
592+
let mut reduced_pool_set = HashSet::new();
593+
594+
for mut x in possibles.enumerator(&t, 6) {
586595
println!("{x}");
587596
let o = x.get_type()?;
588597
assert_eq!(o, t);
598+
count += 1;
599+
pool_set.insert(x.clone());
600+
x.reduce()?;
601+
x.cleanup();
602+
reduced_pool_set.insert(x);
589603
}
604+
assert!(count > 0);
605+
assert_eq!(pool_set.len(), count);
606+
assert_eq!(pool_set.len(), reduced_pool_set.len());
590607
}
591608
Ok(())
592609
}

src/language/mutations/samplers.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,23 +157,20 @@ impl<'src, T: LambdaLanguageOfThought + Clone> PossibleExpressions<'src, T> {
157157
pub(crate) fn terms<'a>(
158158
&'a self,
159159
lambda_type: &LambdaType,
160-
is_subformula: bool,
160+
include_lambdas: bool,
161161
variables: impl Iterator<Item = LambdaExpr<'src, T>>,
162162
applications: impl Iterator<Item = (LambdaType, LambdaType)>,
163163
) -> Vec<PossibleExpr<'a, 'src, T>> {
164164
let mut possibilities = vec![];
165-
if !is_subformula {
166-
if let Some(x) = self.expressions.get(lambda_type).map(|x| {
167-
x.iter()
168-
.flat_map(|(_, v)| v.iter().map(PossibleExpr::new_borrowed))
169-
}) {
170-
possibilities.extend(x);
171-
}
172-
173-
if let Ok((lhs, _)) = lambda_type.split() {
174-
let e = PossibleExpr::new_owned(LambdaExpr::Lambda(LambdaExprRef(0), lhs.clone()));
175-
possibilities.push(e);
176-
}
165+
if let Some(x) = self.expressions.get(lambda_type).map(|x| {
166+
x.iter()
167+
.flat_map(|(_, v)| v.iter().map(PossibleExpr::new_borrowed))
168+
}) {
169+
possibilities.extend(x);
170+
}
171+
if include_lambdas && let Ok((lhs, _)) = lambda_type.split() {
172+
let e = PossibleExpr::new_owned(LambdaExpr::Lambda(LambdaExprRef(0), lhs.clone()));
173+
possibilities.push(e);
177174
}
178175
possibilities.extend(variables.map(PossibleExpr::new_owned));
179176
possibilities.extend(

0 commit comments

Comments
 (0)