src/Pure/codegen.ML
changeset 22680 31a2303f4283
parent 22596 d0d2af4db18f
child 22749 189efc4a9f54
equal deleted inserted replaced
22679:68cd69a388e2 22680:31a2303f4283
   195 fun set_iterations iterations ({size, default_type, ...} : test_params) =
   195 fun set_iterations iterations ({size, default_type, ...} : test_params) =
   196   {size = size, iterations = iterations, default_type = default_type};
   196   {size = size, iterations = iterations, default_type = default_type};
   197 
   197 
   198 fun set_default_type s thy ({size, iterations, ...} : test_params) =
   198 fun set_default_type s thy ({size, iterations, ...} : test_params) =
   199   {size = size, iterations = iterations,
   199   {size = size, iterations = iterations,
   200    default_type = SOME (typ_of (read_ctyp thy s))};
   200    default_type = SOME (Sign.read_typ thy s)};
   201 
   201 
   202 (* data kind 'Pure/codegen' *)
   202 (* data kind 'Pure/codegen' *)
   203 
   203 
   204 structure CodegenData = TheoryDataFun
   204 structure CodegenData = TheoryDataFun
   205 (struct
   205 (struct
   392          end
   392          end
   393      | _ => error ("Not a constant: " ^ s))
   393      | _ => error ("Not a constant: " ^ s))
   394   end) (thy, xs);
   394   end) (thy, xs);
   395 
   395 
   396 val assoc_consts_i = gen_assoc_consts (K I);
   396 val assoc_consts_i = gen_assoc_consts (K I);
   397 val assoc_consts = gen_assoc_consts (typ_of oo read_ctyp);
   397 val assoc_consts = gen_assoc_consts Sign.read_typ;
   398 
   398 
   399 
   399 
   400 (**** associate types with target language types ****)
   400 (**** associate types with target language types ****)
   401 
   401 
   402 fun assoc_types xs thy = Library.foldl (fn (thy, (s, syn)) =>
   402 fun assoc_types xs thy = Library.foldl (fn (thy, (s, syn)) =>
   567 fun get_aux_code xs = map_filter (fn (m, code) =>
   567 fun get_aux_code xs = map_filter (fn (m, code) =>
   568   if m = "" orelse member (op =) (!mode) m then SOME code else NONE) xs;
   568   if m = "" orelse member (op =) (!mode) m then SOME code else NONE) xs;
   569 
   569 
   570 fun mk_deftab thy =
   570 fun mk_deftab thy =
   571   let
   571   let
   572     val axmss = map (fn thy' =>
   572     val axmss = map (fn thy' => (Context.theory_name thy', Theory.axiom_table thy'))
   573       (Context.theory_name thy', snd (#axioms (Theory.rep_theory thy'))))
       
   574       (thy :: Theory.ancestors_of thy);
   573       (thy :: Theory.ancestors_of thy);
   575     fun prep_def def = (case preprocess thy [def] of
   574     fun prep_def def = (case preprocess thy [def] of
   576       [def'] => prop_of def' | _ => error "mk_deftab: bad preprocessor");
   575       [def'] => prop_of def' | _ => error "mk_deftab: bad preprocessor");
   577     fun dest t =
   576     fun dest t =
   578       let
   577       let
   909            (output_code (fst gr') (if_library "" module) ["<Top>"]))
   908            (output_code (fst gr') (if_library "" module) ["<Top>"]))
   910   in
   909   in
   911     (code'', del_nodes ["<Top>"] gr')
   910     (code'', del_nodes ["<Top>"] gr')
   912   end));
   911   end));
   913 
   912 
   914 val generate_code_i = gen_generate_code (K I);
   913 val generate_code_i = gen_generate_code Sign.cert_term;
   915 val generate_code = gen_generate_code
   914 val generate_code = gen_generate_code Sign.read_term;
   916   (fn thy => term_of o read_cterm thy o rpair TypeInfer.logicT);
       
   917 
   915 
   918 
   916 
   919 (**** Reflection ****)
   917 (**** Reflection ****)
   920 
   918 
   921 val strip_tname = implode o tl o explode;
   919 val strip_tname = implode o tl o explode;
  1121   OuterSyntax.command "types_code"
  1119   OuterSyntax.command "types_code"
  1122   "associate types with target language types" K.thy_decl
  1120   "associate types with target language types" K.thy_decl
  1123     (Scan.repeat1 (P.xname --| P.$$$ "(" -- P.string --| P.$$$ ")" -- parse_attach) >>
  1121     (Scan.repeat1 (P.xname --| P.$$$ "(" -- P.string --| P.$$$ ")" -- parse_attach) >>
  1124      (fn xs => Toplevel.theory (fn thy => assoc_types
  1122      (fn xs => Toplevel.theory (fn thy => assoc_types
  1125        (map (fn ((name, mfx), aux) => (name, (parse_mixfix
  1123        (map (fn ((name, mfx), aux) => (name, (parse_mixfix
  1126          (typ_of o read_ctyp thy) mfx, aux))) xs) thy)));
  1124          (Sign.read_typ thy) mfx, aux))) xs) thy)));
  1127 
  1125 
  1128 val assoc_constP =
  1126 val assoc_constP =
  1129   OuterSyntax.command "consts_code"
  1127   OuterSyntax.command "consts_code"
  1130   "associate constants with target language code" K.thy_decl
  1128   "associate constants with target language code" K.thy_decl
  1131     (Scan.repeat1
  1129     (Scan.repeat1
  1132        (P.xname -- (Scan.option (P.$$$ "::" |-- P.typ)) --|
  1130        (P.xname -- (Scan.option (P.$$$ "::" |-- P.typ)) --|
  1133         P.$$$ "(" -- P.string --| P.$$$ ")" -- parse_attach) >>
  1131         P.$$$ "(" -- P.string --| P.$$$ ")" -- parse_attach) >>
  1134      (fn xs => Toplevel.theory (fn thy => assoc_consts
  1132      (fn xs => Toplevel.theory (fn thy => assoc_consts
  1135        (map (fn (((name, optype), mfx), aux) => (name, optype, (parse_mixfix
  1133        (map (fn (((name, optype), mfx), aux) =>
  1136          (term_of o read_cterm thy o rpair TypeInfer.logicT) mfx, aux)))
  1134          (name, optype, (parse_mixfix (Sign.read_term thy) mfx, aux))) xs) thy)));
  1137            xs) thy)));
       
  1138 
  1135 
  1139 fun parse_code lib =
  1136 fun parse_code lib =
  1140   Scan.optional (P.$$$ "(" |-- P.enum "," P.xname --| P.$$$ ")") (!mode) --
  1137   Scan.optional (P.$$$ "(" |-- P.enum "," P.xname --| P.$$$ ")") (!mode) --
  1141   (if lib then Scan.optional P.name "" else P.name) --
  1138   (if lib then Scan.optional P.name "" else P.name) --
  1142   Scan.option (P.$$$ "file" |-- P.name) --
  1139   Scan.option (P.$$$ "file" |-- P.name) --
  1181 val parse_test_params = P.short_ident :-- (fn s =>
  1178 val parse_test_params = P.short_ident :-- (fn s =>
  1182   P.$$$ "=" |-- (AList.lookup (op =) params s |> the_default Scan.fail)) >> snd;
  1179   P.$$$ "=" |-- (AList.lookup (op =) params s |> the_default Scan.fail)) >> snd;
  1183 
  1180 
  1184 fun parse_tyinst xs =
  1181 fun parse_tyinst xs =
  1185   (P.type_ident --| P.$$$ "=" -- P.typ >> (fn (v, s) => fn thy =>
  1182   (P.type_ident --| P.$$$ "=" -- P.typ >> (fn (v, s) => fn thy =>
  1186     fn (x, ys) => (x, (v, typ_of (read_ctyp thy s)) :: ys))) xs;
  1183     fn (x, ys) => (x, (v, Sign.read_typ thy s) :: ys))) xs;
  1187 
  1184 
  1188 fun app [] x = x
  1185 fun app [] x = x
  1189   | app (f :: fs) x = app fs (f x);
  1186   | app (f :: fs) x = app fs (f x);
  1190 
  1187 
  1191 val test_paramsP =
  1188 val test_paramsP =