mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-02 00:01:16 +00:00
remove some unused pub functions
This commit is contained in:
parent
773f647f48
commit
a1c12aa55a
2 changed files with 49 additions and 119 deletions
|
@ -38,10 +38,11 @@ pub fn or(left: Bool, right: Bool) -> Bool {
|
||||||
Or(Box::new(left), Box::new(right))
|
Or(Box::new(left), Box::new(right))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn any<I>(mut it: I) -> Bool
|
pub fn any<I>(iterable: I) -> Bool
|
||||||
where
|
where
|
||||||
I: Iterator<Item = Bool>,
|
I: IntoIterator<Item = Bool>,
|
||||||
{
|
{
|
||||||
|
let mut it = iterable.into_iter();
|
||||||
if let Some(first) = it.next() {
|
if let Some(first) = it.next() {
|
||||||
it.fold(first, |x, y| or(y, x))
|
it.fold(first, |x, y| or(y, x))
|
||||||
} else {
|
} else {
|
||||||
|
@ -49,20 +50,11 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn all(terms: Product<Bool>) -> Bool {
|
pub fn all<I>(iterable: I) -> Bool
|
||||||
let mut it = terms.into_iter();
|
|
||||||
|
|
||||||
if let Some(first) = it.next() {
|
|
||||||
it.fold(first, and)
|
|
||||||
} else {
|
|
||||||
One
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn all_iterator<I>(mut it: I) -> Bool
|
|
||||||
where
|
where
|
||||||
I: Iterator<Item = Bool>,
|
I: IntoIterator<Item = Bool>,
|
||||||
{
|
{
|
||||||
|
let mut it = iterable.into_iter();
|
||||||
if let Some(first) = it.next() {
|
if let Some(first) = it.next() {
|
||||||
it.fold(first, and)
|
it.fold(first, and)
|
||||||
} else {
|
} else {
|
||||||
|
@ -179,50 +171,27 @@ pub fn sop_to_term(sop: Sop) -> Bool {
|
||||||
|
|
||||||
accum.sort_by(|x, y| (y.len().cmp(&x.len())));
|
accum.sort_by(|x, y| (y.len().cmp(&x.len())));
|
||||||
|
|
||||||
println!("{:?}", &accum);
|
any(accum.into_iter().map(all))
|
||||||
|
|
||||||
any(accum.into_iter().map(|v| all_iterator(v.into_iter())))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn sop_to_term_vector(sop: Vec<Vec<Bool>>) -> Bool {
|
pub fn sop_to_term_vector(sop: Vec<Vec<Bool>>) -> Bool {
|
||||||
any(sop.into_iter().rev().map(|v| all_iterator(v.into_iter())))
|
any(sop.into_iter().rev().map(all))
|
||||||
}
|
|
||||||
|
|
||||||
pub fn simplify_sop(sop: Sop) -> Sop {
|
|
||||||
// sort by length longest to shortest (proxy for how many variables there are)
|
|
||||||
let mut sorted: Vec<ImSet<Bool>> = sop.clone().into_iter().collect();
|
|
||||||
|
|
||||||
sorted.sort_by(|x, y| y.len().cmp(&x.len()));
|
|
||||||
|
|
||||||
// filter out anything that is included in the remaining elements
|
|
||||||
let mut active = sop;
|
|
||||||
let mut result = ImSet::default();
|
|
||||||
for t in sorted {
|
|
||||||
if !(active.remove(&t).is_some() && included(all(t.clone()), sop_to_term(active.clone()))) {
|
|
||||||
result.insert(t);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn simplify_sop_vector(sorted: Vec<Vec<Bool>>) -> Vec<Vec<Bool>> {
|
pub fn simplify_sop_vector(sorted: Vec<Vec<Bool>>) -> Vec<Vec<Bool>> {
|
||||||
// sort by length longest to shortest (proxy for how many variables there are)
|
// sort by length longest to shortest (proxy for how many variables there are)
|
||||||
// sorted.sort_by(|x, y| y.len().cmp(&x.len()));
|
// sorted.sort_by(|x, y| y.len().cmp(&x.len()));
|
||||||
|
|
||||||
let sorted2 = sorted.clone();
|
let backup = sorted.clone();
|
||||||
|
|
||||||
let mut p: Vec<Vec<Bool>> = Vec::new();
|
let mut p: Vec<Vec<Bool>> = Vec::new();
|
||||||
|
|
||||||
for (i, t) in sorted.into_iter().enumerate() {
|
for (i, t) in sorted.into_iter().enumerate() {
|
||||||
let ts = &sorted2[i + 1..];
|
let ts = &backup[i + 1..];
|
||||||
ts.to_vec().extend(p.clone());
|
ts.to_vec().extend(p.clone());
|
||||||
|
|
||||||
if included(
|
if included(all(t.clone()), sop_to_term_vector(ts.to_vec())) {
|
||||||
all_iterator(t.clone().into_iter()).clone(),
|
|
||||||
sop_to_term_vector(ts.to_vec()),
|
|
||||||
) {
|
|
||||||
// do nothing
|
// do nothing
|
||||||
} else {
|
} else {
|
||||||
p.insert(0, t.into_iter().collect());
|
p.insert(0, t.into_iter().collect());
|
||||||
|
@ -232,21 +201,6 @@ pub fn simplify_sop_vector(sorted: Vec<Vec<Bool>>) -> Vec<Vec<Bool>> {
|
||||||
p
|
p
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
simplify_sop :: BooleanAlgebra a => Sum (Product a) -> Sum (Product a)
|
|
||||||
simplify_sop terms =
|
|
||||||
let
|
|
||||||
sorted = (sortBy (\x y -> compare (length y) (length x)) terms)
|
|
||||||
in
|
|
||||||
f sorted []
|
|
||||||
where
|
|
||||||
f [] p = p
|
|
||||||
f (t:ts) p =
|
|
||||||
if list_to_conj t `included` sop_to_term (ts ++ p)
|
|
||||||
then f ts p
|
|
||||||
else f ts (t:p)
|
|
||||||
*/
|
|
||||||
|
|
||||||
/// Blake canonical form
|
/// Blake canonical form
|
||||||
pub fn bcf(sop: Sop) -> Vec<Vec<Bool>> {
|
pub fn bcf(sop: Sop) -> Vec<Vec<Bool>> {
|
||||||
absorptive(syllogistic(sop))
|
absorptive(syllogistic(sop))
|
||||||
|
@ -275,16 +229,8 @@ pub fn syllogistic(terms: Sop) -> Sop {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
absorptive :: BooleanAlgebra a => Sum (Product a) -> Sum (Product a)
|
|
||||||
absorptive p = f p p
|
|
||||||
where
|
|
||||||
f [] p = p
|
|
||||||
f (t:ts) p = f ts (t : filter (not . absorbs t) p)
|
|
||||||
*/
|
|
||||||
|
|
||||||
/// Absorption (apply the identify p + pq = p)
|
/// Absorption (apply the identify p + pq = p)
|
||||||
pub fn absorptive(sop: Sop) -> Vec<Vec<Bool>> {
|
fn absorptive(sop: Sop) -> Vec<Vec<Bool>> {
|
||||||
let mut accum: Vec<Vec<Bool>> = Vec::new();
|
let mut accum: Vec<Vec<Bool>> = Vec::new();
|
||||||
|
|
||||||
for s in sop {
|
for s in sop {
|
||||||
|
@ -302,8 +248,6 @@ pub fn absorptive_vector(mut accum: Vec<Vec<Bool>>) -> Vec<Vec<Bool>> {
|
||||||
accum.insert(0, product.clone());
|
accum.insert(0, product.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
// accum.reverse();
|
|
||||||
|
|
||||||
accum
|
accum
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -311,12 +255,6 @@ pub fn absorbs_vector(p: &[Bool], q: &[Bool]) -> bool {
|
||||||
p.iter().all(|x| q.contains(x))
|
p.iter().all(|x| q.contains(x))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Does p absorb q? (can we replace p + q by p?)
|
|
||||||
/// TODO investigate: either the comment or the implementation is wrong I think?
|
|
||||||
pub fn absorbs(p: &Product<Bool>, q: &Product<Bool>) -> bool {
|
|
||||||
p.iter().all(|x| q.contains(x))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn consensus(p: &Product<Bool>, q: &Product<Bool>) -> Option<Product<Bool>> {
|
fn consensus(p: &Product<Bool>, q: &Product<Bool>) -> Option<Product<Bool>> {
|
||||||
let mut it = oppositions(p, q).into_iter();
|
let mut it = oppositions(p, q).into_iter();
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,9 @@ mod test_boolean_algebra {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn absorbs_eq(a: ImSet<Bool>, b: ImSet<Bool>, expected: bool) {
|
fn absorbs_eq(a: ImSet<Bool>, b: ImSet<Bool>, expected: bool) {
|
||||||
assert_eq!(boolean_algebra::absorbs(&a, &b), expected);
|
let x: Vec<Bool> = a.into_iter().collect();
|
||||||
|
let y: Vec<Bool> = b.into_iter().collect();
|
||||||
|
assert_eq!(boolean_algebra::absorbs_vector(&x, &y), expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn vecs_to_sets(input: Vec<Vec<Bool>>) -> ImSet<ImSet<Bool>> {
|
fn vecs_to_sets(input: Vec<Vec<Bool>>) -> ImSet<ImSet<Bool>> {
|
||||||
|
@ -53,7 +55,8 @@ mod test_boolean_algebra {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn absorptive_eq(actual: Vec<Vec<Bool>>, expected: Vec<Vec<Bool>>) {
|
fn absorptive_eq(actual: Vec<Vec<Bool>>, expected: Vec<Vec<Bool>>) {
|
||||||
let actual_vec: ImSet<ImSet<Bool>> = vecs_to_sets(actual);
|
let result = boolean_algebra::absorptive_vector(actual);
|
||||||
|
let actual_vec: ImSet<ImSet<Bool>> = vecs_to_sets(result);
|
||||||
let expected_vec: ImSet<ImSet<Bool>> = vecs_to_sets(expected);
|
let expected_vec: ImSet<ImSet<Bool>> = vecs_to_sets(expected);
|
||||||
assert_eq!(actual_vec, expected_vec);
|
assert_eq!(actual_vec, expected_vec);
|
||||||
}
|
}
|
||||||
|
@ -640,7 +643,7 @@ mod test_boolean_algebra {
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
boolean_algebra::simplify_sop(
|
boolean_algebra::simplify_sop_vector(
|
||||||
vec![
|
vec![
|
||||||
vec![Variable(a), Variable(d)].into(),
|
vec![Variable(a), Variable(d)].into(),
|
||||||
vec![Variable(a), Variable(c)].into(),
|
vec![Variable(a), Variable(c)].into(),
|
||||||
|
@ -652,12 +655,11 @@ mod test_boolean_algebra {
|
||||||
.into()
|
.into()
|
||||||
),
|
),
|
||||||
vec![
|
vec![
|
||||||
vec![Bool::not(Variable(b)), Variable(c)].into(),
|
vec![Bool::not(Variable(b)), Variable(c)],
|
||||||
vec![Bool::not(Variable(b)), Variable(d)].into(),
|
vec![Bool::not(Variable(b)), Variable(d)],
|
||||||
vec![Variable(a), Variable(c)].into(),
|
vec![Variable(a), Variable(c)],
|
||||||
vec![Variable(a), Variable(d)].into()
|
vec![Variable(a), Variable(d)]
|
||||||
]
|
]
|
||||||
.into()
|
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -684,33 +686,27 @@ mod test_boolean_algebra {
|
||||||
);
|
);
|
||||||
|
|
||||||
absorptive_eq(
|
absorptive_eq(
|
||||||
boolean_algebra::absorptive(
|
vec![vec![Bool::not(Variable(b)), Variable(c)].into()].into(),
|
||||||
vec![vec![Bool::not(Variable(b)), Variable(c)].into()].into(),
|
|
||||||
),
|
|
||||||
vec![vec![Bool::not(Variable(b)), Variable(c)]],
|
vec![vec![Bool::not(Variable(b)), Variable(c)]],
|
||||||
);
|
);
|
||||||
absorptive_eq(
|
absorptive_eq(
|
||||||
boolean_algebra::absorptive(
|
vec![
|
||||||
vec![
|
vec![Bool::not(Variable(b)), Variable(c)].into(),
|
||||||
vec![Bool::not(Variable(b)), Variable(c)].into(),
|
vec![Bool::not(Variable(b)), Variable(d)].into(),
|
||||||
vec![Bool::not(Variable(b)), Variable(d)].into(),
|
]
|
||||||
]
|
.into(),
|
||||||
.into(),
|
|
||||||
),
|
|
||||||
vec![
|
vec![
|
||||||
vec![Bool::not(Variable(b)), Variable(d)],
|
vec![Bool::not(Variable(b)), Variable(d)],
|
||||||
vec![Bool::not(Variable(b)), Variable(c)],
|
vec![Bool::not(Variable(b)), Variable(c)],
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
absorptive_eq(
|
absorptive_eq(
|
||||||
boolean_algebra::absorptive(
|
vec![
|
||||||
vec![
|
vec![Bool::not(Variable(b)), Variable(c)].into(),
|
||||||
vec![Bool::not(Variable(b)), Variable(c)].into(),
|
vec![Bool::not(Variable(b)), Variable(d)].into(),
|
||||||
vec![Bool::not(Variable(b)), Variable(d)].into(),
|
vec![Bool::not(Variable(b)), Bool::not(Variable(d)), Variable(c)].into(),
|
||||||
vec![Bool::not(Variable(b)), Bool::not(Variable(d)), Variable(c)].into(),
|
]
|
||||||
]
|
.into(),
|
||||||
.into(),
|
|
||||||
),
|
|
||||||
vec![
|
vec![
|
||||||
vec![Bool::not(Variable(b)), Bool::not(Variable(d)), Variable(c)],
|
vec![Bool::not(Variable(b)), Bool::not(Variable(d)), Variable(c)],
|
||||||
vec![Bool::not(Variable(b)), Variable(d)],
|
vec![Bool::not(Variable(b)), Variable(d)],
|
||||||
|
@ -718,15 +714,13 @@ mod test_boolean_algebra {
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
absorptive_eq(
|
absorptive_eq(
|
||||||
boolean_algebra::absorptive(
|
vec![
|
||||||
vec![
|
vec![Bool::not(Variable(b)), Variable(c)].into(),
|
||||||
vec![Bool::not(Variable(b)), Variable(c)].into(),
|
vec![Bool::not(Variable(b)), Variable(d)].into(),
|
||||||
vec![Bool::not(Variable(b)), Variable(d)].into(),
|
vec![Bool::not(Variable(b)), Bool::not(Variable(d)), Variable(c)].into(),
|
||||||
vec![Bool::not(Variable(b)), Bool::not(Variable(d)), Variable(c)].into(),
|
vec![Bool::not(Variable(c)), Bool::not(Variable(b)), Variable(d)].into(),
|
||||||
vec![Bool::not(Variable(c)), Bool::not(Variable(b)), Variable(d)].into(),
|
]
|
||||||
]
|
.into(),
|
||||||
.into(),
|
|
||||||
),
|
|
||||||
vec![
|
vec![
|
||||||
vec![Bool::not(Variable(c)), Bool::not(Variable(b)), Variable(d)],
|
vec![Bool::not(Variable(c)), Bool::not(Variable(b)), Variable(d)],
|
||||||
vec![Bool::not(Variable(b)), Bool::not(Variable(d)), Variable(c)],
|
vec![Bool::not(Variable(b)), Bool::not(Variable(d)), Variable(c)],
|
||||||
|
@ -735,16 +729,14 @@ mod test_boolean_algebra {
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
absorptive_eq(
|
absorptive_eq(
|
||||||
boolean_algebra::absorptive(
|
vec![
|
||||||
vec![
|
vec![Bool::not(Variable(b)), Variable(c)].into(),
|
||||||
vec![Bool::not(Variable(b)), Variable(c)].into(),
|
vec![Bool::not(Variable(b)), Variable(d)].into(),
|
||||||
vec![Bool::not(Variable(b)), Variable(d)].into(),
|
vec![Bool::not(Variable(b)), Bool::not(Variable(d)), Variable(c)].into(),
|
||||||
vec![Bool::not(Variable(b)), Bool::not(Variable(d)), Variable(c)].into(),
|
vec![Bool::not(Variable(c)), Bool::not(Variable(b)), Variable(d)].into(),
|
||||||
vec![Bool::not(Variable(c)), Bool::not(Variable(b)), Variable(d)].into(),
|
vec![Variable(a), Variable(c)].into(),
|
||||||
vec![Variable(a), Variable(c)].into(),
|
]
|
||||||
]
|
.into(),
|
||||||
.into(),
|
|
||||||
),
|
|
||||||
vec![
|
vec![
|
||||||
vec![Variable(a), Variable(c)].into(),
|
vec![Variable(a), Variable(c)].into(),
|
||||||
vec![Bool::not(Variable(c)), Bool::not(Variable(b)), Variable(d)],
|
vec![Bool::not(Variable(c)), Bool::not(Variable(b)), Variable(d)],
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue