diff options
author | Joel Kronqvist <joel.kronqvist@iki.fi> | 2025-08-10 19:16:51 +0300 |
---|---|---|
committer | Joel Kronqvist <joel.kronqvist@iki.fi> | 2025-08-10 19:16:51 +0300 |
commit | d6d1ec80ffcc0b13234b170a91b920371078a027 (patch) | |
tree | a8347b04ad354e3f2cb265b5d506a4891853cf7f /src/type | |
parent | 06798d622327707ca3f3b42d65fc3d1a25ae3c57 (diff) | |
download | myslip-d6d1ec80ffcc0b13234b170a91b920371078a027.tar.gz myslip-d6d1ec80ffcc0b13234b170a91b920371078a027.zip |
Added tests for functions
Diffstat (limited to 'src/type')
-rw-r--r-- | src/type/check.rs | 50 | ||||
-rw-r--r-- | src/type/conversion.rs | 12 | ||||
-rw-r--r-- | src/type/display.rs | 1 | ||||
-rw-r--r-- | src/type/mod.rs | 2 |
4 files changed, 63 insertions, 2 deletions
diff --git a/src/type/check.rs b/src/type/check.rs index 24c255d..1712446 100644 --- a/src/type/check.rs +++ b/src/type/check.rs @@ -78,7 +78,7 @@ impl SExp { /// assert_eq!(Atom(And).type_check(), Ok(arr(List(vec![Boolean, Boolean]), Boolean))); /// assert_eq!(Atom(Or) .type_check(), Ok(arr(List(vec![Boolean, Boolean]), Boolean))); /// assert_eq!(Atom(Xor).type_check(), Ok(arr(List(vec![Boolean, Boolean]), Boolean))); - /// assert_eq!(Atom(Not).type_check(), Ok(arr(List(vec![Boolean]), Boolean))); + /// assert_eq!(Atom(Not).type_check(), Ok(arr(Boolean, Boolean))); /// ``` /// /// @@ -101,6 +101,50 @@ impl SExp { /// ); /// ``` /// + /// **Functions** + /// + /// One variable: + /// ```rust + /// use myslip::{ + /// r#type::{*, Type::*, TypeError::*, util::*}, + /// sexp::{SExp::*, SLeaf::*, util::*}, + /// }; + /// + /// let varlist = scons(var("a"), Nil); + /// let typelist = scons(Ty(Integer), Nil); + /// let ret = Atom(Ty(Integer)); + /// let body = scons(var("a"), Nil); + /// assert_eq!( + /// scons(Fun, scons(varlist, scons(typelist, scons(ret, scons(body, Nil))))).type_check(), + /// Ok(arr(List(vec![Integer]), Integer)) + /// ); + /// ``` + /// Two-variable: + /// ```rust + /// use myslip::{ + /// r#type::{*, Type::*, TypeError::*, util::*}, + /// sexp::{SExp::*, SLeaf::*, util::*}, + /// }; + /// + /// let varlist = scons(var("a"), scons(var("b"), Nil)); + /// let typelist = scons(Ty(List(vec![Integer])), Nil); + /// let ret = Atom(Ty(Boolean)); + /// let body = scons(Eq, varlist.clone()); + /// assert_eq!( + /// scons(Fun, scons(varlist, scons(typelist, scons(ret, scons(body, Nil))))).type_check(), + /// Ok(arr(List(vec![Integer, Integer]), Boolean)) + /// ); + /// ``` + /// Only keyword shouldnt panic: + /// ```rust + /// use myslip::{ + /// r#type::{*, Type::*, TypeError::*, util::*}, + /// sexp::{SExp::*, SLeaf::*, util::*}, + /// }; + /// Atom(Fun).type_check(); + /// ``` + /// + /// /// Though perhaps the most important task of the type system /// is to increase safety by being able to warn about errors /// before evaluation. Here are some failing examples: @@ -174,7 +218,7 @@ impl SExp { Atom(Eq | Neq | Lt | Gt | Le | Ge) => Ok(arr(List(vec![Integer, Integer]), Boolean)), Atom(Or | And | Xor) => Ok(arr(List(vec![Boolean, Boolean]), Boolean)), - Atom(Not) => Ok(arr(List(vec!(Boolean)), Boolean)), + Atom(Not) => Ok(arr(Boolean, Boolean)), Atom(Nil) => Ok(NilType), Atom(Quote) => Ok(arr( vt("T"), @@ -188,6 +232,7 @@ impl SExp { Atom(Print) => Ok(arr(vt("_"), List(vec![]))), Atom(Ty(_)) => Ok(TypeLit), Atom(Arr) => Ok(arr(List(vec![TypeLit, TypeLit]), TypeLit)), + Atom(Fun) => todo!(), SCons(op, l) => { @@ -204,6 +249,7 @@ impl SExp { return Err(LetAsOperator(scons(op.clone(), l.clone()))); } + // Normal operation let opertype = (*op).infer_type(ctx.clone())?; let argstype = (*l).infer_list_type(ctx)?; diff --git a/src/type/conversion.rs b/src/type/conversion.rs index 3be2874..0a96790 100644 --- a/src/type/conversion.rs +++ b/src/type/conversion.rs @@ -36,6 +36,8 @@ impl Type { (a, b) if a == b => a.clone(), + (List(v), b) if v.len() == 1 && &v[0] == b => b.clone(), + (VecOf(a), VecOf(b)) => vecof(a.least_general_supertype(b)), (List(v1), List(v2)) if v1.len() == v2.len() => { @@ -73,6 +75,7 @@ impl Type { /// Tries to convert this type into a subtype of the other. /// + /// Currently the most important conversion is from list to vec. /// ```rust /// use myslip::r#type::{Type::*, util::*}; /// @@ -92,6 +95,13 @@ impl Type { /// Ok(vecof(vt("T"))) /// ); /// ``` + /// + /// Though the conversion from (a) to a is also convenient: + /// ```rust + /// use myslip::r#type::{Type::*, util::*}; + /// + /// assert_eq!(List(vec![Integer]).into_type(&Integer), Ok(Integer)); + /// ``` pub fn into_type(self, other: &Type) -> Result<Type, ()> { if !self.aka(other) { return Err(()); @@ -103,6 +113,8 @@ impl Type { (_, VarType(_)) => Ok(self), + (List(x), b) if x.len() == 1 && &x[0] == b => Ok(x[0].clone()), + (List(v), VecOf(b)) => match v.get(0) { Some(first) => { let cand = v.into_iter() diff --git a/src/type/display.rs b/src/type/display.rs index cd60673..ded97a0 100644 --- a/src/type/display.rs +++ b/src/type/display.rs @@ -112,6 +112,7 @@ impl fmt::Display for TypeError { letexp ) }, + FunAsAtom => write!(f, "'fn' used as atom doesn't make sense"), OtherError => write!(f, "uncategorized error"), } } diff --git a/src/type/mod.rs b/src/type/mod.rs index c138bba..d020ab3 100644 --- a/src/type/mod.rs +++ b/src/type/mod.rs @@ -66,6 +66,8 @@ pub enum TypeError { LetAsOperator(SExp), + FunAsAtom, + OtherError } |