tuned
authorhaftmann
Tue Jan 22 23:07:25 2008 +0100 (2008-01-22)
changeset 25944f0301d6c7b79
parent 25943 d431d65346a1
child 25945 ddc6a3312636
tuned
src/Tools/nbe.ML
     1.1 --- a/src/Tools/nbe.ML	Tue Jan 22 23:07:24 2008 +0100
     1.2 +++ b/src/Tools/nbe.ML	Tue Jan 22 23:07:25 2008 +0100
     1.3 @@ -17,12 +17,10 @@
     1.4      | BVar of int * Univ list
     1.5      | Abs of (int * (Univ list -> Univ)) * Univ list;
     1.6    val apps: Univ -> Univ list -> Univ        (*explicit applications*)
     1.7 -  val abs: int -> (Univ list -> Univ) -> Univ
     1.8 +  val abss: int -> (Univ list -> Univ) -> Univ
     1.9                                              (*abstractions as closures*)
    1.10  
    1.11    val univs_ref: (unit -> Univ list -> Univ list) option ref
    1.12 -  val norm_invoke: theory -> CodeThingol.code -> term
    1.13 -    -> CodeThingol.typscheme * CodeThingol.iterm -> string list -> thm
    1.14    val trace: bool ref
    1.15  
    1.16    val setup: theory -> theory
    1.17 @@ -67,7 +65,7 @@
    1.18  
    1.19  (* constructor functions *)
    1.20  
    1.21 -fun abs n f = Abs ((n, f), []);
    1.22 +fun abss n f = Abs ((n, f), []);
    1.23  fun apps (Abs ((n, f), xs)) ys = let val k = n - length ys in
    1.24        if k = 0 then f (ys @ xs)
    1.25        else if k < 0 then
    1.26 @@ -81,7 +79,6 @@
    1.27  (* universe graph *)
    1.28  
    1.29  type univ_gr = Univ option Graph.T;
    1.30 -val compiled : univ_gr -> string -> bool = can o Graph.get_node;
    1.31  
    1.32  
    1.33  (** assembling and compiling ML code from terms **)
    1.34 @@ -96,7 +93,7 @@
    1.35  
    1.36  fun ml_cases t cs =
    1.37    "(case " ^ t ^ " of " ^ space_implode " | " (map (fn (p, t) => p ^ " => " ^ t) cs) ^ ")";
    1.38 -fun ml_Let ds e = "let\n" ^ space_implode "\n" ds ^ " in " ^ e ^ " end";
    1.39 +fun ml_Let d e = "let\n" ^ d ^ " in " ^ e ^ " end";
    1.40  
    1.41  fun ml_list es = "[" ^ commas es ^ "]";
    1.42  
    1.43 @@ -114,15 +111,20 @@
    1.44          |> suffix "\n"
    1.45        end;
    1.46  
    1.47 -(* nbe specific syntax *)
    1.48 +(* nbe specific syntax and sandbox communication *)
    1.49 +
    1.50 +val univs_ref = ref (NONE : (unit -> Univ list -> Univ list) option);
    1.51  
    1.52  local
    1.53    val prefix =          "Nbe.";
    1.54 +  val name_ref =        prefix ^ "univs_ref";
    1.55    val name_const =      prefix ^ "Const";
    1.56 -  val name_abs =        prefix ^ "abs";
    1.57 +  val name_abss =       prefix ^ "abss";
    1.58    val name_apps =       prefix ^ "apps";
    1.59  in
    1.60  
    1.61 +val univs_cookie = (name_ref, univs_ref);
    1.62 +
    1.63  fun nbe_fun "" = "nbe_value"
    1.64    | nbe_fun c = "c_" ^ translate_string (fn "." => "_" | c => c) c;
    1.65  fun nbe_dict v n = "d_" ^ v ^ "_" ^ string_of_int n;
    1.66 @@ -136,89 +138,95 @@
    1.67    name_const `$` ("(" ^ ML_Syntax.print_string c ^ ", " ^ ml_list (rev ts) ^ ")");
    1.68  
    1.69  fun nbe_abss 0 f = f `$` ml_list []
    1.70 -  | nbe_abss n f = name_abs `$$` [string_of_int n, f];
    1.71 +  | nbe_abss n f = name_abss `$$` [string_of_int n, f];
    1.72  
    1.73  end;
    1.74  
    1.75  open BasicCodeThingol;
    1.76  
    1.77 -(* sandbox communication *)
    1.78 -
    1.79 -val univs_ref = ref (NONE : (unit -> Univ list -> Univ list) option);
    1.80 -
    1.81 -val compile =
    1.82 -  tracing (fn s => "\n--- code to be evaluated:\n" ^ s)
    1.83 -  #> ML_Context.evaluate
    1.84 -      (Output.tracing o enclose "\n---compiler echo:\n" "\n---\n",
    1.85 -      Output.tracing o enclose "\n--- compiler echo (with error):\n" "\n---\n")
    1.86 -      (!trace) ("Nbe.univs_ref", univs_ref);
    1.87 -
    1.88  (* code generation *)
    1.89  
    1.90 -datatype const_kind = Local of int | Global | Constr;
    1.91 -
    1.92 -fun assemble_constapp kind c dss ts = 
    1.93 +fun assemble_eqnss deps eqnss =
    1.94 +  let
    1.95 +    fun prep_eqns (c, (vs, eqns)) =
    1.96        let
    1.97 -        val ts' = (maps o map) (assemble_idict kind) dss @ ts;
    1.98 -      in case kind c
    1.99 -       of Local n => if n <= length ts'
   1.100 +        val dicts = maps (fn (v, sort) => map_index (nbe_dict v o fst) sort) vs;
   1.101 +        val num_args = length dicts + (length o fst o hd) eqns;
   1.102 +      in (c, (num_args, (dicts, eqns))) end;
   1.103 +    val eqnss' = map prep_eqns eqnss;
   1.104 +
   1.105 +    fun assemble_constapp c dss ts = 
   1.106 +      let
   1.107 +        val ts' = (maps o map) assemble_idict dss @ ts;
   1.108 +      in case AList.lookup (op =) eqnss' c
   1.109 +       of SOME (n, _) => if n <= length ts'
   1.110              then let val (ts1, ts2) = chop n ts'
   1.111              in nbe_apps (nbe_apps_local c ts1) ts2
   1.112              end else nbe_apps (nbe_abss n (nbe_fun c)) ts'
   1.113 -        | Global => nbe_apps (nbe_fun c) ts'
   1.114 -        | Constr => nbe_apps_constr c ts'
   1.115 +        | NONE => if member (op =) deps c
   1.116 +            then nbe_apps (nbe_fun c) ts'
   1.117 +            else nbe_apps_constr c ts'
   1.118        end
   1.119 -and assemble_idict kind (DictConst (inst, dss)) =
   1.120 -      assemble_constapp kind inst dss []
   1.121 -  | assemble_idict kind (DictVar (supers, (v, (n, _)))) =
   1.122 -      fold_rev (fn super => assemble_constapp kind super [] o single) supers (nbe_dict v n);
   1.123 +    and assemble_idict (DictConst (inst, dss)) =
   1.124 +          assemble_constapp inst dss []
   1.125 +      | assemble_idict (DictVar (supers, (v, (n, _)))) =
   1.126 +          fold_rev (fn super => assemble_constapp super [] o single) supers (nbe_dict v n);
   1.127  
   1.128 -fun assemble_iterm kind =
   1.129 -  let
   1.130 -    fun of_iterm t =
   1.131 +    fun assemble_iterm constapp =
   1.132        let
   1.133 -        val (t', ts) = CodeThingol.unfold_app t
   1.134 -      in of_iapp t' (fold_rev (cons o of_iterm) ts []) end
   1.135 -    and of_iapp (IConst (c, (dss, _))) ts = assemble_constapp kind c dss ts
   1.136 -      | of_iapp (IVar v) ts = nbe_apps (nbe_bound v) ts
   1.137 -      | of_iapp ((v, _) `|-> t) ts =
   1.138 -          nbe_apps (nbe_abss 1 (ml_abs (ml_list [nbe_bound v]) (of_iterm t))) ts
   1.139 -      | of_iapp (ICase (((t, _), cs), t0)) ts =
   1.140 -          nbe_apps (ml_cases (of_iterm t) (map (pairself of_iterm) cs
   1.141 -            @ [("_", of_iterm t0)])) ts
   1.142 -  in of_iterm end;
   1.143 +        fun of_iterm t =
   1.144 +          let
   1.145 +            val (t', ts) = CodeThingol.unfold_app t
   1.146 +          in of_iapp t' (fold_rev (cons o of_iterm) ts []) end
   1.147 +        and of_iapp (IConst (c, (dss, _))) ts = constapp c dss ts
   1.148 +          | of_iapp (IVar v) ts = nbe_apps (nbe_bound v) ts
   1.149 +          | of_iapp ((v, _) `|-> t) ts =
   1.150 +              nbe_apps (nbe_abss 1 (ml_abs (ml_list [nbe_bound v]) (of_iterm t))) ts
   1.151 +          | of_iapp (ICase (((t, _), cs), t0)) ts =
   1.152 +              nbe_apps (ml_cases (of_iterm t) (map (pairself of_iterm) cs
   1.153 +                @ [("_", of_iterm t0)])) ts
   1.154 +      in of_iterm end;
   1.155  
   1.156 -fun assemble_eqns kind (c, (vs, eqns)) =
   1.157 -  let
   1.158 -    val dict_args = maps (fn (v, sort) => map_index (nbe_dict v o fst) sort) vs;
   1.159 -    val assemble_arg = assemble_iterm (K Constr);
   1.160 -    val assemble_rhs = assemble_iterm kind;
   1.161 -    fun assemble_eqn (args, rhs) =
   1.162 -      ([ml_list (rev (dict_args @ map assemble_arg args))], assemble_rhs rhs);
   1.163 -    val default_args = dict_args @ map nbe_bound (Name.invent_list [] "a" ((length o fst o hd) eqns));
   1.164 -    val default_eqn = ([ml_list (rev default_args)], nbe_apps_constr c default_args);
   1.165 -  in (nbe_fun c, map assemble_eqn eqns @ [default_eqn]) end;
   1.166 +    fun assemble_eqns (c, (num_args, (dicts, eqns))) =
   1.167 +      let
   1.168 +        val assemble_arg = assemble_iterm (fn c => fn _ => fn ts => nbe_apps_constr c ts);
   1.169 +        val assemble_rhs = assemble_iterm assemble_constapp;
   1.170 +        fun assemble_eqn (args, rhs) =
   1.171 +          ([ml_list (rev (dicts @ map assemble_arg args))], assemble_rhs rhs);
   1.172 +        val default_args = map nbe_bound (Name.invent_list [] "a" num_args);
   1.173 +        val default_eqn = ([ml_list (rev default_args)], nbe_apps_constr c default_args);
   1.174 +      in
   1.175 +        ((nbe_fun c, map assemble_eqn eqns @ [default_eqn]),
   1.176 +          nbe_abss num_args (nbe_fun c))
   1.177 +      end;
   1.178  
   1.179 -fun assemble_eqnss gr deps [] = ([], ("", []))
   1.180 -  | assemble_eqnss gr deps eqnss =
   1.181 +    val (fun_vars, fun_vals) = map_split assemble_eqns eqnss';
   1.182 +    val deps_vars = ml_list (map nbe_fun deps);
   1.183 +  in ml_abs deps_vars (ml_Let (ml_fundefs fun_vars) (ml_list fun_vals)) end;
   1.184 +
   1.185 +(* code compilation *)
   1.186 +
   1.187 +fun compile_eqnss gr raw_deps [] = []
   1.188 +  | compile_eqnss gr raw_deps eqnss = 
   1.189        let
   1.190          val cs = map fst eqnss;
   1.191 -        val num_args = cs ~~ map (fn (_, (vs, (args, rhs) :: _)) =>
   1.192 -          length (maps snd vs) + length args) eqnss;
   1.193 -        fun kind c = case AList.lookup (op =) num_args c
   1.194 -         of SOME n => Local n
   1.195 -          | NONE => if (is_some o Option.join o try (Graph.get_node gr)) c
   1.196 -              then Global else Constr;
   1.197 -        val deps' = filter (is_some o Option.join o try (Graph.get_node gr)) deps;
   1.198 -        val bind_deps = ml_list (map nbe_fun deps');
   1.199 -        val bind_locals = ml_fundefs (map (assemble_eqns kind) eqnss);
   1.200 -        val result = ml_list (map (fn (c, n) => nbe_abss n (nbe_fun c)) num_args);
   1.201 -        val arg_deps = map (the o Graph.get_node gr) deps';
   1.202 -      in (cs, (ml_abs bind_deps (ml_Let [bind_locals] result), arg_deps)) end;
   1.203 +        val (deps, deps_vals) = raw_deps
   1.204 +          |> map_filter (fn dep => case Graph.get_node gr dep of NONE => NONE
   1.205 +              | SOME univ => SOME (dep, univ))
   1.206 +          |> split_list;
   1.207 +        val s = assemble_eqnss deps eqnss;
   1.208 +      in
   1.209 +        s
   1.210 +        |> tracing (fn s => "\n--- code to be evaluated:\n" ^ s)
   1.211 +        |> ML_Context.evaluate
   1.212 +            (Output.tracing o enclose "\n---compiler echo:\n" "\n---\n",
   1.213 +            Output.tracing o enclose "\n--- compiler echo (with error):\n" "\n---\n")
   1.214 +            (!trace) univs_cookie
   1.215 +        |> (fn f => f deps_vals)
   1.216 +        |> (fn univs => cs ~~ univs)
   1.217 +      end;
   1.218  
   1.219 -fun compile_eqnss gr deps eqnss = case assemble_eqnss gr deps eqnss
   1.220 - of ([], _) => []
   1.221 -  | (cs, (s, deps)) => cs ~~ compile s deps;
   1.222 +(* preparing function equations *)
   1.223  
   1.224  fun eqns_of_stmt (_, CodeThingol.Fun (_, [])) =
   1.225        []
   1.226 @@ -250,15 +258,16 @@
   1.227      val names = map (fst o fst) stmts_deps;
   1.228      val names_deps = map (fn ((name, _), deps) => (name, deps)) stmts_deps;
   1.229      val eqnss = maps (eqns_of_stmt o fst) stmts_deps;
   1.230 -    val compiled_deps = names_deps
   1.231 +    val deps = names_deps
   1.232        |> maps snd
   1.233        |> distinct (op =)
   1.234        |> subtract (op =) names;
   1.235      fun compile gr = eqnss
   1.236 -      |> compile_eqnss gr compiled_deps
   1.237 +      |> compile_eqnss gr deps
   1.238        |> rpair gr;
   1.239    in
   1.240 -    fold (fn name => Graph.new_node (name, NONE)) names
   1.241 +    fold (fn name => Graph.default_node (name, NONE)) names
   1.242 +    #> fold (fn name => Graph.default_node (name, NONE)) deps
   1.243      #> fold (fn (name, deps) => fold (curry Graph.add_edge name) deps) names_deps
   1.244      #> compile
   1.245      #-> fold (fn (name, univ) => Graph.map_node name (K (SOME univ)))
   1.246 @@ -266,11 +275,16 @@
   1.247  
   1.248  fun ensure_stmts code =
   1.249    let
   1.250 -    fun add_stmts names gr = if exists (compiled gr) names then gr else gr
   1.251 +    fun add_stmts names gr = if exists ((can o Graph.get_node) gr) names then gr else gr
   1.252        |> compile_stmts (map (fn name => ((name, Graph.get_node code name),
   1.253            Graph.imm_succs code name)) names);
   1.254    in fold_rev add_stmts (Graph.strong_conn code) end;
   1.255  
   1.256 +
   1.257 +(** evaluation **)
   1.258 +
   1.259 +(* term evaluation *)
   1.260 +
   1.261  fun eval_term gr deps ((vs, ty), t) =
   1.262    let 
   1.263      val frees = CodeThingol.fold_unbound_varnames (insert (op =)) t []
   1.264 @@ -283,9 +297,6 @@
   1.265      |> (fn t => apps t (rev (dict_frees @ frees')))
   1.266    end;
   1.267  
   1.268 -
   1.269 -(** evaluation **)
   1.270 -
   1.271  (* reification *)
   1.272  
   1.273  fun term_of_univ thy t =