pub fn heal_splices__with<E, S: Clone>(
        &mut self,
        other: &EnvMBE<S>,
        f: &dyn Fn(&T, &dyn Fn() -> Vec<S>) -> Result<Option<Vec<T>>, E>,
    ) -> Result<(), E>
    where
        T: std::fmt::Debug,
    {
        for repeat in &mut self.repeats {
            // Find the same repeat in `other`:
            let mut names_needed = vec![];
            for (name, _) in self.leaf_locations.iter_pairs() {
                names_needed.push(name);
            }
            let other__rep_loc = match other.leaf_locations.find(names_needed[0]) {
                Some(Some(l)) => *l,
                _ => {
                    return Ok(()); // Should become a mismatch error elsewhere (TODO: make an `E`)
                }
            };

            let other__cur_repeat: &Vec<EnvMBE<S>> = &*other.repeats[other__rep_loc];
            let mut cur_repeat: Vec<EnvMBE<T>> = (**repeat).clone();

            // If an item splices, how wide does the other side need to be
            //  in order to make everything line up?
            let splice_length =
                (other__cur_repeat.len() + 1).checked_sub(cur_repeat.len()).unwrap();

            let mut i = 0;
            let mut other_i = 0;
            while i < cur_repeat.len() && other_i < other__cur_repeat.len() {
                cur_repeat[i].heal_splices__with(&other__cur_repeat[other_i], f)?;

                let mut splices = vec![];
                {
                    let n_and_vals = cur_repeat[i].leaves.iter_pairs();
                    for (n, val) in n_and_vals {
                        let concrete_splice__thunk = || {
                            let mut result = vec![];
                            for other_i in other__cur_repeat.iter().skip(i).take(splice_length) {
                                result.push(other_i.leaves.find_or_panic(n).clone())
                            }
                            result
                        };

                        if let Some(splice) = f(val, &concrete_splice__thunk)? {
                            splices.push((*n, splice));
                        }
                    }
                }

                if !splices.is_empty() {
                    let mut template = cur_repeat.remove(i);

                    // TODO: each of the splices better be the same length.
                    // I don't know what has to go wrong to violate that rule.
                    for rep in 0..splices[0].1.len() {
                        for splice in &splices {
                            template.add_leaf(splice.0, splice.1[rep].clone());
                        }
                        cur_repeat.insert(i + rep, template.clone())
                    }
                    i += splice_length;
                    other_i += splice_length;
                } else {
                    i += 1;
                    other_i += 1;
                }
            }
            // The lengths might not line up, but that doesn't mean matching will fail!
            // struct {a : Int b : Nat}  <:  struct {a : Int b : Nat c : Float}

            *repeat = Rc::new(cur_repeat)
        }
        Ok(())
    }
// TODO: this should return a usable error
this should return a usable error
this should return a usable error
pub fn heal_splices__with<E, S: Clone>(
        &mut self,
        other: &EnvMBE<S>,
        f: &dyn Fn(&T, &dyn Fn() -> Vec<S>) -> Result<Option<Vec<T>>, E>,
    ) -> Result<(), E>
    where
        T: std::fmt::Debug,
    {
        for repeat in &mut self.repeats {
            // Find the same repeat in `other`:
            let mut names_needed = vec![];
            for (name, _) in self.leaf_locations.iter_pairs() {
                names_needed.push(name);
            }
            let other__rep_loc = match other.leaf_locations.find(names_needed[0]) {
                Some(Some(l)) => *l,
                _ => {
                    return Ok(()); // Should become a mismatch error elsewhere (TODO: make an `E`)
                }
            };
            let other__cur_repeat: &Vec<EnvMBE<S>> = &*other.repeats[other__rep_loc];
            let mut cur_repeat: Vec<EnvMBE<T>> = (**repeat).clone();
            // If an item splices, how wide does the other side need to be
            //  in order to make everything line up?
            let splice_length =
                (other__cur_repeat.len() + 1).checked_sub(cur_repeat.len()).unwrap();
            let mut i = 0;
            let mut other_i = 0;
            while i < cur_repeat.len() && other_i < other__cur_repeat.len() {
                cur_repeat[i].heal_splices__with(&other__cur_repeat[other_i], f)?;
                let mut splices = vec![];
                {
                    let n_and_vals = cur_repeat[i].leaves.iter_pairs();
                    for (n, val) in n_and_vals {
                        let concrete_splice__thunk = || {
                            let mut result = vec![];
                            for other_i in other__cur_repeat.iter().skip(i).take(splice_length) {
                                result.push(other_i.leaves.find_or_panic(n).clone())
                            }
                            result
                        };
                        if let Some(splice) = f(val, &concrete_splice__thunk)? {
                            splices.push((*n, splice));
                        }
                    }
                }
                if !splices.is_empty() {
                    let mut template = cur_repeat.remove(i);
                    // TODO: each of the splices better be the same length.
                    // I don't know what has to go wrong to violate that rule.
                    for rep in 0..splices[0].1.len() {
                        for splice in &splices {
                            template.add_leaf(splice.0, splice.1[rep].clone());
                        }
                        cur_repeat.insert(i + rep, template.clone())
                    }
                    i += splice_length;
                    other_i += splice_length;
                } else {
                    i += 1;
                    other_i += 1;
                }
            }
            // The lengths might not line up, but that doesn't mean matching will fail!
            // struct {a : Int b : Nat}  <:  struct {a : Int b : Nat c : Float}
            *repeat = Rc::new(cur_repeat)
        }
        Ok(())
    }
