src/Pure/codegen.ML
changeset 24280 c9867bdf2424
parent 24219 e558fe311376
child 24293 7e67b9706211
equal deleted inserted replaced
24279:165648d5679f 24280:c9867bdf2424
    83   val replace_quotes: 'b list -> 'a mixfix list -> 'b mixfix list
    83   val replace_quotes: 'b list -> 'a mixfix list -> 'b mixfix list
    84   val fillin_mixfix: ('a -> Pretty.T) -> 'a mixfix list -> 'a list -> Pretty.T
    84   val fillin_mixfix: ('a -> Pretty.T) -> 'a mixfix list -> 'a list -> Pretty.T
    85   val mk_deftab: theory -> deftab
    85   val mk_deftab: theory -> deftab
    86   val add_unfold: thm -> theory -> theory
    86   val add_unfold: thm -> theory -> theory
    87 
    87 
       
    88   val setup: theory -> theory
       
    89 
    88   val get_node: codegr -> string -> node
    90   val get_node: codegr -> string -> node
    89   val add_edge: string * string -> codegr -> codegr
    91   val add_edge: string * string -> codegr -> codegr
    90   val add_edge_acyclic: string * string -> codegr -> codegr
    92   val add_edge_acyclic: string * string -> codegr -> codegr
    91   val del_nodes: string list -> codegr -> codegr
    93   val del_nodes: string list -> codegr -> codegr
    92   val map_node: string -> (node -> node) -> codegr -> codegr
    94   val map_node: string -> (node -> node) -> codegr -> codegr
   334         then rewrite_rule [eqn'] (Thm.transfer thy th)
   336         then rewrite_rule [eqn'] (Thm.transfer thy th)
   335         else th
   337         else th
   336       end)
   338       end)
   337   in add_preprocessor prep end;
   339   in add_preprocessor prep end;
   338 
   340 
   339 val _ = Context.add_setup
       
   340   (let
       
   341     fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
       
   342   in
       
   343     Code.add_attribute ("unfold", Scan.succeed (mk_attribute
       
   344       (fn thm => add_unfold thm #> Code.add_inline thm)))
       
   345   end);
       
   346 
   341 
   347 
   342 
   348 (**** associate constants with target language code ****)
   343 (**** associate constants with target language code ****)
   349 
   344 
   350 fun gen_assoc_const prep_const (raw_const, syn) thy =
   345 fun gen_assoc_const prep_const (raw_const, syn) thy =
   796                      p module'))
   791                      p module'))
   797              | SOME (_, module'', _) =>
   792              | SOME (_, module'', _) =>
   798                  (add_edge (node_id, dep) gr'', p module''))
   793                  (add_edge (node_id, dep) gr'', p module''))
   799            end);
   794            end);
   800 
   795 
   801 val _ = Context.add_setup
       
   802  (add_codegen "default" default_codegen #>
       
   803   add_tycodegen "default" default_tycodegen);
       
   804 
       
   805 
       
   806 fun mk_struct name s = "structure " ^ name ^ " =\nstruct\n\n" ^ s ^ "end;\n";
   796 fun mk_struct name s = "structure " ^ name ^ " =\nstruct\n\n" ^ s ^ "end;\n";
   807 
   797 
   808 fun add_to_module name s = AList.map_entry (op =) name (suffix s);
   798 fun add_to_module name s = AList.map_entry (op =) name (suffix s);
   809 
   799 
   810 fun output_code gr module xs =
   800 fun output_code gr module xs =
  1032 fun evaluation_oracle (thy, Evaluation t) =
  1022 fun evaluation_oracle (thy, Evaluation t) =
  1033   Logic.mk_equals (t, eval_term thy t);
  1023   Logic.mk_equals (t, eval_term thy t);
  1034 
  1024 
  1035 fun evaluation_conv ct =
  1025 fun evaluation_conv ct =
  1036   let val {thy, t, ...} = rep_cterm ct
  1026   let val {thy, t, ...} = rep_cterm ct
  1037   in Thm.invoke_oracle_i thy "Pure.evaluation" (thy, Evaluation t) end;
  1027   in Thm.invoke_oracle_i thy "Code_Setup.evaluation" (thy, Evaluation t) end;
  1038 
  1028 
  1039 val _ = Context.add_setup
       
  1040   (Theory.add_oracle ("evaluation", evaluation_oracle));
       
  1041 
  1029 
  1042 (**** Interface ****)
  1030 (**** Interface ****)
  1043 
  1031 
  1044 val str = PrintMode.with_default Pretty.str;
  1032 val str = PrintMode.with_default Pretty.str;
  1045 
  1033 
  1058             || Scan.unless ($$ "_" || $$ "?" || $$ "\\<module>" || $$ "/" || $$ "{" |-- $$ "*")
  1046             || Scan.unless ($$ "_" || $$ "?" || $$ "\\<module>" || $$ "/" || $$ "{" |-- $$ "*")
  1059                  (Scan.one Symbol.is_regular)) >> (Pretty o str o implode)))
  1047                  (Scan.one Symbol.is_regular)) >> (Pretty o str o implode)))
  1060        (Symbol.explode s) of
  1048        (Symbol.explode s) of
  1061      (p, []) => p
  1049      (p, []) => p
  1062    | _ => error ("Malformed annotation: " ^ quote s));
  1050    | _ => error ("Malformed annotation: " ^ quote s));
  1063 
       
  1064 val _ = Context.add_setup
       
  1065   (fold assoc_type [("fun", (parse_mixfix (K dummyT) "(_ ->/ _)",
       
  1066      [("term_of",
       
  1067        "fun term_of_fun_type _ T _ U _ = Free (\"<function>\", T --> U);\n"),
       
  1068       ("test",
       
  1069        "fun gen_fun_type _ G i =\n\
       
  1070        \  let\n\
       
  1071        \    val f = ref (fn x => raise Match);\n\
       
  1072        \    val _ = (f := (fn x =>\n\
       
  1073        \      let\n\
       
  1074        \        val y = G i;\n\
       
  1075        \        val f' = !f\n\
       
  1076        \      in (f := (fn x' => if x = x' then y else f' x'); y) end))\n\
       
  1077        \  in (fn x => !f x) end;\n")]))]);
       
  1078 
  1051 
  1079 
  1052 
  1080 structure P = OuterParse and K = OuterKeyword;
  1053 structure P = OuterParse and K = OuterKeyword;
  1081 
  1054 
  1082 fun strip_whitespace s = implode (fst (take_suffix (equal "\n" orf equal " ")
  1055 fun strip_whitespace s = implode (fst (take_suffix (equal "\n" orf equal " ")
  1178 val _ = OuterSyntax.add_keywords ["attach", "file", "contains"];
  1151 val _ = OuterSyntax.add_keywords ["attach", "file", "contains"];
  1179 
  1152 
  1180 val _ = OuterSyntax.add_parsers
  1153 val _ = OuterSyntax.add_parsers
  1181   [assoc_typeP, assoc_constP, code_libraryP, code_moduleP, test_paramsP, testP, valueP];
  1154   [assoc_typeP, assoc_constP, code_libraryP, code_moduleP, test_paramsP, testP, valueP];
  1182 
  1155 
       
  1156 val setup = 
       
  1157   let
       
  1158     fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
       
  1159   in
       
  1160     Code.add_attribute ("unfold", Scan.succeed (mk_attribute
       
  1161       (fn thm => add_unfold thm #> Code.add_inline thm)))
       
  1162     #> add_codegen "default" default_codegen
       
  1163     #> add_tycodegen "default" default_tycodegen
       
  1164     #> Theory.add_oracle ("evaluation", evaluation_oracle)
       
  1165     #> fold assoc_type [("fun", (parse_mixfix (K dummyT) "(_ ->/ _)",
       
  1166         [("term_of",
       
  1167             "fun term_of_fun_type _ T _ U _ = Free (\"<function>\", T --> U);\n"),
       
  1168         ("test",
       
  1169            "fun gen_fun_type _ G i =\n\
       
  1170            \  let\n\
       
  1171            \    val f = ref (fn x => raise Match);\n\
       
  1172            \    val _ = (f := (fn x =>\n\
       
  1173            \      let\n\
       
  1174            \        val y = G i;\n\
       
  1175            \        val f' = !f\n\
       
  1176            \      in (f := (fn x' => if x = x' then y else f' x'); y) end))\n\
       
  1177            \  in (fn x => !f x) end;\n")]))]
       
  1178   end;
       
  1179 
  1183 end;
  1180 end;