src/Pure/thm.ML
changeset 79124 89d4a8f52738
parent 79119 cf29db6c95e1
child 79126 bdb33a2d4167
equal deleted inserted replaced
79123:419519d5230d 79124:89d4a8f52738
  1314         raise THM ("forall_intr: variable " ^ quote a ^ " free in assumptions", 0, [th])
  1314         raise THM ("forall_intr: variable " ^ quote a ^ " free in assumptions", 0, [th])
  1315       else
  1315       else
  1316         let
  1316         let
  1317           val cert = join_certificate1 (ct, th);
  1317           val cert = join_certificate1 (ct, th);
  1318           val prf = Proofterm.forall_intr_proof (a, x) NONE;
  1318           val prf = Proofterm.forall_intr_proof (a, x) NONE;
  1319           fun zprf p = ZTerm.forall_intr_proof (Context.certificate_theory cert) (a, x) T p;
  1319           fun zprf p = ZTerm.forall_intr_proof (Context.certificate_theory cert) T (a, x) p;
  1320         in
  1320         in
  1321           Thm (deriv_rule1 (prf, zprf) der,
  1321           Thm (deriv_rule1 (prf, zprf) der,
  1322            {cert = cert,
  1322            {cert = cert,
  1323             tags = [],
  1323             tags = [],
  1324             maxidx = Int.max (maxidx1, maxidx2),
  1324             maxidx = Int.max (maxidx1, maxidx2),
  1368 (* Equality *)
  1368 (* Equality *)
  1369 
  1369 
  1370 (*Reflexivity
  1370 (*Reflexivity
  1371   t \<equiv> t
  1371   t \<equiv> t
  1372 *)
  1372 *)
  1373 fun reflexive (Cterm {cert, t, T = _, maxidx, sorts}) =
  1373 fun reflexive (Cterm {cert, t, T, maxidx, sorts}) =
  1374   Thm (deriv_rule0 (fn () => Proofterm.reflexive_proof, ZTerm.todo_proof),
  1374   let fun zprf () = ZTerm.reflexive_proof (Context.certificate_theory cert) T t in
  1375    {cert = cert,
  1375     Thm (deriv_rule0 (fn () => Proofterm.reflexive_proof, zprf),
  1376     tags = [],
  1376      {cert = cert,
  1377     maxidx = maxidx,
  1377       tags = [],
  1378     constraints = [],
  1378       maxidx = maxidx,
  1379     shyps = sorts,
  1379       constraints = [],
  1380     hyps = [],
  1380       shyps = sorts,
  1381     tpairs = [],
  1381       hyps = [],
  1382     prop = Logic.mk_equals (t, t)});
  1382       tpairs = [],
       
  1383       prop = Logic.mk_equals (t, t)})
       
  1384   end;
  1383 
  1385 
  1384 (*Symmetry
  1386 (*Symmetry
  1385   t \<equiv> u
  1387   t \<equiv> u
  1386   ------
  1388   ------
  1387   u \<equiv> t
  1389   u \<equiv> t
  1388 *)
  1390 *)
  1389 fun symmetric (th as Thm (der, {cert, maxidx, constraints, shyps, hyps, tpairs, prop, ...})) =
  1391 fun symmetric (th as Thm (der, {cert, maxidx, constraints, shyps, hyps, tpairs, prop, ...})) =
  1390   (case prop of
  1392   (case prop of
  1391     (eq as Const ("Pure.eq", _)) $ t $ u =>
  1393     (eq as Const ("Pure.eq", Type ("fun", [T, _]))) $ t $ u =>
  1392       Thm (deriv_rule1 (Proofterm.symmetric_proof, ZTerm.todo_proof) der,
  1394       let fun zprf prf = ZTerm.symmetric_proof (Context.certificate_theory cert) T t u prf in
  1393        {cert = cert,
  1395         Thm (deriv_rule1 (Proofterm.symmetric_proof, zprf) der,
  1394         tags = [],
  1396          {cert = cert,
  1395         maxidx = maxidx,
  1397           tags = [],
  1396         constraints = constraints,
  1398           maxidx = maxidx,
  1397         shyps = shyps,
  1399           constraints = constraints,
  1398         hyps = hyps,
  1400           shyps = shyps,
  1399         tpairs = tpairs,
  1401           hyps = hyps,
  1400         prop = eq $ u $ t})
  1402           tpairs = tpairs,
       
  1403           prop = eq $ u $ t})
       
  1404       end
  1401     | _ => raise THM ("symmetric", 0, [th]));
  1405     | _ => raise THM ("symmetric", 0, [th]));
  1402 
  1406 
  1403 (*Transitivity
  1407 (*Transitivity
  1404   t1 \<equiv> u    u \<equiv> t2
  1408   t1 \<equiv> u    u \<equiv> t2
  1405   ------------------
  1409   ------------------
  1412     and Thm (der2, {maxidx = maxidx2, hyps = hyps2, constraints = constraints2, shyps = shyps2,
  1416     and Thm (der2, {maxidx = maxidx2, hyps = hyps2, constraints = constraints2, shyps = shyps2,
  1413         tpairs = tpairs2, prop = prop2, ...}) = th2;
  1417         tpairs = tpairs2, prop = prop2, ...}) = th2;
  1414     fun err msg = raise THM ("transitive: " ^ msg, 0, [th1, th2]);
  1418     fun err msg = raise THM ("transitive: " ^ msg, 0, [th1, th2]);
  1415   in
  1419   in
  1416     case (prop1, prop2) of
  1420     case (prop1, prop2) of
  1417       ((eq as Const ("Pure.eq", Type (_, [U, _]))) $ t1 $ u, Const ("Pure.eq", _) $ u' $ t2) =>
  1421       ((eq as Const ("Pure.eq", Type (_, [T, _]))) $ t1 $ u, Const ("Pure.eq", _) $ u' $ t2) =>
  1418         if not (u aconv u') then err "middle term"
  1422         if not (u aconv u') then err "middle term"
  1419         else
  1423         else
  1420           Thm (deriv_rule2 (Proofterm.transitive_proof U u, K ZTerm.todo_proof) der1 der2,
  1424           let
  1421            {cert = join_certificate2 (th1, th2),
  1425             val cert = join_certificate2 (th1, th2);
  1422             tags = [],
  1426             fun zprf prf1 prf2 =
  1423             maxidx = Int.max (maxidx1, maxidx2),
  1427               ZTerm.transitive_proof (Context.certificate_theory cert) T t1 u t2 prf1 prf2;
  1424             constraints = union_constraints constraints1 constraints2,
  1428           in
  1425             shyps = Sorts.union shyps1 shyps2,
  1429             Thm (deriv_rule2 (Proofterm.transitive_proof T u, zprf) der1 der2,
  1426             hyps = union_hyps hyps1 hyps2,
  1430              {cert = cert,
  1427             tpairs = union_tpairs tpairs1 tpairs2,
  1431               tags = [],
  1428             prop = eq $ t1 $ t2})
  1432               maxidx = Int.max (maxidx1, maxidx2),
  1429      | _ =>  err "premises"
  1433               constraints = union_constraints constraints1 constraints2,
       
  1434               shyps = Sorts.union shyps1 shyps2,
       
  1435               hyps = union_hyps hyps1 hyps2,
       
  1436               tpairs = union_tpairs tpairs1 tpairs2,
       
  1437               prop = eq $ t1 $ t2})
       
  1438           end
       
  1439      | _ => err "premises"
  1430   end;
  1440   end;
  1431 
  1441 
  1432 (*Beta-conversion
  1442 (*Beta-conversion
  1433   (\<lambda>x. t) u \<equiv> t[u/x]
  1443   (\<lambda>x. t) u \<equiv> t[u/x]
  1434   fully beta-reduces the term if full = true
  1444   fully beta-reduces the term if full = true
  1435 *)
  1445 *)
  1436 fun beta_conversion full (Cterm {cert, t, T = _, maxidx, sorts}) =
  1446 fun beta_conversion full (Cterm {cert, t, T, maxidx, sorts}) =
  1437   let val t' =
  1447   let
  1438     if full then Envir.beta_norm t
  1448     val t' =
  1439     else
  1449       if full then Envir.beta_norm t
  1440       (case t of Abs (_, _, bodt) $ u => subst_bound (u, bodt)
  1450       else
  1441       | _ => raise THM ("beta_conversion: not a redex", 0, []));
  1451         (case t of Abs (_, _, bodt) $ u => subst_bound (u, bodt)
  1442   in
  1452         | _ => raise THM ("beta_conversion: not a redex", 0, []));
  1443     Thm (deriv_rule0 (fn () => Proofterm.reflexive_proof, ZTerm.todo_proof),
  1453     fun zprf () = ZTerm.reflexive_proof (Context.certificate_theory cert) T t;
       
  1454   in
       
  1455     Thm (deriv_rule0 (fn () => Proofterm.reflexive_proof, zprf),
  1444      {cert = cert,
  1456      {cert = cert,
  1445       tags = [],
  1457       tags = [],
  1446       maxidx = maxidx,
  1458       maxidx = maxidx,
  1447       constraints = [],
  1459       constraints = [],
  1448       shyps = sorts,
  1460       shyps = sorts,
  1449       hyps = [],
  1461       hyps = [],
  1450       tpairs = [],
  1462       tpairs = [],
  1451       prop = Logic.mk_equals (t, t')})
  1463       prop = Logic.mk_equals (t, t')})
  1452   end;
  1464   end;
  1453 
  1465 
  1454 fun eta_conversion (Cterm {cert, t, T = _, maxidx, sorts}) =
  1466 fun eta_conversion (Cterm {cert, t, T, maxidx, sorts}) =
  1455   Thm (deriv_rule0 (fn () => Proofterm.reflexive_proof, ZTerm.todo_proof),
  1467   let fun zprf () = ZTerm.reflexive_proof (Context.certificate_theory cert) T t in
  1456    {cert = cert,
  1468     Thm (deriv_rule0 (fn () => Proofterm.reflexive_proof, zprf),
  1457     tags = [],
  1469      {cert = cert,
  1458     maxidx = maxidx,
  1470       tags = [],
  1459     constraints = [],
  1471       maxidx = maxidx,
  1460     shyps = sorts,
  1472       constraints = [],
  1461     hyps = [],
  1473       shyps = sorts,
  1462     tpairs = [],
  1474       hyps = [],
  1463     prop = Logic.mk_equals (t, Envir.eta_contract t)});
  1475       tpairs = [],
  1464 
  1476       prop = Logic.mk_equals (t, Envir.eta_contract t)})
  1465 fun eta_long_conversion (Cterm {cert, t, T = _, maxidx, sorts}) =
  1477   end;
  1466   Thm (deriv_rule0 (fn () => Proofterm.reflexive_proof, ZTerm.todo_proof),
  1478 
  1467    {cert = cert,
  1479 fun eta_long_conversion (Cterm {cert, t, T, maxidx, sorts}) =
  1468     tags = [],
  1480   let fun zprf () = ZTerm.reflexive_proof (Context.certificate_theory cert) T t in
  1469     maxidx = maxidx,
  1481     Thm (deriv_rule0 (fn () => Proofterm.reflexive_proof, zprf),
  1470     constraints = [],
  1482      {cert = cert,
  1471     shyps = sorts,
  1483       tags = [],
  1472     hyps = [],
  1484       maxidx = maxidx,
  1473     tpairs = [],
  1485       constraints = [],
  1474     prop = Logic.mk_equals (t, Envir.eta_long [] t)});
  1486       shyps = sorts,
       
  1487       hyps = [],
       
  1488       tpairs = [],
       
  1489       prop = Logic.mk_equals (t, Envir.eta_long [] t)})
       
  1490   end;
  1475 
  1491 
  1476 (*The abstraction rule.  The Free or Var x must not be free in the hypotheses.
  1492 (*The abstraction rule.  The Free or Var x must not be free in the hypotheses.
  1477   The bound variable will be named "a" (since x will be something like x320)
  1493   The bound variable will be named "a" (since x will be something like x320)
  1478       t \<equiv> u
  1494       t \<equiv> u
  1479   --------------
  1495   --------------
  1481 *)
  1497 *)
  1482 fun abstract_rule b
  1498 fun abstract_rule b
  1483     (Cterm {t = x, T, sorts, ...})
  1499     (Cterm {t = x, T, sorts, ...})
  1484     (th as Thm (der, {cert, maxidx, hyps, constraints, shyps, tpairs, prop, ...})) =
  1500     (th as Thm (der, {cert, maxidx, hyps, constraints, shyps, tpairs, prop, ...})) =
  1485   let
  1501   let
  1486     val (t, u) = Logic.dest_equals prop
  1502     val (U, t, u) =
  1487       handle TERM _ => raise THM ("abstract_rule: premise not an equality", 0, [th]);
  1503       (case prop of
       
  1504         Const ("Pure.eq", Type ("fun", [U, _])) $ t $ u => (U, t, u)
       
  1505       | _ => raise THM ("abstract_rule: premise not an equality", 0, [th]));
  1488     fun check_result a ts =
  1506     fun check_result a ts =
  1489       if occs x ts tpairs then
  1507       if occs x ts tpairs then
  1490         raise THM ("abstract_rule: variable " ^ quote a ^ " free in assumptions", 0, [th])
  1508         raise THM ("abstract_rule: variable " ^ quote a ^ " free in assumptions", 0, [th])
  1491       else
  1509       else
  1492         Thm (deriv_rule1 (Proofterm.abstract_rule_proof (b, x), ZTerm.todo_proof) der,
  1510         let
  1493          {cert = cert,
  1511           val f = Abs (b, T, abstract_over (x, t));
  1494           tags = [],
  1512           val g = Abs (b, T, abstract_over (x, u));
  1495           maxidx = maxidx,
  1513           fun zprf prf =
  1496           constraints = constraints,
  1514             ZTerm.abstract_rule_proof (Context.certificate_theory cert) T U (b, x) f g prf;
  1497           shyps = Sorts.union sorts shyps,
  1515         in
  1498           hyps = hyps,
  1516           Thm (deriv_rule1 (Proofterm.abstract_rule_proof (b, x), zprf) der,
  1499           tpairs = tpairs,
  1517            {cert = cert,
  1500           prop = Logic.mk_equals
  1518             tags = [],
  1501             (Abs (b, T, abstract_over (x, t)), Abs (b, T, abstract_over (x, u)))});
  1519             maxidx = maxidx,
       
  1520             constraints = constraints,
       
  1521             shyps = Sorts.union sorts shyps,
       
  1522             hyps = hyps,
       
  1523             tpairs = tpairs,
       
  1524             prop = Logic.mk_equals (f, g)})
       
  1525         end;
  1502   in
  1526   in
  1503     (case x of
  1527     (case x of
  1504       Free (a, _) => check_result a hyps
  1528       Free (a, _) => check_result a hyps
  1505     | Var ((a, _), _) => check_result a []
  1529     | Var ((a, _), _) => check_result a []
  1506     | _ => raise THM ("abstract_rule: not a variable", 0, [th]))
  1530     | _ => raise THM ("abstract_rule: not a variable", 0, [th]))
  1515   let
  1539   let
  1516     val Thm (der1, {maxidx = maxidx1, constraints = constraints1, shyps = shyps1,
  1540     val Thm (der1, {maxidx = maxidx1, constraints = constraints1, shyps = shyps1,
  1517         hyps = hyps1, tpairs = tpairs1, prop = prop1, ...}) = th1
  1541         hyps = hyps1, tpairs = tpairs1, prop = prop1, ...}) = th1
  1518     and Thm (der2, {maxidx = maxidx2, constraints = constraints2, shyps = shyps2,
  1542     and Thm (der2, {maxidx = maxidx2, constraints = constraints2, shyps = shyps2,
  1519         hyps = hyps2, tpairs = tpairs2, prop = prop2, ...}) = th2;
  1543         hyps = hyps2, tpairs = tpairs2, prop = prop2, ...}) = th2;
  1520     fun chktypes fT tT =
       
  1521       (case fT of
       
  1522         Type ("fun", [T1, _]) =>
       
  1523           if T1 <> tT then
       
  1524             raise THM ("combination: types", 0, [th1, th2])
       
  1525           else ()
       
  1526       | _ => raise THM ("combination: not function type", 0, [th1, th2]));
       
  1527   in
  1544   in
  1528     (case (prop1, prop2) of
  1545     (case (prop1, prop2) of
  1529       (Const ("Pure.eq", Type ("fun", [fT, _])) $ f $ g,
  1546       (Const ("Pure.eq", Type ("fun", [fT, _])) $ f $ g,
  1530        Const ("Pure.eq", Type ("fun", [tT, _])) $ t $ u) =>
  1547        Const ("Pure.eq", Type ("fun", [tT, _])) $ t $ u) =>
  1531         (chktypes fT tT;
  1548         let
  1532           Thm (deriv_rule2 (Proofterm.combination_proof f g t u, K ZTerm.todo_proof) der1 der2,
  1549           val U =
  1533            {cert = join_certificate2 (th1, th2),
  1550             (case fT of
       
  1551               Type ("fun", [T1, U]) =>
       
  1552                 if T1 = tT then U
       
  1553                 else raise THM ("combination: types", 0, [th1, th2])
       
  1554             | _ => raise THM ("combination: not function type", 0, [th1, th2]));
       
  1555           val cert = join_certificate2 (th1, th2);
       
  1556           fun zprf prf1 prf2 =
       
  1557             ZTerm.combination_proof (Context.certificate_theory cert) fT U f g t u prf1 prf2;
       
  1558         in
       
  1559           Thm (deriv_rule2 (Proofterm.combination_proof f g t u, zprf) der1 der2,
       
  1560            {cert = cert,
  1534             tags = [],
  1561             tags = [],
  1535             maxidx = Int.max (maxidx1, maxidx2),
  1562             maxidx = Int.max (maxidx1, maxidx2),
  1536             constraints = union_constraints constraints1 constraints2,
  1563             constraints = union_constraints constraints1 constraints2,
  1537             shyps = Sorts.union shyps1 shyps2,
  1564             shyps = Sorts.union shyps1 shyps2,
  1538             hyps = union_hyps hyps1 hyps2,
  1565             hyps = union_hyps hyps1 hyps2,
  1539             tpairs = union_tpairs tpairs1 tpairs2,
  1566             tpairs = union_tpairs tpairs1 tpairs2,
  1540             prop = Logic.mk_equals (f $ t, g $ u)}))
  1567             prop = Logic.mk_equals (f $ t, g $ u)})
       
  1568         end
  1541      | _ => raise THM ("combination: premises", 0, [th1, th2]))
  1569      | _ => raise THM ("combination: premises", 0, [th1, th2]))
  1542   end;
  1570   end;
  1543 
  1571 
  1544 (*Equality introduction
  1572 (*Equality introduction
  1545   A \<Longrightarrow> B  B \<Longrightarrow> A
  1573   A \<Longrightarrow> B  B \<Longrightarrow> A
  1555     fun err msg = raise THM ("equal_intr: " ^ msg, 0, [th1, th2]);
  1583     fun err msg = raise THM ("equal_intr: " ^ msg, 0, [th1, th2]);
  1556   in
  1584   in
  1557     (case (prop1, prop2) of
  1585     (case (prop1, prop2) of
  1558       (Const("Pure.imp", _) $ A $ B, Const("Pure.imp", _) $ B' $ A') =>
  1586       (Const("Pure.imp", _) $ A $ B, Const("Pure.imp", _) $ B' $ A') =>
  1559         if A aconv A' andalso B aconv B' then
  1587         if A aconv A' andalso B aconv B' then
  1560           Thm (deriv_rule2 (Proofterm.equal_intr_proof A B, K ZTerm.todo_proof) der1 der2,
  1588           let
  1561            {cert = join_certificate2 (th1, th2),
  1589             val cert = join_certificate2 (th1, th2);
  1562             tags = [],
  1590             fun zprf prf1 prf2 =
  1563             maxidx = Int.max (maxidx1, maxidx2),
  1591               ZTerm.equal_intr_proof (Context.certificate_theory cert) A B prf1 prf2;
  1564             constraints = union_constraints constraints1 constraints2,
  1592           in
  1565             shyps = Sorts.union shyps1 shyps2,
  1593             Thm (deriv_rule2 (Proofterm.equal_intr_proof A B, zprf) der1 der2,
  1566             hyps = union_hyps hyps1 hyps2,
  1594              {cert = cert,
  1567             tpairs = union_tpairs tpairs1 tpairs2,
  1595               tags = [],
  1568             prop = Logic.mk_equals (A, B)})
  1596               maxidx = Int.max (maxidx1, maxidx2),
       
  1597               constraints = union_constraints constraints1 constraints2,
       
  1598               shyps = Sorts.union shyps1 shyps2,
       
  1599               hyps = union_hyps hyps1 hyps2,
       
  1600               tpairs = union_tpairs tpairs1 tpairs2,
       
  1601               prop = Logic.mk_equals (A, B)})
       
  1602           end
  1569         else err "not equal"
  1603         else err "not equal"
  1570     | _ =>  err "premises")
  1604     | _ =>  err "premises")
  1571   end;
  1605   end;
  1572 
  1606 
  1573 (*The equal propositions rule
  1607 (*The equal propositions rule
  1584     fun err msg = raise THM ("equal_elim: " ^ msg, 0, [th1, th2]);
  1618     fun err msg = raise THM ("equal_elim: " ^ msg, 0, [th1, th2]);
  1585   in
  1619   in
  1586     (case prop1 of
  1620     (case prop1 of
  1587       Const ("Pure.eq", _) $ A $ B =>
  1621       Const ("Pure.eq", _) $ A $ B =>
  1588         if prop2 aconv A then
  1622         if prop2 aconv A then
  1589           Thm (deriv_rule2 (Proofterm.equal_elim_proof A B, K ZTerm.todo_proof) der1 der2,
  1623           let
  1590            {cert = join_certificate2 (th1, th2),
  1624             val cert = join_certificate2 (th1, th2);
  1591             tags = [],
  1625             fun zprf prf1 prf2 =
  1592             maxidx = Int.max (maxidx1, maxidx2),
  1626               ZTerm.equal_elim_proof (Context.certificate_theory cert) A B prf1 prf2;
  1593             constraints = union_constraints constraints1 constraints2,
  1627           in
  1594             shyps = Sorts.union shyps1 shyps2,
  1628             Thm (deriv_rule2 (Proofterm.equal_elim_proof A B, zprf) der1 der2,
  1595             hyps = union_hyps hyps1 hyps2,
  1629              {cert = cert,
  1596             tpairs = union_tpairs tpairs1 tpairs2,
  1630               tags = [],
  1597             prop = B})
  1631               maxidx = Int.max (maxidx1, maxidx2),
       
  1632               constraints = union_constraints constraints1 constraints2,
       
  1633               shyps = Sorts.union shyps1 shyps2,
       
  1634               hyps = union_hyps hyps1 hyps2,
       
  1635               tpairs = union_tpairs tpairs1 tpairs2,
       
  1636               prop = B})
       
  1637           end
  1598         else err "not equal"
  1638         else err "not equal"
  1599      | _ =>  err "major premise")
  1639      | _ =>  err "major premise")
  1600   end;
  1640   end;
  1601 
  1641 
  1602 
  1642