53 val add_nbe_default_eqn: thm -> theory -> theory |
53 val add_nbe_default_eqn: thm -> theory -> theory |
54 val add_nbe_default_eqn_attribute: attribute |
54 val add_nbe_default_eqn_attribute: attribute |
55 val add_nbe_default_eqn_attrib: Attrib.src |
55 val add_nbe_default_eqn_attrib: Attrib.src |
56 val del_eqn: thm -> theory -> theory |
56 val del_eqn: thm -> theory -> theory |
57 val del_eqns: string -> theory -> theory |
57 val del_eqns: string -> theory -> theory |
|
58 val del_exception: string -> theory -> theory |
58 val add_case: thm -> theory -> theory |
59 val add_case: thm -> theory -> theory |
59 val add_undefined: string -> theory -> theory |
60 val add_undefined: string -> theory -> theory |
60 val get_type: theory -> string |
61 val get_type: theory -> string |
61 -> ((string * sort) list * (string * ((string * sort) list * typ list)) list) * bool |
62 -> ((string * sort) list * (string * ((string * sort) list * typ list)) list) * bool |
62 val get_type_of_constr_or_abstr: theory -> string -> (string * bool) option |
63 val get_type_of_constr_or_abstr: theory -> string -> (string * bool) option |
717 val add_consts = fold_aterms add_const |
719 val add_consts = fold_aterms add_const |
718 in add_consts rhs o fold add_consts args end; |
720 in add_consts rhs o fold add_consts args end; |
719 |
721 |
720 val dest_eqn = apfst (snd o strip_comb) o Logic.dest_equals o Logic.unvarify_global; |
722 val dest_eqn = apfst (snd o strip_comb) o Logic.dest_equals o Logic.unvarify_global; |
721 |
723 |
722 abstype cert = Equations of thm * bool list |
724 abstype cert = Nothing of thm |
|
725 | Equations of thm * bool list |
723 | Projection of term * string |
726 | Projection of term * string |
724 | Abstract of thm * string |
727 | Abstract of thm * string |
725 with |
728 with |
726 |
729 |
727 fun empty_cert thy c = |
730 fun dummy_thm thy c = |
728 let |
731 let |
729 val raw_ty = devarify (const_typ thy c); |
732 val raw_ty = devarify (const_typ thy c); |
730 val (vs, _) = typscheme thy (c, raw_ty); |
733 val (vs, _) = typscheme thy (c, raw_ty); |
731 val sortargs = case Axclass.class_of_param thy c |
734 val sortargs = case Axclass.class_of_param thy c |
732 of SOME class => [[class]] |
735 of SOME class => [[class]] |
735 (AList.lookup (op =) ((snd o fst o get_type thy) tyco) c) |
738 (AList.lookup (op =) ((snd o fst o get_type thy) tyco) c) |
736 | NONE => replicate (length vs) []); |
739 | NONE => replicate (length vs) []); |
737 val the_sort = the o AList.lookup (op =) (map fst vs ~~ sortargs); |
740 val the_sort = the o AList.lookup (op =) (map fst vs ~~ sortargs); |
738 val ty = map_type_tfree (fn (v, _) => TFree (v, the_sort v)) raw_ty |
741 val ty = map_type_tfree (fn (v, _) => TFree (v, the_sort v)) raw_ty |
739 val chead = build_head thy (c, ty); |
742 val chead = build_head thy (c, ty); |
740 in Equations (Thm.weaken chead Drule.dummy_thm, []) end; |
743 in Thm.weaken chead Drule.dummy_thm end; |
741 |
744 |
742 fun cert_of_eqns thy c [] = empty_cert thy c |
745 fun nothing_cert thy c = Nothing (dummy_thm thy c); |
|
746 |
|
747 fun cert_of_eqns thy c [] = Equations (dummy_thm thy c, []) |
743 | cert_of_eqns thy c raw_eqns = |
748 | cert_of_eqns thy c raw_eqns = |
744 let |
749 let |
745 val eqns = burrow_fst (canonize_thms thy) raw_eqns; |
750 val eqns = burrow_fst (canonize_thms thy) raw_eqns; |
746 val _ = map (assert_eqn thy) eqns; |
751 val _ = map (assert_eqn thy) eqns; |
747 val (thms, propers) = split_list eqns; |
752 val (thms, propers) = split_list eqns; |
778 val _ = if c = const_abs_eqn thy abs_thm then () |
783 val _ = if c = const_abs_eqn thy abs_thm then () |
779 else error ("Wrong head of abstract code equation,\nexpected constant " |
784 else error ("Wrong head of abstract code equation,\nexpected constant " |
780 ^ string_of_const thy c ^ "\n" ^ Display.string_of_thm_global thy abs_thm); |
785 ^ string_of_const thy c ^ "\n" ^ Display.string_of_thm_global thy abs_thm); |
781 in Abstract (Thm.legacy_freezeT abs_thm, tyco) end; |
786 in Abstract (Thm.legacy_freezeT abs_thm, tyco) end; |
782 |
787 |
783 fun constrain_cert thy sorts (Equations (cert_thm, propers)) = |
788 fun constrain_cert_thm thy sorts cert_thm = |
784 let |
789 let |
785 val ((vs, _), head) = get_head thy cert_thm; |
790 val ((vs, _), head) = get_head thy cert_thm; |
786 val (subst, cert_thm') = cert_thm |
791 val (subst, cert_thm') = cert_thm |
787 |> Thm.implies_intr head |
792 |> Thm.implies_intr head |
788 |> constrain_thm thy vs sorts; |
793 |> constrain_thm thy vs sorts; |
789 val head' = Thm.term_of head |
794 val head' = Thm.term_of head |
790 |> subst |
795 |> subst |
791 |> Thm.cterm_of thy; |
796 |> Thm.cterm_of thy; |
792 val cert_thm'' = cert_thm' |
797 val cert_thm'' = cert_thm' |
793 |> Thm.elim_implies (Thm.assume head'); |
798 |> Thm.elim_implies (Thm.assume head'); |
794 in Equations (cert_thm'', propers) end |
799 in cert_thm'' end; |
|
800 |
|
801 fun constrain_cert thy sorts (Nothing cert_thm) = |
|
802 Nothing (constrain_cert_thm thy sorts cert_thm) |
|
803 | constrain_cert thy sorts (Equations (cert_thm, propers)) = |
|
804 Equations (constrain_cert_thm thy sorts cert_thm, propers) |
795 | constrain_cert thy _ (cert as Projection _) = |
805 | constrain_cert thy _ (cert as Projection _) = |
796 cert |
806 cert |
797 | constrain_cert thy sorts (Abstract (abs_thm, tyco)) = |
807 | constrain_cert thy sorts (Abstract (abs_thm, tyco)) = |
798 Abstract (snd (constrain_thm thy (fst (typscheme_abs thy abs_thm)) sorts abs_thm), tyco); |
808 Abstract (snd (constrain_thm thy (fst (typscheme_abs thy abs_thm)) sorts abs_thm), tyco); |
799 |
809 |
800 fun conclude_cert (Equations (cert_thm, propers)) = |
810 fun conclude_cert (Nothing cert_thm) = |
801 (Equations (Thm.close_derivation cert_thm, propers)) |
811 Nothing (Thm.close_derivation cert_thm) |
|
812 | conclude_cert (Equations (cert_thm, propers)) = |
|
813 Equations (Thm.close_derivation cert_thm, propers) |
802 | conclude_cert (cert as Projection _) = |
814 | conclude_cert (cert as Projection _) = |
803 cert |
815 cert |
804 | conclude_cert (Abstract (abs_thm, tyco)) = |
816 | conclude_cert (Abstract (abs_thm, tyco)) = |
805 (Abstract (Thm.close_derivation abs_thm, tyco)); |
817 Abstract (Thm.close_derivation abs_thm, tyco); |
806 |
818 |
807 fun typscheme_of_cert thy (Equations (cert_thm, _)) = |
819 fun typscheme_of_cert thy (Nothing cert_thm) = |
|
820 fst (get_head thy cert_thm) |
|
821 | typscheme_of_cert thy (Equations (cert_thm, _)) = |
808 fst (get_head thy cert_thm) |
822 fst (get_head thy cert_thm) |
809 | typscheme_of_cert thy (Projection (proj, _)) = |
823 | typscheme_of_cert thy (Projection (proj, _)) = |
810 typscheme_projection thy proj |
824 typscheme_projection thy proj |
811 | typscheme_of_cert thy (Abstract (abs_thm, _)) = |
825 | typscheme_of_cert thy (Abstract (abs_thm, _)) = |
812 typscheme_abs thy abs_thm; |
826 typscheme_abs thy abs_thm; |
813 |
827 |
814 fun typargs_deps_of_cert thy (Equations (cert_thm, propers)) = |
828 fun typargs_deps_of_cert thy (Nothing cert_thm) = |
|
829 let |
|
830 val vs = (fst o fst) (get_head thy cert_thm); |
|
831 in (vs, []) end |
|
832 | typargs_deps_of_cert thy (Equations (cert_thm, propers)) = |
815 let |
833 let |
816 val vs = (fst o fst) (get_head thy cert_thm); |
834 val vs = (fst o fst) (get_head thy cert_thm); |
817 val equations = if null propers then [] else |
835 val equations = if null propers then [] else |
818 Thm.prop_of cert_thm |
836 Thm.prop_of cert_thm |
819 |> Logic.dest_conjunction_balanced (length propers); |
837 |> Logic.dest_conjunction_balanced (length propers); |
824 let |
842 let |
825 val vs = fst (typscheme_abs thy abs_thm); |
843 val vs = fst (typscheme_abs thy abs_thm); |
826 val (_, concrete_thm) = concretify_abs thy tyco abs_thm; |
844 val (_, concrete_thm) = concretify_abs thy tyco abs_thm; |
827 in (vs, add_rhss_of_eqn thy (Logic.unvarify_types_global (Thm.prop_of concrete_thm)) []) end; |
845 in (vs, add_rhss_of_eqn thy (Logic.unvarify_types_global (Thm.prop_of concrete_thm)) []) end; |
828 |
846 |
829 fun equations_of_cert thy (cert as Equations (cert_thm, propers)) = |
847 fun equations_of_cert thy (cert as Nothing _) = |
|
848 (typscheme_of_cert thy cert, NONE) |
|
849 | equations_of_cert thy (cert as Equations (cert_thm, propers)) = |
830 let |
850 let |
831 val tyscm = typscheme_of_cert thy cert; |
851 val tyscm = typscheme_of_cert thy cert; |
832 val thms = if null propers then [] else |
852 val thms = if null propers then [] else |
833 cert_thm |
853 cert_thm |
834 |> Local_Defs.expand [snd (get_head thy cert_thm)] |
854 |> Local_Defs.expand [snd (get_head thy cert_thm)] |
835 |> Thm.varifyT_global |
855 |> Thm.varifyT_global |
836 |> Conjunction.elim_balanced (length propers); |
856 |> Conjunction.elim_balanced (length propers); |
837 fun abstractions (args, rhs) = (map (rpair NONE) args, (rhs, NONE)); |
857 fun abstractions (args, rhs) = (map (rpair NONE) args, (rhs, NONE)); |
838 in (tyscm, map (abstractions o dest_eqn o Thm.prop_of) thms ~~ (map SOME thms ~~ propers)) end |
858 in (tyscm, SOME (map (abstractions o dest_eqn o Thm.prop_of) thms ~~ (map SOME thms ~~ propers))) end |
839 | equations_of_cert thy (Projection (t, tyco)) = |
859 | equations_of_cert thy (Projection (t, tyco)) = |
840 let |
860 let |
841 val (_, ((abs, _), _)) = get_abstype_spec thy tyco; |
861 val (_, ((abs, _), _)) = get_abstype_spec thy tyco; |
842 val tyscm = typscheme_projection thy t; |
862 val tyscm = typscheme_projection thy t; |
843 val t' = Logic.varify_types_global t; |
863 val t' = Logic.varify_types_global t; |
844 fun abstractions (args, rhs) = (map (rpair (SOME abs)) args, (rhs, NONE)); |
864 fun abstractions (args, rhs) = (map (rpair (SOME abs)) args, (rhs, NONE)); |
845 in (tyscm, [((abstractions o dest_eqn) t', (NONE, true))]) end |
865 in (tyscm, SOME [((abstractions o dest_eqn) t', (NONE, true))]) end |
846 | equations_of_cert thy (Abstract (abs_thm, tyco)) = |
866 | equations_of_cert thy (Abstract (abs_thm, tyco)) = |
847 let |
867 let |
848 val tyscm = typscheme_abs thy abs_thm; |
868 val tyscm = typscheme_abs thy abs_thm; |
849 val (abs, concrete_thm) = concretify_abs thy tyco abs_thm; |
869 val (abs, concrete_thm) = concretify_abs thy tyco abs_thm; |
850 fun abstractions (args, rhs) = (map (rpair NONE) args, (rhs, (SOME abs))); |
870 fun abstractions (args, rhs) = (map (rpair NONE) args, (rhs, (SOME abs))); |
851 in |
871 in |
852 (tyscm, [((abstractions o dest_eqn o Thm.prop_of) concrete_thm, |
872 (tyscm, SOME [((abstractions o dest_eqn o Thm.prop_of) concrete_thm, |
853 (SOME (Thm.varifyT_global abs_thm), true))]) |
873 (SOME (Thm.varifyT_global abs_thm), true))]) |
854 end; |
874 end; |
855 |
875 |
856 fun pretty_cert thy (cert as Equations _) = |
876 fun pretty_cert thy (cert as Nothing _) = |
|
877 [Pretty.str "(not implemented)"] |
|
878 | pretty_cert thy (cert as Equations _) = |
857 (map_filter (Option.map (Display.pretty_thm_global thy o Axclass.overload thy) o fst o snd) |
879 (map_filter (Option.map (Display.pretty_thm_global thy o Axclass.overload thy) o fst o snd) |
858 o snd o equations_of_cert thy) cert |
880 o these o snd o equations_of_cert thy) cert |
859 | pretty_cert thy (Projection (t, _)) = |
881 | pretty_cert thy (Projection (t, _)) = |
860 [Syntax.pretty_term_global thy (Logic.varify_types_global t)] |
882 [Syntax.pretty_term_global thy (Logic.varify_types_global t)] |
861 | pretty_cert thy (Abstract (abs_thm, _)) = |
883 | pretty_cert thy (Abstract (abs_thm, _)) = |
862 [(Display.pretty_thm_global thy o Axclass.overload thy o Thm.varifyT_global) abs_thm]; |
884 [(Display.pretty_thm_global thy o Axclass.overload thy o Thm.varifyT_global) abs_thm]; |
863 |
885 |
891 #> (map o apfst) (Axclass.unoverload thy) |
913 #> (map o apfst) (Axclass.unoverload thy) |
892 #> cert_of_eqns thy c; |
914 #> cert_of_eqns thy c; |
893 |
915 |
894 fun get_cert thy { functrans, ss } c = |
916 fun get_cert thy { functrans, ss } c = |
895 case retrieve_raw thy c |
917 case retrieve_raw thy c |
896 of Default (_, eqns_lazy) => Lazy.force eqns_lazy |
918 of Default (eqns, eqns_lazy) => Lazy.force eqns_lazy |
897 |> cert_of_eqns_preprocess thy functrans ss c |
919 |> cert_of_eqns_preprocess thy functrans ss c |
898 | Eqns eqns => eqns |
920 | Eqns eqns => eqns |
899 |> cert_of_eqns_preprocess thy functrans ss c |
921 |> cert_of_eqns_preprocess thy functrans ss c |
900 | Proj (_, tyco) => |
922 | None => nothing_cert thy c |
901 cert_of_proj thy c tyco |
923 | Proj (_, tyco) => cert_of_proj thy c tyco |
902 | Abstr (abs_thm, tyco) => abs_thm |
924 | Abstr (abs_thm, tyco) => abs_thm |
903 |> Thm.transfer thy |
925 |> Thm.transfer thy |
904 |> rewrite_eqn thy Conv.arg_conv ss |
926 |> rewrite_eqn thy Conv.arg_conv ss |
905 |> Axclass.unoverload thy |
927 |> Axclass.unoverload thy |
906 |> cert_of_abs thy tyco c; |
928 |> cert_of_abs thy tyco c; |
964 (Pretty.block o Pretty.fbreaks) |
986 (Pretty.block o Pretty.fbreaks) |
965 (Pretty.str (string_of_const thy const) :: map (Display.pretty_thm_item ctxt) thms); |
987 (Pretty.str (string_of_const thy const) :: map (Display.pretty_thm_item ctxt) thms); |
966 fun pretty_function (const, Default (_, eqns_lazy)) = |
988 fun pretty_function (const, Default (_, eqns_lazy)) = |
967 pretty_equations const (map fst (Lazy.force eqns_lazy)) |
989 pretty_equations const (map fst (Lazy.force eqns_lazy)) |
968 | pretty_function (const, Eqns eqns) = pretty_equations const (map fst eqns) |
990 | pretty_function (const, Eqns eqns) = pretty_equations const (map fst eqns) |
|
991 | pretty_function (const, None) = pretty_equations const [] |
969 | pretty_function (const, Proj (proj, _)) = Pretty.block |
992 | pretty_function (const, Proj (proj, _)) = Pretty.block |
970 [Pretty.str (string_of_const thy const), Pretty.fbrk, Syntax.pretty_term ctxt proj] |
993 [Pretty.str (string_of_const thy const), Pretty.fbrk, Syntax.pretty_term ctxt proj] |
971 | pretty_function (const, Abstr (thm, _)) = pretty_equations const [thm]; |
994 | pretty_function (const, Abstr (thm, _)) = pretty_equations const [thm]; |
972 fun pretty_typ (tyco, vs) = Pretty.str |
995 fun pretty_typ (tyco, vs) = Pretty.str |
973 (string_of_typ thy (Type (tyco, map TFree vs))); |
996 (string_of_typ thy (Type (tyco, map TFree vs))); |
1052 in (thm, proper) :: filter_out drop eqns end; |
1075 in (thm, proper) :: filter_out drop eqns end; |
1053 fun natural_order eqns = |
1076 fun natural_order eqns = |
1054 (eqns, Lazy.lazy (fn () => fold (update_subsume false) eqns [])) |
1077 (eqns, Lazy.lazy (fn () => fold (update_subsume false) eqns [])) |
1055 fun add_eqn' true (Default (eqns, _)) = Default (natural_order ((thm, proper) :: eqns)) |
1078 fun add_eqn' true (Default (eqns, _)) = Default (natural_order ((thm, proper) :: eqns)) |
1056 (*this restores the natural order and drops syntactic redundancies*) |
1079 (*this restores the natural order and drops syntactic redundancies*) |
|
1080 | add_eqn' true None = Default (natural_order [(thm, proper)]) |
1057 | add_eqn' true fun_spec = fun_spec |
1081 | add_eqn' true fun_spec = fun_spec |
1058 | add_eqn' false (Eqns eqns) = Eqns (update_subsume true (thm, proper) eqns) |
1082 | add_eqn' false (Eqns eqns) = Eqns (update_subsume true (thm, proper) eqns) |
1059 | add_eqn' false _ = Eqns [(thm, proper)]; |
1083 | add_eqn' false _ = Eqns [(thm, proper)]; |
1060 in change_fun_spec c (add_eqn' default) thy end; |
1084 in change_fun_spec c (add_eqn' default) thy end; |
1061 |
1085 |
1098 fun del_eqn thm thy = case mk_eqn_liberal thy thm |
1122 fun del_eqn thm thy = case mk_eqn_liberal thy thm |
1099 of SOME (thm, _) => |
1123 of SOME (thm, _) => |
1100 let |
1124 let |
1101 fun del_eqn' (Default _) = initial_fun_spec |
1125 fun del_eqn' (Default _) = initial_fun_spec |
1102 | del_eqn' (Eqns eqns) = |
1126 | del_eqn' (Eqns eqns) = |
1103 Eqns (filter_out (fn (thm', _) => Thm.eq_thm_prop (thm, thm')) eqns) |
1127 let |
|
1128 val eqns' = filter_out (fn (thm', _) => Thm.eq_thm_prop (thm, thm')) eqns |
|
1129 in if null eqns' then None else Eqns eqns' end |
1104 | del_eqn' spec = spec |
1130 | del_eqn' spec = spec |
1105 in change_fun_spec (const_eqn thy thm) del_eqn' thy end |
1131 in change_fun_spec (const_eqn thy thm) del_eqn' thy end |
1106 | NONE => thy; |
1132 | NONE => thy; |
1107 |
1133 |
1108 fun del_eqns c = change_fun_spec c (K initial_fun_spec); |
1134 fun del_eqns c = change_fun_spec c (K None); |
|
1135 |
|
1136 fun del_exception c = change_fun_spec c (K (Eqns [])); |
1109 |
1137 |
1110 |
1138 |
1111 (* cases *) |
1139 (* cases *) |
1112 |
1140 |
1113 fun case_cong thy case_const (num_args, (pos, _)) = |
1141 fun case_cong thy case_const (num_args, (pos, _)) = |
1227 (* setup *) |
1255 (* setup *) |
1228 |
1256 |
1229 val _ = Theory.setup |
1257 val _ = Theory.setup |
1230 (let |
1258 (let |
1231 fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I); |
1259 fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I); |
|
1260 fun mk_const_attribute f cs = |
|
1261 mk_attribute (K (fold (fn c => fn thy => f (read_const thy c) thy) cs)); |
1232 val code_attribute_parser = |
1262 val code_attribute_parser = |
1233 Args.del |-- Scan.succeed (mk_attribute del_eqn) |
1263 Args.$$$ "equation" |-- Scan.succeed (mk_attribute add_eqn) |
1234 || Args.$$$ "equation" |-- Scan.succeed (mk_attribute add_eqn) |
|
1235 || Args.$$$ "nbe" |-- Scan.succeed (mk_attribute add_nbe_eqn) |
1264 || Args.$$$ "nbe" |-- Scan.succeed (mk_attribute add_nbe_eqn) |
1236 || Args.$$$ "abstype" |-- Scan.succeed (mk_attribute add_abstype) |
1265 || Args.$$$ "abstype" |-- Scan.succeed (mk_attribute add_abstype) |
1237 || Args.$$$ "abstract" |-- Scan.succeed (mk_attribute add_abs_eqn) |
1266 || Args.$$$ "abstract" |-- Scan.succeed (mk_attribute add_abs_eqn) |
|
1267 || Args.del |-- Scan.succeed (mk_attribute del_eqn) |
|
1268 || Args.$$$ "drop" -- Args.colon |-- (Scan.repeat1 Parse.term >> mk_const_attribute del_eqns) |
|
1269 || Args.$$$ "abort" -- Args.colon |-- (Scan.repeat1 Parse.term >> mk_const_attribute del_exception) |
1238 || Scan.succeed (mk_attribute add_eqn_maybe_abs); |
1270 || Scan.succeed (mk_attribute add_eqn_maybe_abs); |
1239 in |
1271 in |
1240 Datatype_Interpretation.init |
1272 Datatype_Interpretation.init |
1241 #> Attrib.setup (Binding.name "code") (Scan.lift code_attribute_parser) |
1273 #> Attrib.setup (Binding.name "code") (Scan.lift code_attribute_parser) |
1242 "declare theorems for code generation" |
1274 "declare theorems for code generation" |