Getting back to hindley-milner

First, clear out some of this cruft in the compiler warnings
This commit is contained in:
greg 2018-05-15 03:25:40 -07:00
parent e64861b602
commit cb316a973e
1 changed files with 19 additions and 9 deletions

View File

@ -1,6 +1,6 @@
use std::rc::Rc; use std::rc::Rc;
use std::collections::HashMap; use std::collections::HashMap;
use std::char; //use std::char;
use std::fmt; use std::fmt;
use std::fmt::Write; use std::fmt::Write;
@ -9,7 +9,7 @@ use itertools::Itertools;
use parsing; use parsing;
pub struct TypeContext { pub struct TypeContext {
type_var_count: u64, //type_var_count: u64,
bindings: HashMap<Rc<String>, Type>, bindings: HashMap<Rc<String>, Type>,
pub symbol_table: SymbolTable pub symbol_table: SymbolTable
} }
@ -36,8 +36,8 @@ pub enum Type {
Const(TConst), Const(TConst),
Sum(Vec<Type>), Sum(Vec<Type>),
Func(Box<Type>, Box<Type>), Func(Box<Type>, Box<Type>),
UVar(String), //UVar(String),
EVar(u64), //EVar(u64),
Void Void
} }
@ -57,13 +57,14 @@ impl fmt::Display for Type {
write!(f, ")") write!(f, ")")
}, },
&Func(ref a, ref b) => write!(f, "{} -> {}", a, b), &Func(ref a, ref b) => write!(f, "{} -> {}", a, b),
&UVar(ref s) => write!(f, "{}_u", s), //&UVar(ref s) => write!(f, "{}_u", s),
&EVar(ref n) => write!(f, "{}_e", n), //&EVar(ref n) => write!(f, "{}_e", n),
&Void => write!(f, "Void") &Void => write!(f, "Void")
} }
} }
} }
/*
#[derive(Default)] #[derive(Default)]
struct UVarGenerator { struct UVarGenerator {
n: u32, n: u32,
@ -79,6 +80,7 @@ impl UVarGenerator {
Type::UVar(s) Type::UVar(s)
} }
} }
*/
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub enum TConst { pub enum TConst {
@ -91,6 +93,7 @@ pub enum TConst {
Custom(String), Custom(String),
} }
//TODO get rid of this, just instantiate builtin types to the environment
impl parsing::TypeName { impl parsing::TypeName {
fn to_type(&self) -> TypeResult<Type> { fn to_type(&self) -> TypeResult<Type> {
use self::parsing::TypeSingletonName; use self::parsing::TypeSingletonName;
@ -116,13 +119,15 @@ pub type TypeResult<T> = Result<T, String>;
impl TypeContext { impl TypeContext {
pub fn new() -> TypeContext { pub fn new() -> TypeContext {
TypeContext { bindings: HashMap::new(), type_var_count: 0, symbol_table: SymbolTable::new() } TypeContext { bindings: HashMap::new(), /*type_var_count: 0*/ symbol_table: SymbolTable::new() }
} }
/*
pub fn fresh(&mut self) -> Type { pub fn fresh(&mut self) -> Type {
let ret = self.type_var_count; let ret = self.type_var_count;
self.type_var_count += 1; self.type_var_count += 1;
Type::EVar(ret) Type::EVar(ret)
} }
*/
} }
impl TypeContext { impl TypeContext {
@ -134,6 +139,7 @@ impl TypeContext {
if let Statement::Declaration(decl) = statement { if let Statement::Declaration(decl) = statement {
match decl { match decl {
FuncSig(signature) | FuncDecl(signature, _) => { FuncSig(signature) | FuncDecl(signature, _) => {
/*
let mut uvar_gen = UVarGenerator::new(); let mut uvar_gen = UVarGenerator::new();
let mut ty: Type = signature.type_anno.as_ref().map(|name: &TypeName| name.to_type()).unwrap_or_else(|| {Ok(uvar_gen.next())} )?; let mut ty: Type = signature.type_anno.as_ref().map(|name: &TypeName| name.to_type()).unwrap_or_else(|| {Ok(uvar_gen.next())} )?;
for &(_, ref type_name) in signature.params.iter().rev() { for &(_, ref type_name) in signature.params.iter().rev() {
@ -141,7 +147,7 @@ impl TypeContext {
ty = Func(bx!(arg_type), bx!(ty)); ty = Func(bx!(arg_type), bx!(ty));
} }
self.bindings.insert(signature.name.clone(), ty); self.bindings.insert(signature.name.clone(), ty);
*/
self.symbol_table.values.insert( self.symbol_table.values.insert(
signature.name.clone(), signature.name.clone(),
Symbol { name: signature.name.clone(), ty: Func(Box::new(Void), Box::new(Void)) } Symbol { name: signature.name.clone(), ty: Func(Box::new(Void), Box::new(Void)) }
@ -194,7 +200,7 @@ impl TypeContext {
use self::parsing::Declaration::*; use self::parsing::Declaration::*;
use self::Type::*; use self::Type::*;
match decl { match decl {
&Binding { ref name, ref expr, .. } => { Binding { name, expr, .. } => {
let ty = self.infer(expr)?; let ty = self.infer(expr)?;
self.bindings.insert(name.clone(), ty); self.bindings.insert(name.clone(), ty);
}, },
@ -287,5 +293,9 @@ impl TypeContext {
(a, b) => Err(format!("Types {:?} and {:?} don't unify", a, b)) (a, b) => Err(format!("Types {:?} and {:?} don't unify", a, b))
} }
} }
//fn bind(&mut self, var: TVar, ty: Type) -> `Infer Subst` {
//
//}
} }