Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Elaborating where clauses directly in the logic #49

Merged
merged 7 commits into from
Jul 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion chalk-parse/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ pub struct Field {
pub enum Goal {
ForAll(Vec<ParameterKind>, Box<Goal>),
Exists(Vec<ParameterKind>, Box<Goal>),
Implies(Vec<WhereClause>, Box<Goal>),

// The `bool` flag indicates whether we should elaborate where clauses or not
Implies(Vec<WhereClause>, Box<Goal>, bool),

And(Box<Goal>, Box<Goal>),
Not(Box<Goal>),

Expand Down
7 changes: 6 additions & 1 deletion chalk-parse/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ pub Goal: Box<Goal> = {
Goal1: Box<Goal> = {
"forall" "<" <p:Comma<ParameterKind>> ">" "{" <g:Goal> "}" => Box::new(Goal::ForAll(p, g)),
"exists" "<" <p:Comma<ParameterKind>> ">" "{" <g:Goal> "}" => Box::new(Goal::Exists(p, g)),
"if" "(" <w:Comma<WhereClause>> ")" "{" <g:Goal> "}" => Box::new(Goal::Implies(w, g)),
<i:IfKeyword> "(" <w:Comma<WhereClause>> ")" "{" <g:Goal> "}" => Box::new(Goal::Implies(w, g, i)),
"not" "{" <g:Goal> "}" => Box::new(Goal::Not(g)),
<w:WhereClause> => Box::new(Goal::Leaf(w)),
};

IfKeyword: bool = {
"if" => true,
"if_raw" => false,
};

StructDefn: StructDefn = {
"struct" <n:Id><p:Angle<ParameterKind>> <w:WhereClauses> "{" <f:Fields> "}" => StructDefn {
name: n,
Expand Down
33 changes: 32 additions & 1 deletion src/cast.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use ir::*;
use std::marker::PhantomData;

pub trait Cast<T>: Sized {
fn cast(self) -> T;
Expand Down Expand Up @@ -130,7 +131,7 @@ impl<T, U> Cast<Vec<U>> for Vec<T>
where T: Cast<U>
{
fn cast(self) -> Vec<U> {
self.into_iter().map(|v| v.cast()).collect()
self.into_iter().casted().collect()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice :)

}
}

Expand All @@ -145,3 +146,33 @@ impl Cast<Parameter> for Lifetime {
ParameterKind::Lifetime(self)
}
}

pub struct Casted<I, U> {
iterator: I,
_cast: PhantomData<U>,
}

impl<I: Iterator, U> Iterator for Casted<I, U> where I::Item: Cast<U> {
type Item = U;

fn next(&mut self) -> Option<Self::Item> {
self.iterator.next().map(|item| item.cast())
}

fn size_hint(&self) -> (usize, Option<usize>) {
self.iterator.size_hint()
}
}

pub trait Caster<U>: Sized {
fn casted(self) -> Casted<Self, U>;
}

impl<I: Iterator, U> Caster<U> for I {
fn casted(self) -> Casted<Self, U> {
Casted {
iterator: self,
_cast: PhantomData,
}
}
}
78 changes: 24 additions & 54 deletions src/ir/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use cast::Cast;
use chalk_parse::ast;
use fold::Subst;
use lalrpop_intern::InternedString;
use solve::infer::{TyInferenceVariable, LifetimeInferenceVariable};
use std::collections::{HashSet, HashMap, BTreeMap};
use std::collections::{HashMap, BTreeMap};
use std::sync::Arc;

pub type Identifier = InternedString;
Expand Down Expand Up @@ -95,58 +94,6 @@ impl Environment {
env.universe = UniverseIndex { counter: self.universe.counter + 1 };
Arc::new(env)
}

/// Generate the full set of clauses that are "syntactically implied" by the
/// clauses in this environment. Currently this consists of two kinds of expansions:
///
/// - Supertraits are added, so `T: Ord` would expand to `T: Ord, T: PartialOrd`
/// - Projections imply that a trait is implemented, to `T: Iterator<Item=Foo>` expands to
/// `T: Iterator, T: Iterator<Item=Foo>`
pub fn elaborated_clauses(&self, program: &ProgramEnvironment) -> impl Iterator<Item = DomainGoal> {
let mut set = HashSet::new();
set.extend(self.clauses.iter().cloned());

let mut stack: Vec<_> = set.iter().cloned().collect();

while let Some(clause) = stack.pop() {
let mut push_clause = |clause: DomainGoal| {
if !set.contains(&clause) {
set.insert(clause.clone());
stack.push(clause);
}
};

match clause {
DomainGoal::Implemented(ref trait_ref) => {
// trait Foo<A> where Self: Bar<A> { }
// T: Foo<U>
// ----------------------------------------------------------
// T: Bar<U>

let trait_datum = &program.trait_data[&trait_ref.trait_id];
for where_clause in &trait_datum.binders.value.where_clauses {
let where_clause = Subst::apply(&trait_ref.parameters, where_clause);
push_clause(where_clause);
}
}
DomainGoal::Normalize(Normalize { ref projection, ty: _ }) => {
// <T as Trait<U>>::Foo ===> V
// ----------------------------------------------------------
// T: Trait<U>

let (associated_ty_data, trait_params, _) = program.split_projection(projection);
let trait_ref = TraitRef {
trait_id: associated_ty_data.trait_id,
parameters: trait_params.to_owned()
};
push_clause(trait_ref.cast());
}
_ => {}
}
}

set.into_iter()
}
}

#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
Expand Down Expand Up @@ -249,6 +196,7 @@ pub struct StructDatum {
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct StructDatumBound {
pub self_ty: ApplicationTy,
pub fields: Vec<Ty>,
pub where_clauses: Vec<DomainGoal>,
}

Expand Down Expand Up @@ -420,6 +368,28 @@ impl DomainGoal {
fallback_clause: false,
}
}

/// A clause of the form (T: Foo) expands to (T: Foo), WF(T: Foo).
/// A clause of the form (T: Foo<Item = U>) expands to (T: Foo<Item = U>), WF(T: Foo).
pub fn expanded(self, program: &Program) -> impl Iterator<Item = DomainGoal> {
let mut expanded = vec![];
match self {
DomainGoal::Implemented(ref trait_ref) => {
expanded.push(WellFormed::TraitRef(trait_ref.clone()).cast());
}
DomainGoal::Normalize(Normalize { ref projection, .. }) => {
let (associated_ty_data, trait_params, _) = program.split_projection(&projection);
let trait_ref = TraitRef {
trait_id: associated_ty_data.trait_id,
parameters: trait_params.to_owned()
};
expanded.push(WellFormed::TraitRef(trait_ref).cast());
}
_ => ()
};
expanded.push(self.cast());
expanded.into_iter()
}
}

#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
Expand Down
Loading