pub fn heal_splices__with < E , S : Clone > ( & mut self , other : & EnvMBE < S > , f : & dyn Fn ( & T , & dyn Fn ( ) -> Vec < S > ) -> Result < Option < Vec < T > > , E > , ) -> Result < ( ) , E > where T : std :: fmt :: Debug , { for repeat in & mut self . repeats { // Find the same repeat in `other`: let mut names_needed = vec ! [ ] ; for ( name , _ ) in self . leaf_locations . iter_pairs ( ) { names_needed . push ( name ) ; } let other__rep_loc = match other . leaf_locations . find ( names_needed [ 0 ] ) { Some ( Some ( l ) ) => * l , _ => { return Ok ( ( ) ) ; // Should become a mismatch error elsewhere (TODO: make an `E`) } } ; let other__cur_repeat : & Vec < EnvMBE < S > > = & * other . repeats [ other__rep_loc ] ; let mut cur_repeat : Vec < EnvMBE < T > > = ( * * repeat ) . clone ( ) ; // If an item splices, how wide does the other side need to be //  in order to make everything line up? let splice_length = ( other__cur_repeat . len ( ) + 1 ) . checked_sub ( cur_repeat . len ( ) ) . unwrap ( ) ; let mut i = 0 ; let mut other_i = 0 ; while i < cur_repeat . len ( ) && other_i < other__cur_repeat . len ( ) { cur_repeat [ i ] . heal_splices__with ( & other__cur_repeat [ other_i ] , f ) ? ; let mut splices = vec ! [ ] ; { let n_and_vals = cur_repeat [ i ] . leaves . iter_pairs ( ) ; for ( n , val ) in n_and_vals { let concrete_splice__thunk = | | { let mut result = vec ! [ ] ; for other_i in other__cur_repeat . iter ( ) . skip ( i ) . take ( splice_length ) { result . push ( other_i . leaves . find_or_panic ( n ) . clone ( ) ) } result } ; if let Some ( splice ) = f ( val , & concrete_splice__thunk ) ? { splices . push ( ( * n , splice ) ) ; } } } if ! splices . is_empty ( ) { let mut template = cur_repeat . remove ( i ) ; // TODO: each of the splices better be the same length. // I don't know what has to go wrong to violate that rule. for rep in 0 .. splices [ 0 ] . 1 . len ( ) { for splice in & splices { template . add_leaf ( splice . 0 , splice . 1 [ rep ] . clone ( ) ) ; } cur_repeat . insert ( i + rep , template . clone ( ) ) } i += splice_length ; other_i += splice_length ; } else { i += 1 ; other_i += 1 ; } } // The lengths might not line up, but that doesn't mean matching will fail! // struct {a : Int b : Nat}  <:  struct {a : Int b : Nat c : Float} * repeat = Rc :: new ( cur_repeat ) } Ok ( ( ) ) }
TODO: this should return a usable error
TODO : this should return a usable error
&mut self
other
&EnvMBE<S>
f
&dyn Fn(&T, &dyn Fn() -> Vec<S>) -> Result<Option<Vec<T>>, E>
&mut self
other
&EnvMBE<S>
f
&dyn Fn(&T, &dyn Fn() -> Vec<S>) -> Result<Option<Vec<T>>, E>