208 |
208 |
209 fun overloaded_const (c, ty, mx) thy = |
209 fun overloaded_const (c, ty, mx) thy = |
210 let |
210 let |
211 val _ = if mx <> NoSyn then |
211 val _ = if mx <> NoSyn then |
212 error ("Illegal mixfix syntax for constant to be instantiated " ^ quote c) |
212 error ("Illegal mixfix syntax for constant to be instantiated " ^ quote c) |
213 else () |
213 else (); |
214 val SOME class = AxClass.class_of_param thy c; |
214 val SOME class = AxClass.class_of_param thy c; |
215 val SOME tyco = inst_tyco thy (c, ty); |
215 val SOME tyco = inst_tyco thy (c, ty); |
216 val name_inst = AxClass.instance_name (tyco, class) ^ "_inst"; |
216 val name_inst = AxClass.instance_name (tyco, class) ^ "_inst"; |
217 val c' = NameSpace.base c ^ "_" ^ NameSpace.base tyco; |
217 val c' = NameSpace.base c ^ "_" ^ NameSpace.base tyco; |
218 val ty' = Type.strip_sorts ty; |
218 val ty' = Type.strip_sorts ty; |
839 fun init _ = Instantiation { arities = [], params = [] }; |
839 fun init _ = Instantiation { arities = [], params = [] }; |
840 ); |
840 ); |
841 |
841 |
842 fun mk_instantiation (arities, params) = |
842 fun mk_instantiation (arities, params) = |
843 Instantiation { arities = arities, params = params }; |
843 Instantiation { arities = arities, params = params }; |
844 fun get_instantiation ctxt = case Instantiation.get ctxt |
844 fun get_instantiation lthy = case Instantiation.get (LocalTheory.target_of lthy) |
845 of Instantiation data => data; |
845 of Instantiation data => data; |
846 fun map_instantiation f (Instantiation { arities, params }) = |
846 fun map_instantiation f = (LocalTheory.target o Instantiation.map) |
847 mk_instantiation (f (arities, params)); |
847 (fn Instantiation { arities, params } => mk_instantiation (f (arities, params))); |
848 |
848 |
849 fun the_instantiation ctxt = case get_instantiation ctxt |
849 fun the_instantiation lthy = case get_instantiation lthy |
850 of { arities = [], ... } => error "No instantiation target" |
850 of { arities = [], ... } => error "No instantiation target" |
851 | data => data; |
851 | data => data; |
852 |
852 |
853 val instantiation_params = #params o get_instantiation; |
853 val instantiation_params = #params o get_instantiation; |
854 |
854 |
855 fun instantiation_param ctxt v = instantiation_params ctxt |
855 fun instantiation_param lthy v = instantiation_params lthy |
856 |> find_first (fn (_, (v', _)) => v = v') |
856 |> find_first (fn (_, (v', _)) => v = v') |
857 |> Option.map (fst o fst); |
857 |> Option.map (fst o fst); |
858 |
858 |
859 fun confirm_declaration c = (Instantiation.map o map_instantiation o apsnd) |
859 fun confirm_declaration c = (map_instantiation o apsnd) |
860 (filter_out (fn (_, (c', _)) => c' = c)); |
860 (filter_out (fn (_, (c', _)) => c' = c)); |
861 |
861 |
862 |
862 |
863 (* syntax *) |
863 (* syntax *) |
864 |
864 |
865 fun inst_term_check ts ctxt = |
865 fun inst_term_check ts lthy = |
866 let |
866 let |
867 val params = instantiation_params ctxt; |
867 val params = instantiation_params lthy; |
868 val tsig = ProofContext.tsig_of ctxt; |
868 val tsig = ProofContext.tsig_of lthy; |
869 val thy = ProofContext.theory_of ctxt; |
869 val thy = ProofContext.theory_of lthy; |
870 |
870 |
871 fun check_improve (Const (c, ty)) = (case inst_tyco thy (c, ty) |
871 fun check_improve (Const (c, ty)) = (case inst_tyco thy (c, ty) |
872 of SOME tyco => (case AList.lookup (op =) params (c, tyco) |
872 of SOME tyco => (case AList.lookup (op =) params (c, tyco) |
873 of SOME (_, ty') => perhaps (try (Type.typ_match tsig (ty, ty'))) |
873 of SOME (_, ty') => perhaps (try (Type.typ_match tsig (ty, ty'))) |
874 | NONE => I) |
874 | NONE => I) |
880 of SOME tyco => (case AList.lookup (op =) params (c, tyco) |
880 of SOME tyco => (case AList.lookup (op =) params (c, tyco) |
881 of SOME v_ty => Free v_ty |
881 of SOME v_ty => Free v_ty |
882 | NONE => t) |
882 | NONE => t) |
883 | NONE => t) |
883 | NONE => t) |
884 | t => t) ts'; |
884 | t => t) ts'; |
885 in if eq_list (op aconv) (ts, ts'') then NONE else SOME (ts'', ctxt) end; |
885 in if eq_list (op aconv) (ts, ts'') then NONE else SOME (ts'', lthy) end; |
886 |
886 |
887 fun inst_term_uncheck ts ctxt = |
887 fun inst_term_uncheck ts lthy = |
888 let |
888 let |
889 val params = instantiation_params ctxt; |
889 val params = instantiation_params lthy; |
890 val ts' = (map o map_aterms) (fn t as Free (v, ty) => |
890 val ts' = (map o map_aterms) (fn t as Free (v, ty) => |
891 (case get_first (fn ((c, _), (v', _)) => if v = v' then SOME c else NONE) params |
891 (case get_first (fn ((c, _), (v', _)) => if v = v' then SOME c else NONE) params |
892 of SOME c => Const (c, ty) |
892 of SOME c => Const (c, ty) |
893 | NONE => t) |
893 | NONE => t) |
894 | t => t) ts; |
894 | t => t) ts; |
895 in if eq_list (op aconv) (ts, ts') then NONE else SOME (ts', ctxt) end; |
895 in if eq_list (op aconv) (ts, ts') then NONE else SOME (ts', lthy) end; |
896 |
896 |
897 |
897 |
898 (* target *) |
898 (* target *) |
899 |
899 |
900 val sanatize_name = (*FIXME*) |
900 val sanatize_name = (*FIXME*) |
944 #> Syntax.add_term_uncheck 0 "instance" inst_term_uncheck) |
944 #> Syntax.add_term_uncheck 0 "instance" inst_term_uncheck) |
945 end; |
945 end; |
946 |
946 |
947 fun gen_instantiation_instance do_proof after_qed lthy = |
947 fun gen_instantiation_instance do_proof after_qed lthy = |
948 let |
948 let |
949 val ctxt = LocalTheory.target_of lthy; |
949 val arities = (#arities o the_instantiation) lthy; |
950 val arities = (#arities o the_instantiation) ctxt; |
|
951 val arities_proof = maps Logic.mk_arities arities; |
950 val arities_proof = maps Logic.mk_arities arities; |
952 fun after_qed' results = |
951 fun after_qed' results = |
953 LocalTheory.theory (fold (AxClass.add_arity o Thm.varifyT) results) |
952 LocalTheory.theory (fold (AxClass.add_arity o Thm.varifyT) results) |
954 #> after_qed; |
953 #> after_qed; |
955 in |
954 in |