Skip to content

Commit

Permalink
Add holes to the type syntax, and use them when building the AST
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanzab committed Aug 21, 2016
1 parent 93791f0 commit fb9bd82
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions base/src/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ fn walk_move_type2<F, I, T>(typ: &Type<I, T>, f: &mut F) -> Option<T>
|v| walk_move_type2(&v.1, f).map(|t| (v.0.clone(), t)))
.map(Type::variants)
}
Type::Hole |
Type::Builtin(_) |
Type::Variable(_) |
Type::Generic(_) |
Expand Down
11 changes: 10 additions & 1 deletion base/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ pub fn instantiate<F>(typ: TcType, mut f: F) -> TcType
/// the pointer wrapper directly.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum Type<Id, T = AstType<Id>> {
/// An unbound type `_`, awaiting ascription.
Hole,
/// An application with multiple arguments.
/// `Map String Int` would be represented as `App(Map, [String, Int])`
App(T, Vec<T>),
Expand All @@ -107,7 +109,7 @@ pub enum Type<Id, T = AstType<Id>> {
/// The fields of this record type
fields: Vec<Field<Id, T>>,
},
/// An identifier type. Anything which is not a builting type.
/// An identifier type. Anything that is not a builtin type.
Id(Id),
Alias(AliasData<Id, T>),
}
Expand Down Expand Up @@ -419,6 +421,10 @@ pub struct Field<Id, T = AstType<Id>> {
impl<Id, T> Type<Id, T>
where T: From<Type<Id, T>>
{
pub fn hole() -> T {
T::from(Type::Hole)
}

pub fn array(typ: T) -> T {
Type::app(Type::builtin(BuiltinType::Array), vec![typ])
}
Expand Down Expand Up @@ -734,6 +740,7 @@ impl<'a, I, T, E> DisplayType<'a, I, T, E>
{
let p = self.prec;
match *self.typ {
Type::Hole => arena.text("_"),
Type::Variable(ref var) => arena.text(format!("{}", var.id)),
Type::Generic(ref gen) => arena.text(gen.id.as_ref()),
Type::App(ref t, ref args) => {
Expand Down Expand Up @@ -892,6 +899,7 @@ pub fn walk_type_<I, T, F: ?Sized>(typ: &T, f: &mut F)
f.walk(&variant.1);
}
}
Type::Hole |
Type::Builtin(_) |
Type::Variable(_) |
Type::Generic(_) |
Expand Down Expand Up @@ -1037,6 +1045,7 @@ pub fn walk_move_type_opt<F: ?Sized, I, T>(typ: &Type<I, T>, f: &mut F) -> Optio
walk_move_types(variants, |v| f.visit(&v.1).map(|t| (v.0.clone(), t)))
.map(Type::variants)
}
Type::Hole |
Type::Builtin(_) |
Type::Variable(_) |
Type::Generic(_) |
Expand Down
1 change: 1 addition & 0 deletions check/src/kindcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ impl<'a> KindCheck<'a> {

fn kindcheck(&mut self, typ: &TcType) -> Result<(RcKind, TcType)> {
match **typ {
Type::Hole => Ok((self.subs.new_var(), Type::hole())),
Type::Generic(ref gen) => {
let mut gen = gen.clone();
gen.kind = try!(self.find(&gen.id));
Expand Down
7 changes: 2 additions & 5 deletions parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use base::ast;
use base::ast::*;
use base::error::Errors;
use base::pos::{self, Location, Located, Span};
use base::types::{Type, Generic, Alias, Field, Kind, TypeVariable};
use base::types::{Type, Generic, Alias, Field, Kind};
use base::symbol::{Name, Symbol, SymbolModule};

use combine::primitives::{Consumed, Stream, StreamOnce, Error as CombineError, Info,
Expand Down Expand Up @@ -801,10 +801,7 @@ pub fn parse_tc
input: &str)
-> Result<SpannedExpr<TcIdent<Symbol>>, (Option<SpannedExpr<TcIdent<Symbol>>>, Errors<Error>)> {
let mut env = ast::TcIdentEnv {
typ: Type::variable(TypeVariable {
id: 0,
kind: Kind::typ(),
}),
typ: Type::hole(),
env: symbols,
};
parse_expr(&mut env, input)
Expand Down

0 comments on commit fb9bd82

Please sign in to comment.