repr tree: add type alias ReprTreeArc
This commit is contained in:
parent
dad789227c
commit
41c02465be
3 changed files with 19 additions and 17 deletions
|
@ -1,7 +1,7 @@
|
||||||
use {
|
use {
|
||||||
crate::{
|
crate::{
|
||||||
edit_tree::EditTree,
|
edit_tree::EditTree,
|
||||||
repr_tree::{GenericReprTreeMorphism, ReprTree, ReprTreeExt},
|
repr_tree::{GenericReprTreeMorphism, ReprTree, ReprTreeExt, ReprTreeArc},
|
||||||
},
|
},
|
||||||
laddertypes::{
|
laddertypes::{
|
||||||
parser::ParseLadderType, sugar::SugaredTypeTerm, unparser::UnparseLadderType,
|
parser::ParseLadderType, sugar::SugaredTypeTerm, unparser::UnparseLadderType,
|
||||||
|
@ -152,7 +152,7 @@ impl Context {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn make_repr(ctx: &Arc<RwLock<Self>>, t: &TypeTerm) -> Arc<RwLock<ReprTree>> {
|
pub fn make_repr(ctx: &Arc<RwLock<Self>>, t: &TypeTerm) -> ReprTreeArc {
|
||||||
let rt = Arc::new(RwLock::new(ReprTree::new( TypeTerm::unit() )));
|
let rt = Arc::new(RwLock::new(ReprTree::new( TypeTerm::unit() )));
|
||||||
ctx.read().unwrap().apply_morphism( &rt, &MorphismType{ src_type: TypeTerm::unit(), dst_type: t.clone() } );
|
ctx.read().unwrap().apply_morphism( &rt, &MorphismType{ src_type: TypeTerm::unit(), dst_type: t.clone() } );
|
||||||
rt
|
rt
|
||||||
|
@ -242,7 +242,7 @@ impl Context {
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_obj(&self, name: &String) -> Option< Arc<RwLock<ReprTree>> > {
|
pub fn get_obj(&self, name: &String) -> Option< ReprTreeArc > {
|
||||||
if let Some(obj) = self.nodes.get(name) {
|
if let Some(obj) = self.nodes.get(name) {
|
||||||
Some(obj.clone())
|
Some(obj.clone())
|
||||||
} else if let Some(parent) = self.parent.as_ref() {
|
} else if let Some(parent) = self.parent.as_ref() {
|
||||||
|
|
|
@ -9,7 +9,7 @@ mod tests;
|
||||||
pub use {
|
pub use {
|
||||||
context::{Context},
|
context::{Context},
|
||||||
leaf::ReprLeaf,
|
leaf::ReprLeaf,
|
||||||
node::ReprTree,
|
node::{ReprTree, ReprTreeArc},
|
||||||
morphism::{GenericReprTreeMorphism}
|
morphism::{GenericReprTreeMorphism}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ pub trait ReprTreeExt {
|
||||||
fn insert_leaf(&mut self, type_ladder: impl Into<TypeTerm>, leaf: ReprLeaf);
|
fn insert_leaf(&mut self, type_ladder: impl Into<TypeTerm>, leaf: ReprLeaf);
|
||||||
fn insert_branch(&mut self, repr: Arc<RwLock<ReprTree>>);
|
fn insert_branch(&mut self, repr: Arc<RwLock<ReprTree>>);
|
||||||
fn create_branch(&mut self, rung: impl Into<TypeTerm>);
|
fn create_branch(&mut self, rung: impl Into<TypeTerm>);
|
||||||
fn descend(&self, target_type: impl Into<TypeTerm>) -> Option<Arc<RwLock<ReprTree>>>;
|
fn descend(&self, target_type: impl Into<TypeTerm>) -> Option< ReprTreeArc >;
|
||||||
|
|
||||||
fn attach_leaf_to<V: View + ?Sized + 'static>(&self, t: impl Into<TypeTerm>, v: OuterViewPort<V>) where V::Msg: Clone;
|
fn attach_leaf_to<V: View + ?Sized + 'static>(&self, t: impl Into<TypeTerm>, v: OuterViewPort<V>) where V::Msg: Clone;
|
||||||
fn get_port<V: View + ?Sized + 'static>(&self) -> Option<OuterViewPort<V>> where V::Msg: Clone;
|
fn get_port<V: View + ?Sized + 'static>(&self) -> Option<OuterViewPort<V>> where V::Msg: Clone;
|
||||||
|
@ -100,7 +100,7 @@ impl ReprTreeExt for Arc<RwLock<ReprTree>> {
|
||||||
self.write().unwrap().attach_leaf_to::<V>(type_ladder.into().get_lnf_vec().into_iter(), v)
|
self.write().unwrap().attach_leaf_to::<V>(type_ladder.into().get_lnf_vec().into_iter(), v)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn descend(&self, target_type: impl Into<TypeTerm>) -> Option<Arc<RwLock<ReprTree>>> {
|
fn descend(&self, target_type: impl Into<TypeTerm>) -> Option< ReprTreeArc > {
|
||||||
ReprTree::descend( self, target_type )
|
ReprTree::descend( self, target_type )
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,10 +28,12 @@ use {
|
||||||
pub struct ReprTree {
|
pub struct ReprTree {
|
||||||
halo: TypeTerm,
|
halo: TypeTerm,
|
||||||
type_tag: TypeTerm,
|
type_tag: TypeTerm,
|
||||||
branches: HashMap<TypeTerm, Arc<RwLock<ReprTree>>>,
|
branches: HashMap<TypeTerm, ReprTreeArc>,
|
||||||
leaf: Option< ReprLeaf >
|
leaf: Option< ReprLeaf >
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub type ReprTreeArc = Arc<RwLock<ReprTree>>;
|
||||||
|
|
||||||
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
|
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
|
||||||
|
|
||||||
impl std::fmt::Debug for ReprTree {
|
impl std::fmt::Debug for ReprTree {
|
||||||
|
@ -67,7 +69,7 @@ impl ReprTree {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_arc(type_tag: impl Into<TypeTerm>) -> Arc<RwLock<Self>> {
|
pub fn new_arc(type_tag: impl Into<TypeTerm>) -> ReprTreeArc {
|
||||||
Arc::new(RwLock::new(Self::new(type_tag)))
|
Arc::new(RwLock::new(Self::new(type_tag)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,7 +108,7 @@ impl ReprTree {
|
||||||
leaf_types
|
leaf_types
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_branch(&mut self, repr: Arc<RwLock<ReprTree>>) {
|
pub fn insert_branch(&mut self, repr: ReprTreeArc) {
|
||||||
let branch_type = repr.read().unwrap().get_type().clone();
|
let branch_type = repr.read().unwrap().get_type().clone();
|
||||||
|
|
||||||
assert!(branch_type.is_flat());
|
assert!(branch_type.is_flat());
|
||||||
|
@ -119,14 +121,14 @@ impl ReprTree {
|
||||||
self.branches.insert(branch_type, repr.clone());
|
self.branches.insert(branch_type, repr.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_char(ctx: &Arc<RwLock<Context>>, c: char ) -> Arc<RwLock<Self>> {
|
pub fn from_char(ctx: &Arc<RwLock<Context>>, c: char ) -> ReprTreeArc {
|
||||||
ReprTree::from_singleton_buffer(
|
ReprTree::from_singleton_buffer(
|
||||||
Context::parse(ctx, "Char"),
|
Context::parse(ctx, "Char"),
|
||||||
SingletonBuffer::new(c)
|
SingletonBuffer::new(c)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_view<V>( type_tag: impl Into<TypeTerm>, view: OuterViewPort<V> ) -> Arc<RwLock<Self>>
|
pub fn from_view<V>( type_tag: impl Into<TypeTerm>, view: OuterViewPort<V> ) -> ReprTreeArc
|
||||||
where V: View + ?Sized + 'static,
|
where V: View + ?Sized + 'static,
|
||||||
V::Msg: Clone
|
V::Msg: Clone
|
||||||
{
|
{
|
||||||
|
@ -135,7 +137,7 @@ impl ReprTree {
|
||||||
Arc::new(RwLock::new(rt))
|
Arc::new(RwLock::new(rt))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_singleton_buffer<T>( type_tag: impl Into<TypeTerm>, buf: SingletonBuffer<T> ) -> Arc<RwLock<Self>>
|
pub fn from_singleton_buffer<T>( type_tag: impl Into<TypeTerm>, buf: SingletonBuffer<T> ) -> ReprTreeArc
|
||||||
where T: Clone + Send + Sync + 'static
|
where T: Clone + Send + Sync + 'static
|
||||||
{
|
{
|
||||||
let mut rt = ReprTree::new(type_tag);
|
let mut rt = ReprTree::new(type_tag);
|
||||||
|
@ -146,7 +148,7 @@ impl ReprTree {
|
||||||
pub fn from_str(
|
pub fn from_str(
|
||||||
type_tag: impl Into<TypeTerm>,
|
type_tag: impl Into<TypeTerm>,
|
||||||
val: &str
|
val: &str
|
||||||
) -> Arc<RwLock<Self>> {
|
) -> ReprTreeArc {
|
||||||
let mut lnf = type_tag.into().get_lnf_vec();
|
let mut lnf = type_tag.into().get_lnf_vec();
|
||||||
|
|
||||||
let mut rt = ReprTree::from_vec_buffer(
|
let mut rt = ReprTree::from_vec_buffer(
|
||||||
|
@ -163,7 +165,7 @@ impl ReprTree {
|
||||||
rt
|
rt
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_vec_buffer<T>( type_tag: impl Into<TypeTerm>, buf: VecBuffer<T> ) -> Arc<RwLock<Self>>
|
pub fn from_vec_buffer<T>( type_tag: impl Into<TypeTerm>, buf: VecBuffer<T> ) -> ReprTreeArc
|
||||||
where T: Clone + Send + Sync + 'static
|
where T: Clone + Send + Sync + 'static
|
||||||
{
|
{
|
||||||
let mut rt = ReprTree::new(type_tag);
|
let mut rt = ReprTree::new(type_tag);
|
||||||
|
@ -340,13 +342,13 @@ impl ReprTree {
|
||||||
|
|
||||||
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
|
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
|
||||||
|
|
||||||
pub fn descend_one(&self, dst_type: impl Into<TypeTerm>) -> Option<Arc<RwLock<ReprTree>>> {
|
pub fn descend_one(&self, dst_type: impl Into<TypeTerm>) -> Option< ReprTreeArc > {
|
||||||
let dst_type = dst_type.into();
|
let dst_type = dst_type.into();
|
||||||
assert!( dst_type.is_flat() );
|
assert!( dst_type.is_flat() );
|
||||||
self.branches.get(&dst_type).cloned()
|
self.branches.get(&dst_type).cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn descend_ladder(rt: &Arc<RwLock<Self>>, mut repr_ladder: impl Iterator<Item = TypeTerm>) -> Option<Arc<RwLock<ReprTree>>> {
|
pub fn descend_ladder(rt: &Arc<RwLock<Self>>, mut repr_ladder: impl Iterator<Item = TypeTerm>) -> Option< ReprTreeArc > {
|
||||||
if let Some(first) = repr_ladder.next() {
|
if let Some(first) = repr_ladder.next() {
|
||||||
let rt = rt.read().unwrap();
|
let rt = rt.read().unwrap();
|
||||||
repr_ladder.fold(
|
repr_ladder.fold(
|
||||||
|
@ -357,7 +359,7 @@ impl ReprTree {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn descend(rt: &Arc<RwLock<Self>>, dst_type: impl Into<TypeTerm>) -> Option<Arc<RwLock<ReprTree>>> {
|
pub fn descend(rt: &Arc<RwLock<Self>>, dst_type: impl Into<TypeTerm>) -> Option< ReprTreeArc > {
|
||||||
let mut lnf = dst_type.into().get_lnf_vec();
|
let mut lnf = dst_type.into().get_lnf_vec();
|
||||||
if lnf.len() > 0 {
|
if lnf.len() > 0 {
|
||||||
if lnf[0] == rt.get_type() {
|
if lnf[0] == rt.get_type() {
|
||||||
|
|
Loading…
Reference in a new issue