Convert unify to are types

b/c Type implements Clone
Maybe wanna kill this later for efficiency
This commit is contained in:
greg 2017-10-10 04:26:40 -07:00
parent ddd861fbea
commit 47975cf8f6

View File

@ -28,9 +28,14 @@ pub enum Type {
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub enum TypeVar { pub enum TypeVar {
Univ(String), Univ(Rc<String>),
Exist(u64), Exist(u64),
} }
impl TypeVar {
fn univ(label: &str) -> TypeVar {
TypeVar::Univ(Rc::new(label.to_string()))
}
}
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub enum TypeConst { pub enum TypeConst {
@ -186,11 +191,11 @@ impl TypeContext {
match op_type { match op_type {
TConst(FunctionT(box t1, box t2)) => { TConst(FunctionT(box t1, box t2)) => {
let _ = self.unify(&t1, &lhs_type)?; let _ = self.unify(t1, lhs_type)?;
let rhs_type = self.infer(&rhs)?; let rhs_type = self.infer(&rhs)?;
match t2 { match t2 {
TConst(FunctionT(box t3, box t_ret)) => { TConst(FunctionT(box t3, box t_ret)) => {
let _ = self.unify(&t3, &rhs_type)?; let _ = self.unify(t3, rhs_type)?;
t_ret t_ret
}, },
_ => return Err(format!("Another bad type for operator")) _ => return Err(format!("Another bad type for operator"))
@ -204,7 +209,7 @@ impl TypeContext {
let arg_type = self.infer(arguments.get(0).unwrap())?; // TODO fix later let arg_type = self.infer(arguments.get(0).unwrap())?; // TODO fix later
match f_type { match f_type {
TConst(FunctionT(box t1, box ret_type)) => { TConst(FunctionT(box t1, box ret_type)) => {
let _ = self.unify(&t1, &arg_type)?; let _ = self.unify(t1, arg_type)?;
ret_type ret_type
}, },
_ => return Err(format!("Type error")) _ => return Err(format!("Type error"))
@ -222,10 +227,10 @@ impl TypeContext {
if opstr == "+" { if opstr == "+" {
return Ok( return Ok(
TConst(FunctionT( TConst(FunctionT(
Box::new(TConst(StringT)), Box::new(TVar(TypeVar::univ("a"))),
Box::new(TConst(FunctionT( Box::new(TConst(FunctionT(
Box::new(TConst(StringT)), Box::new(TVar(TypeVar::univ("a"))),
Box::new(TConst(StringT)) Box::new(TVar(TypeVar::univ("a")))
))) )))
)) ))
) )
@ -242,7 +247,7 @@ impl TypeContext {
) )
} }
fn unify(&mut self, t1: &Type, t2: &Type) -> TypeCheckResult { fn unify(&mut self, t1: Type, t2: Type) -> TypeCheckResult {
if t1 == t2 { if t1 == t2 {
Ok(t1.clone()) Ok(t1.clone())
} else { } else {