src/Pure/thm.ML
changeset 1065 8425cb5acb77
parent 1028 88c8df00905b
child 1160 8845eb5f0e5e
equal deleted inserted replaced
1064:5d6fb2c938e0 1065:8425cb5acb77
  1201   | lextermord([],[]) = EQUAL
  1201   | lextermord([],[]) = EQUAL
  1202   | lextermord _ = error("lextermord");
  1202   | lextermord _ = error("lextermord");
  1203 
  1203 
  1204 fun termless tu = (termord tu = LESS);
  1204 fun termless tu = (termord tu = LESS);
  1205 
  1205 
  1206 fun check_conv(thm as Thm{hyps,prop,sign,...}, prop0) =
  1206 fun check_conv(thm as Thm{hyps,prop,sign,maxidx,...}, prop0) =
  1207   let fun err() = (trace_thm "Proved wrong thm (Check subgoaler?)" thm;
  1207   let fun err() = (trace_thm "Proved wrong thm (Check subgoaler?)" thm;
  1208                    trace_term "Should have proved" sign prop0;
  1208                    trace_term "Should have proved" sign prop0;
  1209                    None)
  1209                    None)
  1210       val (lhs0,_) = Logic.dest_equals(Logic.strip_imp_concl prop0)
  1210       val (lhs0,_) = Logic.dest_equals(Logic.strip_imp_concl prop0)
  1211   in case prop of
  1211   in case prop of
  1212        Const("==",_) $ lhs $ rhs =>
  1212        Const("==",_) $ lhs $ rhs =>
  1213          if (lhs = lhs0) orelse
  1213          if (lhs = lhs0) orelse
  1214             (lhs aconv Envir.norm_term (Envir.empty 0) lhs0)
  1214             (lhs aconv Envir.norm_term (Envir.empty 0) lhs0)
  1215          then (trace_thm "SUCCEEDED" thm; Some(hyps,rhs))
  1215          then (trace_thm "SUCCEEDED" thm; Some(hyps,maxidx,rhs))
  1216          else err()
  1216          else err()
  1217      | _ => err()
  1217      | _ => err()
  1218   end;
  1218   end;
  1219 
  1219 
  1220 fun ren_inst(insts,prop,pat,obj) =
  1220 fun ren_inst(insts,prop,pat,obj) =
  1225         | renAbs(t) = t
  1225         | renAbs(t) = t
  1226   in subst_vars insts (if null(ren) then prop else renAbs(prop)) end;
  1226   in subst_vars insts (if null(ren) then prop else renAbs(prop)) end;
  1227 
  1227 
  1228 
  1228 
  1229 (*Conversion to apply the meta simpset to a term*)
  1229 (*Conversion to apply the meta simpset to a term*)
  1230 fun rewritec (prover,signt) (mss as Mss{net,...}) (hypst,t) =
  1230 fun rewritec (prover,signt) (mss as Mss{net,...}) (hypst,maxidxt,t) =
  1231   let val etat = Pattern.eta_contract t;
  1231   let val etat = Pattern.eta_contract t;
  1232       fun rew {thm as Thm{sign,hyps,maxidx,prop,...}, lhs, perm} =
  1232       fun rew {thm as Thm{sign,hyps,maxidx,prop,...}, lhs, perm} =
  1233         let val unit = if Sign.subsig(sign,signt) then ()
  1233         let val unit = if Sign.subsig(sign,signt) then ()
  1234                   else (trace_thm"Warning: rewrite rule from different theory"
  1234                   else (trace_thm"Warning: rewrite rule from different theory"
  1235                           thm;
  1235                           thm;
  1236                         raise Pattern.MATCH)
  1236                         raise Pattern.MATCH)
  1237             val insts = Pattern.match (#tsig(Sign.rep_sg signt)) (lhs,etat)
  1237             val rprop = if maxidxt = ~1 then prop
  1238             val prop' = ren_inst(insts,prop,lhs,t);
  1238                         else Logic.incr_indexes([],maxidxt+1) prop;
       
  1239             val rlhs = if maxidxt = ~1 then lhs
       
  1240                        else fst(Logic.dest_equals(Logic.strip_imp_concl rprop))
       
  1241             val insts = Pattern.match (#tsig(Sign.rep_sg signt)) (rlhs,etat)
       
  1242             val prop' = ren_inst(insts,rprop,rlhs,t);
  1239             val hyps' = hyps union hypst;
  1243             val hyps' = hyps union hypst;
  1240             val thm' = Thm{sign=signt, hyps=hyps', prop=prop',
  1244             val maxidx' = maxidx_of_term prop'
  1241                            maxidx=maxidx_of_term prop'}
  1245             val thm' = Thm{sign=signt, hyps=hyps', prop=prop', maxidx=maxidx'}
  1242             val (lhs',rhs') = Logic.dest_equals(Logic.strip_imp_concl prop')
  1246             val (lhs',rhs') = Logic.dest_equals(Logic.strip_imp_concl prop')
  1243         in if perm andalso not(termless(rhs',lhs')) then None else
  1247         in if perm andalso not(termless(rhs',lhs')) then None else
  1244            if Logic.count_prems(prop',0) = 0
  1248            if Logic.count_prems(prop',0) = 0
  1245            then (trace_thm "Rewriting:" thm'; Some(hyps',rhs'))
  1249            then (trace_thm "Rewriting:" thm'; Some(hyps',maxidx',rhs'))
  1246            else (trace_thm "Trying to rewrite:" thm';
  1250            else (trace_thm "Trying to rewrite:" thm';
  1247                  case prover mss thm' of
  1251                  case prover mss thm' of
  1248                    None       => (trace_thm "FAILED" thm'; None)
  1252                    None       => (trace_thm "FAILED" thm'; None)
  1249                  | Some(thm2) => check_conv(thm2,prop'))
  1253                  | Some(thm2) => check_conv(thm2,prop'))
  1250         end
  1254         end
  1253         | rews (rrule::rrules) =
  1257         | rews (rrule::rrules) =
  1254             let val opt = rew rrule handle Pattern.MATCH => None
  1258             let val opt = rew rrule handle Pattern.MATCH => None
  1255             in case opt of None => rews rrules | some => some end;
  1259             in case opt of None => rews rrules | some => some end;
  1256 
  1260 
  1257   in case etat of
  1261   in case etat of
  1258        Abs(_,_,body) $ u => Some(hypst,subst_bounds([u], body))
  1262        Abs(_,_,body) $ u => Some(hypst, maxidxt, subst_bounds([u], body))
  1259      | _                 => rews(Net.match_term net etat)
  1263      | _                 => rews(Net.match_term net etat)
  1260   end;
  1264   end;
  1261 
  1265 
  1262 (*Conversion to apply a congruence rule to a term*)
  1266 (*Conversion to apply a congruence rule to a term*)
  1263 fun congc (prover,signt) {thm=cong,lhs=lhs} (hypst,t) =
  1267 fun congc (prover,signt) {thm=cong,lhs=lhs} (hypst,maxidxt,t) =
  1264   let val Thm{sign,hyps,maxidx,prop,...} = cong
  1268   let val Thm{sign,hyps,maxidx,prop,...} = cong
  1265       val unit = if Sign.subsig(sign,signt) then ()
  1269       val unit = if Sign.subsig(sign,signt) then ()
  1266                  else error("Congruence rule from different theory")
  1270                  else error("Congruence rule from different theory")
  1267       val tsig = #tsig(Sign.rep_sg signt)
  1271       val tsig = #tsig(Sign.rep_sg signt)
  1268       val insts = Pattern.match tsig (lhs,t) handle Pattern.MATCH =>
  1272       val rprop = if maxidxt = ~1 then prop
       
  1273                   else Logic.incr_indexes([],maxidxt+1) prop;
       
  1274       val rlhs = if maxidxt = ~1 then lhs
       
  1275                  else fst(Logic.dest_equals(Logic.strip_imp_concl rprop))
       
  1276       val insts = Pattern.match tsig (rlhs,t) handle Pattern.MATCH =>
  1269                   error("Congruence rule did not match")
  1277                   error("Congruence rule did not match")
  1270       val prop' = ren_inst(insts,prop,lhs,t);
  1278       val prop' = ren_inst(insts,rprop,rlhs,t);
  1271       val thm' = Thm{sign=signt, hyps=hyps union hypst,
  1279       val thm' = Thm{sign=signt, hyps=hyps union hypst,
  1272                      prop=prop', maxidx=maxidx_of_term prop'}
  1280                      prop=prop', maxidx=maxidx_of_term prop'}
  1273       val unit = trace_thm "Applying congruence rule" thm';
  1281       val unit = trace_thm "Applying congruence rule" thm';
  1274       fun err() = error("Failed congruence proof!")
  1282       fun err() = error("Failed congruence proof!")
  1275 
  1283 
  1296       and try_botc mss trec = (case botc true mss trec of
  1304       and try_botc mss trec = (case botc true mss trec of
  1297                                  Some(trec1) => trec1
  1305                                  Some(trec1) => trec1
  1298                                | None => trec)
  1306                                | None => trec)
  1299 
  1307 
  1300       and subc (mss as Mss{net,congs,bounds,prems,mk_rews})
  1308       and subc (mss as Mss{net,congs,bounds,prems,mk_rews})
  1301                (trec as (hyps,t)) =
  1309                (trec as (hyps,maxidx,t)) =
  1302         (case t of
  1310         (case t of
  1303             Abs(a,T,t) =>
  1311             Abs(a,T,t) =>
  1304               let val b = variant bounds a
  1312               let val b = variant bounds a
  1305                   val v = Free("." ^ b,T)
  1313                   val v = Free("." ^ b,T)
  1306                   val mss' = Mss{net=net, congs=congs, bounds=b::bounds,
  1314                   val mss' = Mss{net=net, congs=congs, bounds=b::bounds,
  1307                                  prems=prems,mk_rews=mk_rews}
  1315                                  prems=prems,mk_rews=mk_rews}
  1308               in case botc true mss' (hyps,subst_bounds([v],t)) of
  1316               in case botc true mss' (hyps,maxidx,subst_bounds([v],t)) of
  1309                    Some(hyps',t') =>
  1317                    Some(hyps',maxidx',t') =>
  1310                      Some(hyps', Abs(a, T, abstract_over(v,t')))
  1318                      Some(hyps', maxidx', Abs(a, T, abstract_over(v,t')))
  1311                  | None => None
  1319                  | None => None
  1312               end
  1320               end
  1313           | t$u => (case t of
  1321           | t$u => (case t of
  1314               Const("==>",_)$s  => Some(impc(hyps,s,u,mss))
  1322               Const("==>",_)$s  => Some(impc(hyps,maxidx,s,u,mss))
  1315             | Abs(_,_,body) =>
  1323             | Abs(_,_,body) =>
  1316                 let val trec = (hyps,subst_bounds([u], body))
  1324                 let val trec = (hyps,maxidx,subst_bounds([u], body))
  1317                 in case subc mss trec of
  1325                 in case subc mss trec of
  1318                      None => Some(trec)
  1326                      None => Some(trec)
  1319                    | trec => trec
  1327                    | trec => trec
  1320                 end
  1328                 end
  1321             | _  =>
  1329             | _  =>
  1322                 let fun appc() =
  1330                 let fun appc() =
  1323                           (case botc true mss (hyps,t) of
  1331                           (case botc true mss (hyps,maxidx,t) of
  1324                              Some(hyps1,t1) =>
  1332                              Some(hyps1,maxidx1,t1) =>
  1325                                (case botc true mss (hyps1,u) of
  1333                                (case botc true mss (hyps1,maxidx,u) of
  1326                                   Some(hyps2,u1) => Some(hyps2,t1$u1)
  1334                                   Some(hyps2,maxidx2,u1) =>
  1327                                 | None => Some(hyps1,t1$u))
  1335                                     Some(hyps2,max[maxidx1,maxidx2],t1$u1)
       
  1336                                 | None =>
       
  1337                                     Some(hyps1,max[maxidx1,maxidx],t1$u))
  1328                            | None =>
  1338                            | None =>
  1329                                (case botc true mss (hyps,u) of
  1339                                (case botc true mss (hyps,maxidx,u) of
  1330                                   Some(hyps1,u1) => Some(hyps1,t$u1)
  1340                                   Some(hyps1,maxidx1,u1) =>
       
  1341                                     Some(hyps1,max[maxidx,maxidx1],t$u1)
  1331                                 | None => None))
  1342                                 | None => None))
  1332                     val (h,ts) = strip_comb t
  1343                     val (h,ts) = strip_comb t
  1333                 in case h of
  1344                 in case h of
  1334                      Const(a,_) =>
  1345                      Const(a,_) =>
  1335                        (case assoc(congs,a) of
  1346                        (case assoc(congs,a) of
  1337                         | Some(cong) => congc (prover mss,sign) cong trec)
  1348                         | Some(cong) => congc (prover mss,sign) cong trec)
  1338                    | _ => appc()
  1349                    | _ => appc()
  1339                 end)
  1350                 end)
  1340           | _ => None)
  1351           | _ => None)
  1341 
  1352 
  1342       and impc(hyps,s,u,mss as Mss{mk_rews,...}) =
  1353       and impc(hyps,maxidx,s,u,mss as Mss{mk_rews,...}) =
  1343         let val (hyps1,s1) = if simprem then try_botc mss (hyps,s)
  1354         let val (hyps1,_,s1) = if simprem then try_botc mss (hyps,maxidx,s)
  1344                              else (hyps,s)
  1355                                else (hyps,0,s);
       
  1356             val maxidx1 = maxidx_of_term s1
  1345             val mss1 =
  1357             val mss1 =
  1346               if not useprem orelse maxidx_of_term s1 <> ~1 then mss
  1358               if not useprem orelse maxidx1 <> ~1 then mss
  1347               else let val thm = Thm{sign=sign,hyps=[s1],prop=s1,maxidx= ~1}
  1359               else let val thm = Thm{sign=sign,hyps=[s1],prop=s1,maxidx= ~1}
  1348                    in add_simps(add_prems(mss,[thm]), mk_rews thm) end
  1360                    in add_simps(add_prems(mss,[thm]), mk_rews thm) end
  1349             val (hyps2,u1) = try_botc mss1 (hyps1,u)
  1361             val (hyps2,maxidx2,u1) = try_botc mss1 (hyps1,maxidx,u)
  1350             val hyps3 = if s1 mem hyps1 then hyps2 else hyps2\s1
  1362             val hyps3 = if s1 mem hyps1 then hyps2 else hyps2\s1
  1351         in (hyps3, Logic.mk_implies(s1,u1)) end
  1363         in (hyps3, max[maxidx1,maxidx2], Logic.mk_implies(s1,u1)) end
  1352 
  1364 
  1353   in try_botc end;
  1365   in try_botc end;
  1354 
  1366 
  1355 
  1367 
  1356 (*** Meta-rewriting: rewrites t to u and returns the theorem t==u ***)
  1368 (*** Meta-rewriting: rewrites t to u and returns the theorem t==u ***)
  1361 *)
  1373 *)
  1362 
  1374 
  1363 (*** FIXME: check that #bounds(mss) does not "occur" in ct alread ***)
  1375 (*** FIXME: check that #bounds(mss) does not "occur" in ct alread ***)
  1364 fun rewrite_cterm mode mss prover ct =
  1376 fun rewrite_cterm mode mss prover ct =
  1365   let val {sign, t, T, maxidx} = rep_cterm ct;
  1377   let val {sign, t, T, maxidx} = rep_cterm ct;
  1366       val (hyps,u) = bottomc (mode,prover,sign) mss ([],t);
  1378       val (hyps,maxidxu,u) = bottomc (mode,prover,sign) mss ([],maxidx,t);
  1367       val prop = Logic.mk_equals(t,u)
  1379       val prop = Logic.mk_equals(t,u)
  1368   in  Thm{sign= sign, hyps= hyps, maxidx= maxidx_of_term prop, prop= prop}
  1380   in  Thm{sign= sign, hyps= hyps, maxidx= max[maxidx,maxidxu], prop= prop}
  1369   end
  1381   end
  1370 
  1382 
  1371 end;
  1383 end;
  1372 
  1384