/// Do type inference over top level definitions
Do type inference over top level definitions
Do type inference over top level definitions
pub fn infer_defs<'a>(&mut self,
                      _env: &'a TypeEnv<'a>,
                      program: &'a mut Vec<Def>)
                      -> Result<(), TypeError> {
        // Give each definitions a temporary type if no annotation
        let mut env = {
            let name_scms = program
                .iter()
                .filter(|ref v| v.is_form())
                .map(|ref d| (d.name(), match d.form_annot() {
                    Some(s) => s.clone(),
                    _ => to_mono(self.fresh())
                }));
            // Add them into environment
            _env.extend_n(name_scms)
        };
        for d in program.iter_mut() {
            if d.is_form() {
                let sub = {
                    self.infer(&mut env, d.form_body_mut())?;
                    self.solve()?
                };
                d.form_body_mut().apply_mut(&sub);
                let general = {
                    let ty = d.form_type().body().to_owned();
                    let fvs: Vec<_> = ty.ftv().into_iter().collect();
                    if fvs.is_empty() {
                        Scheme::Mono(ty)
                    } else {
                        Scheme::Poly(fvs, ty)
                    }
                };
                d.form_body_mut().tag.set_scheme(general);
                env.insert(d.name().to_owned(), d.form_type().clone());
            }
        }
        Ok(())
    }
pub fn infer_defs < ' a > ( & mut self , _env : & ' a TypeEnv < ' a > , program : & ' a mut Vec < Def > ) -> Result < ( ) , TypeError > { // Give each definitions a temporary type if no annotation let mut env = { let name_scms = program . iter ( ) . filter ( | ref v | v . is_form ( ) ) . map ( | ref d | ( d . name ( ) , match d . form_annot ( ) { Some ( s ) => s . clone ( ) , _ => to_mono ( self . fresh ( ) ) } ) ) ; // Add them into environment _env . extend_n ( name_scms ) } ; for d in program . iter_mut ( ) { if d . is_form ( ) { let sub = { self . infer ( & mut env , d . form_body_mut ( ) ) ? ; self . solve ( ) ? } ; d . form_body_mut ( ) . apply_mut ( & sub ) ; let general = { let ty = d . form_type ( ) . body ( ) . to_owned ( ) ; let fvs : Vec < _ > = ty . ftv ( ) . into_iter ( ) . collect ( ) ; if fvs . is_empty ( ) { Scheme :: Mono ( ty ) } else { Scheme :: Poly ( fvs , ty ) } } ; d . form_body_mut ( ) . tag . set_scheme ( general ) ; env . insert ( d . name ( ) . to_owned ( ) , d . form_type ( ) . clone ( ) ) ; } } Ok ( ( ) ) }
Do type inference over top level definitions
Do type inference over top level definitions
&mut self
_env
&'a TypeEnv<'a>
program
&'a mut Vec<Def>