add Boolean to Structure

This commit is contained in:
Folkert 2020-01-07 17:11:28 +01:00
parent 4adecb12a2
commit 3268c267bd
5 changed files with 58 additions and 2 deletions

View file

@ -102,6 +102,9 @@ fn layout_from_flat_type<'a>(
EmptyTagUnion => { EmptyTagUnion => {
panic!("TODO make Layout for empty Tag Union"); panic!("TODO make Layout for empty Tag Union");
} }
Boolean(_) => {
panic!("TODO make Layout for Boolean");
}
Erroneous(_) => Err(()), Erroneous(_) => Err(()),
EmptyRecord => Ok(Layout::Struct(&[])), EmptyRecord => Ok(Layout::Struct(&[])),
} }

View file

@ -104,6 +104,11 @@ fn find_names_needed(
find_names_needed(ext_var, subs, roots, root_appearances, names_taken); find_names_needed(ext_var, subs, roots, root_appearances, names_taken);
} }
Structure(Boolean(b)) => {
for var in b.variables() {
find_names_needed(var, subs, roots, root_appearances, names_taken);
}
}
RigidVar(name) => { RigidVar(name) => {
// User-defined names are already taken. // User-defined names are already taken.
// We must not accidentally generate names that collide with them! // We must not accidentally generate names that collide with them!
@ -295,6 +300,7 @@ fn write_flat_type(flat_type: FlatType, subs: &mut Subs, buf: &mut String, paren
} }
} }
} }
Boolean(_) => panic!("pretty print type"),
Erroneous(problem) => { Erroneous(problem) => {
buf.push_str(&format!("<Type Mismatch: {:?}>", problem)); buf.push_str(&format!("<Type Mismatch: {:?}>", problem));
} }

View file

@ -673,6 +673,15 @@ fn adjust_rank_content(
rank rank
} }
Boolean(b) => {
let mut rank = Rank::toplevel();
for var in b.variables() {
rank = rank.max(adjust_rank(subs, young_mark, visit_mark, group_rank, var));
}
rank
}
Erroneous(_) => group_rank, Erroneous(_) => group_rank,
} }
} }
@ -806,6 +815,12 @@ fn deep_copy_var_help(
TagUnion(new_tags, deep_copy_var_help(subs, max_rank, pools, ext_var)) TagUnion(new_tags, deep_copy_var_help(subs, max_rank, pools, ext_var))
} }
Boolean(b) => {
let mut mapper = |var| deep_copy_var_help(subs, max_rank, pools, var);
Boolean(b.map_variables(&mut mapper))
}
}; };
subs.set(copy, make_descriptor(Structure(new_flat_type))); subs.set(copy, make_descriptor(Structure(new_flat_type)));

View file

@ -3,6 +3,7 @@ use crate::can::symbol::Symbol;
use crate::collections::{ImMap, ImSet, MutSet, SendMap}; use crate::collections::{ImMap, ImSet, MutSet, SendMap};
use crate::ena::unify::{InPlace, UnificationTable, UnifyKey}; use crate::ena::unify::{InPlace, UnificationTable, UnifyKey};
use crate::types::{name_type_var, ErrorType, Problem, RecordFieldLabel, TypeExt}; use crate::types::{name_type_var, ErrorType, Problem, RecordFieldLabel, TypeExt};
use crate::uniqueness::boolean_algebra;
use std::fmt; use std::fmt;
use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::atomic::{AtomicUsize, Ordering};
@ -439,6 +440,7 @@ pub enum FlatType {
Erroneous(Problem), Erroneous(Problem),
EmptyRecord, EmptyRecord,
EmptyTagUnion, EmptyTagUnion,
Boolean(boolean_algebra::Bool),
} }
#[derive(PartialEq, Eq, Debug, Clone, Copy)] #[derive(PartialEq, Eq, Debug, Clone, Copy)]
@ -482,6 +484,10 @@ fn occurs(subs: &mut Subs, seen: &ImSet<Variable>, var: Variable) -> bool {
.values() .values()
.any(|vars| vars.iter().any(|var| occurs(subs, &new_seen, *var))) .any(|vars| vars.iter().any(|var| occurs(subs, &new_seen, *var)))
} }
Boolean(b) => b
.variables()
.iter()
.any(|var| occurs(subs, &new_seen, *var)),
EmptyRecord | EmptyTagUnion | Erroneous(_) => false, EmptyRecord | EmptyTagUnion | Erroneous(_) => false,
} }
} }
@ -560,6 +566,12 @@ fn get_var_names(
taken_names taken_names
} }
FlatType::Boolean(b) => b
.variables()
.into_iter()
.fold(taken_names, |answer, arg_var| {
get_var_names(subs, arg_var, answer)
}),
}, },
} }
} }
@ -759,6 +771,8 @@ fn flat_type_to_err_type(subs: &mut Subs, state: &mut NameState, flat_type: Flat
} }
} }
Boolean(_) => panic!("TODO make error type"),
Erroneous(_) => ErrorType::Error, Erroneous(_) => ErrorType::Error,
} }
} }
@ -810,6 +824,11 @@ fn restore_content(subs: &mut Subs, content: &Content) {
subs.restore(*ext_var); subs.restore(*ext_var);
} }
Boolean(b) => {
for var in b.variables() {
subs.restore(var);
}
}
Erroneous(_) => (), Erroneous(_) => (),
}, },
Alias(_, _, args, var) => { Alias(_, _, args, var) => {

View file

@ -82,6 +82,20 @@ impl Bool {
}; };
} }
pub fn map_variables<F>(&self, f: &mut F) -> Self
where
F: FnMut(Variable) -> Variable,
{
match self {
Zero => Zero,
One => One,
And(left, right) => and(left.map_variables(f), right.map_variables(f)),
Or(left, right) => or(left.map_variables(f), right.map_variables(f)),
Not(nested) => not(nested.map_variables(f)),
Variable(current) => Variable(f(*current)),
}
}
pub fn substitute(&self, substitutions: &Substitution) -> Self { pub fn substitute(&self, substitutions: &Substitution) -> Self {
match self { match self {
Zero => Zero, Zero => Zero,
@ -131,8 +145,7 @@ pub fn simplify(term: Bool) -> Bool {
let a = term_to_sop(normalized); let a = term_to_sop(normalized);
let b = normalize_sop(a); let b = normalize_sop(a);
let after_bcf = bcf(b); let after_bcf = bcf(b);
let answer = sop_to_term(simplify_sop(after_bcf)); sop_to_term(simplify_sop(after_bcf))
answer
} }
#[inline(always)] #[inline(always)]