From 8e6f605fabbdca0a8e5c82c8064255ddbd2de1db Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 20 May 2018 22:37:58 -0700 Subject: [PATCH] Type alias "TypeName" --- schala-lang/src/typechecking.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/schala-lang/src/typechecking.rs b/schala-lang/src/typechecking.rs index 64fb071..41eb8b1 100644 --- a/schala-lang/src/typechecking.rs +++ b/schala-lang/src/typechecking.rs @@ -11,6 +11,8 @@ use itertools::Itertools; use parsing; +type TypeName = Rc; + pub struct TypeContext { environment: TypeEnvironment, } @@ -26,7 +28,7 @@ pub type TypeResult = Result; #[derive(Debug, PartialEq, Clone)] enum MonoType { Const(TypeConst), - Var(Rc), + Var(TypeName), Function(Box, Box), } @@ -42,7 +44,7 @@ enum TypeConst { } impl MonoType { - fn free_vars(&self) -> HashSet> { + fn free_vars(&self) -> HashSet { use self::MonoType::*; match self { Const(_) => HashSet::new(), @@ -72,16 +74,16 @@ impl MonoType { } #[derive(Debug, PartialEq, Clone)] -struct PolyType(HashSet>, MonoType); +struct PolyType(HashSet, MonoType); impl PolyType { - fn free_vars(&self) -> HashSet> { + fn free_vars(&self) -> HashSet { let mtype = self.1.free_vars(); self.0.difference(&mtype).cloned().collect() } fn apply_substitution(&self, s: &Substitution) -> PolyType { - let mut map: HashMap, MonoType> = HashMap::new(); + let mut map: HashMap = HashMap::new(); for (name, monotype) in s.0.iter() { if let None = self.0.get(name) { map.insert(name.clone(), monotype.clone()); @@ -94,14 +96,14 @@ impl PolyType { } #[derive(Debug, PartialEq, Clone)] -struct Substitution(HashMap, MonoType>); +struct Substitution(HashMap); impl Substitution { fn new() -> Substitution { Substitution(HashMap::new()) } - fn bind_variable(name: &Rc, var: &MonoType) -> Substitution { + fn bind_variable(name: &TypeName, var: &MonoType) -> Substitution { Substitution(hashmap! { name.clone() => var.clone() }) @@ -122,7 +124,7 @@ impl Substitution { #[derive(Debug, Default)] struct TypeEnvironment { - map: HashMap, PolyType>, + map: HashMap, } impl TypeEnvironment { @@ -134,7 +136,7 @@ impl TypeEnvironment { TypeEnvironment { map } } - fn lookup(&self, name: &Rc) -> Option { + fn lookup(&self, name: &TypeName) -> Option { self.map.get(name).map(|x| x.clone()) } }