src/Tools/Code/code_runtime.ML
changeset 64990 c6a7de505796
parent 64989 40c36a4aee1f
child 64991 d2c79b16e133
equal deleted inserted replaced
64989:40c36a4aee1f 64990:c6a7de505796
   204 val generated_computationN = "Generated_Computation";
   204 val generated_computationN = "Generated_Computation";
   205 
   205 
   206 
   206 
   207 (* possible type signatures of constants *)
   207 (* possible type signatures of constants *)
   208 
   208 
   209 fun typ_signatures_for T =
   209 fun typ_signatures' T =
   210   let
   210   let
   211     val (Ts, T') = strip_type T;
   211     val (Ts, T') = strip_type T;
   212   in map_range (fn n => (drop n Ts ---> T', take n Ts)) (length Ts + 1) end;
   212   in map_range (fn n => (drop n Ts ---> T', take n Ts)) (length Ts + 1) end;
   213 
   213 
   214 fun typ_signatures cTs =
   214 fun typ_signatures cTs =
   215   let
   215   let
   216     fun add (c, T) =
   216     fun add (c, T) =
   217       fold (fn (T, Ts) => Typtab.map_default (T, []) (cons (c, Ts)))
   217       fold (fn (T, Ts) => Typtab.map_default (T, []) (cons (c, Ts)))
   218         (typ_signatures_for T);
   218         (typ_signatures' T);
   219   in
   219   in
   220     Typtab.empty
   220     Typtab.empty
   221     |> fold add cTs
   221     |> fold add cTs
   222     |> Typtab.lookup_list
   222     |> Typtab.lookup_list
   223   end;
   223   end;
   282 
   282 
   283 (* code generation for of the universal morphism *)
   283 (* code generation for of the universal morphism *)
   284 
   284 
   285 val print_const = ML_Syntax.print_pair ML_Syntax.print_string ML_Syntax.print_typ;
   285 val print_const = ML_Syntax.print_pair ML_Syntax.print_string ML_Syntax.print_typ;
   286 
   286 
   287 fun print_of_term_funs { typ_sign_for, eval_for_const, of_term_for_typ } Ts =
   287 fun print_of_term_funs { typ_signatures_for, eval_for_const, of_term_for_typ } Ts =
   288   let
   288   let
   289     val var_names = map_range (fn n => "t" ^ string_of_int (n + 1));
   289     val var_names = map_range (fn n => "t" ^ string_of_int (n + 1));
   290     fun print_lhs c xs = "Const (" ^ quote c ^ ", _)"
   290     fun print_lhs c xs = "Const (" ^ quote c ^ ", _)"
   291       |> fold (fn x => fn s => s ^ " $ " ^ x) xs
   291       |> fold (fn x => fn s => s ^ " $ " ^ x) xs
   292       |> enclose "(" ")";
   292       |> enclose "(" ")";
   297       let
   297       let
   298         val xs = var_names (length Ts);
   298         val xs = var_names (length Ts);
   299       in print_lhs c xs ^ " = " ^ print_rhs c Ts T xs end;
   299       in print_lhs c xs ^ " = " ^ print_rhs c Ts T xs end;
   300     fun print_eqs T =
   300     fun print_eqs T =
   301       let
   301       let
   302         val typ_signs = typ_sign_for T;
   302         val typ_signs = typ_signatures_for T;
   303         val name = of_term_for_typ T;
   303         val name = of_term_for_typ T;
   304       in
   304       in
   305         map (print_eq T) typ_signs
   305         map (print_eq T) typ_signs
   306         |> map (prefix (name ^ " "))
   306         |> map (prefix (name ^ " "))
   307         |> space_implode "\n  | "
   307         |> space_implode "\n  | "
   310     map print_eqs Ts
   310     map print_eqs Ts
   311     |> space_implode "\nand "
   311     |> space_implode "\nand "
   312     |> prefix "fun "
   312     |> prefix "fun "
   313   end;
   313   end;
   314 
   314 
   315 fun print_computation_code ctxt compiled_value [] requested_Ts = ("", [])
   315 fun print_computation_code ctxt compiled_value [] requested_Ts =
       
   316       if null requested_Ts then ("", [])
       
   317       else error ("No equation available for requested type "
       
   318         ^ Syntax.string_of_typ ctxt (hd requested_Ts))
   316   | print_computation_code ctxt compiled_value cTs requested_Ts =
   319   | print_computation_code ctxt compiled_value cTs requested_Ts =
   317       let
   320       let
   318         val proper_cTs = map_filter I cTs;
   321         val proper_cTs = map_filter I cTs;
   319         val typ_sign_for = typ_signatures proper_cTs;
   322         val typ_signatures_for = typ_signatures proper_cTs;
   320         fun add_typ T Ts =
   323         fun add_typ T Ts =
   321           if member (op =) Ts T
   324           if member (op =) Ts T
   322           then Ts
   325           then Ts
   323           else Ts
   326           else case typ_signatures_for T of
   324             |> cons T
   327             [] => error ("No equation available for requested type "
   325             |> fold (fold add_typ o snd) (typ_sign_for T);
   328               ^ Syntax.string_of_typ ctxt T)
       
   329           | typ_signs =>
       
   330               Ts
       
   331               |> cons T
       
   332               |> fold (fold add_typ o snd) typ_signs;
   326         val required_Ts = fold add_typ requested_Ts [];
   333         val required_Ts = fold add_typ requested_Ts [];
   327         val of_term_for_typ' = of_term_for_typ required_Ts;
   334         val of_term_for_typ' = of_term_for_typ required_Ts;
   328         val eval_for_const' = eval_for_const ctxt proper_cTs;
   335         val eval_for_const' = eval_for_const ctxt proper_cTs;
   329         val eval_for_const'' = the_default "_" o Option.map eval_for_const';
   336         val eval_for_const'' = the_default "_" o Option.map eval_for_const';
   330         val eval_tuple = enclose "(" ")" (commas (map eval_for_const' proper_cTs));
   337         val eval_tuple = enclose "(" ")" (commas (map eval_for_const' proper_cTs));
   331         fun mk_abs s = "fn " ^ s ^ " => ";
   338         fun mk_abs s = "fn " ^ s ^ " => ";
   332         val eval_abs = space_implode ""
   339         val eval_abs = space_implode ""
   333           (map (mk_abs o eval_for_const'') cTs);
   340           (map (mk_abs o eval_for_const'') cTs);
   334         val of_term_code = print_of_term_funs {
   341         val of_term_code = print_of_term_funs {
   335           typ_sign_for = typ_sign_for,
   342           typ_signatures_for = typ_signatures_for,
   336           eval_for_const = eval_for_const',
   343           eval_for_const = eval_for_const',
   337           of_term_for_typ = of_term_for_typ' } required_Ts;
   344           of_term_for_typ = of_term_for_typ' } required_Ts;
   338         val of_term_names = map (Long_Name.append generated_computationN
   345         val of_term_names = map (Long_Name.append generated_computationN
   339           o of_term_for_typ') requested_Ts;
   346           o of_term_for_typ') requested_Ts;
   340       in
   347       in