2018-02-21 02:31:28 -08:00
|
|
|
use std::rc::Rc;
|
2019-02-10 12:21:12 -08:00
|
|
|
//THINGS TODO
|
|
|
|
// look at the haskell compiler, see where in its flow the typechecking happens
|
|
|
|
// -nope, ghc deliberately does typechecking before desugaring to core
|
|
|
|
// cf. a history of haskell, peyton-jones
|
2018-02-21 02:31:28 -08:00
|
|
|
|
2019-01-07 13:00:37 -08:00
|
|
|
use crate::ast::*;
|
|
|
|
use crate::util::ScopeStack;
|
2019-02-10 12:21:12 -08:00
|
|
|
use crate::builtin::PrefixOp;
|
2018-11-06 13:44:52 -08:00
|
|
|
|
2018-05-27 02:44:06 -07:00
|
|
|
pub type TypeName = Rc<String>;
|
2018-11-06 13:44:52 -08:00
|
|
|
|
2018-11-08 20:30:17 -08:00
|
|
|
pub struct TypeContext<'a> {
|
2019-02-09 00:25:12 -08:00
|
|
|
variable_map: ScopeStack<'a, Rc<String>, ()>,
|
2019-02-10 05:33:55 -08:00
|
|
|
//evar_count: u32
|
2018-11-06 13:44:52 -08:00
|
|
|
}
|
|
|
|
|
2018-11-11 18:04:44 -08:00
|
|
|
/// `InferResult` is the monad in which type inference takes place.
|
2018-11-07 17:01:07 -08:00
|
|
|
type InferResult<T> = Result<T, TypeError>;
|
2018-11-06 16:47:34 -08:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2018-11-08 02:26:02 -08:00
|
|
|
struct TypeError { msg: String }
|
|
|
|
|
|
|
|
impl TypeError {
|
2019-02-10 07:05:01 -08:00
|
|
|
fn new<A>(msg: &str) -> InferResult<A> { //TODO make these kinds of error-producing functions CoW-ready
|
2018-11-08 02:26:02 -08:00
|
|
|
Err(TypeError { msg: msg.to_string() })
|
|
|
|
}
|
|
|
|
}
|
2018-11-06 16:47:34 -08:00
|
|
|
|
2019-02-10 06:53:11 -08:00
|
|
|
#[derive(Debug, Clone)]
|
2019-02-10 04:30:37 -08:00
|
|
|
pub enum Type {
|
|
|
|
Const(TypeConst),
|
|
|
|
Arrow(Box<Type>, Box<Type>)
|
|
|
|
}
|
|
|
|
|
2019-02-10 07:32:12 -08:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2019-02-10 04:30:37 -08:00
|
|
|
pub enum TypeConst {
|
|
|
|
Unit,
|
|
|
|
Nat,
|
|
|
|
Int,
|
|
|
|
Float,
|
|
|
|
StringT,
|
|
|
|
Bool,
|
2019-02-10 07:06:30 -08:00
|
|
|
Ordering,
|
2019-02-10 04:30:37 -08:00
|
|
|
UserDefined
|
|
|
|
}
|
|
|
|
|
2019-02-10 07:05:01 -08:00
|
|
|
//TODO find a better way to capture the to/from string logic
|
2019-02-10 04:30:37 -08:00
|
|
|
impl Type {
|
2019-02-10 05:31:58 -08:00
|
|
|
fn to_string(&self) -> String {
|
|
|
|
use self::Type::*;
|
|
|
|
use self::TypeConst::*;
|
|
|
|
match self {
|
|
|
|
Const(Unit) => format!("()"),
|
|
|
|
Const(Nat) => format!("Nat"),
|
|
|
|
Const(Int) => format!("Int"),
|
|
|
|
Const(Float) => format!("Float"),
|
|
|
|
Const(StringT) => format!("String"),
|
|
|
|
Const(Bool) => format!("Bool"),
|
2019-02-10 07:06:30 -08:00
|
|
|
Const(Ordering) => format!("Ordering"),
|
2019-02-10 05:31:58 -08:00
|
|
|
_ => format!("UNKNOWN TYPE"),
|
|
|
|
}
|
|
|
|
}
|
2019-02-10 07:05:01 -08:00
|
|
|
|
|
|
|
fn from_string(string: &str) -> Option<Type> {
|
|
|
|
use self::Type::*;
|
|
|
|
use self::TypeConst::*;
|
|
|
|
Some(match string {
|
|
|
|
"()" | "Unit" => Const(Unit),
|
|
|
|
"Nat" => Const(Nat),
|
|
|
|
"Int" => Const(Int),
|
|
|
|
"Float" => Const(Float),
|
|
|
|
"String" => Const(StringT),
|
|
|
|
"Bool" => Const(Bool),
|
2019-02-10 07:06:30 -08:00
|
|
|
"Ordering" => Const(Ordering),
|
2019-02-10 07:05:01 -08:00
|
|
|
_ => return None
|
|
|
|
})
|
|
|
|
}
|
2019-02-10 05:31:58 -08:00
|
|
|
}
|
2019-02-10 04:30:37 -08:00
|
|
|
|
2019-02-09 00:25:12 -08:00
|
|
|
/*
|
2018-11-11 18:04:44 -08:00
|
|
|
/// `Type` is parameterized by whether the type variables can be just universal, or universal or
|
|
|
|
/// existential.
|
2018-11-07 13:44:28 -08:00
|
|
|
#[derive(Debug, Clone)]
|
2018-11-09 02:50:29 -08:00
|
|
|
enum Type<A> {
|
|
|
|
Var(A),
|
2018-11-07 15:39:40 -08:00
|
|
|
Const(TConst),
|
2018-11-09 02:50:29 -08:00
|
|
|
Arrow(Box<Type<A>>, Box<Type<A>>),
|
|
|
|
}
|
|
|
|
|
2018-11-10 16:33:42 -08:00
|
|
|
#[derive(Debug, Clone)]
|
2018-11-09 02:50:29 -08:00
|
|
|
enum TVar {
|
2018-11-10 16:33:42 -08:00
|
|
|
Univ(UVar),
|
2018-11-10 14:11:29 -08:00
|
|
|
Exist(ExistentialVar)
|
2018-11-08 02:26:02 -08:00
|
|
|
}
|
|
|
|
|
2018-11-10 16:33:42 -08:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct UVar(Rc<String>);
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2018-11-09 02:50:29 -08:00
|
|
|
struct ExistentialVar(u32);
|
|
|
|
|
2018-11-10 16:33:42 -08:00
|
|
|
impl Type<UVar> {
|
|
|
|
fn to_tvar(&self) -> Type<TVar> {
|
|
|
|
match self {
|
|
|
|
Type::Var(UVar(name)) => Type::Var(TVar::Univ(UVar(name.clone()))),
|
|
|
|
Type::Const(ref c) => Type::Const(c.clone()),
|
|
|
|
Type::Arrow(a, b) => Type::Arrow(
|
|
|
|
Box::new(a.to_tvar()),
|
|
|
|
Box::new(b.to_tvar())
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Type<TVar> {
|
|
|
|
fn skolemize(&self) -> Type<UVar> {
|
|
|
|
match self {
|
|
|
|
Type::Var(TVar::Univ(uvar)) => Type::Var(uvar.clone()),
|
2018-11-13 02:39:02 -08:00
|
|
|
Type::Var(TVar::Exist(_)) => Type::Var(UVar(Rc::new(format!("sk")))),
|
2018-11-10 16:33:42 -08:00
|
|
|
Type::Const(ref c) => Type::Const(c.clone()),
|
|
|
|
Type::Arrow(a, b) => Type::Arrow(
|
|
|
|
Box::new(a.skolemize()),
|
|
|
|
Box::new(b.skolemize())
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-08 02:26:02 -08:00
|
|
|
impl TypeIdentifier {
|
2018-11-10 16:33:42 -08:00
|
|
|
fn to_monotype(&self) -> Type<UVar> {
|
2018-11-08 20:30:17 -08:00
|
|
|
match self {
|
2018-11-13 02:39:02 -08:00
|
|
|
TypeIdentifier::Tuple(_) => Type::Const(TConst::Nat),
|
2018-11-08 20:30:17 -08:00
|
|
|
TypeIdentifier::Singleton(TypeSingletonName { name, .. }) => {
|
|
|
|
match &name[..] {
|
2018-11-09 02:05:59 -08:00
|
|
|
"Nat" => Type::Const(TConst::Nat),
|
|
|
|
"Int" => Type::Const(TConst::Int),
|
|
|
|
"Float" => Type::Const(TConst::Float),
|
|
|
|
"Bool" => Type::Const(TConst::Bool),
|
|
|
|
"String" => Type::Const(TConst::StringT),
|
2018-11-13 02:39:02 -08:00
|
|
|
_ => Type::Const(TConst::Nat),
|
2018-11-08 20:30:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-08 02:26:02 -08:00
|
|
|
}
|
2018-11-07 03:39:31 -08:00
|
|
|
}
|
2018-11-06 16:47:34 -08:00
|
|
|
|
2018-11-07 15:39:40 -08:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
enum TConst {
|
|
|
|
User(Rc<String>),
|
|
|
|
Unit,
|
|
|
|
Nat,
|
2018-11-07 16:39:32 -08:00
|
|
|
Int,
|
|
|
|
Float,
|
|
|
|
StringT,
|
2018-11-08 02:12:01 -08:00
|
|
|
Bool,
|
2018-11-07 15:39:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TConst {
|
|
|
|
fn user(name: &str) -> TConst {
|
|
|
|
TConst::User(Rc::new(name.to_string()))
|
|
|
|
}
|
|
|
|
}
|
2019-02-09 00:25:12 -08:00
|
|
|
*/
|
2018-11-07 15:39:40 -08:00
|
|
|
|
2018-11-08 20:30:17 -08:00
|
|
|
impl<'a> TypeContext<'a> {
|
|
|
|
pub fn new() -> TypeContext<'a> {
|
|
|
|
TypeContext {
|
|
|
|
variable_map: ScopeStack::new(None),
|
2019-02-10 05:33:55 -08:00
|
|
|
//evar_count: 0
|
2018-11-08 20:30:17 -08:00
|
|
|
}
|
2018-11-06 13:44:52 -08:00
|
|
|
}
|
|
|
|
|
2019-02-10 06:53:11 -08:00
|
|
|
fn get_type_from_name(&self, name: &TypeIdentifier) -> InferResult<Type> {
|
2019-02-10 07:05:01 -08:00
|
|
|
use self::TypeIdentifier::*;
|
|
|
|
Ok(match name {
|
|
|
|
Singleton(TypeSingletonName { name, params }) => {
|
|
|
|
match Type::from_string(&name) {
|
|
|
|
Some(ty) => ty,
|
|
|
|
None => return TypeError::new("Unknown type name")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Tuple(_) => return TypeError::new("tuples aren't ready yet"),
|
|
|
|
})
|
|
|
|
|
2019-02-10 06:53:11 -08:00
|
|
|
}
|
|
|
|
|
2018-11-07 13:44:28 -08:00
|
|
|
pub fn typecheck(&mut self, ast: &AST) -> Result<String, String> {
|
2019-02-10 05:15:57 -08:00
|
|
|
let mut returned_type = Type::Const(TypeConst::Unit);
|
2019-02-10 04:42:30 -08:00
|
|
|
for statement in ast.0.iter() {
|
2019-02-10 05:15:57 -08:00
|
|
|
returned_type = self.typecheck_statement(statement.node()).map_err(|err| { err.msg })?
|
2019-02-10 04:42:30 -08:00
|
|
|
}
|
2019-02-10 05:31:58 -08:00
|
|
|
Ok(returned_type.to_string())
|
2019-02-10 04:42:30 -08:00
|
|
|
}
|
|
|
|
|
2019-02-10 05:15:57 -08:00
|
|
|
fn typecheck_statement(&mut self, statement: &Statement) -> InferResult<Type> {
|
2019-02-10 04:42:30 -08:00
|
|
|
match statement {
|
|
|
|
Statement::ExpressionStatement(e) => self.typecheck_expr(e.node()),
|
|
|
|
Statement::Declaration(decl) => self.typecheck_decl(decl),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-10 05:31:58 -08:00
|
|
|
fn typecheck_decl(&mut self, _decl: &Declaration) -> InferResult<Type> {
|
2019-02-10 05:15:57 -08:00
|
|
|
Ok(Type::Const(TypeConst::Unit))
|
2019-02-10 04:42:30 -08:00
|
|
|
}
|
|
|
|
|
2019-02-10 05:15:57 -08:00
|
|
|
fn typecheck_expr(&mut self, expr: &Expression) -> InferResult<Type> {
|
|
|
|
match expr {
|
2019-02-10 06:53:11 -08:00
|
|
|
Expression(expr_type, Some(anno)) => {
|
|
|
|
let t1 = self.typecheck_expr_type(expr_type)?;
|
|
|
|
let t2 = self.get_type_from_name(anno)?;
|
|
|
|
self.unify(t2, t1)
|
2019-02-10 05:15:57 -08:00
|
|
|
},
|
|
|
|
Expression(expr_type, None) => self.typecheck_expr_type(expr_type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn typecheck_expr_type(&mut self, expr: &ExpressionType) -> InferResult<Type> {
|
2019-02-10 05:31:58 -08:00
|
|
|
use self::ExpressionType::*;
|
|
|
|
Ok(match expr {
|
|
|
|
NatLiteral(_) => Type::Const(TypeConst::Nat),
|
|
|
|
BoolLiteral(_) => Type::Const(TypeConst::Bool),
|
2019-02-10 05:33:55 -08:00
|
|
|
FloatLiteral(_) => Type::Const(TypeConst::Float),
|
|
|
|
StringLiteral(_) => Type::Const(TypeConst::StringT),
|
2019-02-10 12:21:12 -08:00
|
|
|
PrefixExp(op, expr) => self.typecheck_prefix(op, expr.node())?,
|
|
|
|
IfExpression { discriminator, body } => self.typecheck_if_expr(discriminator, body)?,
|
2019-02-12 21:14:13 -08:00
|
|
|
Value(val) => self.handle_value(val)?,
|
2019-02-10 05:31:58 -08:00
|
|
|
_ => Type::Const(TypeConst::Unit)
|
|
|
|
})
|
2018-11-06 13:44:52 -08:00
|
|
|
}
|
2019-02-10 06:48:25 -08:00
|
|
|
|
2019-02-10 12:21:12 -08:00
|
|
|
//TODO get rid of 'typecheck_' on all these methods - too wordy, already in typecontext
|
|
|
|
fn typecheck_prefix(&mut self, op: &PrefixOp, expr: &Expression) -> InferResult<Type> {
|
|
|
|
let f = match op.get_type() {
|
|
|
|
Ok(ty) => ty,
|
|
|
|
Err(e) => return TypeError::new("Couldn't find a type for this prefix op")
|
|
|
|
};
|
|
|
|
|
|
|
|
let x = self.typecheck_expr(expr)?;
|
|
|
|
self.handle_apply(f, x)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_apply(&mut self, f: Type, x: Type) -> InferResult<Type> {
|
|
|
|
Ok(Type::Const(TypeConst::Unit))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn typecheck_if_expr(&mut self, discriminator: &Discriminator, body: &IfExpressionBody) -> InferResult<Type> {
|
|
|
|
//only handle simple case for now
|
|
|
|
|
|
|
|
Ok(Type::Const(TypeConst::Unit))
|
|
|
|
}
|
2019-02-10 06:48:25 -08:00
|
|
|
|
2019-02-12 21:14:13 -08:00
|
|
|
fn handle_value(&mut self, val: &Rc<String>) -> InferResult<Type> {
|
|
|
|
match self.variable_map.lookup(val) {
|
|
|
|
Some(ty) => Ok(ty.clone()),
|
|
|
|
None => TypeError::new(&format!("Couldn't find variable: {}", val))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-10 06:53:11 -08:00
|
|
|
fn unify(&mut self, t1: Type, t2: Type) -> InferResult<Type> {
|
2019-02-10 07:32:12 -08:00
|
|
|
use self::Type::*; use self::TypeConst::*;
|
|
|
|
Ok(match (t1, t2) {
|
|
|
|
(Const(ref c1), Const(ref c2)) if c1 == c2 => Const(c1.clone()), //choice of c1 is arbitrary I *think*
|
|
|
|
(a, b) => return TypeError::new(&format!("{:?} and {:?} do not unify", a, b)),
|
|
|
|
})
|
2019-02-10 06:48:25 -08:00
|
|
|
}
|
2018-11-06 13:44:52 -08:00
|
|
|
}
|