src/Pure/goals.ML
changeset 13646 46ed3d042ba5
parent 13272 eb0b565909a0
child 13660 e36798726ca4
equal deleted inserted replaced
13645:430310b12c5b 13646:46ed3d042ba5
   105   include BASIC_GOALS
   105   include BASIC_GOALS
   106   type locale
   106   type locale
   107   val print_locales: theory -> unit
   107   val print_locales: theory -> unit
   108   val get_thm: theory -> xstring -> thm
   108   val get_thm: theory -> xstring -> thm
   109   val get_thms: theory -> xstring -> thm list
   109   val get_thms: theory -> xstring -> thm list
       
   110   val find_intros_goal : theory -> thm -> int -> (string * thm) list
   110   val add_locale: bstring -> (bstring option) -> (string * string * mixfix) list ->
   111   val add_locale: bstring -> (bstring option) -> (string * string * mixfix) list ->
   111     (string * string) list -> (string * string) list -> theory -> theory
   112     (string * string) list -> (string * string) list -> theory -> theory
   112   val add_locale_i: bstring -> (bstring option) -> (string * typ * mixfix) list ->
   113   val add_locale_i: bstring -> (bstring option) -> (string * typ * mixfix) list ->
   113     (string * term) list -> (string * term) list -> theory -> theory
   114     (string * term) list -> (string * term) list -> theory -> theory
   114   val open_locale: xstring -> theory -> theory
   115   val open_locale: xstring -> theory -> theory
   897 (*Return the result UNCHECKED that it equals the goal -- for synthesis,
   898 (*Return the result UNCHECKED that it equals the goal -- for synthesis,
   898   answer extraction, or other instantiation of Vars *)
   899   answer extraction, or other instantiation of Vars *)
   899 fun uresult () = !curr_mkresult (false, topthm());
   900 fun uresult () = !curr_mkresult (false, topthm());
   900 
   901 
   901 (*Get subgoal i from goal stack*)
   902 (*Get subgoal i from goal stack*)
   902 fun getgoal i = List.nth (prems_of (topthm()), i-1)
   903 fun get_goal st i = List.nth (prems_of st, i-1)
   903                 handle Subscript => error"getgoal: Goal number out of range";
   904                 handle Subscript => error"getgoal: Goal number out of range";
       
   905 
       
   906 fun getgoal i = get_goal (topthm()) i;
   904 
   907 
   905 (*Return subgoal i's hypotheses as meta-level assumptions.
   908 (*Return subgoal i's hypotheses as meta-level assumptions.
   906   For debugging uses of METAHYPS*)
   909   For debugging uses of METAHYPS*)
   907 local exception GETHYPS of thm list
   910 local exception GETHYPS of thm list
   908 in
   911 in
  1132     val consts = map (Sign.intern_const (Theory.sign_of thy)) raw_consts;
  1135     val consts = map (Sign.intern_const (Theory.sign_of thy)) raw_consts;
  1133   in
  1136   in
  1134     (case filter (is_none o Sign.const_type (Theory.sign_of thy)) consts of
  1137     (case filter (is_none o Sign.const_type (Theory.sign_of thy)) consts of
  1135       [] => ()
  1138       [] => ()
  1136     | cs => error ("thms_containing: undeclared consts " ^ commas_quote cs));
  1139     | cs => error ("thms_containing: undeclared consts " ^ commas_quote cs));
  1137     PureThy.thms_containing thy (consts, [])
  1140     PureThy.thms_containing_consts thy consts
  1138     |> map #2 |> flat
       
  1139     |> map (fn th => (Thm.name_of_thm th, th))
       
  1140   end;
  1141   end;
  1141 
  1142 
  1142 (*top_const: main constant, ignoring Trueprop*)
       
  1143 fun top_const (_ $ t) = (case head_of t of Const (c, _) => Some c | _ => None)
       
  1144   | top_const _ = None;
       
  1145 
       
  1146 val intro_const = top_const o concl_of;
       
  1147 fun elim_const thm = case prems_of thm of [] => None | p::_ => top_const p;
       
  1148 
       
  1149 fun index_intros c = thms_containing [c] |> filter (fn (_, thm) => intro_const thm = Some c);
       
  1150 fun index_elims c = thms_containing [c] |> filter (fn (_, thm) => elim_const thm = Some c);
       
  1151 
       
  1152 (*assume that parameters have unique names; reverse them for substitution*)
  1143 (*assume that parameters have unique names; reverse them for substitution*)
  1153 fun goal_params i =
  1144 fun goal_params st i =
  1154   let val gi = getgoal i
  1145   let val gi = get_goal st i
  1155       val rfrees = rev(map Free (Logic.strip_params gi))
  1146       val rfrees = rev(map Free (Logic.strip_params gi))
  1156   in (gi,rfrees) end;
  1147   in (gi,rfrees) end;
  1157 
  1148 
  1158 fun concl_of_goal i =
  1149 fun concl_of_goal st i =
  1159   let val (gi,rfrees) = goal_params i
  1150   let val (gi,rfrees) = goal_params st i
  1160       val B = Logic.strip_assums_concl gi
  1151       val B = Logic.strip_assums_concl gi
  1161   in subst_bounds(rfrees,B) end;
  1152   in subst_bounds(rfrees,B) end;
  1162 
  1153 
  1163 fun prems_of_goal i =
  1154 fun prems_of_goal st i =
  1164   let val (gi,rfrees) = goal_params i
  1155   let val (gi,rfrees) = goal_params st i
  1165       val As = Logic.strip_assums_hyp gi
  1156       val As = Logic.strip_assums_hyp gi
  1166   in map (fn A => subst_bounds(rfrees,A)) As end;
  1157   in map (fn A => subst_bounds(rfrees,A)) As end;
  1167 
  1158 
  1168 (*returns all those named_thms whose subterm extracted by extract can be
  1159 fun find_intros_goal thy st i = PureThy.find_intros thy (concl_of_goal st i);
  1169   instantiated to obj; the list is sorted according to the number of premises
  1160 
  1170   and the size of the required substitution.*)
  1161 fun findI i = find_intros_goal (theory_of_goal()) (topthm()) i;
  1171 fun select_match(obj, signobj, named_thms, extract) =
       
  1172   let fun matches(prop, tsig) =
       
  1173             case extract prop of
       
  1174               None => false
       
  1175             | Some pat => Pattern.matches tsig (pat, obj);
       
  1176 
       
  1177       fun substsize(prop, tsig) =
       
  1178             let val Some pat = extract prop
       
  1179                 val (_,subst) = Pattern.match tsig (pat,obj)
       
  1180             in foldl op+
       
  1181                 (0, map (fn (_,t) => size_of_term t) subst)
       
  1182             end
       
  1183 
       
  1184       fun thm_ord ((p0,s0,_),(p1,s1,_)) =
       
  1185             prod_ord (int_ord o pairself (fn 0 => 0 | x => 1)) int_ord ((p0,s0),(p1,s1));
       
  1186 
       
  1187       fun select((p as (_,thm))::named_thms, sels) =
       
  1188             let val {prop, sign, ...} = rep_thm thm
       
  1189             in select(named_thms,
       
  1190                       if Sign.subsig(sign, signobj) andalso
       
  1191                          matches(prop,#tsig(Sign.rep_sg signobj))
       
  1192                       then
       
  1193                         (nprems_of thm,substsize(prop,#tsig(Sign.rep_sg signobj)),p)::sels
       
  1194                       else sels)
       
  1195             end
       
  1196         | select([],sels) = sels
       
  1197 
       
  1198   in map (fn (_,_,t) => t) (sort thm_ord (select(named_thms, []))) end;
       
  1199 
       
  1200 fun find_matching(prop,sign,index,extract) =
       
  1201   (case top_const prop of
       
  1202      Some c => select_match(prop,sign,index c,extract)
       
  1203    | _      => []);
       
  1204 
       
  1205 fun find_intros(prop,sign) =
       
  1206   find_matching(prop,sign,index_intros,Some o Logic.strip_imp_concl);
       
  1207 
       
  1208 fun find_elims sign prop =
       
  1209   let fun major prop = case Logic.strip_imp_prems prop of
       
  1210                          [] => None | p::_ => Some p
       
  1211   in find_matching(prop,sign,index_elims,major) end;
       
  1212 
       
  1213 fun findI i = find_intros(concl_of_goal i,#sign(rep_thm(topthm())));
       
  1214 
  1162 
  1215 fun findEs i =
  1163 fun findEs i =
  1216   let fun eq_nth((n1,th1),(n2,th2)) = n1=n2 andalso eq_thm(th1,th2);
  1164   let fun eq_nth((n1,th1),(n2,th2)) = n1=n2 andalso eq_thm(th1,th2);
  1217       val sign = #sign(rep_thm(topthm()))
  1165       val prems = prems_of_goal (topthm()) i
  1218       val thmss = map (find_elims sign) (prems_of_goal i)
  1166       val thy = theory_of_goal();
       
  1167       val thmss = map (PureThy.find_elims thy) prems
  1219   in foldl (gen_union eq_nth) ([],thmss) end;
  1168   in foldl (gen_union eq_nth) ([],thmss) end;
  1220 
  1169 
  1221 fun findE i j =
  1170 fun findE i j =
  1222   let val sign = #sign(rep_thm(topthm()))
  1171   let val prems = prems_of_goal (topthm()) i
  1223   in find_elims sign (nth_elem(j-1, prems_of_goal i)) end;
  1172       val thy = theory_of_goal();
       
  1173   in PureThy.find_elims thy (nth_elem(j-1, prems)) end;
  1224 
  1174 
  1225 end;
  1175 end;
  1226 
  1176 
  1227 structure BasicGoals: BASIC_GOALS = Goals;
  1177 structure BasicGoals: BASIC_GOALS = Goals;
  1228 open BasicGoals;
  1178 open BasicGoals;