From 7226cb0162a8ea04530f7dbcbed717caad55507a Mon Sep 17 00:00:00 2001
From: Michael Sippel <micha@fragmental.art>
Date: Thu, 22 Aug 2024 17:41:57 +0200
Subject: [PATCH] add static TypeIDs to create basic types without context

---
 lib-nested-core/src/repr_tree/context.rs  | 26 ++++++++--
 lib-nested-core/src/repr_tree/morphism.rs | 63 ++++++++++++++++++-----
 lib-nested-core/src/repr_tree/node.rs     | 28 +++++++---
 3 files changed, 90 insertions(+), 27 deletions(-)

diff --git a/lib-nested-core/src/repr_tree/context.rs b/lib-nested-core/src/repr_tree/context.rs
index cca1894..a9ed8ec 100644
--- a/lib-nested-core/src/repr_tree/context.rs
+++ b/lib-nested-core/src/repr_tree/context.rs
@@ -13,6 +13,14 @@ use {
 
 //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
 
+pub static TYPEID_edittree : TypeID = TypeID::Fun(0);
+pub static TYPEID_char : TypeID = TypeID::Fun(1);
+pub static TYPEID_u64  : TypeID = TypeID::Fun(2);
+pub static TYPEID_list : TypeID = TypeID::Fun(3);
+pub static TYPEID_vec : TypeID = TypeID::Fun(4);
+
+//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
+
 #[derive(Clone)]
 pub struct Context {
     /// assigns a name to every type
@@ -35,19 +43,27 @@ pub struct Context {
     parent: Option<Arc<RwLock<Context>>>,
 }
 
+//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
+
 impl Context {
     pub fn with_parent(
         parent: Option<Arc<RwLock<Context>>>
     ) -> Self {
-        let mut dict = TypeDict::new();
-        let list_typeid = dict.add_typename("List".into());
-
         Context {
             type_dict: match parent.as_ref() {
                 Some(p) => p.read().unwrap().type_dict.clone(),
-                None => Arc::new(RwLock::new(dict))
+                None => {
+                    let mut dict = TypeDict::new();
+                    assert_eq!( TYPEID_edittree, dict.add_typename("EditTree".into()) );
+                    assert_eq!( TYPEID_char, dict.add_typename("Char".into()) );
+                    assert_eq!( TYPEID_u64, dict.add_typename("machine.UInt64".into()) );
+                    assert_eq!( TYPEID_list, dict.add_typename("List".into()) );
+                    assert_eq!( TYPEID_vec, dict.add_typename("Vec".into()) );
+
+                    Arc::new(RwLock::new(dict))
+                }
             },
-            morphisms: MorphismBase::new( list_typeid ),
+            morphisms: MorphismBase::new( TYPEID_list ),
             nodes: HashMap::new(),
             list_types: match parent.as_ref() {
                 Some(p) => p.read().unwrap().list_types.clone(),
diff --git a/lib-nested-core/src/repr_tree/morphism.rs b/lib-nested-core/src/repr_tree/morphism.rs
index d8eaf3d..61575a6 100644
--- a/lib-nested-core/src/repr_tree/morphism.rs
+++ b/lib-nested-core/src/repr_tree/morphism.rs
@@ -122,29 +122,64 @@ impl GenericReprTreeMorphism {
 
     pub fn into_list_map_dyn(&self, typeid_list: TypeID)
     -> Option< GenericReprTreeMorphism >
-    { 
-        let typeid_char = TypeID::Fun(1);
-        let typeid_u64 = TypeID::Fun(5);
-        let typeid_edittree = TypeID::Fun(2);
-
+    {        
         let src_item_type_lnf = self.morph_type.src_type.clone().get_lnf_vec();
         let dst_item_type_lnf = self.morph_type.dst_type.clone().get_lnf_vec();
 
-        if src_item_type_lnf.last() == Some(&TypeTerm::TypeID(typeid_char)) &&
-           dst_item_type_lnf.last() == Some(&TypeTerm::TypeID(typeid_u64))
+        eprintln!("into list map dyn");
+
+        if src_item_type_lnf.last() == Some(&TypeTerm::TypeID(TYPEID_char)) &&
+           dst_item_type_lnf.last() == Some(&TypeTerm::TypeID(TYPEID_char))
         {
-            Some( self.into_list_map::< char, u64 >(typeid_list) )
+            Some( self.into_list_map::< char, char >(TYPEID_list) )
         }
-        else if src_item_type_lnf.last() == Some(&TypeTerm::TypeID(typeid_u64)) &&
-                dst_item_type_lnf.last() == Some(&TypeTerm::TypeID(typeid_char))
+        else if src_item_type_lnf.last() == Some(&TypeTerm::TypeID(TYPEID_u64)) &&
+           dst_item_type_lnf.last() == Some(&TypeTerm::TypeID(TYPEID_u64))
         {
-            Some( self.into_list_map::< u64, char >(typeid_list) )
+            Some( self.into_list_map::< u64, u64 >(TYPEID_list) )
         }
-        else if src_item_type_lnf.last() == Some(&TypeTerm::TypeID(typeid_char)) &&
-                dst_item_type_lnf.last() == Some(&TypeTerm::TypeID(typeid_edittree))
+        else if src_item_type_lnf.last() == Some(&TypeTerm::TypeID(TYPEID_char)) &&
+           dst_item_type_lnf.last() == Some(&TypeTerm::TypeID(TYPEID_u64))
         {
-            Some( self.into_list_map::< char, Arc<RwLock<crate::edit_tree::EditTree>> >(typeid_list) )
+            Some( self.into_list_map::< char, u64 >(TYPEID_list) )
         }
+        else if src_item_type_lnf.last() == Some(&TypeTerm::TypeID(TYPEID_u64)) &&
+                dst_item_type_lnf.last() == Some(&TypeTerm::TypeID(TYPEID_char))
+        {
+            Some( self.into_list_map::< u64, char >(TYPEID_list) )
+        }
+        else if src_item_type_lnf.last() == Some(&TypeTerm::TypeID(TYPEID_char)) &&
+                dst_item_type_lnf.last() == Some(&TypeTerm::TypeID(TYPEID_edittree))
+        {
+            Some( self.into_list_map::< char, Arc<RwLock<crate::edit_tree::EditTree>> >(TYPEID_list) )
+        }
+        else if src_item_type_lnf.last() == Some(&TypeTerm::TypeID(TYPEID_edittree)) &&
+                dst_item_type_lnf.last() == Some(&TypeTerm::TypeID(TYPEID_char))
+        {
+            Some( self.into_list_map::< Arc<RwLock<crate::edit_tree::EditTree>>, char >(TYPEID_list) )
+        }
+/*
+        else if src_item_type_lnf.last() == Some(&TypeTerm::App(vec![ TypeTerm::TypeID(typeid_list), TypeTerm::TypeID(typeid_char) ])) &&
+                dst_item_type_lnf.last() == Some(&TypeTerm::App(vec![ TypeTerm::TypeID(typeid_list), TypeTerm::TypeID(typeid_char) ]))
+        {
+            Some( self.into_list_map::< char, char >(typeid_list) )
+        }
+        else if src_item_type_lnf.last() == Some(&TypeTerm::App(vec![ TypeTerm::TypeID(typeid_list), TypeTerm::TypeID(typeid_u64) ])) &&
+                dst_item_type_lnf.last() == Some(&TypeTerm::App(vec![ TypeTerm::TypeID(typeid_list), TypeTerm::TypeID(typeid_u64) ]))
+        {
+            Some( self.into_list_map::< u64, u64 >(typeid_list) )
+        }
+        else if src_item_type_lnf.last() == Some(&TypeTerm::App(vec![ TypeTerm::TypeID(typeid_list), TypeTerm::TypeID(typeid_char) ])) &&
+                dst_item_type_lnf.last() == Some(&TypeTerm::App(vec![ TypeTerm::TypeID(typeid_list), TypeTerm::TypeID(typeid_u64) ]))
+        {
+            Some( self.into_list_map::< OuterViewPort<dyn ListView<char>>, OuterViewPort<dyn ListView<u64>> >(typeid_list) )
+        }
+        else if src_item_type_lnf.last() == Some(&TypeTerm::App(vec![ TypeTerm::TypeID(typeid_list), TypeTerm::TypeID(typeid_u64) ])) &&
+                dst_item_type_lnf.last() == Some(&TypeTerm::App(vec![ TypeTerm::TypeID(typeid_list), TypeTerm::TypeID(typeid_char) ]))
+        {
+            Some( self.into_list_map::< OuterViewPort<dyn ListView<u64>>, OuterViewPort<dyn ListView<char>> >(typeid_list) )
+        }
+*/
         else
         {
             eprintln!("no list map type for {:?}", dst_item_type_lnf.last());
diff --git a/lib-nested-core/src/repr_tree/node.rs b/lib-nested-core/src/repr_tree/node.rs
index 6882b7d..efb52fb 100644
--- a/lib-nested-core/src/repr_tree/node.rs
+++ b/lib-nested-core/src/repr_tree/node.rs
@@ -19,7 +19,7 @@ use {
         sync::{Arc, RwLock},
         any::Any
     },
-    super::{Context, ReprLeaf, ReprTreeExt}
+    super::{Context, ReprLeaf, ReprTreeExt, context::{TYPEID_vec, TYPEID_char, TYPEID_u64, TYPEID_edittree}}
 };
 
 //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
@@ -139,7 +139,6 @@ impl ReprTree {
         Arc::new(RwLock::new(rt))
     }
 
-
     pub fn from_str(
         type_tag: impl Into<TypeTerm>,
         val: &str
@@ -204,13 +203,13 @@ impl ReprTree {
                 return;
             }
         }
-        
+
         if let Some(leaf) = self.leaf.as_mut() {
             leaf.attach_to(src_port);
         } else {
             if self.type_tag == TypeTerm::App(vec![
-                TypeTerm::TypeID(TypeID::Fun(11)),
-                TypeTerm::TypeID(TypeID::Fun(2))
+                TypeTerm::TypeID(TYPEID_vec),
+                TypeTerm::TypeID(TYPEID_edittree)
             ]) {
                 let mut leaf = ReprLeaf::from_vec_buffer(
                     VecBuffer::<
@@ -222,8 +221,8 @@ impl ReprTree {
                 self.leaf = Some(leaf);
             }           
             else if self.type_tag == TypeTerm::App(vec![
-                TypeTerm::TypeID(TypeID::Fun(11)),
-                TypeTerm::TypeID(TypeID::Fun(1))
+                TypeTerm::TypeID(TYPEID_vec),
+                TypeTerm::TypeID(TYPEID_char)
             ]) {
                 let mut leaf = ReprLeaf::from_vec_buffer(
                     VecBuffer::<char>::new()
@@ -231,13 +230,25 @@ impl ReprTree {
 
                 leaf.attach_to(src_port);
                 self.leaf = Some(leaf);
-            } else {
+            }
+            else if self.type_tag == TypeTerm::App(vec![
+                TypeTerm::TypeID(TYPEID_vec),
+                TypeTerm::TypeID(TYPEID_u64)
+            ]) {
+                let mut leaf = ReprLeaf::from_vec_buffer(
+                    VecBuffer::<u64>::new()
+                );
+
+                leaf.attach_to(src_port);
+                self.leaf = Some(leaf);
+             } else {
                 self.leaf = Some(ReprLeaf::from_view(src_port));
             }
          }
     }
 
     pub fn detach(&mut self, ctx: &Arc<RwLock<Context>>) {
+//        eprintln!("DETACH {:?}", self.get_type());
         if let Some(leaf) = self.leaf.as_mut() {
             if self.type_tag == Context::parse(&ctx, "Char") {
                 leaf.detach::<dyn SingletonView<Item = char>>();
@@ -392,3 +403,4 @@ impl ReprTree {
 
 //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
 
+