97 fun cert_class ctxt class = |
97 fun cert_class ctxt class = |
98 let |
98 let |
99 val _ = Axclass.get_info (Proof_Context.theory_of ctxt) class; |
99 val _ = Axclass.get_info (Proof_Context.theory_of ctxt) class; |
100 in class end; |
100 in class end; |
101 |
101 |
102 val parse_classrel_ident = Parse.class --| @{keyword "<"} -- Parse.class; |
102 val parse_classrel_ident = Parse.class --| \<^keyword>\<open><\<close> -- Parse.class; |
103 |
103 |
104 fun cert_inst ctxt (class, tyco) = |
104 fun cert_inst ctxt (class, tyco) = |
105 (cert_class ctxt class, cert_tyco ctxt tyco); |
105 (cert_class ctxt class, cert_tyco ctxt tyco); |
106 |
106 |
107 fun read_inst ctxt (raw_tyco, raw_class) = |
107 fun read_inst ctxt (raw_tyco, raw_class) = |
108 (read_tyco ctxt raw_tyco, Proof_Context.read_class ctxt raw_class); |
108 (read_tyco ctxt raw_tyco, Proof_Context.read_class ctxt raw_class); |
109 |
109 |
110 val parse_inst_ident = Parse.name --| @{keyword "::"} -- Parse.class; |
110 val parse_inst_ident = Parse.name --| \<^keyword>\<open>::\<close> -- Parse.class; |
111 |
111 |
112 fun cert_syms ctxt = |
112 fun cert_syms ctxt = |
113 Code_Symbol.map_attr (cert_const ctxt) (cert_tyco ctxt) |
113 Code_Symbol.map_attr (cert_const ctxt) (cert_tyco ctxt) |
114 (cert_class ctxt) (apply2 (cert_class ctxt)) (cert_inst ctxt) I; |
114 (cert_class ctxt) (apply2 (cert_class ctxt)) (cert_inst ctxt) I; |
115 |
115 |
517 in (Sign.intern_class thy raw_tyco, Sign.intern_type thy raw_class) end) Class_Instance; |
517 in (Sign.intern_class thy raw_tyco, Sign.intern_type thy raw_class) end) Class_Instance; |
518 |
518 |
519 in |
519 in |
520 |
520 |
521 val _ = Theory.setup |
521 val _ = Theory.setup |
522 (Thy_Output.antiquotation_raw @{binding code_stmts} |
522 (Thy_Output.antiquotation_raw \<^binding>\<open>code_stmts\<close> |
523 (parse_const_terms -- |
523 (parse_const_terms -- |
524 Scan.repeat (parse_consts || parse_types || parse_classes || parse_instances) |
524 Scan.repeat (parse_consts || parse_types || parse_classes || parse_instances) |
525 -- Scan.lift (Args.parens (Args.name -- Scan.option Parse.int))) |
525 -- Scan.lift (Args.parens (Args.name -- Scan.option Parse.int))) |
526 (fn ctxt => fn ((mk_cs, mk_stmtss), (target_name, some_width)) => |
526 (fn ctxt => fn ((mk_cs, mk_stmtss), (target_name, some_width)) => |
527 Latex.string |
527 Latex.string |
623 |
623 |
624 |
624 |
625 (** Isar setup **) |
625 (** Isar setup **) |
626 |
626 |
627 val (constantK, type_constructorK, type_classK, class_relationK, class_instanceK, code_moduleK) = |
627 val (constantK, type_constructorK, type_classK, class_relationK, class_instanceK, code_moduleK) = |
628 (@{keyword "constant"}, @{keyword "type_constructor"}, @{keyword "type_class"}, |
628 (\<^keyword>\<open>constant\<close>, \<^keyword>\<open>type_constructor\<close>, \<^keyword>\<open>type_class\<close>, |
629 @{keyword "class_relation"}, @{keyword "class_instance"}, @{keyword "code_module"}); |
629 \<^keyword>\<open>class_relation\<close>, \<^keyword>\<open>class_instance\<close>, \<^keyword>\<open>code_module\<close>); |
630 |
630 |
631 local |
631 local |
632 |
632 |
633 val parse_constants = constantK |-- Scan.repeat1 Parse.term >> map Constant; |
633 val parse_constants = constantK |-- Scan.repeat1 Parse.term >> map Constant; |
634 |
634 |
646 || parse_class_relations || parse_instances) >> flat; |
646 || parse_class_relations || parse_instances) >> flat; |
647 |
647 |
648 end; |
648 end; |
649 |
649 |
650 fun parse_single_symbol_pragma parse_keyword parse_isa parse_target = |
650 fun parse_single_symbol_pragma parse_keyword parse_isa parse_target = |
651 parse_keyword |-- Parse.!!! (parse_isa --| (@{keyword "\<rightharpoonup>"} || @{keyword "=>"}) |
651 parse_keyword |-- Parse.!!! (parse_isa --| (\<^keyword>\<open>\<rightharpoonup>\<close> || \<^keyword>\<open>=>\<close>) |
652 -- Parse.and_list1 (@{keyword "("} |-- (Parse.name --| @{keyword ")"} -- Scan.option parse_target))); |
652 -- Parse.and_list1 (\<^keyword>\<open>(\<close> |-- (Parse.name --| \<^keyword>\<open>)\<close> -- Scan.option parse_target))); |
653 |
653 |
654 fun parse_symbol_pragma parse_const parse_tyco parse_class parse_classrel parse_inst parse_module = |
654 fun parse_symbol_pragma parse_const parse_tyco parse_class parse_classrel parse_inst parse_module = |
655 parse_single_symbol_pragma constantK Parse.term parse_const |
655 parse_single_symbol_pragma constantK Parse.term parse_const |
656 >> Constant |
656 >> Constant |
657 || parse_single_symbol_pragma type_constructorK Parse.type_const parse_tyco |
657 || parse_single_symbol_pragma type_constructorK Parse.type_const parse_tyco |
667 |
667 |
668 fun parse_symbol_pragmas parse_const parse_tyco parse_class parse_classrel parse_inst parse_module = |
668 fun parse_symbol_pragmas parse_const parse_tyco parse_class parse_classrel parse_inst parse_module = |
669 Parse.enum1 "|" (Parse.group (fn () => "code symbol pragma") |
669 Parse.enum1 "|" (Parse.group (fn () => "code symbol pragma") |
670 (parse_symbol_pragma parse_const parse_tyco parse_class parse_classrel parse_inst parse_module)); |
670 (parse_symbol_pragma parse_const parse_tyco parse_class parse_classrel parse_inst parse_module)); |
671 |
671 |
672 val code_expr_argsP = Scan.optional (@{keyword "("} |-- Parse.args --| @{keyword ")"}) []; |
672 val code_expr_argsP = Scan.optional (\<^keyword>\<open>(\<close> |-- Parse.args --| \<^keyword>\<open>)\<close>) []; |
673 |
673 |
674 fun code_expr_inP all_public raw_cs = |
674 fun code_expr_inP all_public raw_cs = |
675 Scan.repeat (@{keyword "in"} |-- Parse.!!! (Parse.name |
675 Scan.repeat (\<^keyword>\<open>in\<close> |-- Parse.!!! (Parse.name |
676 -- Scan.optional (@{keyword "module_name"} |-- Parse.name) "" |
676 -- Scan.optional (\<^keyword>\<open>module_name\<close> |-- Parse.name) "" |
677 -- Scan.optional (@{keyword "file"} |-- Parse.position Parse.path) ("", Position.none) |
677 -- Scan.optional (\<^keyword>\<open>file\<close> |-- Parse.position Parse.path) ("", Position.none) |
678 -- code_expr_argsP)) |
678 -- code_expr_argsP)) |
679 >> (fn seri_args => export_code_cmd all_public raw_cs seri_args); |
679 >> (fn seri_args => export_code_cmd all_public raw_cs seri_args); |
680 |
680 |
681 fun code_expr_checkingP all_public raw_cs = |
681 fun code_expr_checkingP all_public raw_cs = |
682 (@{keyword "checking"} |-- Parse.!!! |
682 (\<^keyword>\<open>checking\<close> |-- Parse.!!! |
683 (Scan.repeat (Parse.name -- ((@{keyword "?"} |-- Scan.succeed false) || Scan.succeed true) |
683 (Scan.repeat (Parse.name -- ((\<^keyword>\<open>?\<close> |-- Scan.succeed false) || Scan.succeed true) |
684 -- code_expr_argsP))) |
684 -- code_expr_argsP))) |
685 >> (fn seri_args => check_code_cmd all_public raw_cs seri_args); |
685 >> (fn seri_args => check_code_cmd all_public raw_cs seri_args); |
686 |
686 |
687 val code_exprP = (Scan.optional (@{keyword "open"} |-- Scan.succeed true) false |
687 val code_exprP = (Scan.optional (\<^keyword>\<open>open\<close> |-- Scan.succeed true) false |
688 -- Scan.repeat1 Parse.term) |
688 -- Scan.repeat1 Parse.term) |
689 :|-- (fn (all_public, raw_cs) => (code_expr_checkingP all_public raw_cs || code_expr_inP all_public raw_cs)); |
689 :|-- (fn (all_public, raw_cs) => (code_expr_checkingP all_public raw_cs || code_expr_inP all_public raw_cs)); |
690 |
690 |
691 val _ = |
691 val _ = |
692 Outer_Syntax.command @{command_keyword code_reserved} |
692 Outer_Syntax.command \<^command_keyword>\<open>code_reserved\<close> |
693 "declare words as reserved for target language" |
693 "declare words as reserved for target language" |
694 (Parse.name -- Scan.repeat1 Parse.name |
694 (Parse.name -- Scan.repeat1 Parse.name |
695 >> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds)); |
695 >> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds)); |
696 |
696 |
697 val _ = |
697 val _ = |
698 Outer_Syntax.command @{command_keyword code_identifier} "declare mandatory names for code symbols" |
698 Outer_Syntax.command \<^command_keyword>\<open>code_identifier\<close> "declare mandatory names for code symbols" |
699 (parse_symbol_pragmas Parse.name Parse.name Parse.name Parse.name Parse.name Parse.name |
699 (parse_symbol_pragmas Parse.name Parse.name Parse.name Parse.name Parse.name Parse.name |
700 >> (Toplevel.theory o fold set_identifiers_cmd)); |
700 >> (Toplevel.theory o fold set_identifiers_cmd)); |
701 |
701 |
702 val _ = |
702 val _ = |
703 Outer_Syntax.command @{command_keyword code_printing} "declare dedicated printing for code symbols" |
703 Outer_Syntax.command \<^command_keyword>\<open>code_printing\<close> "declare dedicated printing for code symbols" |
704 (parse_symbol_pragmas (Code_Printer.parse_const_syntax) (Code_Printer.parse_tyco_syntax) |
704 (parse_symbol_pragmas (Code_Printer.parse_const_syntax) (Code_Printer.parse_tyco_syntax) |
705 Parse.string (Parse.minus >> K ()) (Parse.minus >> K ()) |
705 Parse.string (Parse.minus >> K ()) (Parse.minus >> K ()) |
706 (Parse.text -- Scan.optional (@{keyword for} |-- parse_symbols) []) |
706 (Parse.text -- Scan.optional (\<^keyword>\<open>for\<close> |-- parse_symbols) []) |
707 >> (Toplevel.theory o fold set_printings_cmd)); |
707 >> (Toplevel.theory o fold set_printings_cmd)); |
708 |
708 |
709 val _ = |
709 val _ = |
710 Outer_Syntax.command @{command_keyword export_code} "generate executable code for constants" |
710 Outer_Syntax.command \<^command_keyword>\<open>export_code\<close> "generate executable code for constants" |
711 (Parse.!!! code_exprP >> (fn f => Toplevel.keep (f o Toplevel.context_of))); |
711 (Parse.!!! code_exprP >> (fn f => Toplevel.keep (f o Toplevel.context_of))); |
712 |
712 |
713 end; (*struct*) |
713 end; (*struct*) |