src/Tools/Code/code_eval.ML
author haftmann
Mon Jan 04 14:09:56 2010 +0100 (2010-01-04)
changeset 34244 03f8dcab55f3
parent 34032 f13b5c023e65
child 35019 1ec0a3ff229e
permissions -rw-r--r--
code cache without copy; tuned
     1 (*  Title:      Tools/code/code_eval.ML_
     2     Author:     Florian Haftmann, TU Muenchen
     3 
     4 Runtime services building on code generation into implementation language SML.
     5 *)
     6 
     7 signature CODE_EVAL =
     8 sig
     9   val target: string
    10   val eval: string option -> string * (unit -> 'a) option Unsynchronized.ref
    11     -> ((term -> term) -> 'a -> 'a) -> theory -> term -> string list -> 'a
    12   val evaluation_code: theory -> string list -> string list
    13     -> string * ((string * string) list * (string * string) list)
    14   val setup: theory -> theory
    15 end;
    16 
    17 structure Code_Eval : CODE_EVAL =
    18 struct
    19 
    20 (** generic **)
    21 
    22 val target = "Eval";
    23 
    24 val eval_struct_name = "Code";
    25 
    26 fun evaluation_code thy tycos consts =
    27   let
    28     val (consts', (naming, program)) = Code_Thingol.consts_program thy consts;
    29     val tycos' = map (the o Code_Thingol.lookup_tyco naming) tycos;
    30     val (ml_code, target_names) = Code_ML.evaluation_code_of thy target
    31       eval_struct_name naming program (consts' @ tycos');
    32     val (consts'', tycos'') = chop (length consts') target_names;
    33     val consts_map = map2 (fn const => fn NONE =>
    34         error ("Constant " ^ (quote o Code.string_of_const thy) const
    35           ^ "\nhas a user-defined serialization")
    36       | SOME const'' => (const, const'')) consts consts''
    37     val tycos_map = map2 (fn tyco => fn NONE =>
    38         error ("Type " ^ (quote o Sign.extern_type thy) tyco
    39           ^ "\nhas a user-defined serialization")
    40       | SOME tyco'' => (tyco, tyco'')) tycos tycos'';
    41   in (ml_code, (tycos_map, consts_map)) end;
    42 
    43 
    44 (** evaluation **)
    45 
    46 fun eval some_target reff postproc thy t args =
    47   let
    48     val ctxt = ProofContext.init thy;
    49     fun evaluator naming program ((_, (_, ty)), t) deps =
    50       let
    51         val _ = if Code_Thingol.contains_dictvar t then
    52           error "Term to be evaluated contains free dictionaries" else ();
    53         val value_name = "Value.VALUE.value"
    54         val program' = program
    55           |> Graph.new_node (value_name,
    56               Code_Thingol.Fun (Term.dummy_patternN, (([], ty), [(([], t), (Drule.dummy_thm, true))])))
    57           |> fold (curry Graph.add_edge value_name) deps;
    58         val (value_code, [SOME value_name']) = Code_ML.evaluation_code_of thy
    59           (the_default target some_target) "" naming program' [value_name];
    60         val sml_code = "let\n" ^ value_code ^ "\nin " ^ value_name'
    61           ^ space_implode " " (map (enclose "(" ")") args) ^ " end";
    62       in ML_Context.evaluate ctxt false reff sml_code end;
    63   in Code_Thingol.eval thy postproc evaluator t end;
    64 
    65 
    66 (** instrumentalization by antiquotation **)
    67 
    68 local
    69 
    70 structure CodeAntiqData = Proof_Data
    71 (
    72   type T = (string list * string list) * (bool * (string
    73     * (string * ((string * string) list * (string * string) list)) lazy));
    74   fun init _ = (([], []), (true, ("", Lazy.value ("", ([], [])))));
    75 );
    76 
    77 val is_first_occ = fst o snd o CodeAntiqData.get;
    78 
    79 fun register_code new_tycos new_consts ctxt =
    80   let
    81     val ((tycos, consts), (_, (struct_name, _))) = CodeAntiqData.get ctxt;
    82     val tycos' = fold (insert (op =)) new_tycos tycos;
    83     val consts' = fold (insert (op =)) new_consts consts;
    84     val (struct_name', ctxt') = if struct_name = ""
    85       then ML_Antiquote.variant eval_struct_name ctxt
    86       else (struct_name, ctxt);
    87     val acc_code = Lazy.lazy (fn () => evaluation_code (ProofContext.theory_of ctxt) tycos' consts');
    88   in CodeAntiqData.put ((tycos', consts'), (false, (struct_name', acc_code))) ctxt' end;
    89 
    90 fun register_const const = register_code [] [const];
    91 
    92 fun register_datatype tyco constrs = register_code [tyco] constrs;
    93 
    94 fun print_const const all_struct_name tycos_map consts_map =
    95   (Long_Name.append all_struct_name o the o AList.lookup (op =) consts_map) const;
    96 
    97 fun print_datatype tyco constrs all_struct_name tycos_map consts_map =
    98   let
    99     val upperize = implode o nth_map 0 Symbol.to_ascii_upper o explode;
   100     fun check_base name name'' =
   101       if upperize (Long_Name.base_name name) = upperize name''
   102       then () else error ("Name as printed " ^ quote name''
   103         ^ "\ndiffers from logical base name " ^ quote (Long_Name.base_name name) ^ "; sorry.");
   104     val tyco'' = (the o AList.lookup (op =) tycos_map) tyco;
   105     val constrs'' = map (the o AList.lookup (op =) consts_map) constrs;
   106     val _ = check_base tyco tyco'';
   107     val _ = map2 check_base constrs constrs'';
   108   in "datatype " ^ tyco'' ^ " = datatype " ^ Long_Name.append all_struct_name tyco'' end;
   109 
   110 fun print_code struct_name is_first print_it ctxt =
   111   let
   112     val (_, (_, (struct_code_name, acc_code))) = CodeAntiqData.get ctxt;
   113     val (ml_code, (tycos_map, consts_map)) = Lazy.force acc_code;
   114     val ml_code = if is_first then ml_code
   115       else "";
   116     val all_struct_name = Long_Name.append struct_name struct_code_name;
   117   in (ml_code, print_it all_struct_name tycos_map consts_map) end;
   118 
   119 in
   120 
   121 fun ml_code_antiq raw_const {struct_name, background} =
   122   let
   123     val const = Code.check_const (ProofContext.theory_of background) raw_const;
   124     val is_first = is_first_occ background;
   125     val background' = register_const const background;
   126   in (print_code struct_name is_first (print_const const), background') end;
   127 
   128 fun ml_code_datatype_antiq (raw_tyco, raw_constrs) {struct_name, background} =
   129   let
   130     val thy = ProofContext.theory_of background;
   131     val tyco = Sign.intern_type thy raw_tyco;
   132     val constrs = map (Code.check_const thy) raw_constrs;
   133     val constrs' = (map fst o snd o Code.get_datatype thy) tyco;
   134     val _ = if eq_set (op =) (constrs, constrs') then ()
   135       else error ("Type " ^ quote tyco ^ ": given constructors diverge from real constructors")
   136     val is_first = is_first_occ background;
   137     val background' = register_datatype tyco constrs background;
   138   in (print_code struct_name is_first (print_datatype tyco constrs), background') end;
   139 
   140 end; (*local*)
   141 
   142 
   143 (** Isar setup **)
   144 
   145 val _ = ML_Context.add_antiq "code" (fn _ => Args.term >> ml_code_antiq);
   146 val _ = ML_Context.add_antiq "code_datatype" (fn _ =>
   147   (Args.tyname --| Scan.lift (Args.$$$ "=")
   148     -- (Args.term ::: Scan.repeat (Scan.lift (Args.$$$ "|") |-- Args.term)))
   149       >> ml_code_datatype_antiq);
   150 
   151 val setup = Code_Target.extend_target (target, (Code_ML.target_SML, K I));
   152 
   153 end; (*struct*)