fixup examples

also fix behaviour of insert_leaf() & attach_leaf_to() when type of current repr-node occurs inside the type of the leaf
This commit is contained in:
Michael Sippel 2024-08-03 15:44:04 +02:00
parent 830ce613ea
commit 4c0e9da2d3
Signed by: senvas
GPG key ID: F96CF119C34B64A6
6 changed files with 101 additions and 118 deletions

View file

@ -72,13 +72,16 @@ async fn main() {
ctx.read().unwrap().morphisms.apply_morphism( ctx.read().unwrap().morphisms.apply_morphism(
rt_digit.clone(), rt_digit.clone(),
&Context::parse(&ctx, "<Digit 16>~Char"), &Context::parse(&ctx, "<Digit 16>~Char"),
&Context::parse(&ctx, "<Digit 16>~_256~machine::UInt8") &Context::parse(&ctx, "<Digit 16>~_2^64~machine.UInt64")
);
ctx.read().unwrap().morphisms.apply_morphism(
rt_digit.clone(),
&Context::parse(&ctx, "<Digit 16>~Char"),
&Context::parse(&ctx, "<Digit 16>~EditTree")
); );
/* setup TTY-Display for DigitEditor /* setup TTY-Display for DigitEditor
*
* `setup_edittree` will setup the projection
* Char -> Char~EditTree
* and call the hook defined above with `set_edittree_hook()` * and call the hook defined above with `set_edittree_hook()`
* *
*/ */
@ -86,11 +89,11 @@ async fn main() {
.setup_edittree( .setup_edittree(
rt_digit.clone(), rt_digit.clone(),
SingletonBuffer::new(0).get_port() SingletonBuffer::new(0).get_port()
); ).unwrap();
let mut digit_u8_buffer = rt_digit let mut digit_u64_buffer = rt_digit
.descend(Context::parse(&ctx, "_256~machine::UInt8")).unwrap() .descend(Context::parse(&ctx, "_2^64~machine.UInt64")).unwrap()
.singleton_buffer::<u8>(); .singleton_buffer::<u64>();
/* setup terminal /* setup terminal
*/ */
@ -101,7 +104,7 @@ async fn main() {
let mut edittree_digit = edittree_digit.clone(); let mut edittree_digit = edittree_digit.clone();
move |ev| { move |ev| {
edittree_digit.get().send_cmd_obj(ev.to_repr_tree(&ctx)); edittree_digit.get().write().unwrap().send_cmd_obj(ev.to_repr_tree(&ctx));
} }
}); });
@ -128,11 +131,11 @@ async fn main() {
.offset(Vector2::new(1, 1)) .offset(Vector2::new(1, 1))
); );
comp.push( comp.push(
edittree_digit.get().display_view() edittree_digit.get().read().unwrap().display_view()
.offset(Vector2::new(3,2)) .offset(Vector2::new(3,2))
); );
comp.push( comp.push(
digit_u8_buffer.get_port().map( digit_u64_buffer.get_port().map(
|d| nested_tty::make_label(&format!("Digit value={}", d)) |d| nested_tty::make_label(&format!("Digit value={}", d))
) )
.to_grid() .to_grid()

View file

@ -25,7 +25,7 @@ use {
}, },
r3vi::{ r3vi::{
buffer::{singleton::*, vec::*}, buffer::{singleton::*, vec::*},
view::{port::UpdateTask, list::*} view::{port::UpdateTask, list::*, sequence::SequenceViewExt}
}, },
std::sync::{Arc, RwLock}, std::sync::{Arc, RwLock},
}; };
@ -43,15 +43,32 @@ async fn main() {
/* Create a Representation-Tree of type <List Char> /* Create a Representation-Tree of type <List Char>
*/ */
let rt_string = ReprTree::new_arc( Context::parse(&ctx, "<List Char>") ); let mut rt_string = ReprTree::new_arc( Context::parse(&ctx, "<List Char>") );
/* Setup an Editor for this ReprTree let vec = VecBuffer::<Arc<RwLock<EditTree>>>::new();
* (this will add the representation <List Char>~EditTree to the ReprTree) rt_string.insert_leaf(
Context::parse(&ctx, "<List EditTree> ~ <Vec EditTree>"),
nested::repr_tree::ReprLeaf::from_vec_buffer( vec.clone() )
);
let v2 = VecBuffer::<char>::new();
rt_string.insert_leaf(
Context::parse(&ctx, "<Vec Char>"),
nested::repr_tree::ReprLeaf::from_vec_buffer( v2.clone() )
);
ctx.read().unwrap().morphisms.apply_morphism(
rt_string.clone(),
&Context::parse(&ctx, "<List Char~EditTree> ~ <Vec EditTree>"),
&Context::parse(&ctx, "<List Char> ~ EditTree")
);
/* Setup the Editor-View for this ReprTree
*/ */
let edittree_list = ctx.read().unwrap() let edittree_list = ctx.read().unwrap()
.setup_edittree( .setup_edittree(
rt_string.clone(), rt_string.clone(),
SingletonBuffer::new(0).get_port()); SingletonBuffer::new(0).get_port()
).unwrap();
/* In order to get acces to the values that are modified by the Editor, /* In order to get acces to the values that are modified by the Editor,
* we apply a morphism that, given the List of Edit-Trees, extracts * we apply a morphism that, given the List of Edit-Trees, extracts
@ -71,7 +88,6 @@ async fn main() {
.get_port::<dyn ListView<char>>() .get_port::<dyn ListView<char>>()
.unwrap(); .unwrap();
/* Lets add another morphism which will store the values /* Lets add another morphism which will store the values
* of the character-list in a `Vec<char>` * of the character-list in a `Vec<char>`
*/ */
@ -104,7 +120,7 @@ async fn main() {
*/ */
let ctx = ctx.clone(); let ctx = ctx.clone();
move |ev| { move |ev| {
edittree_list.get().send_cmd_obj(ev.to_repr_tree(&ctx)); edittree_list.get().write().unwrap().send_cmd_obj(ev.to_repr_tree(&ctx));
} }
}); });
@ -127,6 +143,7 @@ async fn main() {
comp.push( comp.push(
edittree_list.get() edittree_list.get()
.read().unwrap()
.display_view() .display_view()
.offset(Vector2::new(3,2))); .offset(Vector2::new(3,2)));
@ -145,8 +162,11 @@ async fn main() {
/* Vec<char> to String /* Vec<char> to String
*/ */
let string = chars_vec.data let string = chars_vec
.read().unwrap() .get_port()
.to_sequence()
.get_view().unwrap()
//.data.read().unwrap()
.iter().collect::<String>(); .iter().collect::<String>();
eprintln!("value of the editor was: {}\n\n", string); eprintln!("value of the editor was: {}\n\n", string);

View file

@ -19,10 +19,10 @@ pub fn init_ctx( ctx: Arc<RwLock<Context>> ) {
// todo: proper scoping of Radix variable // todo: proper scoping of Radix variable
ctx.write().unwrap().add_varname("Radix"); ctx.write().unwrap().add_varname("Radix");
/*
let morphtype = let morphtype =
crate::repr_tree::MorphismType { crate::repr_tree::MorphismType {
src_type: Context::parse(&ctx, "<Digit Radix>"), src_type: Context::parse(&ctx, "<Digit Radix>~Char"),
dst_type: Context::parse(&ctx, "<Digit Radix>~EditTree") dst_type: Context::parse(&ctx, "<Digit Radix>~EditTree")
}; };
@ -45,6 +45,9 @@ pub fn init_ctx( ctx: Arc<RwLock<Context>> ) {
if let Some(crt) = src_rt.descend(Context::parse(&ctx, "Char")) { if let Some(crt) = src_rt.descend(Context::parse(&ctx, "Char")) {
crt crt
} else { } else {
/* TODO: replace this with some formal specification
* of "required representations"
*/
let crt = ReprTree::from_singleton_buffer( let crt = ReprTree::from_singleton_buffer(
Context::parse(&ctx, "Char"), Context::parse(&ctx, "Char"),
SingletonBuffer::new('\0') SingletonBuffer::new('\0')
@ -70,13 +73,13 @@ pub fn init_ctx( ctx: Arc<RwLock<Context>> ) {
.insert_branch( .insert_branch(
ReprTree::from_singleton_buffer( ReprTree::from_singleton_buffer(
Context::parse(&ctx, "EditTree"), Context::parse(&ctx, "EditTree"),
SingletonBuffer::new(edittree) SingletonBuffer::new(Arc::new(RwLock::new(edittree)))
) )
); );
} }
} }
); );
*/
let morphtype = let morphtype =
crate::repr_tree::MorphismType { crate::repr_tree::MorphismType {
src_type: Context::parse(&ctx, "<Digit Radix>~Char"), src_type: Context::parse(&ctx, "<Digit Radix>~Char"),

View file

@ -24,49 +24,7 @@ use {
pub fn init_ctx(ctx: Arc<RwLock<Context>>) { pub fn init_ctx(ctx: Arc<RwLock<Context>>) {
ctx.write().unwrap().add_varname("Item"); ctx.write().unwrap().add_varname("Item");
/*
let mt = crate::repr_tree::MorphismType {
src_type: Context::parse(&ctx, "<List Char>"),
dst_type: Context::parse(&ctx, "<List Char~EditTree>")
};
ctx.write().unwrap().morphisms.add_morphism(mt, {
let ctx = ctx.clone();
move |src_rt, σ| {
let list_port = src_rt.descend(Context::parse(&ctx, "<List Char>")).expect("descend").get_port::<dyn ListView<char>>().clone();
if let Some(list_port) = list_port {
// for each char, create EditTree
let edit_tree_list =
list_port
.map({
let ctx = ctx.clone();
move |c| {
let item_rt = ReprTree::from_char(&ctx, *c);
ctx.read().unwrap().setup_edittree(
item_rt.clone(),
SingletonBuffer::new(0).get_port()
);
let et = item_rt
.descend(Context::parse(&ctx, "Char ~ EditTree")).expect("cant descend repr tree")
.get_port::< dyn SingletonView<Item = EditTree> >().expect("cant get view port (EditTree)")
.get_view().unwrap()
.get();
Arc::new(RwLock::new(et))
}
});
src_rt.attach_leaf_to(
Context::parse(&ctx, "<List Char>~<List EditTree>"),
edit_tree_list
);
} else {
eprintln!("morphism missing view port");
}
}
});
*/
let mt = crate::repr_tree::MorphismType { let mt = crate::repr_tree::MorphismType {
src_type: Context::parse(&ctx, "<List Item>~<List EditTree>~<Vec EditTree>"), src_type: Context::parse(&ctx, "<List Item>~<List EditTree>~<Vec EditTree>"),
dst_type: Context::parse(&ctx, "<List Item>~EditTree") dst_type: Context::parse(&ctx, "<List Item>~EditTree")
@ -93,6 +51,7 @@ pub fn init_ctx(ctx: Arc<RwLock<Context>>) {
src_rt.insert_leaf( src_rt.insert_leaf(
Context::parse(&ctx, "<List Item> ~ EditTree") Context::parse(&ctx, "<List Item> ~ EditTree")
.apply_substitution(&|id| σ.get(id).cloned()).clone(), .apply_substitution(&|id| σ.get(id).cloned()).clone(),
ReprLeaf::from_singleton_buffer( ReprLeaf::from_singleton_buffer(
SingletonBuffer::new(Arc::new(RwLock::new(edittree_list))) SingletonBuffer::new(Arc::new(RwLock::new(edittree_list)))
) )
@ -130,7 +89,8 @@ pub fn init_ctx(ctx: Arc<RwLock<Context>>) {
.get_edit::<CharEditor>().unwrap() .get_edit::<CharEditor>().unwrap()
.read().unwrap() .read().unwrap()
.get() .get()
)) )
)
); );
} }
} }
@ -148,13 +108,12 @@ pub fn init_ctx(ctx: Arc<RwLock<Context>>) {
{ {
let ctx = ctx.clone(); let ctx = ctx.clone();
move |src_rt, σ| { move |src_rt, σ| {
let list_view = src_rt.view_list::<char>();
src_rt src_rt
.attach_leaf_to( .attach_leaf_to(
Context::parse(&ctx, "<List Char>~<Vec Char>"), Context::parse(&ctx, "<Vec Char>"),
src_rt list_view
.descend(Context::parse(&ctx, "<List Char>"))
.expect("descend")
.view_list::<char>()
); );
} }
} }

View file

@ -345,41 +345,6 @@ impl ReprTree {
Arc::new(RwLock::new(rt)) Arc::new(RwLock::new(rt))
} }
/// find, and if necessary, create corresponding path in repr-tree.
/// Attach src_port to input of that node
pub fn attach_leaf_to<V>(
&mut self,
mut type_ladder: impl Iterator<Item = TypeTerm>,
src_port: OuterViewPort<V>
)
where V: View + ?Sized + 'static,
V::Msg: Clone
{
if let Some(rung_type) = type_ladder.next() {
if &rung_type == self.get_type() {
if let Some(leaf) = self.leaf.as_mut() {
leaf.attach_to(src_port);
} else {
self.leaf = Some(ReprLeaf::from_view(src_port));
}
} else {
if let Some(next_repr) = self.branches.get(&rung_type) {
next_repr.write().unwrap().attach_leaf_to(type_ladder, src_port);
} else {
let mut next_repr = ReprTree::new(rung_type.clone());
next_repr.attach_leaf_to(type_ladder, src_port);
self.insert_branch(Arc::new(RwLock::new(next_repr)));
}
}
} else {
if let Some(leaf) = self.leaf.as_mut() {
leaf.attach_to(src_port);
} else {
self.leaf = Some(ReprLeaf::from_view(src_port));
}
}
}
pub fn attach_to<V>( pub fn attach_to<V>(
&mut self, &mut self,
src_port: OuterViewPort<V> src_port: OuterViewPort<V>
@ -394,6 +359,36 @@ impl ReprTree {
} }
} }
/// find, and if necessary, create corresponding path in repr-tree.
/// Attach src_port to input of that node
pub fn attach_leaf_to<V>(
&mut self,
mut type_ladder: impl Iterator<Item = TypeTerm>,
src_port: OuterViewPort<V>
)
where V: View + ?Sized + 'static,
V::Msg: Clone
{
while let Some(rung_type) = type_ladder.next() {
if &rung_type != self.get_type() {
if let Some(next_repr) = self.branches.get(&rung_type) {
next_repr.write().unwrap().attach_leaf_to(type_ladder, src_port);
} else {
let mut next_repr = ReprTree::new(rung_type.clone());
next_repr.attach_leaf_to(type_ladder, src_port);
self.insert_branch(Arc::new(RwLock::new(next_repr)));
}
return;
}
}
if let Some(leaf) = self.leaf.as_mut() {
leaf.attach_to(src_port);
} else {
self.leaf = Some(ReprLeaf::from_view(src_port));
}
}
pub fn detach(&mut self, ctx: &Arc<RwLock<Context>>) { pub fn detach(&mut self, ctx: &Arc<RwLock<Context>>) {
if let Some(leaf) = self.leaf.as_mut() { if let Some(leaf) = self.leaf.as_mut() {
if self.type_tag == Context::parse(&ctx, "Char") { if self.type_tag == Context::parse(&ctx, "Char") {
@ -417,19 +412,22 @@ impl ReprTree {
mut type_ladder: impl Iterator<Item = TypeTerm>, mut type_ladder: impl Iterator<Item = TypeTerm>,
leaf: ReprLeaf leaf: ReprLeaf
) { ) {
if let Some(type_term) = type_ladder.next() { while let Some(type_term) = type_ladder.next() {
if &type_term != self.get_type() {
if let Some(next_repr) = self.branches.get(&type_term) { if let Some(next_repr) = self.branches.get(&type_term) {
next_repr.write().unwrap().insert_leaf(type_ladder, leaf); next_repr.write().unwrap().insert_leaf(type_ladder, leaf.clone());
} else { } else {
let mut next_repr = ReprTree::new(type_term.clone()); let mut next_repr = ReprTree::new(type_term.clone());
next_repr.insert_leaf(type_ladder, leaf); next_repr.insert_leaf(type_ladder, leaf.clone());
self.insert_branch(Arc::new(RwLock::new(next_repr))); self.insert_branch(Arc::new(RwLock::new(next_repr)));
} }
} else { return;
self.leaf = Some(leaf)
} }
} }
self.leaf = Some(leaf);
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
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<Arc<RwLock<ReprTree>>> {

View file

@ -251,7 +251,7 @@ impl MorphismBase {
Some((m, σ)) Some((m, σ))
} else { } else {
eprintln!("could not find item morphism\n"); // eprintln!("could not find item morphism\n");
None None
} }
} }