src/Tools/nbe.ML
changeset 57435 312660c1a70a
parent 57174 db969ff6a8b3
child 58397 1c036d6216d3
     1.1 --- a/src/Tools/nbe.ML	Sun Jun 29 18:30:24 2014 +0200
     1.2 +++ b/src/Tools/nbe.ML	Sun Jun 29 18:02:18 2014 +0200
     1.3 @@ -22,7 +22,7 @@
     1.4    val same: Univ * Univ -> bool
     1.5  
     1.6    val put_result: (unit -> Univ list -> Univ list) -> Proof.context -> Proof.context
     1.7 -  val trace: bool Unsynchronized.ref
     1.8 +  val trace: bool Config.T
     1.9  
    1.10    val add_const_alias: thm -> theory -> theory
    1.11  end;
    1.12 @@ -32,8 +32,8 @@
    1.13  
    1.14  (* generic non-sense *)
    1.15  
    1.16 -val trace = Unsynchronized.ref false;
    1.17 -fun traced f x = if !trace then (tracing (f x); x) else x;
    1.18 +val trace = Attrib.setup_config_bool @{binding "nbe_trace"} (K false);
    1.19 +fun traced ctxt f x = if Config.get ctxt trace then (tracing (f x); x) else x;
    1.20  
    1.21  
    1.22  (** certificates and oracle for "trivial type classes" **)
    1.23 @@ -263,9 +263,10 @@
    1.24  fun nbe_apps t [] = t
    1.25    | nbe_apps t ts = name_apps `$$` [t, ml_list (rev ts)];
    1.26  fun nbe_apps_local idx_of i c ts = nbe_fun idx_of i c `$` ml_list (rev ts);
    1.27 -fun nbe_apps_constr idx_of c ts =
    1.28 +fun nbe_apps_constr ctxt idx_of c ts =
    1.29    let
    1.30 -    val c' = if !trace then string_of_int (idx_of c) ^ " (*" ^ Code_Symbol.default_base c ^ "*)"
    1.31 +    val c' = if Config.get ctxt trace
    1.32 +      then string_of_int (idx_of c) ^ " (*" ^ Code_Symbol.default_base c ^ "*)"
    1.33        else string_of_int (idx_of c);
    1.34    in name_const `$` ("(" ^ c' ^ ", " ^ ml_list (rev ts) ^ ")") end;
    1.35  
    1.36 @@ -282,7 +283,7 @@
    1.37  
    1.38  (* code generation *)
    1.39  
    1.40 -fun assemble_eqnss idx_of deps eqnss =
    1.41 +fun assemble_eqnss ctxt idx_of deps eqnss =
    1.42    let
    1.43      fun prep_eqns (c, (vs, eqns)) =
    1.44        let
    1.45 @@ -301,7 +302,7 @@
    1.46              end else nbe_apps (nbe_abss num_args (nbe_fun idx_of 0 sym)) ts'
    1.47          | NONE => if member (op =) deps sym
    1.48              then nbe_apps (nbe_fun idx_of 0 sym) ts'
    1.49 -            else nbe_apps_constr idx_of sym ts'
    1.50 +            else nbe_apps_constr ctxt idx_of sym ts'
    1.51        end
    1.52      and assemble_classrels classrels =
    1.53        fold_rev (fn classrel => assemble_constapp (Class_Relation classrel) [] o single) classrels
    1.54 @@ -358,7 +359,8 @@
    1.55          val match_cont = if Code_Symbol.is_value sym then NONE
    1.56            else SOME (nbe_apps_local idx_of (i + 1) sym (dicts @ default_args));
    1.57          val assemble_arg = assemble_iterm
    1.58 -          (fn sym' => fn dss => fn ts => nbe_apps_constr idx_of sym' ((maps o map) (K "_") dss @ ts)) NONE;
    1.59 +          (fn sym' => fn dss => fn ts => nbe_apps_constr ctxt idx_of sym' ((maps o map) (K "_")
    1.60 +            dss @ ts)) NONE;
    1.61          val assemble_rhs = assemble_iterm assemble_constapp match_cont;
    1.62          val (samepairs, args') = subst_nonlin_vars args;
    1.63          val s_args = map assemble_arg args';
    1.64 @@ -379,7 +381,7 @@
    1.65          val eqns' = map_index (assemble_eqn sym dicts default_args) eqns
    1.66            @ (if Code_Symbol.is_value sym then [] else [(nbe_fun idx_of (length eqns) sym,
    1.67              [([ml_list (rev (dicts @ default_args))],
    1.68 -              nbe_apps_constr idx_of sym (dicts @ default_args))])]);
    1.69 +              nbe_apps_constr ctxt idx_of sym (dicts @ default_args))])]);
    1.70        in (eqns', nbe_abss num_args (nbe_fun idx_of 0 sym)) end;
    1.71  
    1.72      val (fun_vars, fun_vals) = map_split assemble_eqns eqnss';
    1.73 @@ -398,11 +400,11 @@
    1.74            |> map (fn dep => (dep, snd (Code_Symbol.Graph.get_node nbe_program dep)))
    1.75            |> AList.lookup (op =)
    1.76            |> (fn f => the o f);
    1.77 -        val s = assemble_eqnss idx_of deps eqnss;
    1.78 +        val s = assemble_eqnss ctxt idx_of deps eqnss;
    1.79          val cs = map fst eqnss;
    1.80        in
    1.81          s
    1.82 -        |> traced (fn s => "\n--- code to be evaluated:\n" ^ s)
    1.83 +        |> traced ctxt (fn s => "\n--- code to be evaluated:\n" ^ s)
    1.84          |> pair ""
    1.85          |> Code_Runtime.value ctxt univs_cookie
    1.86          |> (fn f => f deps_vals)
    1.87 @@ -549,11 +551,11 @@
    1.88    in
    1.89      compile_term ctxt nbe_program deps (vs, t)
    1.90      |> term_of_univ ctxt idx_tab
    1.91 -    |> traced (fn t => "Normalized:\n" ^ string_of_term t)
    1.92 +    |> traced ctxt (fn t => "Normalized:\n" ^ string_of_term t)
    1.93      |> type_infer
    1.94 -    |> traced (fn t => "Types inferred:\n" ^ string_of_term t)
    1.95 +    |> traced ctxt (fn t => "Types inferred:\n" ^ string_of_term t)
    1.96      |> check_tvars
    1.97 -    |> traced (fn _ => "---\n")
    1.98 +    |> traced ctxt (fn _ => "---\n")
    1.99    end;
   1.100  
   1.101