src/Tools/Code/code_target.ML
changeset 55372 3662c44d018c
parent 55306 b1ca6ce9e1e0
child 55683 5732a55b9232
equal deleted inserted replaced
55371:cb0c6cb10681 55372:3662c44d018c
    51 
    51 
    52   type ('a, 'b, 'c, 'd, 'e, 'f) symbol_attr_decl
    52   type ('a, 'b, 'c, 'd, 'e, 'f) symbol_attr_decl
    53   type identifier_data
    53   type identifier_data
    54   val set_identifiers: (string, string, string, string, string, string) symbol_attr_decl
    54   val set_identifiers: (string, string, string, string, string, string) symbol_attr_decl
    55     -> theory -> theory
    55     -> theory -> theory
    56   type raw_const_syntax = Code_Printer.raw_const_syntax
    56   val set_printings: (Code_Printer.raw_const_syntax, Code_Printer.tyco_syntax, string, unit, unit, (string * string list)) symbol_attr_decl
    57   type tyco_syntax = Code_Printer.tyco_syntax
       
    58   val set_printings: (raw_const_syntax, tyco_syntax, string, unit, unit, (string * string list)) symbol_attr_decl
       
    59     -> theory -> theory
    57     -> theory -> theory
    60   val add_const_syntax: string -> string -> raw_const_syntax option -> theory -> theory
       
    61   val add_tyco_syntax: string -> string -> tyco_syntax option -> theory -> theory
       
    62   val add_class_syntax: string -> class -> string option -> theory -> theory
       
    63   val add_instance_syntax: string -> class * string -> unit option -> theory -> theory
       
    64   val add_reserved: string -> string -> theory -> theory
    58   val add_reserved: string -> string -> theory -> theory
    65   val add_include: string -> string * (string * string list) option -> theory -> theory
       
    66 
    59 
    67   val codegen_tool: string (*theory name*) -> string (*export_code expr*) -> unit
    60   val codegen_tool: string (*theory name*) -> string (*export_code expr*) -> unit
    68 
    61 
    69   val setup: theory -> theory
    62   val setup: theory -> theory
    70 end;
    63 end;
   582   fold set_identifier (prep_name_decl thy raw_name_decls) thy;
   575   fold set_identifier (prep_name_decl thy raw_name_decls) thy;
   583 
   576 
   584 val set_identifiers = gen_set_identifiers cert_name_decls;
   577 val set_identifiers = gen_set_identifiers cert_name_decls;
   585 val set_identifiers_cmd = gen_set_identifiers read_name_decls;
   578 val set_identifiers_cmd = gen_set_identifiers read_name_decls;
   586 
   579 
   587 fun add_module_alias_cmd target aliasses thy =
       
   588   let
       
   589     val _ = legacy_feature "prefer \"code_identifier\" over \"code_modulename\"";
       
   590   in
       
   591     fold (fn (sym, name) => set_identifier
       
   592       (target, Code_Symbol.Module (sym, if name = "" then NONE else SOME (check_name true name))))
       
   593       aliasses thy
       
   594   end;
       
   595 
       
   596 
   580 
   597 (* custom printings *)
   581 (* custom printings *)
   598 
   582 
   599 fun arrange_printings prep_const thy =
   583 fun arrange_printings prep_const thy =
   600   let
   584   let
   618   fold set_printing (prep_print_decl thy raw_print_decls) thy;
   602   fold set_printing (prep_print_decl thy raw_print_decls) thy;
   619 
   603 
   620 val set_printings = gen_set_printings cert_printings;
   604 val set_printings = gen_set_printings cert_printings;
   621 val set_printings_cmd = gen_set_printings read_printings;
   605 val set_printings_cmd = gen_set_printings read_printings;
   622 
   606 
   623 fun gen_add_syntax Symbol prep_x prep_syn target raw_x some_raw_syn thy =
       
   624   let
       
   625     val _ = legacy_feature "prefer \"code_printing\" for custom serialisations"
       
   626     val x = prep_x thy raw_x;
       
   627   in set_printing (target, Symbol (x, Option.map (prep_syn thy target x) some_raw_syn)) thy end;
       
   628 
       
   629 fun gen_add_const_syntax prep_const =
       
   630   gen_add_syntax Constant prep_const check_const_syntax;
       
   631 
       
   632 fun gen_add_tyco_syntax prep_tyco =
       
   633   gen_add_syntax Type_Constructor prep_tyco check_tyco_syntax;
       
   634 
       
   635 fun gen_add_class_syntax prep_class =
       
   636   gen_add_syntax Type_Class prep_class ((K o K o K) I);
       
   637 
       
   638 fun gen_add_instance_syntax prep_inst =
       
   639   gen_add_syntax Class_Instance prep_inst ((K o K o K) I);
       
   640 
       
   641 fun gen_add_include prep_const target (name, some_content) thy =
       
   642   gen_add_syntax Module (K I)
       
   643     (fn thy => fn _ => fn _ => fn (raw_content, raw_cs) => (Code_Printer.str raw_content, map (prep_const thy) raw_cs))
       
   644     target name some_content thy;
       
   645 
       
   646 
   607 
   647 (* concrete syntax *)
   608 (* concrete syntax *)
   648 
       
   649 local
       
   650 
       
   651 fun zip_list (x :: xs) f g =
       
   652   f
       
   653   :|-- (fn y =>
       
   654     fold_map (fn x => g |-- f >> pair x) xs
       
   655     :|-- (fn xys => pair ((x, y) :: xys)));
       
   656 
       
   657 fun process_multi_syntax parse_thing parse_syntax change =
       
   658   (Parse.and_list1 parse_thing
       
   659   :|-- (fn things => Scan.repeat1 (@{keyword "("} |-- Parse.name --
       
   660         (zip_list things (Scan.option parse_syntax) @{keyword "and"}) --| @{keyword ")"})))
       
   661   >> (Toplevel.theory oo fold)
       
   662     (fn (target, syns) => fold (fn (raw_x, syn) => change target raw_x syn) syns);
       
   663 
       
   664 in
       
   665 
       
   666 val add_reserved = add_reserved;
       
   667 val add_const_syntax = gen_add_const_syntax (K I);
       
   668 val add_tyco_syntax = gen_add_tyco_syntax cert_tyco;
       
   669 val add_class_syntax = gen_add_class_syntax cert_class;
       
   670 val add_instance_syntax = gen_add_instance_syntax cert_inst;
       
   671 val add_include = gen_add_include (K I);
       
   672 
       
   673 val add_const_syntax_cmd = gen_add_const_syntax Code.read_const;
       
   674 val add_tyco_syntax_cmd = gen_add_tyco_syntax read_tyco;
       
   675 val add_class_syntax_cmd = gen_add_class_syntax read_class;
       
   676 val add_instance_syntax_cmd = gen_add_instance_syntax read_inst;
       
   677 val add_include_cmd = gen_add_include Code.read_const;
       
   678 
   609 
   679 fun parse_args f args =
   610 fun parse_args f args =
   680   case Scan.read Token.stopper f args
   611   case Scan.read Token.stopper f args
   681    of SOME x => x
   612    of SOME x => x
   682     | NONE => error "Bad serializer arguments";
   613     | NONE => error "Bad serializer arguments";
   734   Outer_Syntax.command @{command_spec "code_identifier"} "declare mandatory names for code symbols"
   665   Outer_Syntax.command @{command_spec "code_identifier"} "declare mandatory names for code symbols"
   735     (parse_symbol_pragmas Parse.name Parse.name Parse.name Parse.name Parse.name Parse.name
   666     (parse_symbol_pragmas Parse.name Parse.name Parse.name Parse.name Parse.name Parse.name
   736       >> (Toplevel.theory o fold set_identifiers_cmd));
   667       >> (Toplevel.theory o fold set_identifiers_cmd));
   737 
   668 
   738 val _ =
   669 val _ =
   739   Outer_Syntax.command @{command_spec "code_modulename"} "alias module to other name"
       
   740     (Parse.name -- Scan.repeat1 (Parse.name -- Parse.name)
       
   741       >> (fn (target, modlnames) => (Toplevel.theory o add_module_alias_cmd target) modlnames));
       
   742 
       
   743 val _ =
       
   744   Outer_Syntax.command @{command_spec "code_printing"} "declare dedicated printing for code symbols"
   670   Outer_Syntax.command @{command_spec "code_printing"} "declare dedicated printing for code symbols"
   745     (parse_symbol_pragmas (Code_Printer.parse_const_syntax) (Code_Printer.parse_tyco_syntax)
   671     (parse_symbol_pragmas (Code_Printer.parse_const_syntax) (Code_Printer.parse_tyco_syntax)
   746       Parse.string (Parse.minus >> K ()) (Parse.minus >> K ())
   672       Parse.string (Parse.minus >> K ()) (Parse.minus >> K ())
   747       (Parse.text -- Scan.optional (@{keyword "attach"} |-- Scan.repeat1 Parse.term) [])
   673       (Parse.text -- Scan.optional (@{keyword "attach"} |-- Scan.repeat1 Parse.term) [])
   748       >> (Toplevel.theory o fold set_printings_cmd));
   674       >> (Toplevel.theory o fold set_printings_cmd));
   749 
   675 
   750 val _ =
   676 val _ =
   751   Outer_Syntax.command @{command_spec "code_const"} "define code syntax for constant"
       
   752     (process_multi_syntax Parse.term Code_Printer.parse_const_syntax
       
   753       add_const_syntax_cmd);
       
   754 
       
   755 val _ =
       
   756   Outer_Syntax.command @{command_spec "code_type"} "define code syntax for type constructor"
       
   757     (process_multi_syntax Parse.type_const Code_Printer.parse_tyco_syntax
       
   758       add_tyco_syntax_cmd);
       
   759 
       
   760 val _ =
       
   761   Outer_Syntax.command @{command_spec "code_class"} "define code syntax for class"
       
   762     (process_multi_syntax Parse.class Parse.string
       
   763       add_class_syntax_cmd);
       
   764 
       
   765 val _ =
       
   766   Outer_Syntax.command @{command_spec "code_instance"} "define code syntax for instance"
       
   767     (process_multi_syntax parse_inst_ident (Parse.minus >> K ())
       
   768       add_instance_syntax_cmd);
       
   769 
       
   770 val _ =
       
   771   Outer_Syntax.command @{command_spec "code_include"}
       
   772     "declare piece of code to be included in generated code"
       
   773     (Parse.name -- Parse.name -- (Parse.text :|--
       
   774       (fn "-" => Scan.succeed NONE
       
   775         | s => Scan.optional (@{keyword "attach"} |-- Scan.repeat1 Parse.term) [] >> pair s >> SOME))
       
   776       >> (fn ((target, name), content_consts) =>
       
   777           (Toplevel.theory o add_include_cmd target) (name, content_consts)));
       
   778 
       
   779 val _ =
       
   780   Outer_Syntax.command @{command_spec "export_code"} "generate executable code for constants"
   677   Outer_Syntax.command @{command_spec "export_code"} "generate executable code for constants"
   781     (Parse.!!! code_exprP >> (fn f => Toplevel.keep (f o Toplevel.theory_of)));
   678     (Parse.!!! code_exprP >> (fn f => Toplevel.keep (f o Toplevel.theory_of)));
   782 
       
   783 end; (*local*)
       
   784 
   679 
   785 
   680 
   786 (** external entrance point -- for codegen tool **)
   681 (** external entrance point -- for codegen tool **)
   787 
   682 
   788 fun codegen_tool thyname cmd_expr =
   683 fun codegen_tool thyname cmd_expr =