improvements over initial graph search implementation

- turn Morphism into trait and add find_morphism() function
- morphism base: store vec of seq-types
- morphism base: find shortest path instead of just some path
- fix returned halo type in find_morphism_with_subtyping()
- fix find_morphism_path:
    * also apply substitution from src-type match
    * get this substitution as result from `enum_morphisms_with_subtyping`
This commit is contained in:
Michael Sippel 2024-08-05 02:54:35 +02:00
parent 0d891e8677
commit aa7a39bf17
Signed by: senvas
GPG key ID: F96CF119C34B64A6
3 changed files with 204 additions and 74 deletions

View file

@ -27,4 +27,5 @@ pub use {
substitution::*,
sugar::*,
unification::*,
morphism::*
};

View file

@ -8,20 +8,31 @@ use {
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct MorphismType {
pub src_type: TypeTerm,
pub dst_type: TypeTerm,
}
pub struct MorphismBase<Morphism: Clone> {
morphisms: Vec< (MorphismType, Morphism) >,
list_typeid: TypeID
pub trait Morphism : Sized {
fn get_type(&self) -> MorphismType;
fn map_morphism(&self, seq_type: TypeTerm) -> Option< Self >;
fn weight(&self) -> u64 {
1
}
}
#[derive(Clone)]
pub struct MorphismBase<M: Morphism + Clone> {
morphisms: Vec< M >,
seq_types: Vec< TypeTerm >
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\
impl MorphismType {
fn normalize(self) -> Self {
pub fn normalize(self) -> Self {
MorphismType {
src_type: self.src_type.normalize(),
dst_type: self.dst_type.normalize()
@ -31,16 +42,16 @@ impl MorphismType {
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\
impl<Morphism: Clone> MorphismBase<Morphism> {
pub fn new(list_typeid: TypeID) -> Self {
impl<M: Morphism + Clone> MorphismBase<M> {
pub fn new(seq_types: Vec<TypeTerm>) -> Self {
MorphismBase {
morphisms: Vec::new(),
list_typeid
seq_types
}
}
pub fn add_morphism(&mut self, morph_type: MorphismType, morphism: Morphism) {
self.morphisms.push( (morph_type.normalize(), morphism) );
pub fn add_morphism(&mut self, m: M) {
self.morphisms.push( m );
}
pub fn enum_morphisms(&self, src_type: &TypeTerm)
@ -49,15 +60,15 @@ impl<Morphism: Clone> MorphismBase<Morphism> {
let mut dst_types = Vec::new();
// first enumerate all "direct" morphisms,
for (ty,m) in self.morphisms.iter() {
for m in self.morphisms.iter() {
if let Ok(σ) = crate::unification::unify(
&ty.src_type,
&m.get_type().src_type,
&src_type.clone().normalize()
) {
let dst_type =
ty.dst_type.clone()
.apply_substitution(&σ)
.clone();
m.get_type().dst_type.clone()
.apply_substitution( &σ )
.clone();
dst_types.push( (σ, dst_type) );
}
@ -69,9 +80,10 @@ impl<Morphism: Clone> MorphismBase<Morphism> {
// TODO: function for generating fresh variables
let item_variable = TypeID::Var(100);
for seq_type in self.seq_types.iter() {
if let Ok(σ) = crate::unification::unify(
&TypeTerm::App(vec![
TypeTerm::TypeID(self.list_typeid),
seq_type.clone(),
TypeTerm::TypeID(item_variable)
]),
&src_type.clone().param_normalize(),
@ -81,7 +93,7 @@ impl<Morphism: Clone> MorphismBase<Morphism> {
for (γ, dst_item_type) in self.enum_morphisms( &src_item_type ) {
let dst_type =
TypeTerm::App(vec![
TypeTerm::TypeID(self.list_typeid),
seq_type.clone(),
dst_item_type.clone()
.apply_substitution(&γ).clone()
]).normalize();
@ -89,29 +101,32 @@ impl<Morphism: Clone> MorphismBase<Morphism> {
dst_types.push( (γ.clone(), dst_type) );
}
}
}
dst_types
}
pub fn enum_morphisms_with_subtyping(&self, src_type: &TypeTerm)
-> Vec< (TypeTerm, TypeTerm) >
{
pub fn enum_morphisms_with_subtyping(
&self,
src_type: &TypeTerm,
) -> Vec<(TypeTerm, TypeTerm, HashMap<TypeID, TypeTerm>)> {
let mut src_lnf = src_type.clone().get_lnf_vec();
let mut halo_lnf = vec![];
let mut dst_types = Vec::new();
while src_lnf.len() > 0 {
let src_type = TypeTerm::Ladder( src_lnf.clone() );
let halo_type = TypeTerm::Ladder( halo_lnf.clone() );
let src_type = TypeTerm::Ladder(src_lnf.clone());
let halo_type = TypeTerm::Ladder(halo_lnf.clone());
for (σ, t) in self.enum_morphisms( &src_type ) {
dst_types.push(
(halo_type.clone()
.apply_substitution(&σ).clone(),
t.clone()
.apply_substitution(&σ).clone()
)
);
for (σ, t) in self.enum_morphisms(&src_type) {
dst_types.push((
halo_type
.clone()
.apply_substitution(&σ)
.clone(),
t.clone().apply_substitution(&σ).clone(),
σ,
));
}
// continue with next supertype
@ -121,41 +136,46 @@ impl<Morphism: Clone> MorphismBase<Morphism> {
dst_types
}
/* performs DFS to find a morphism-path for a given type
* will return the first matching path, not the shortest
/* try to find shortest morphism-path for a given type
*/
pub fn find_morphism_path(&self, ty: MorphismType)
-> Option< Vec<TypeTerm> >
{
let ty = ty.normalize();
let mut visited = Vec::new();
let mut queue = vec![
vec![ ty.src_type.clone().normalize() ]
(0, vec![ ty.src_type.clone().normalize() ])
];
while let Some(current_path) = queue.pop() {
let current_type = current_path.last().unwrap();
while ! queue.is_empty() {
queue.sort_by( |&(w1,_),&(w2,_)| w2.cmp(&w1));
if ! visited.contains( current_type ) {
visited.push( current_type.clone() );
if let Some((current_weight, current_path)) = queue.pop() {
let current_type = current_path.last().unwrap();
for (h, t, σp) in self.enum_morphisms_with_subtyping(&current_type) {
let tt = TypeTerm::Ladder(vec![h, t]).normalize();
for (h, t) in self.enum_morphisms_with_subtyping(&current_type) {
let tt = TypeTerm::Ladder( vec![ h, t ] ).normalize();
if ! visited.contains( &tt ) {
if !current_path.contains(&tt) {
let unification_result = crate::unification::unify(&tt, &ty.dst_type);
let morphism_weight = 1;
/*self.find_morphism( &tt ).unwrap().0.get_weight()*/
let new_weight = current_weight + morphism_weight;
let mut new_path = current_path.clone();
new_path.push( tt );
new_path.push(tt);
for n in new_path.iter_mut() {
n.apply_substitution(&σp);
}
if let Ok(σ) = unification_result {
new_path = new_path.into_iter().map(
|mut t: TypeTerm| t.apply_substitution(&σ).clone()
).collect::<Vec<TypeTerm>>();
for n in new_path.iter_mut() {
n.apply_substitution(&σ);
}
return Some(new_path);
} else {
queue.push( new_path );
queue.push((new_weight, new_path));
}
}
}
@ -165,26 +185,78 @@ impl<Morphism: Clone> MorphismBase<Morphism> {
None
}
pub fn find_morphism(&self, ty: &MorphismType)
-> Option< Morphism > {
-> Option< ( M, HashMap<TypeID, TypeTerm> ) > {
// TODO
// try to find primitive morphism
for m in self.morphisms.iter() {
let unification_problem = UnificationProblem::new_sub(
vec![
( ty.src_type.clone().normalize(), m.get_type().src_type.clone() ),
( ty.dst_type.clone().normalize(), m.get_type().dst_type.clone() )
]
);
None
}
let unification_result = unification_problem.solve();
if let Ok((ψ,σ)) = unification_result {
return Some((m.clone(), σ));
}
}
pub fn find_list_map_morphism(&self, item_ty: &MorphismType)
-> Option< Morphism > {
// try list-map morphism
for seq_type in self.seq_types.iter() {
eprintln!("try seq type {:?}", seq_type);
// TODO
eprintln!("");
if let Ok((ψ,σ)) = UnificationProblem::new_sub(vec![
(ty.src_type.clone().param_normalize(),
TypeTerm::App(vec![ seq_type.clone(), TypeTerm::TypeID(TypeID::Var(100)) ])),
(ty.dst_type.clone().param_normalize(),
TypeTerm::App(vec![ seq_type.clone(), TypeTerm::TypeID(TypeID::Var(101)) ])),
]).solve() {
// TODO: use real fresh variable names
let item_morph_type = MorphismType {
src_type: σ.get(&TypeID::Var(100)).unwrap().clone(),
dst_type: σ.get(&TypeID::Var(101)).unwrap().clone(),
}.normalize();
if let Some((m, σ)) = self.find_morphism( &item_morph_type ) {
if let Some(list_morph) = m.map_morphism( seq_type.clone() ) {
return Some( (list_morph, σ) );
}
}
}
}
None
}
pub fn find_morphism_with_subtyping(&self, ty: &MorphismType)
-> Option<( Morphism, TypeTerm, HashMap<TypeID, TypeTerm> )> {
-> Option<( M, TypeTerm, HashMap<TypeID, TypeTerm> )> {
let mut src_lnf = ty.src_type.clone().get_lnf_vec();
let mut dst_lnf = ty.dst_type.clone().get_lnf_vec();
let mut halo = vec![];
// TODO
while src_lnf.len() > 0 && dst_lnf.len() > 0 {
if let Some((m, σ)) = self.find_morphism(&MorphismType{
src_type: TypeTerm::Ladder(src_lnf.clone()),
dst_type: TypeTerm::Ladder(dst_lnf.clone())
}) {
halo.push(src_lnf.get(0).unwrap().clone());
return Some((m,
TypeTerm::Ladder(halo).apply_substitution(&σ).clone(),
σ));
} else {
if src_lnf[0] == dst_lnf[0] {
src_lnf.remove(0);
halo.push(dst_lnf.remove(0));
} else {
return None;
}
}
}
None
}

View file

@ -1,55 +1,72 @@
use {
crate::{dict::*, morphism::*, parser::*}
crate::{dict::*, morphism::*, parser::*, TypeTerm}
};
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\
#[derive(Clone, Debug, PartialEq)]
struct DummyMorphism(MorphismType);
impl Morphism for DummyMorphism {
fn get_type(&self) -> MorphismType {
self.0.clone().normalize()
}
fn map_morphism(&self, seq_type: TypeTerm) -> Option<DummyMorphism> {
Some(DummyMorphism(MorphismType {
src_type: TypeTerm::App(vec![
seq_type.clone(),
self.0.src_type.clone()
]),
dst_type: TypeTerm::App(vec![
seq_type.clone(),
self.0.dst_type.clone()
])
}))
}
}
#[test]
fn test_morphism_path() {
let mut dict = BimapTypeDict::new();
let mut base = MorphismBase::<u64>::new( dict.add_typename("Seq".into()) );
let mut base = MorphismBase::<DummyMorphism>::new( vec![ dict.parse("Seq").expect("") ] );
dict.add_varname("Radix".into());
dict.add_varname("SrcRadix".into());
dict.add_varname("DstRadix".into());
base.add_morphism(
MorphismType{
DummyMorphism(MorphismType{
src_type: dict.parse("<Digit Radix> ~ Char").unwrap(),
dst_type: dict.parse("<Digit Radix> ~ _2^64 ~ machine.UInt64").unwrap()
},
11
})
);
base.add_morphism(
MorphismType{
DummyMorphism(MorphismType{
src_type: dict.parse("<Digit Radix> ~ _2^64 ~ machine.UInt64").unwrap(),
dst_type: dict.parse("<Digit Radix> ~ Char").unwrap()
},
22
})
);
base.add_morphism(
MorphismType{
DummyMorphism(MorphismType{
src_type: dict.parse(" ~ <PosInt Radix BigEndian> ~ <Seq <Digit Radix>~_2^64~machine.UInt64>").unwrap(),
dst_type: dict.parse(" ~ <PosInt Radix LittleEndian> ~ <Seq <Digit Radix>~_2^64~machine.UInt64>").unwrap()
},
333
})
);
base.add_morphism(
MorphismType{
DummyMorphism(MorphismType{
src_type: dict.parse(" ~ <PosInt Radix LittleEndian> ~ <Seq <Digit Radix>~_2^64~machine.UInt64>").unwrap(),
dst_type: dict.parse(" ~ <PosInt Radix BigEndian> ~ <Seq <Digit Radix>~_2^64~machine.UInt64>").unwrap()
},
444
})
);
base.add_morphism(
MorphismType{
DummyMorphism(MorphismType{
src_type: dict.parse(" ~ <PosInt SrcRadix LittleEndian> ~ <Seq <Digit SrcRadix>~_2^64~machine.UInt64>").unwrap(),
dst_type: dict.parse(" ~ <PosInt DstRadix LittleEndian> ~ <Seq <Digit DstRadix>~_2^64~machine.UInt64>").unwrap()
},
555
})
);
assert_eq!(
base.find_morphism_path(MorphismType {
src_type: dict.parse(" ~ <PosInt 10 BigEndian> ~ <Seq <Digit 10> ~ Char>").unwrap(),
@ -66,4 +83,44 @@ fn test_morphism_path() {
]
)
);
assert_eq!(
base.find_morphism_path(MorphismType {
src_type: dict.parse("Symbol ~ ~ <PosInt 10 BigEndian> ~ <Seq <Digit 10> ~ Char>").unwrap(),
dst_type: dict.parse("Symbol ~ ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16> ~ Char>").unwrap()
}),
Some(
vec![
dict.parse("Symbol ~ ~ <PosInt 10 BigEndian> ~ <Seq <Digit 10> ~ Char>").unwrap().normalize(),
dict.parse("Symbol ~ ~ <PosInt 10 BigEndian> ~ <Seq <Digit 10> ~ _2^64 ~ machine.UInt64>").unwrap().normalize(),
dict.parse("Symbol ~ ~ <PosInt 10 LittleEndian> ~ <Seq <Digit 10> ~ _2^64 ~ machine.UInt64>").unwrap().normalize(),
dict.parse("Symbol ~ ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16> ~ _2^64 ~ machine.UInt64>").unwrap().normalize(),
dict.parse("Symbol ~ ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16> ~ _2^64 ~ machine.UInt64>").unwrap().normalize(),
dict.parse("Symbol ~ ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16> ~ Char>").unwrap().normalize(),
]
)
);
assert_eq!(
base.find_morphism_with_subtyping(
&MorphismType {
src_type: dict.parse("Symbol ~ ~ <PosInt 10 BigEndian> ~ <Seq <Digit 10> ~ Char>").unwrap(),
dst_type: dict.parse("Symbol ~ ~ <PosInt 10 BigEndian> ~ <Seq <Digit 10> ~ _2^64 ~ machine.UInt64>").unwrap()
}
),
Some((
DummyMorphism(MorphismType{
src_type: dict.parse("<Seq <Digit Radix> ~ Char>").unwrap(),
dst_type: dict.parse("<Seq <Digit Radix> ~ _2^64 ~ machine.UInt64>").unwrap()
}),
dict.parse("Symbol ~ ~ <PosInt 10 BigEndian>").unwrap(),
vec![
(dict.get_typeid(&"Radix".into()).unwrap(),
dict.parse("10").unwrap())
].into_iter().collect::<std::collections::HashMap<TypeID, TypeTerm>>()
))
);
}