repr tree: add type alias ReprTreeArc

This commit is contained in:
Michael Sippel 2024-11-17 14:15:17 +01:00
parent dad789227c
commit 41c02465be
Signed by: senvas
GPG key ID: F96CF119C34B64A6
3 changed files with 19 additions and 17 deletions

View file

@ -1,7 +1,7 @@
use {
crate::{
edit_tree::EditTree,
repr_tree::{GenericReprTreeMorphism, ReprTree, ReprTreeExt},
repr_tree::{GenericReprTreeMorphism, ReprTree, ReprTreeExt, ReprTreeArc},
},
laddertypes::{
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() )));
ctx.read().unwrap().apply_morphism( &rt, &MorphismType{ src_type: TypeTerm::unit(), dst_type: t.clone() } );
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) {
Some(obj.clone())
} else if let Some(parent) = self.parent.as_ref() {

View file

@ -9,7 +9,7 @@ mod tests;
pub use {
context::{Context},
leaf::ReprLeaf,
node::ReprTree,
node::{ReprTree, ReprTreeArc},
morphism::{GenericReprTreeMorphism}
};
@ -42,7 +42,7 @@ pub trait ReprTreeExt {
fn insert_leaf(&mut self, type_ladder: impl Into<TypeTerm>, leaf: ReprLeaf);
fn insert_branch(&mut self, repr: Arc<RwLock<ReprTree>>);
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 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)
}
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 )
}

View file

@ -28,10 +28,12 @@ use {
pub struct ReprTree {
halo: TypeTerm,
type_tag: TypeTerm,
branches: HashMap<TypeTerm, Arc<RwLock<ReprTree>>>,
branches: HashMap<TypeTerm, ReprTreeArc>,
leaf: Option< ReprLeaf >
}
pub type ReprTreeArc = Arc<RwLock<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)))
}
@ -106,7 +108,7 @@ impl ReprTree {
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();
assert!(branch_type.is_flat());
@ -119,14 +121,14 @@ impl ReprTree {
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(
Context::parse(ctx, "Char"),
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,
V::Msg: Clone
{
@ -135,7 +137,7 @@ impl ReprTree {
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
{
let mut rt = ReprTree::new(type_tag);
@ -146,7 +148,7 @@ impl ReprTree {
pub fn from_str(
type_tag: impl Into<TypeTerm>,
val: &str
) -> Arc<RwLock<Self>> {
) -> ReprTreeArc {
let mut lnf = type_tag.into().get_lnf_vec();
let mut rt = ReprTree::from_vec_buffer(
@ -163,7 +165,7 @@ impl ReprTree {
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
{
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();
assert!( dst_type.is_flat() );
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() {
let rt = rt.read().unwrap();
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();
if lnf.len() > 0 {
if lnf[0] == rt.get_type() {