further work on substitutions & subtypes in graph search
This commit is contained in:
parent
27638e3cf8
commit
0a2d77a66f
5 changed files with 314 additions and 209 deletions
|
@ -221,8 +221,8 @@ impl<M: Morphism + Clone> MorphismInstance<M> {
|
|||
pub fn get_weight(&self) -> u64 {
|
||||
match self {
|
||||
MorphismInstance::Id { τ } => 0,
|
||||
MorphismInstance::Sub { ψ, m } => 0,
|
||||
MorphismInstance::Specialize { σ, m } => 0,
|
||||
MorphismInstance::Sub { ψ, m } => m.get_weight(),
|
||||
MorphismInstance::Specialize { σ, m } => m.get_weight(),
|
||||
MorphismInstance::Primitive { m } => 10,
|
||||
MorphismInstance::Chain { path } => path.iter().map(|m| m.get_weight()).sum(),
|
||||
MorphismInstance::MapSeq { seq_repr, item_morph } => item_morph.get_weight() + 15,
|
||||
|
|
|
@ -62,7 +62,7 @@ impl<M: Morphism + Clone> MorphismBase<M> {
|
|||
}
|
||||
}
|
||||
|
||||
eprintln!("Attempt Complex Decomposition....");
|
||||
eprintln!("Attempt Complex Decomposition....");
|
||||
match (src_floor, dst_floor) {
|
||||
(TypeTerm::Struct{ struct_repr: struct_repr_lhs, members: members_lhs},
|
||||
TypeTerm::Struct { struct_repr: struct_repr_rhs, members: members_rhs })
|
||||
|
|
|
@ -42,13 +42,13 @@ pub struct SearchNode<M: Morphism+Clone> {
|
|||
weight: u64,
|
||||
|
||||
/// the advancement over pred
|
||||
step: Step<M>
|
||||
step: Step<M>,
|
||||
ψ: TypeTerm,
|
||||
}
|
||||
|
||||
pub enum Step<M: Morphism+Clone> {
|
||||
Id { τ: TypeTerm },
|
||||
Inst{ m: MorphismInstance<M> },
|
||||
Sub{ ψ: TypeTerm },
|
||||
Specialize { σ: HashMap<TypeID, TypeTerm> },
|
||||
MapSeq { item: GraphSearch<M> },
|
||||
MapStruct { members: Vec< (String, GraphSearch<M>) > },
|
||||
|
@ -62,18 +62,90 @@ impl<M:Morphism+Clone> SearchNode<M> {
|
|||
pub trait SearchNodeExt<M: Morphism+Clone> {
|
||||
fn specialize(&self, σ: HashMap<TypeID, TypeTerm>) -> Arc<RwLock<SearchNode<M>>>;
|
||||
fn chain(&self, m: &MorphismInstance<M>) -> Arc<RwLock<SearchNode<M>>>;
|
||||
fn sub(&self, ψ: TypeTerm) -> Arc<RwLock<SearchNode<M>>>;
|
||||
fn set_sub(&self, ψ: TypeTerm) -> Arc<RwLock<SearchNode<M>>>;
|
||||
fn map_seq(&self, goal: MorphismType) -> Arc<RwLock<SearchNode<M>>>;
|
||||
fn map_struct(&self, goals: Vec<(String, MorphismType)>) -> Arc<RwLock<SearchNode<M>>>;
|
||||
fn map_enum(&self, goals: Vec<(String, MorphismType)>) -> Arc<RwLock<SearchNode<M>>>;
|
||||
fn to_morphism_instance(&self) -> Option< MorphismInstance<M> >;
|
||||
fn get_type(&self) -> MorphismType;
|
||||
|
||||
fn advance(&self, base: &MorphismBase<M>, dict: &mut impl TypeDict) -> Result<bool, GraphSearchError>;
|
||||
|
||||
fn is_ready(&self) -> bool;
|
||||
fn get_weight(&self) -> u64;
|
||||
}
|
||||
|
||||
impl<M: Morphism+Clone> SearchNodeExt<M> for Arc<RwLock<SearchNode<M>>> {
|
||||
fn get_weight(&self) -> u64 {
|
||||
self.read().unwrap().weight
|
||||
}
|
||||
fn is_ready(&self) -> bool {
|
||||
let n = self.read().unwrap();
|
||||
match &n.step {
|
||||
Step::Id { τ } => true,
|
||||
//Step::Sub { ψ } => n.pred.as_ref().unwrap().is_ready(),
|
||||
Step::Specialize { σ } => true,
|
||||
Step::MapSeq { item } => {
|
||||
item.get_solution().is_some()
|
||||
}
|
||||
Step::MapStruct { members } => {
|
||||
//members.
|
||||
todo!()
|
||||
}
|
||||
Step::MapEnum { variants } => {
|
||||
todo!()
|
||||
}
|
||||
Step::Inst { m } => true
|
||||
}
|
||||
}
|
||||
|
||||
fn advance(&self, base: &MorphismBase<M>, dict: &mut impl TypeDict) -> Result<bool, GraphSearchError> {
|
||||
let mut n = self.write().unwrap();
|
||||
match &mut n.step {
|
||||
Step::MapSeq { item } => {
|
||||
eprintln!("advance seq-map");
|
||||
match item.advance(base, dict) {
|
||||
GraphSearchState::Solved(item_morph) => {
|
||||
|
||||
let map_morph = MorphismInstance::MapSeq {
|
||||
seq_repr: None,
|
||||
item_morph: Box::new(item_morph)
|
||||
};
|
||||
return Ok(false);
|
||||
}
|
||||
GraphSearchState::Err(err) => {
|
||||
return Err(err)
|
||||
},
|
||||
GraphSearchState::Continue => {
|
||||
//self.explore_queue.push(node.clone());
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
Step::MapStruct { members } => {
|
||||
todo!()
|
||||
}
|
||||
Step::MapEnum { variants } => {
|
||||
todo!()
|
||||
}
|
||||
/*
|
||||
Step::Sub { ψ } =>{
|
||||
return n.pred.as_ref().unwrap().advance(base, dict);
|
||||
}
|
||||
*/
|
||||
|
||||
_ => {}
|
||||
}
|
||||
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
fn specialize(&self, σ: HashMap<TypeID, TypeTerm>) -> Arc<RwLock<SearchNode<M>>> {
|
||||
Arc::new(RwLock::new(SearchNode {
|
||||
pred: Some(self.clone()),
|
||||
weight: self.read().unwrap().weight,
|
||||
step: Step::Specialize { σ }
|
||||
step: Step::Specialize { σ },
|
||||
ψ: self.read().unwrap().ψ.clone()
|
||||
}))
|
||||
}
|
||||
|
||||
|
@ -81,23 +153,47 @@ impl<M: Morphism+Clone> SearchNodeExt<M> for Arc<RwLock<SearchNode<M>>> {
|
|||
Arc::new(RwLock::new(SearchNode {
|
||||
pred: Some(self.clone()),
|
||||
weight: self.read().unwrap().weight + m.get_weight(),
|
||||
step: Step::Inst{ m: m.clone() }
|
||||
step: Step::Inst{ m: m.clone() },
|
||||
ψ: self.read().unwrap().ψ.clone()
|
||||
}))
|
||||
}
|
||||
|
||||
fn sub(&self, ψ: TypeTerm) -> Arc<RwLock<SearchNode<M>>> {
|
||||
fn set_sub(&self, ψ: TypeTerm) -> Arc<RwLock<SearchNode<M>>> {
|
||||
self.write().unwrap().ψ = ψ;
|
||||
self.clone()
|
||||
/*
|
||||
Arc::new(RwLock::new(SearchNode {
|
||||
pred: Some(self.clone()),
|
||||
weight: self.read().unwrap().weight,
|
||||
step: Step::Sub{ ψ }
|
||||
}))
|
||||
*/
|
||||
}
|
||||
|
||||
fn map_seq(&self, goal: MorphismType) -> Arc<RwLock<SearchNode<M>>> {
|
||||
Arc::new(RwLock::new(SearchNode {
|
||||
pred: Some(self.clone()),
|
||||
weight: self.read().unwrap().weight,
|
||||
step: Step::MapSeq { item: GraphSearch::new(goal) }
|
||||
step: Step::MapSeq { item: GraphSearch::new(goal) },
|
||||
ψ: self.read().unwrap().ψ.clone()
|
||||
}))
|
||||
}
|
||||
|
||||
fn map_struct(&self, goals: Vec<(String, MorphismType)>) -> Arc<RwLock<SearchNode<M>>> {
|
||||
Arc::new(RwLock::new(SearchNode {
|
||||
pred: Some(self.clone()),
|
||||
weight: self.read().unwrap().weight,
|
||||
step: Step::MapStruct { members: goals.into_iter().map(|(name,goal)| (name, GraphSearch::new(goal))).collect() },
|
||||
ψ: self.read().unwrap().ψ.clone()
|
||||
}))
|
||||
}
|
||||
|
||||
fn map_enum(&self, goals: Vec<(String, MorphismType)>) -> Arc<RwLock<SearchNode<M>>> {
|
||||
Arc::new(RwLock::new(SearchNode {
|
||||
pred: Some(self.clone()),
|
||||
weight: self.read().unwrap().weight,
|
||||
step: Step::MapEnum { variants: goals.into_iter().map(|(name,goal)| (name, GraphSearch::new(goal))).collect() },
|
||||
ψ: self.read().unwrap().ψ.clone()
|
||||
}))
|
||||
}
|
||||
|
||||
|
@ -106,7 +202,7 @@ impl<M: Morphism+Clone> SearchNodeExt<M> for Arc<RwLock<SearchNode<M>>> {
|
|||
let mut path = Vec::new();
|
||||
let mut subst = HashMap::new();
|
||||
|
||||
let mut halos = Vec::new();
|
||||
// let mut halos = Vec::new();
|
||||
|
||||
let mut cur_node = Some(self.clone());
|
||||
while let Some(n) = cur_node {
|
||||
|
@ -116,17 +212,25 @@ impl<M: Morphism+Clone> SearchNodeExt<M> for Arc<RwLock<SearchNode<M>>> {
|
|||
Step::Inst{ m } => {
|
||||
path.push(m.clone());
|
||||
},
|
||||
/*
|
||||
Step::Sub { ψ } => {
|
||||
halos.push(ψ.clone());
|
||||
}
|
||||
*/
|
||||
Step::Specialize { σ } => {
|
||||
subst = subst.append(&σ);
|
||||
}
|
||||
Step::MapSeq { item } => {
|
||||
path.push(MorphismInstance::MapSeq {
|
||||
let mut m = MorphismInstance::MapSeq {
|
||||
seq_repr: None,
|
||||
item_morph: Box::new(item.get_solution().expect(""))
|
||||
})
|
||||
};
|
||||
|
||||
if ! n.ψ.is_empty() {
|
||||
m = MorphismInstance::Sub { ψ: n.ψ.clone(), m: Box::new(m) };
|
||||
}
|
||||
|
||||
path.push(m);
|
||||
}
|
||||
Step::MapStruct { members } => {
|
||||
todo!();
|
||||
|
@ -151,41 +255,46 @@ impl<M: Morphism+Clone> SearchNodeExt<M> for Arc<RwLock<SearchNode<M>>> {
|
|||
MorphismInstance::Chain { path }
|
||||
};
|
||||
|
||||
|
||||
eprintln!("to_morph_instance() σ = {:?}", subst);
|
||||
|
||||
subst = subst.filter_morphtype(&m.get_type());
|
||||
if !subst.is_empty() {
|
||||
m = MorphismInstance::Specialize { σ: subst, m: Box::new(m) };
|
||||
}
|
||||
|
||||
if halos.len() > 0 {
|
||||
m = MorphismInstance::Sub { ψ: TypeTerm::Ladder(halos).normalize(), m: Box::new(m) };
|
||||
}
|
||||
|
||||
Some(m)
|
||||
}
|
||||
|
||||
fn get_type(&self) -> MorphismType {
|
||||
match &self.read().unwrap().step {
|
||||
let s = self.read().unwrap();
|
||||
let t = match &s.step {
|
||||
Step::Id { τ } => MorphismType {
|
||||
src_type: τ.clone(),
|
||||
dst_type: τ.clone()
|
||||
},
|
||||
/*
|
||||
Step::Sub { ψ } => {
|
||||
if let Some(p) = self.read().unwrap().pred.as_ref() {
|
||||
MorphismType {
|
||||
src_type: TypeTerm::Ladder(vec![ ψ.clone(), p.get_type().src_type ]).normalize(),
|
||||
dst_type: TypeTerm::Ladder(vec![ ψ.clone(), p.get_type().dst_type ]).normalize()
|
||||
}
|
||||
} else {
|
||||
unreachable!()
|
||||
MorphismType {
|
||||
src_type: TypeTerm::Ladder(vec![
|
||||
ψ.clone(),
|
||||
s.pred.as_ref().unwrap().get_type().src_type
|
||||
]),
|
||||
dst_type: TypeTerm::Ladder(vec![
|
||||
ψ.clone(),
|
||||
s.pred.as_ref().unwrap().get_type().dst_type
|
||||
])
|
||||
}
|
||||
}
|
||||
*/
|
||||
Step::Sub { ψ } => {
|
||||
todo!()
|
||||
}
|
||||
Step::Inst{ m } => m.get_type(),
|
||||
Step::Inst{ m } => {
|
||||
MorphismType {
|
||||
src_type: s.pred.as_ref().unwrap().get_type().src_type,
|
||||
dst_type: m.get_type().dst_type
|
||||
}
|
||||
},
|
||||
Step::Specialize { σ } => {
|
||||
if let Some(p) = self.read().unwrap().pred.as_ref() {
|
||||
if let Some(p) = s.pred.as_ref() {
|
||||
MorphismType {
|
||||
src_type: p.get_type().src_type.apply_subst(σ).clone().normalize(),
|
||||
dst_type: p.get_type().dst_type.apply_subst(σ).clone().normalize()
|
||||
|
@ -202,6 +311,11 @@ impl<M: Morphism+Clone> SearchNodeExt<M> for Arc<RwLock<SearchNode<M>>> {
|
|||
}
|
||||
|
||||
_ => todo!()
|
||||
};
|
||||
|
||||
MorphismType {
|
||||
src_type: TypeTerm::Ladder(vec![ s.ψ.clone(), t.src_type ]).normalize(),
|
||||
dst_type: TypeTerm::Ladder(vec![ s.ψ.clone(), t.dst_type ]).normalize(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -242,7 +356,8 @@ impl<M: Morphism+Clone> GraphSearch<M> {
|
|||
Arc::new(RwLock::new(SearchNode {
|
||||
pred: None,
|
||||
weight: 0,
|
||||
step: Step::Id { τ: goal.src_type.clone() }
|
||||
step: Step::Id { τ: goal.src_type.clone() },
|
||||
ψ: TypeTerm::unit()
|
||||
}))
|
||||
]
|
||||
}
|
||||
|
@ -268,9 +383,9 @@ impl<M: Morphism+Clone> GraphSearch<M> {
|
|||
let goal= self.goal.clone();
|
||||
self.explore_queue.sort_by(
|
||||
|a,b| {
|
||||
Self::est_remain(&goal, b)
|
||||
(Self::est_remain(&goal, b) + b.get_weight() )
|
||||
.cmp(
|
||||
&Self::est_remain(&goal, a)
|
||||
&(Self::est_remain(&goal, a) + a.get_weight())
|
||||
)
|
||||
}
|
||||
);
|
||||
|
@ -281,7 +396,8 @@ impl<M: Morphism+Clone> GraphSearch<M> {
|
|||
);
|
||||
for i in 1 ..= usize::min(self.explore_queue.len(), 5) {
|
||||
let n = &self.explore_queue[self.explore_queue.len() - i];
|
||||
eprintln!("[[ {} ]] (est remain: {}) --- {} --> {}", i,
|
||||
eprintln!("[[ {} ]] (weight: {} + est remain: {}) --- {} --> {}", i,
|
||||
n.get_weight(),
|
||||
Self::est_remain(&goal, &n),
|
||||
n.get_type().src_type.pretty(dict, 0),
|
||||
n.get_type().dst_type.pretty(dict, 0));
|
||||
|
@ -292,34 +408,19 @@ impl<M: Morphism+Clone> GraphSearch<M> {
|
|||
|
||||
pub fn advance(&mut self, base: &MorphismBase<M>, dict: &mut impl TypeDict) -> GraphSearchState<M> {
|
||||
if let Some(node) = self.choose_next_node(dict) {
|
||||
{
|
||||
let mut n = node.write().unwrap();
|
||||
match &mut n.step {
|
||||
Step::MapSeq { item } => {
|
||||
eprintln!("advance seq-map");
|
||||
match item.advance(base, dict) {
|
||||
GraphSearchState::Solved(item_morph) => {
|
||||
let map_morph = MorphismInstance::MapSeq {
|
||||
seq_repr: None,
|
||||
item_morph: Box::new(item_morph)
|
||||
};
|
||||
}
|
||||
GraphSearchState::Err(err) => {
|
||||
return GraphSearchState::Err(err);
|
||||
},
|
||||
GraphSearchState::Continue => {
|
||||
self.explore_queue.push(node.clone());
|
||||
return GraphSearchState::Continue;
|
||||
}
|
||||
}
|
||||
match node.advance(base, dict) {
|
||||
Ok(_) => {
|
||||
if ! node.is_ready() {
|
||||
self.explore_queue.push(node);
|
||||
return GraphSearchState::Continue;
|
||||
} else {
|
||||
let w = node.to_morphism_instance().unwrap().get_weight();
|
||||
eprintln!("set Weight of complex morph to {}", w);
|
||||
node.write().unwrap().weight = w;
|
||||
}
|
||||
Step::MapStruct { members } => {
|
||||
todo!()
|
||||
}
|
||||
Step::MapEnum { variants } => {
|
||||
todo!()
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Err(err) => {
|
||||
return GraphSearchState::Err(err);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -330,38 +431,20 @@ impl<M: Morphism+Clone> GraphSearch<M> {
|
|||
if σ.is_empty() {
|
||||
node.to_morphism_instance().unwrap()
|
||||
} else {
|
||||
node.specialize(σ).to_morphism_instance().unwrap()
|
||||
node.specialize( σ.filter_morphtype(&node.get_type()) ).to_morphism_instance().unwrap()
|
||||
}
|
||||
);
|
||||
|
||||
return GraphSearchState::Solved(self.get_solution().unwrap());
|
||||
}
|
||||
else if let Some((ψ, decomposition)) = base.morphism_decomposition(&node.get_type().dst_type, &self.goal.dst_type) {
|
||||
eprintln!("decomposition to end!");
|
||||
let final_node = match decomposition {
|
||||
DecomposedMorphismType::SeqMap { item } => {
|
||||
eprintln!("decomposed to seq map with item : {} -> {} ", item.src_type.pretty(dict, 0), item.dst_type.pretty(dict,0));
|
||||
node.map_seq( item )
|
||||
},
|
||||
DecomposedMorphismType::StructMap { members } => {
|
||||
//node.sub(ψ).map_struct( members )
|
||||
/*
|
||||
Step::MapStruct { members: members.into_iter().map(
|
||||
|(symbol, ty)| (symbol, GraphSearch::new(ty))
|
||||
).collect() }
|
||||
*/
|
||||
todo!()
|
||||
},
|
||||
DecomposedMorphismType::EnumMap { variants } => {
|
||||
/*
|
||||
Step::MapEnum { variants: variants.into_iter().map(
|
||||
|(symbol, ty)| (symbol, GraphSearch::new(ty))
|
||||
).collect() }
|
||||
*/
|
||||
todo!()
|
||||
},
|
||||
};
|
||||
self.explore_queue.push(final_node);
|
||||
self.explore_queue.push(
|
||||
match decomposition {
|
||||
DecomposedMorphismType::SeqMap { item } => { node.map_seq( item ) },
|
||||
DecomposedMorphismType::StructMap { members } => { node.map_struct(members) },
|
||||
DecomposedMorphismType::EnumMap { variants } => { node.map_enum(variants) },
|
||||
}.set_sub(ψ.clone())
|
||||
);
|
||||
//return GraphSearchState::Continue;
|
||||
}
|
||||
|
||||
|
@ -375,27 +458,11 @@ impl<M: Morphism+Clone> GraphSearch<M> {
|
|||
for (ψ,decomposition) in base.enum_complex_morphisms(&node.get_type().dst_type) {
|
||||
eprintln!("add decomposition!");
|
||||
self.explore_queue.push(
|
||||
Arc::new(RwLock::new(
|
||||
SearchNode {
|
||||
pred: Some(node.clone()),
|
||||
weight: 0,
|
||||
step: match decomposition {
|
||||
DecomposedMorphismType::SeqMap { item } => {
|
||||
Step::MapSeq { item: GraphSearch::new(item) }
|
||||
},
|
||||
DecomposedMorphismType::StructMap { members } => {
|
||||
Step::MapStruct { members: members.into_iter().map(
|
||||
|(symbol, ty)| (symbol, GraphSearch::new(ty))
|
||||
).collect() }
|
||||
},
|
||||
DecomposedMorphismType::EnumMap { variants } => {
|
||||
Step::MapEnum { variants: variants.into_iter().map(
|
||||
|(symbol, ty)| (symbol, GraphSearch::new(ty))
|
||||
).collect() }
|
||||
},
|
||||
},
|
||||
}
|
||||
)).sub(ψ)
|
||||
match decomposition {
|
||||
DecomposedMorphismType::SeqMap { item } => { node.map_seq( item ) },
|
||||
DecomposedMorphismType::StructMap { members } => { node.map_struct(members) },
|
||||
DecomposedMorphismType::EnumMap { variants } => { node.map_enum(variants) },
|
||||
}.set_sub(ψ)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -12,6 +12,9 @@ pub trait Substitution {
|
|||
fn get(&self, t: &TypeID) -> Option< TypeTerm >;
|
||||
fn add(&mut self, tyid: TypeID, val: TypeTerm);
|
||||
fn append(self, rhs: &Self) -> Self;
|
||||
fn filter(self, f: impl FnMut(&(TypeID, TypeTerm)) -> bool) -> Self;
|
||||
|
||||
fn filter_morphtype(self, ty: &crate::MorphismType) -> Self;
|
||||
}
|
||||
|
||||
impl Substitution for std::collections::HashMap< TypeID, TypeTerm > {
|
||||
|
@ -43,6 +46,21 @@ impl Substitution for std::collections::HashMap< TypeID, TypeTerm > {
|
|||
|
||||
new_σ
|
||||
}
|
||||
|
||||
fn filter(self, f: impl FnMut(&(TypeID, TypeTerm)) -> bool) -> Self {
|
||||
self.into_iter().filter(f).collect()
|
||||
}
|
||||
|
||||
fn filter_morphtype(self, ty: &crate::MorphismType) -> Self {
|
||||
self.filter(|(v,t)| {
|
||||
if let TypeID::Var(v) = v {
|
||||
ty.src_type.contains_var(*v) ||
|
||||
ty.dst_type.contains_var(*v)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeTerm {
|
||||
|
|
|
@ -68,7 +68,6 @@ fn morphism_test_setup() -> ( BimapTypeDict, MorphismBase<DummyMorphism> ) {
|
|||
})
|
||||
);
|
||||
|
||||
/*
|
||||
base.add_morphism(
|
||||
DummyMorphism(MorphismType{
|
||||
src_type: dict.parse_desugared("ℕ ~ <PosInt Radix BigEndian> ~ <Seq <Digit Radix>~ℤ_2^64~machine.UInt64>").unwrap().sugar(&mut dict),
|
||||
|
@ -81,7 +80,6 @@ fn morphism_test_setup() -> ( BimapTypeDict, MorphismBase<DummyMorphism> ) {
|
|||
dst_type: dict.parse_desugared("ℕ ~ <PosInt Radix BigEndian> ~ <Seq <Digit Radix>~ℤ_2^64~machine.UInt64>").unwrap().sugar(&mut dict)
|
||||
})
|
||||
);
|
||||
*/
|
||||
base.add_morphism(
|
||||
DummyMorphism(MorphismType{
|
||||
src_type: dict.parse_desugared("ℕ ~ <PosInt SrcRadix LittleEndian> ~ <Seq <Digit SrcRadix>~ℤ_2^64~machine.UInt64>").unwrap().sugar(&mut dict),
|
||||
|
@ -89,15 +87,12 @@ fn morphism_test_setup() -> ( BimapTypeDict, MorphismBase<DummyMorphism> ) {
|
|||
})
|
||||
);
|
||||
|
||||
/*
|
||||
|
||||
base.add_morphism(
|
||||
DummyMorphism(MorphismType{
|
||||
src_type: dict.parse_desugared("ℤ_2^64 ~ ℕ ~ <PosInt SrcRadix LittleEndian> ~ <Seq <Digit SrcRadix>~ℤ_2^64~machine.UInt64>").unwrap().sugar(&mut dict),
|
||||
dst_type: dict.parse_desugared("ℤ_2^64 ~ machine.UInt64").unwrap().sugar(&mut dict)
|
||||
})
|
||||
);
|
||||
*/
|
||||
base.add_morphism(
|
||||
DummyMorphism(MorphismType{
|
||||
src_type: dict.parse_desugared("ℤ_2^64 ~ machine.UInt64").unwrap().sugar(&mut dict),
|
||||
|
@ -166,7 +161,7 @@ fn test_morphgraph_chain() {
|
|||
path: vec![
|
||||
MorphismInstance::Specialize {
|
||||
σ: vec![
|
||||
(dict.get_typeid(&"Radix".into()).unwrap(), TypeTerm::Num(10))
|
||||
(dict.get_typeid(&"Radix".into()).unwrap(), TypeTerm::Num(10)),
|
||||
].into_iter().collect(),
|
||||
m: Box::new(
|
||||
MorphismInstance::Primitive {
|
||||
|
@ -234,115 +229,140 @@ fn test_morphism_path3() {
|
|||
dst_type: dict.parse("ℕ ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16> ~ ℤ_2^64 ~ machine.UInt64>").unwrap(),
|
||||
}, &mut dict),
|
||||
Ok(
|
||||
MorphismInstance::Chain {
|
||||
path: vec![
|
||||
MorphismInstance::MapSeq {
|
||||
seq_repr: None,
|
||||
item_morph: Box::new(
|
||||
MorphismInstance::Sub {
|
||||
ψ: dict.parse("ℕ ~ <PosInt 10 LittleEndian>").expect(""),
|
||||
m: Box::new(
|
||||
MorphismInstance::Specialize {
|
||||
σ: vec![
|
||||
(dict.get_typeid(&"Radix".into()).unwrap(), TypeTerm::Num(10)),
|
||||
].into_iter().collect(),
|
||||
m: Box::new(MorphismInstance::Primitive {
|
||||
m: DummyMorphism(MorphismType {
|
||||
src_type: dict.parse("<Digit Radix> ~ Char").unwrap(),
|
||||
dst_type: dict.parse("<Digit Radix> ~ ℤ_2^64 ~ machine.UInt64").unwrap()
|
||||
}),
|
||||
})
|
||||
}
|
||||
)
|
||||
}
|
||||
)
|
||||
},
|
||||
MorphismInstance::Specialize {
|
||||
σ: vec![
|
||||
(dict.get_typeid(&"DstRadix".into()).unwrap(), TypeTerm::Num(16)),
|
||||
].into_iter().collect(),
|
||||
m: Box::new(
|
||||
MorphismInstance::Chain {
|
||||
path: vec![
|
||||
MorphismInstance::Sub {
|
||||
ψ: dict.parse("ℕ ~ <PosInt 10 LittleEndian>").expect(""),
|
||||
m: Box::new(
|
||||
MorphismInstance::MapSeq {
|
||||
seq_repr: None,
|
||||
item_morph: Box::new(
|
||||
MorphismInstance::Specialize {
|
||||
σ: vec![
|
||||
(dict.get_typeid(&"Radix".into()).unwrap(), TypeTerm::Num(10)),
|
||||
].into_iter().collect(),
|
||||
m: Box::new(MorphismInstance::Primitive {
|
||||
m: DummyMorphism(MorphismType {
|
||||
src_type: dict.parse("<Digit Radix> ~ Char").unwrap(),
|
||||
dst_type: dict.parse("<Digit Radix> ~ ℤ_2^64 ~ machine.UInt64").unwrap()
|
||||
}),
|
||||
})
|
||||
}
|
||||
)
|
||||
}
|
||||
)
|
||||
},
|
||||
|
||||
MorphismInstance::Specialize {
|
||||
σ: vec![
|
||||
(dict.get_typeid(&"SrcRadix".into()).unwrap(), TypeTerm::Num(10)),
|
||||
(dict.get_typeid(&"DstRadix".into()).unwrap(), TypeTerm::Num(16)),
|
||||
].into_iter().collect(),
|
||||
m: Box::new(
|
||||
MorphismInstance::Primitive{
|
||||
m: DummyMorphism(MorphismType {
|
||||
src_type: dict.parse_desugared("ℕ ~ <PosInt SrcRadix LittleEndian> ~ <Seq <Digit SrcRadix> ~ ℤ_2^64 ~ machine.UInt64>").unwrap().sugar(&mut dict),
|
||||
dst_type: dict.parse_desugared("ℕ ~ <PosInt DstRadix LittleEndian> ~ <Seq <Digit DstRadix> ~ ℤ_2^64 ~ machine.UInt64>").unwrap().sugar(&mut dict)
|
||||
}),
|
||||
}
|
||||
)
|
||||
}
|
||||
]
|
||||
}
|
||||
));
|
||||
MorphismInstance::Specialize {
|
||||
σ: vec![
|
||||
(dict.get_typeid(&"SrcRadix".into()).unwrap(), TypeTerm::Num(10)),
|
||||
].into_iter().collect(),
|
||||
m: Box::new(
|
||||
MorphismInstance::Primitive{
|
||||
m: DummyMorphism(MorphismType {
|
||||
src_type: dict.parse_desugared("ℕ ~ <PosInt SrcRadix LittleEndian> ~ <Seq <Digit SrcRadix> ~ ℤ_2^64 ~ machine.UInt64>").unwrap().sugar(&mut dict),
|
||||
dst_type: dict.parse_desugared("ℕ ~ <PosInt DstRadix LittleEndian> ~ <Seq <Digit DstRadix> ~ ℤ_2^64 ~ machine.UInt64>").unwrap().sugar(&mut dict)
|
||||
}),
|
||||
}
|
||||
)
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
#[test]
|
||||
fn test_morphism_path4() {
|
||||
let (mut dict, mut base) = morphism_test_setup();
|
||||
|
||||
let path = ShortestPathProblem::new(&base, MorphismType {
|
||||
src_type: dict.parse_desugared("ℕ ~ <PosInt 10 LittleEndian> ~ <Seq <Digit 10> ~ Char>").unwrap().sugar(&mut dict),
|
||||
dst_type: dict.parse_desugared("ℕ ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16> ~ Char>").unwrap().sugar(&mut dict)
|
||||
}).solve();
|
||||
|
||||
if let Some(path) = path.as_ref() {
|
||||
print_path(&mut dict, path);
|
||||
}
|
||||
let mut morph_graph = MorphismGraph::new(base);
|
||||
|
||||
assert_eq!(
|
||||
path,
|
||||
Some(
|
||||
vec![
|
||||
MorphismInstance::MapSeq {
|
||||
ψ: dict.parse_desugared("ℕ ~ <PosInt 10 LittleEndian>").expect("").sugar(&mut dict),
|
||||
seq_repr: None,
|
||||
item_morph: Box::new(MorphismInstance::Primitive {
|
||||
σ: vec![
|
||||
(dict.get_typeid(&"Radix".into()).unwrap(), TypeTerm::Num(10)),
|
||||
].into_iter().collect(),
|
||||
ψ: TypeTerm::unit(),
|
||||
morph: DummyMorphism(MorphismType {
|
||||
src_type: dict.parse_desugared("<Digit Radix> ~ Char").unwrap().sugar(&mut dict),
|
||||
dst_type: dict.parse_desugared("<Digit Radix> ~ ℤ_2^64 ~ machine.UInt64").unwrap().sugar(&mut dict)
|
||||
}),
|
||||
})
|
||||
},
|
||||
morph_graph.search(MorphismType {
|
||||
src_type: dict.parse("ℕ ~ <PosInt 10 LittleEndian> ~ <Seq <Digit 10> ~ Char>").unwrap(),
|
||||
dst_type: dict.parse("ℕ ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16> ~ Char>").unwrap()
|
||||
}, &mut dict),
|
||||
|
||||
MorphismInstance::Primitive {
|
||||
σ: vec![
|
||||
(dict.get_typeid(&"SrcRadix".into()).unwrap(), TypeTerm::Num(10)),
|
||||
(dict.get_typeid(&"DstRadix".into()).unwrap(), TypeTerm::Num(16)),
|
||||
].into_iter().collect(),
|
||||
ψ: TypeTerm::unit(),
|
||||
morph: DummyMorphism(MorphismType {
|
||||
src_type: dict.parse_desugared("ℕ ~ <PosInt SrcRadix LittleEndian> ~ <Seq <Digit SrcRadix> ~ ℤ_2^64 ~ machine.UInt64>").unwrap().sugar(&mut dict),
|
||||
dst_type: dict.parse_desugared("ℕ ~ <PosInt DstRadix LittleEndian> ~ <Seq <Digit DstRadix> ~ ℤ_2^64 ~ machine.UInt64>").unwrap().sugar(&mut dict)
|
||||
}),
|
||||
},
|
||||
Ok(
|
||||
MorphismInstance::Specialize {
|
||||
σ: vec![
|
||||
(dict.get_typeid(&"DstRadix".into()).unwrap(), TypeTerm::Num(16)),
|
||||
].into_iter().collect(),
|
||||
m: Box::new(
|
||||
MorphismInstance::Chain {
|
||||
path: vec![
|
||||
MorphismInstance::Sub {
|
||||
ψ: dict.parse("ℕ ~ <PosInt 10 LittleEndian>").expect(""),
|
||||
m: Box::new(
|
||||
MorphismInstance::MapSeq {
|
||||
seq_repr: None,
|
||||
item_morph: Box::new(
|
||||
MorphismInstance::Specialize {
|
||||
σ: vec![
|
||||
(dict.get_typeid(&"Radix".into()).unwrap(), TypeTerm::Num(10)),
|
||||
].into_iter().collect(),
|
||||
m: Box::new(MorphismInstance::Primitive {
|
||||
m: DummyMorphism(MorphismType {
|
||||
src_type: dict.parse("<Digit Radix> ~ Char").unwrap(),
|
||||
dst_type: dict.parse("<Digit Radix> ~ ℤ_2^64 ~ machine.UInt64").unwrap()
|
||||
}),
|
||||
})
|
||||
}
|
||||
)
|
||||
}
|
||||
)
|
||||
},
|
||||
|
||||
MorphismInstance::MapSeq {
|
||||
ψ: dict.parse_desugared("ℕ ~ <PosInt 16 LittleEndian>").expect("").sugar(&mut dict),
|
||||
seq_repr: None,
|
||||
item_morph: Box::new(MorphismInstance::Primitive {
|
||||
σ: vec![
|
||||
(dict.get_typeid(&"Radix".into()).unwrap(), TypeTerm::Num(16)),
|
||||
].into_iter().collect(),
|
||||
ψ: TypeTerm::unit(),
|
||||
morph: DummyMorphism(MorphismType {
|
||||
src_type: dict.parse_desugared("<Digit Radix> ~ ℤ_2^64 ~ machine.UInt64").unwrap().sugar(&mut dict),
|
||||
dst_type: dict.parse_desugared("<Digit Radix> ~ Char").unwrap().sugar(&mut dict)
|
||||
}),
|
||||
})
|
||||
},
|
||||
]
|
||||
));
|
||||
MorphismInstance::Specialize {
|
||||
σ: vec![
|
||||
(dict.get_typeid(&"SrcRadix".into()).unwrap(), TypeTerm::Num(10)),
|
||||
(dict.get_typeid(&"Radix".into()).unwrap(), TypeTerm::Num(10)),
|
||||
].into_iter().collect(),
|
||||
m: Box::new(
|
||||
MorphismInstance::Primitive{
|
||||
m: DummyMorphism(MorphismType {
|
||||
src_type: dict.parse_desugared("ℕ ~ <PosInt SrcRadix LittleEndian> ~ <Seq <Digit SrcRadix> ~ ℤ_2^64 ~ machine.UInt64>").unwrap().sugar(&mut dict),
|
||||
dst_type: dict.parse_desugared("ℕ ~ <PosInt DstRadix LittleEndian> ~ <Seq <Digit DstRadix> ~ ℤ_2^64 ~ machine.UInt64>").unwrap().sugar(&mut dict)
|
||||
}),
|
||||
}
|
||||
)
|
||||
},
|
||||
|
||||
MorphismInstance::Sub {
|
||||
ψ: dict.parse("ℕ ~ <PosInt DstRadix LittleEndian>").expect(""),
|
||||
m: Box::new(
|
||||
MorphismInstance::MapSeq {
|
||||
seq_repr: None,
|
||||
item_morph: Box::new(
|
||||
MorphismInstance::Specialize {
|
||||
σ: vec![
|
||||
(dict.get_typeid(&"Radix".into()).unwrap(), dict.parse("DstRadix").expect("")),
|
||||
].into_iter().collect(),
|
||||
m: Box::new(MorphismInstance::Primitive {
|
||||
m: DummyMorphism(MorphismType {
|
||||
src_type: dict.parse("<Digit Radix> ~ ℤ_2^64 ~ machine.UInt64").unwrap(),
|
||||
dst_type: dict.parse("<Digit Radix> ~ Char").unwrap()
|
||||
}),
|
||||
})
|
||||
}
|
||||
)
|
||||
}
|
||||
)
|
||||
},
|
||||
]
|
||||
}
|
||||
)
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
|
||||
#[test]
|
||||
fn test_morphism_path_posint() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue