aboutsummaryrefslogtreecommitdiff
path: root/src/type/mod.rs
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2025-08-04 02:34:51 +0300
committerJoel Kronqvist <joel.kronqvist@iki.fi>2025-08-04 02:34:51 +0300
commit36d2818d39e61b752923e253f8455f50510cb428 (patch)
tree65170598495c9b1f0ee464c9ad1c2301705c3abe /src/type/mod.rs
parent78751b29953f786878549955c050ba38cb514d52 (diff)
downloadmyslip-36d2818d39e61b752923e253f8455f50510cb428.tar.gz
myslip-36d2818d39e61b752923e253f8455f50510cb428.zip
Implemented infer_type. Changed is_concrete tests and implemented it.
Error messages still need improvement, maybe some new variants? is_concrete was changed to hold information on the first unbound variable through Result<(), String>.
Diffstat (limited to 'src/type/mod.rs')
-rw-r--r--src/type/mod.rs42
1 files changed, 31 insertions, 11 deletions
diff --git a/src/type/mod.rs b/src/type/mod.rs
index 37081a1..aeeff93 100644
--- a/src/type/mod.rs
+++ b/src/type/mod.rs
@@ -48,6 +48,7 @@ pub enum TypeError {
OtherError
}
+use Type::*;
impl Type {
/// Tests if the type has no variable types.
@@ -56,21 +57,40 @@ impl Type {
/// ```rust
/// use melisp::r#type::{*, Type::*, TypeError::*, util::*};
///
- /// assert!(Integer.is_concrete());
- /// assert!(!VarType("a".to_string()).is_concrete());
+ /// assert_eq!(Integer.is_concrete(), Ok(()));
+ /// assert_eq!(
+ /// VarType("a".to_string()).is_concrete(),
+ /// Err("a".to_string())
+ /// );
///
- /// assert!(arr(Integer, Integer).is_concrete());
- /// assert!(!arr(VarType("b".to_string()), Integer).is_concrete());
+ /// assert_eq!(arr(Integer, Integer).is_concrete(), Ok(()));
+ /// assert_eq!(
+ /// arr(VarType("b".to_string()), Integer).is_concrete(),
+ /// Err("b".to_string())
+ /// );
///
- /// assert!(
- /// List(vec![Integer, Integer, arr(Integer, Integer)]).is_concrete()
+ /// assert_eq!(
+ /// List(vec![Integer, Integer, arr(Integer, Integer)]).is_concrete(),
+ /// Ok(())
/// );
- /// assert!(
- /// !List(vec![Integer, VarType("a".to_string()), Integer])
- /// .is_concrete()
+ /// assert_eq!(
+ /// List(vec![Integer, VarType("a".to_string()), Integer])
+ /// .is_concrete(),
+ /// Err("a".to_string())
/// );
/// ```
- pub fn is_concrete(&self) -> bool {
- todo!()
+ pub fn is_concrete(&self) -> Result<(), String> {
+ match self {
+ Integer => Ok(()),
+ Arrow(a, b) => b.is_concrete().and_then(|_ok| a.is_concrete()),
+ List(v) => {
+ let mut res = Ok(());
+ for t in v {
+ res = res.and_then(|_ok| t.is_concrete());
+ }
+ res
+ },
+ VarType(s) => Err(s.clone()),
+ }
}
}