substitutions

This commit is contained in:
Michael Sippel 2023-10-30 17:22:00 +01:00
parent aacafb318a
commit 74177d1d30
Signed by: senvas
GPG key ID: 060F22F65102F95C
3 changed files with 63 additions and 0 deletions

View file

@ -78,6 +78,36 @@ impl TypeTerm {
pub fn char_arg(&mut self, c: char) -> &mut Self {
self.arg(TypeTerm::Char(c))
}
/// recursively apply substitution to all subterms,
/// which will replace all occurences of variables which map
/// some type-term in `subst`
pub fn apply_substitution(
&mut self,
subst: &impl Fn(&TypeID) -> Option<TypeTerm>
) -> &mut Self {
match self {
TypeTerm::TypeID(typid) => {
if let Some(t) = subst(typid) {
*self = t;
}
}
TypeTerm::Ladder(rungs) => {
for r in rungs.iter_mut() {
r.apply_substitution(subst);
}
}
TypeTerm::App(args) => {
for r in args.iter_mut() {
r.apply_substitution(subst);
}
}
_ => {}
}
self
}
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\

View file

@ -4,4 +4,5 @@ pub mod parser;
pub mod curry;
pub mod lnf;
pub mod subtype;
pub mod substitution;

32
src/test/substitution.rs Normal file
View file

@ -0,0 +1,32 @@
use {
crate::{dict::*, term::*},
std::iter::FromIterator
};
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\
#[test]
fn test_subst() {
let mut dict = TypeDict::new();
let mut σ = std::collections::HashMap::new();
// T -->
σ.insert
(dict.add_varname(String::from("T")),
dict.parse("").unwrap());
// U --> <Seq Char>
σ.insert
(dict.add_varname(String::from("U")),
dict.parse("<Seq Char>").unwrap());
assert_eq!(
dict.parse("<Seq T~U>").unwrap()
.apply_substitution(&|typid|{ σ.get(typid).cloned() }).clone(),
dict.parse("<Seq ~<Seq Char>>").unwrap()
);
}