src/Tools/Code/code_runtime.ML
changeset 64945 4db1aa362c8c
parent 64944 27b1ba3ef778
child 64946 03b5f4e7f4a6
equal deleted inserted replaced
64944:27b1ba3ef778 64945:4db1aa362c8c
   270 
   270 
   271 val generated_computationN = "Generated_Computation";
   271 val generated_computationN = "Generated_Computation";
   272 
   272 
   273 fun print_computation_code ctxt compiled_value requested_Ts cTs =
   273 fun print_computation_code ctxt compiled_value requested_Ts cTs =
   274   let
   274   let
   275     val typ_sign_for = typ_signatures cTs;
   275     val proper_cTs = map_filter I cTs;
       
   276     val typ_sign_for = typ_signatures proper_cTs;
   276     fun add_typ T Ts =
   277     fun add_typ T Ts =
   277       if member (op =) Ts T
   278       if member (op =) Ts T
   278       then Ts
   279       then Ts
   279       else Ts
   280       else Ts
   280         |> cons T
   281         |> cons T
   281         |> fold (fold add_typ o snd) (typ_sign_for T);
   282         |> fold (fold add_typ o snd) (typ_sign_for T);
   282     val required_Ts = fold add_typ requested_Ts [];
   283     val required_Ts = fold add_typ requested_Ts [];
   283     val of_term_for_typ' = of_term_for_typ required_Ts;
   284     val of_term_for_typ' = of_term_for_typ required_Ts;
   284     val eval_for_const' = eval_for_const ctxt cTs;
   285     val eval_for_const' = eval_for_const ctxt proper_cTs;
   285     val evals = map eval_for_const' cTs;
   286     val eval_for_const'' = the_default "_" o Option.map eval_for_const';
   286     val eval_tuple = enclose "(" ")" (commas evals);
   287     val eval_tuple = enclose "(" ")" (commas (map eval_for_const' proper_cTs));
   287     val eval_abs = space_implode "" (map (fn s => "fn " ^ s ^ " => ") evals);
   288     fun mk_abs s = "fn " ^ s ^ " => ";
       
   289     val eval_abs = space_implode ""
       
   290       (map (mk_abs o eval_for_const'') cTs);
   288     val of_term_code = print_of_term_funs {
   291     val of_term_code = print_of_term_funs {
   289       typ_sign_for = typ_sign_for,
   292       typ_sign_for = typ_sign_for,
   290       eval_for_const = eval_for_const',
   293       eval_for_const = eval_for_const',
   291       of_term_for_typ = of_term_for_typ' } required_Ts;
   294       of_term_for_typ = of_term_for_typ' } required_Ts;
   292   in
   295   in
   325     val _ = check t;
   328     val _ = check t;
   326   in t end;
   329   in t end;
   327 
   330 
   328 fun compile_computation cookie ctxt T program evals vs_ty_evals deps =
   331 fun compile_computation cookie ctxt T program evals vs_ty_evals deps =
   329   let
   332   let
       
   333     fun the_const (Const cT) = cT
       
   334       | the_const t = error ("No constant after preprocessing: " ^ Syntax.string_of_term ctxt t)
   330     val raw_cTs = case evals of
   335     val raw_cTs = case evals of
   331         Abs (_, _, t) => (snd o strip_comb) t
   336         Abs (_, _, t) => (map the_const o snd o strip_comb) t
   332       | _ => error ("Bad term after preprocessing: " ^ Syntax.string_of_term ctxt evals);
   337       | _ => error ("Bad term after preprocessing: " ^ Syntax.string_of_term ctxt evals);
   333     val cTs = map (fn Const cT => cT
   338     val cTs = fold_rev (fn cT => fn cTs =>
   334       | t => error ("No constant after preprocessing: " ^ Syntax.string_of_term ctxt t)) raw_cTs;
   339       (if member (op =) cTs (SOME cT) then NONE else SOME cT) :: cTs) raw_cTs [];
   335     fun comp' vs_ty_evals =
   340     fun comp' vs_ty_evals =
   336       let
   341       let
   337         val (generated_code, compiled_value) =
   342         val (generated_code, compiled_value) =
   338           build_computation_text ctxt NONE program deps vs_ty_evals;
   343           build_computation_text ctxt NONE program deps vs_ty_evals;
   339         val (of_term_code, [of_term]) = print_computation_code ctxt compiled_value [T] cTs;
   344         val (of_term_code, [of_term]) = print_computation_code ctxt compiled_value [T] cTs;
   344     val compiled_computation =
   349     val compiled_computation =
   345       Exn.release (run_computation_text cookie ctxt comp' vs_ty_evals []);
   350       Exn.release (run_computation_text cookie ctxt comp' vs_ty_evals []);
   346   in fn ctxt' =>
   351   in fn ctxt' =>
   347     check_typ ctxt' T
   352     check_typ ctxt' T
   348     #> reject_vars ctxt'
   353     #> reject_vars ctxt'
   349     #> check_computation_input ctxt cTs
   354     #> check_computation_input ctxt (map_filter I cTs)
   350     #> compiled_computation
   355     #> compiled_computation
   351   end;
   356   end;
   352 
   357 
   353 fun experimental_computation cookie { ctxt, lift_postproc, terms = ts, T } =
   358 fun experimental_computation cookie { ctxt, lift_postproc, terms = ts, T } =
   354   let
   359   let