TypeTerm: add unit(), return index of first / last match in is_syntactic_subtype

This commit is contained in:
Michael Sippel 2023-09-07 18:06:52 +02:00
parent a569fb46a8
commit d8d282f9e9
Signed by: senvas
GPG key ID: F96CF119C34B64A6

View file

@ -33,6 +33,10 @@ pub enum TypeTerm {
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
impl TypeTerm { impl TypeTerm {
pub fn unit() -> Self {
TypeTerm::Ladder(vec![])
}
pub fn new(id: TypeID) -> Self { pub fn new(id: TypeID) -> Self {
TypeTerm::TypeID(id) TypeTerm::TypeID(id)
} }
@ -130,6 +134,17 @@ impl TypeTerm {
} }
} }
pub fn get_lnf_vec(self) -> Vec<TypeTerm> {
match self.normalize() {
TypeTerm::Ladder( v ) => {
v
},
_ => {
unreachable!();
}
}
}
/// transmute self into Ladder-Normal-Form /// transmute self into Ladder-Normal-Form
/// ///
/// Example: /// Example:
@ -194,48 +209,43 @@ impl TypeTerm {
TypeTerm::Ladder( new_ladder ) TypeTerm::Ladder( new_ladder )
} }
/* pub fn is_semantic_supertype_of(&self, t: &TypeTerm) -> Option<( usize, TypeTerm )> {
pub fn is_supertype_of(&self, t: &TypeTerm) -> bool {
t.is_semantic_subtype_of(self) t.is_semantic_subtype_of(self)
} }
*/
// returns provided syntax-type, // returns ladder-step of first match and provided representation-type
pub fn is_semantic_subtype_of(&self, expected_type: &TypeTerm) -> Option< TypeTerm > { pub fn is_semantic_subtype_of(&self, expected_type: &TypeTerm) -> Option<( usize, TypeTerm )> {
let provided_lnf = self.clone(); let provided_lnf = self.clone().get_lnf_vec();
let expected_lnf = expected_type.clone(); let expected_lnf = expected_type.clone().get_lnf_vec();
match for i in 0..provided_lnf.len() {
(provided_lnf.normalize(), if provided_lnf[i] == expected_lnf[0] {
expected_lnf.normalize()) return Some((i, TypeTerm::Ladder(
{ provided_lnf[i..].into_iter().cloned().collect()
( TypeTerm::Ladder( provided_ladder ), )))
TypeTerm::Ladder( expected_ladder )
) => {
for i in 0..provided_ladder.len() {
if provided_ladder[i] == expected_ladder[0] {
return Some(TypeTerm::Ladder(
provided_ladder[i..].into_iter().cloned().collect()
))
} }
} }
None None
},
_ => {
// both are in LNF!
unreachable!()
} }
pub fn is_syntactic_subtype_of(&self, expected_type: &TypeTerm) -> Result<usize, Option<(usize, usize)>> {
if let Some((first_match, provided_type)) = self.is_semantic_subtype_of( expected_type ) {
let provided_lnf = self.clone().get_lnf_vec();
let expected_lnf = expected_type.clone().get_lnf_vec();
let l = usize::min( provided_lnf.len(), expected_lnf.len() );
for i in 0..l {
if provided_lnf[i] != expected_lnf[i] {
return Err(Some((first_match, i)))
} }
} }
pub fn is_syntactic_subtype_of(&self, expected_type: &TypeTerm) -> bool { Ok(l-1)
if let Some(provided_type) = self.is_semantic_subtype_of( expected_type ) {
&provided_type == expected_type
} else { } else {
false Err(None)
} }
} }