src/Pure/codegen.ML
changeset 18708 4b3dadb4fe33
parent 18702 7dc7dcd63224
child 18728 6790126ab5f6
equal deleted inserted replaced
18707:9d6154f76476 18708:4b3dadb4fe33
   238     Pretty.writeln (Pretty.chunks
   238     Pretty.writeln (Pretty.chunks
   239       [Pretty.strs ("term code generators:" :: map fst codegens),
   239       [Pretty.strs ("term code generators:" :: map fst codegens),
   240        Pretty.strs ("type code generators:" :: map fst tycodegens)]);
   240        Pretty.strs ("type code generators:" :: map fst tycodegens)]);
   241 end);
   241 end);
   242 
   242 
   243 val _ = Context.add_setup [CodegenData.init];
   243 val _ = Context.add_setup CodegenData.init;
   244 val print_codegens = CodegenData.print;
   244 val print_codegens = CodegenData.print;
   245 
   245 
   246 
   246 
   247 (**** access parameters for random testing ****)
   247 (**** access parameters for random testing ****)
   248 
   248 
   314 val code_attr =
   314 val code_attr =
   315   Attrib.syntax (Scan.peek (fn thy => foldr op || Scan.fail (map mk_parser
   315   Attrib.syntax (Scan.peek (fn thy => foldr op || Scan.fail (map mk_parser
   316     (#attrs (CodegenData.get thy)))));
   316     (#attrs (CodegenData.get thy)))));
   317 
   317 
   318 val _ = Context.add_setup
   318 val _ = Context.add_setup
   319  [Attrib.add_attributes
   319  (Attrib.add_attributes
   320   [("code", (code_attr, K Attrib.undef_local_attribute),
   320   [("code", (code_attr, K Attrib.undef_local_attribute),
   321      "declare theorems for code generation")]];
   321      "declare theorems for code generation")]);
   322 
   322 
   323 
   323 
   324 (**** preprocessors ****)
   324 (**** preprocessors ****)
   325 
   325 
   326 fun add_preprocessor p thy =
   326 fun add_preprocessor p thy =
   359         then rewrite_rule [eqn] (Thm.transfer thy th)
   359         then rewrite_rule [eqn] (Thm.transfer thy th)
   360         else th
   360         else th
   361       end)
   361       end)
   362   in (add_preprocessor prep thy, eqn) end;
   362   in (add_preprocessor prep thy, eqn) end;
   363 
   363 
   364 val _ = Context.add_setup [add_attribute "unfold" (Scan.succeed unfold_attr)];
   364 val _ = Context.add_setup (add_attribute "unfold" (Scan.succeed unfold_attr));
   365 
   365 
   366 
   366 
   367 (**** associate constants with target language code ****)
   367 (**** associate constants with target language code ****)
   368 
   368 
   369 fun gen_assoc_consts prep_type xs thy = Library.foldl (fn (thy, (s, tyopt, syn)) =>
   369 fun gen_assoc_consts prep_type xs thy = Library.foldl (fn (thy, (s, tyopt, syn)) =>
   832              | SOME (_, module'', _) =>
   832              | SOME (_, module'', _) =>
   833                  (add_edge (node_id, dep) gr'', p module''))
   833                  (add_edge (node_id, dep) gr'', p module''))
   834            end);
   834            end);
   835 
   835 
   836 val _ = Context.add_setup
   836 val _ = Context.add_setup
   837  [add_codegen "default" default_codegen,
   837  (add_codegen "default" default_codegen #>
   838   add_tycodegen "default" default_tycodegen];
   838   add_tycodegen "default" default_tycodegen);
   839 
   839 
   840 
   840 
   841 fun mk_struct name s = "structure " ^ name ^ " =\nstruct\n\n" ^ s ^ "end;\n";
   841 fun mk_struct name s = "structure " ^ name ^ " =\nstruct\n\n" ^ s ^ "end;\n";
   842 
   842 
   843 fun add_to_module name s = AList.map_entry (op =) name (suffix s);
   843 fun add_to_module name s = AList.map_entry (op =) name (suffix s);
  1084        (Symbol.explode s) of
  1084        (Symbol.explode s) of
  1085      (p, []) => p
  1085      (p, []) => p
  1086    | _ => error ("Malformed annotation: " ^ quote s));
  1086    | _ => error ("Malformed annotation: " ^ quote s));
  1087 
  1087 
  1088 val _ = Context.add_setup
  1088 val _ = Context.add_setup
  1089   [assoc_types [("fun", (parse_mixfix (K dummyT) "(_ ->/ _)",
  1089   (assoc_types [("fun", (parse_mixfix (K dummyT) "(_ ->/ _)",
  1090      [("term_of",
  1090      [("term_of",
  1091        "fun term_of_fun_type _ T _ U _ = Free (\"<function>\", T --> U);\n"),
  1091        "fun term_of_fun_type _ T _ U _ = Free (\"<function>\", T --> U);\n"),
  1092       ("test",
  1092       ("test",
  1093        "fun gen_fun_type _ G i =\n\
  1093        "fun gen_fun_type _ G i =\n\
  1094        \  let\n\
  1094        \  let\n\
  1096        \    val _ = (f := (fn x =>\n\
  1096        \    val _ = (f := (fn x =>\n\
  1097        \      let\n\
  1097        \      let\n\
  1098        \        val y = G i;\n\
  1098        \        val y = G i;\n\
  1099        \        val f' = !f\n\
  1099        \        val f' = !f\n\
  1100        \      in (f := (fn x' => if x = x' then y else f' x'); y) end))\n\
  1100        \      in (f := (fn x' => if x = x' then y else f' x'); y) end))\n\
  1101        \  in (fn x => !f x) end;\n")]))]];
  1101        \  in (fn x => !f x) end;\n")]))]);
  1102 
  1102 
  1103 
  1103 
  1104 structure P = OuterParse and K = OuterKeyword;
  1104 structure P = OuterParse and K = OuterKeyword;
  1105 
  1105 
  1106 fun strip_whitespace s = implode (fst (take_suffix (equal "\n" orf equal " ")
  1106 fun strip_whitespace s = implode (fst (take_suffix (equal "\n" orf equal " ")