schala/schala-lang/language/src/typechecking.rs

177 lines
3.8 KiB
Rust
Raw Normal View History

2018-02-21 02:31:28 -08:00
use std::rc::Rc;
2019-01-07 13:00:37 -08:00
use crate::ast::*;
use crate::util::ScopeStack;
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>, ()>,
2018-11-09 02:50:29 -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.
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 {
fn new<A>(msg: &str) -> InferResult<A> {
Err(TypeError { msg: msg.to_string() })
}
}
2018-11-06 16:47:34 -08:00
#[derive(Debug)]
pub enum Type {
Const(TypeConst),
Arrow(Box<Type>, Box<Type>)
}
#[derive(Debug)]
pub enum TypeConst {
Unit,
Nat,
Int,
Float,
StringT,
Bool,
Order,
UserDefined
}
impl Type {
fn arrow(a: Type, b: Type) -> Type {
Type::Arrow(Box::new(a), Box::new(b))
}
}
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>>),
}
#[derive(Debug, Clone)]
2018-11-09 02:50:29 -08:00
enum TVar {
Univ(UVar),
2018-11-10 14:11:29 -08:00
Exist(ExistentialVar)
2018-11-08 02:26:02 -08:00
}
#[derive(Debug, Clone)]
struct UVar(Rc<String>);
#[derive(Debug, Clone)]
2018-11-09 02:50:29 -08:00
struct ExistentialVar(u32);
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()),
Type::Var(TVar::Exist(_)) => Type::Var(UVar(Rc::new(format!("sk")))),
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 {
fn to_monotype(&self) -> Type<UVar> {
2018-11-08 20:30:17 -08:00
match self {
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),
_ => 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),
2018-11-09 02:50:29 -08:00
evar_count: 0
2018-11-08 20:30:17 -08:00
}
2018-11-06 13:44:52 -08:00
}
2018-11-07 13:44:28 -08:00
pub fn typecheck(&mut self, ast: &AST) -> Result<String, String> {
let mut returned_type = Type::Const(TypeConst::Unit);
2019-02-10 04:42:30 -08:00
for statement in ast.0.iter() {
returned_type = self.typecheck_statement(statement.node()).map_err(|err| { err.msg })?
2019-02-10 04:42:30 -08:00
}
Ok(format!("{:?}", returned_type))
2019-02-10 04:42:30 -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),
}
}
fn typecheck_decl(&mut self, decl: &Declaration) -> InferResult<Type> {
Ok(Type::Const(TypeConst::Unit))
2019-02-10 04:42:30 -08:00
}
fn typecheck_expr(&mut self, expr: &Expression) -> InferResult<Type> {
match expr {
Expression(expr_type, Some(_anno)) => {
//TODO here
self.typecheck_expr_type(expr_type)
},
Expression(expr_type, None) => self.typecheck_expr_type(expr_type)
}
}
fn typecheck_expr_type(&mut self, expr: &ExpressionType) -> InferResult<Type> {
Ok(Type::Const(TypeConst::Unit))
2018-11-06 13:44:52 -08:00
}
}