src/Tools/Code/code_runtime.ML
changeset 64955 25281bee02ac
parent 64954 e5f535f90d61
child 64956 de7ce0fad5bc
equal deleted inserted replaced
64954:e5f535f90d61 64955:25281bee02ac
   397 
   397 
   398 local
   398 local
   399 
   399 
   400 structure Code_Antiq_Data = Proof_Data
   400 structure Code_Antiq_Data = Proof_Data
   401 (
   401 (
   402   type T = string list * (bool
   402   type T = { named_consts: string list,
   403     * (string * (string * string) list) lazy);
   403     first_occurrence: bool,
   404   val empty: T = ([], (true, (Lazy.value ("", []))));
   404     generated_code: {
       
   405       code: string,
       
   406       consts_map: (string * string) list
       
   407     } lazy
       
   408   };
       
   409   val empty: T = { named_consts = [],
       
   410     first_occurrence = true,
       
   411     generated_code = Lazy.value {
       
   412       code = "",
       
   413       consts_map = []
       
   414     }
       
   415   };
   405   fun init _ = empty;
   416   fun init _ = empty;
   406 );
   417 );
   407 
   418 
   408 val is_first_occ = fst o snd o Code_Antiq_Data.get;
   419 val is_first_occurrence = #first_occurrence o Code_Antiq_Data.get;
       
   420 
       
   421 fun lazy_code ctxt program consts = Lazy.lazy (fn () =>
       
   422   let
       
   423     val (code, (_, consts_map)) =
       
   424       evaluation_code ctxt Code_Target.generatedN program [] consts
       
   425   in { code = code, consts_map = consts_map } end);
   409 
   426 
   410 fun register_const const ctxt =
   427 fun register_const const ctxt =
   411   let
   428   let
   412     val (consts, _) = Code_Antiq_Data.get ctxt;
   429     val consts = insert (op =) const ((#named_consts o Code_Antiq_Data.get) ctxt);
   413     val consts' = insert (op =) const consts;
   430     val program = Code_Thingol.consts_program ctxt consts;
   414     val program = Code_Thingol.consts_program ctxt consts';
   431   in
   415     val acc_code = Lazy.lazy (fn () =>
   432     ctxt
   416       evaluation_code ctxt Code_Target.generatedN program [] consts'
   433     |> Code_Antiq_Data.put { 
   417       |> apsnd snd);
   434         named_consts = consts,
   418   in Code_Antiq_Data.put (consts', (false, acc_code)) ctxt end;
   435         first_occurrence = false,
   419 
   436         generated_code = lazy_code ctxt program consts
   420 fun print_code is_first const ctxt =
   437       }
   421   let
   438   end;
   422     val (_, (_, acc_code)) = Code_Antiq_Data.get ctxt;
   439 
   423     val (ml_code, consts_map) = Lazy.force acc_code;
   440 fun print_code is_first_occ const ctxt =
   424     val ml_code = if is_first then ml_code else "";
   441   let
       
   442     val { code, consts_map } = (Lazy.force o #generated_code o Code_Antiq_Data.get) ctxt;
       
   443     val effective_code = if is_first_occ then code else "";
   425     val body = ML_Context.struct_name ctxt ^ "." ^ the (AList.lookup (op =) consts_map const);
   444     val body = ML_Context.struct_name ctxt ^ "." ^ the (AList.lookup (op =) consts_map const);
   426   in (ml_code, body) end;
   445   in (effective_code, body) end;
   427 
   446 
   428 in
   447 in
   429 
   448 
   430 fun ml_code_antiq raw_const ctxt =
   449 fun ml_code_antiq raw_const ctxt =
   431   let
   450   let
   432     val thy = Proof_Context.theory_of ctxt;
   451     val thy = Proof_Context.theory_of ctxt;
   433     val const = Code.check_const thy raw_const;
   452     val const = Code.check_const thy raw_const;
   434     val is_first = is_first_occ ctxt;
   453     val is_first = is_first_occurrence ctxt;
   435   in (print_code is_first const, register_const const ctxt) end;
   454   in (print_code is_first const, register_const const ctxt) end;
   436 
   455 
   437 end; (*local*)
   456 end; (*local*)
   438 
   457 
   439 
   458