src/HOL/Import/proof_kernel.ML
changeset 17894 f2fdd22accaa
parent 17657 2f5f595eb618
child 17917 3c6e7760da89
equal deleted inserted replaced
17893:aef5a6d11c2a 17894:f2fdd22accaa
   383 
   383 
   384 fun mk_defeq name rhs thy =
   384 fun mk_defeq name rhs thy =
   385     let
   385     let
   386 	val ty = type_of rhs
   386 	val ty = type_of rhs
   387     in
   387     in
   388 	Logic.mk_equals (Const(Sign.intern_const (sign_of thy) name,ty),rhs)
   388 	Logic.mk_equals (Const(Sign.intern_const thy name,ty),rhs)
   389     end
   389     end
   390 
   390 
   391 fun mk_teq name rhs thy =
   391 fun mk_teq name rhs thy =
   392     let
   392     let
   393 	val ty = type_of rhs
   393 	val ty = type_of rhs
   394     in
   394     in
   395 	HOLogic.mk_eq (Const(Sign.intern_const (sign_of thy) name,ty),rhs)
   395 	HOLogic.mk_eq (Const(Sign.intern_const thy name,ty),rhs)
   396     end
   396     end
   397 
   397 
   398 fun intern_const_name thyname const thy =
   398 fun intern_const_name thyname const thy =
   399     case get_hol4_const_mapping thyname const thy of
   399     case get_hol4_const_mapping thyname const thy of
   400 	SOME (_,cname,_) => cname
   400 	SOME (_,cname,_) => cname
   401       | NONE => (case get_hol4_const_renaming thyname const thy of
   401       | NONE => (case get_hol4_const_renaming thyname const thy of
   402 		     SOME cname => Sign.intern_const (sign_of thy) (thyname ^ "." ^ cname)
   402 		     SOME cname => Sign.intern_const thy (thyname ^ "." ^ cname)
   403 		   | NONE => Sign.intern_const (sign_of thy) (thyname ^ "." ^ const))
   403 		   | NONE => Sign.intern_const thy (thyname ^ "." ^ const))
   404 
   404 
   405 fun intern_type_name thyname const thy =
   405 fun intern_type_name thyname const thy =
   406     case get_hol4_type_mapping thyname const thy of
   406     case get_hol4_type_mapping thyname const thy of
   407 	SOME (_,cname) => cname
   407 	SOME (_,cname) => cname
   408       | NONE => Sign.intern_const (sign_of thy) (thyname ^ "." ^ const)
   408       | NONE => Sign.intern_const thy (thyname ^ "." ^ const)
   409 
   409 
   410 fun mk_vartype name = TFree(name,["HOL.type"])
   410 fun mk_vartype name = TFree(name,["HOL.type"])
   411 fun mk_thy_type thy Thy Tyop Args = Type(intern_type_name Thy Tyop thy,Args)
   411 fun mk_thy_type thy Thy Tyop Args = Type(intern_type_name Thy Tyop thy,Args)
   412 
   412 
   413 val mk_var = Free
   413 val mk_var = Free
   416   | dom_rng _ = raise ERR "dom_rng" "Not a functional type"
   416   | dom_rng _ = raise ERR "dom_rng" "Not a functional type"
   417 
   417 
   418 fun mk_thy_const thy Thy Nam Ty = Const(intern_const_name Thy Nam thy,Ty)
   418 fun mk_thy_const thy Thy Nam Ty = Const(intern_const_name Thy Nam thy,Ty)
   419 
   419 
   420 local 
   420 local 
   421     (* fun get_const sg thyname name = 
   421   fun get_const sg thyname name = 
   422        (case term_of (read_cterm sg (name, TypeInfer.logicT)) of
   422     (case Sign.const_type sg name of
   423           c as Const _ => c)
   423       SOME ty => Const (name, ty)
   424        handle _ => raise ERR "get_type" (name ^ ": No such constant")*)
   424     | NONE => raise ERR "get_type" (name ^ ": No such constant"))
   425       fun get_const sg thyname name = 
       
   426        (case Sign.const_type sg name of
       
   427           SOME ty => Const (name, ty)
       
   428         | NONE => raise ERR "get_type" (name ^ ": No such constant"))
       
   429 in
   425 in
   430 fun prim_mk_const thy Thy Nam =
   426 fun prim_mk_const thy Thy Nam =
   431     let
   427     let
   432 	val name = intern_const_name Thy Nam thy
   428       val name = intern_const_name Thy Nam thy
   433 	val cmaps = HOL4ConstMaps.get thy
   429       val cmaps = HOL4ConstMaps.get thy
   434     in
   430     in
   435 	case StringPair.lookup cmaps (Thy,Nam) of
   431       case StringPair.lookup cmaps (Thy,Nam) of
   436 	    SOME(_,_,SOME ty) => Const(name,ty)
   432         SOME(_,_,SOME ty) => Const(name,ty)
   437 	  | _ => get_const (sign_of thy) Thy name
   433       | _ => get_const thy Thy name
   438     end
   434     end
   439 end
   435 end
   440 
   436 
   441 fun mk_comb(f,a) = f $ a
   437 fun mk_comb(f,a) = f $ a
   442 
   438 
   983 val disj1_thm = thm "disjI1"
   979 val disj1_thm = thm "disjI1"
   984 val disj2_thm = thm "disjI2"
   980 val disj2_thm = thm "disjI2"
   985 
   981 
   986 local
   982 local
   987     val th = thm "not_def"
   983     val th = thm "not_def"
   988     val sg = sign_of_thm th
   984     val thy = theory_of_thm th
   989     val pp = reflexive (cterm_of sg (Const("Trueprop",boolT-->propT)))
   985     val pp = reflexive (cterm_of thy (Const("Trueprop",boolT-->propT)))
   990 in
   986 in
   991 val not_elim_thm = combination pp th
   987 val not_elim_thm = combination pp th
   992 end
   988 end
   993 
   989 
   994 val not_intro_thm = symmetric not_elim_thm
   990 val not_intro_thm = symmetric not_elim_thm
  1229     end
  1225     end
  1230     handle _ => NONE
  1226     handle _ => NONE
  1231 
  1227 
  1232 fun rewrite_hol4_term t thy =
  1228 fun rewrite_hol4_term t thy =
  1233     let
  1229     let
  1234 	val sg = sign_of thy
  1230 	val hol4rews1 = map (Thm.transfer thy) (HOL4Rewrites.get thy)
  1235 
  1231 	val hol4ss = Simplifier.theory_context thy empty_ss
  1236 	val hol4rews1 = map (Thm.transfer sg) (HOL4Rewrites.get thy)
  1232           setmksimps single addsimps hol4rews1
  1237 	val hol4ss = empty_ss setmksimps single addsimps hol4rews1
  1233     in
  1238     in
  1234 	Thm.transfer thy (Simplifier.full_rewrite hol4ss (cterm_of thy t))
  1239 	Thm.transfer sg (Simplifier.full_rewrite hol4ss (cterm_of sg t))
       
  1240     end
  1235     end
  1241 
  1236 
  1242 fun get_isabelle_thm thyname thmname hol4conc thy =
  1237 fun get_isabelle_thm thyname thmname hol4conc thy =
  1243     let
  1238     let
  1244 	val sg = sign_of thy
       
  1245 
       
  1246 	val (info,hol4conc') = disamb_term hol4conc
  1239 	val (info,hol4conc') = disamb_term hol4conc
  1247 	val i2h_conc = symmetric (rewrite_hol4_term (HOLogic.mk_Trueprop hol4conc') thy)
  1240 	val i2h_conc = symmetric (rewrite_hol4_term (HOLogic.mk_Trueprop hol4conc') thy)
  1248 	val isaconc =
  1241 	val isaconc =
  1249 	    case concl_of i2h_conc of
  1242 	    case concl_of i2h_conc of
  1250 		Const("==",_) $ lhs $ _ => lhs
  1243 		Const("==",_) $ lhs $ _ => lhs
  1301 fun get_isabelle_thm_and_warn thyname thmname hol4conc thy =
  1294 fun get_isabelle_thm_and_warn thyname thmname hol4conc thy =
  1302   let
  1295   let
  1303     val (a, b) = get_isabelle_thm thyname thmname hol4conc thy
  1296     val (a, b) = get_isabelle_thm thyname thmname hol4conc thy
  1304     fun warn () =
  1297     fun warn () =
  1305         let
  1298         let
  1306             val sg = sign_of thy		     
       
  1307 	    val (info,hol4conc') = disamb_term hol4conc
  1299 	    val (info,hol4conc') = disamb_term hol4conc
  1308 	    val i2h_conc = symmetric (rewrite_hol4_term (HOLogic.mk_Trueprop hol4conc') thy)
  1300 	    val i2h_conc = symmetric (rewrite_hol4_term (HOLogic.mk_Trueprop hol4conc') thy)
  1309 	in
  1301 	in
  1310 	    case concl_of i2h_conc of
  1302 	    case concl_of i2h_conc of
  1311 		Const("==",_) $ lhs $ _ => 
  1303 		Const("==",_) $ lhs $ _ => 
  1350 	arg as (_,SOME _) => arg
  1342 	arg as (_,SOME _) => arg
  1351       | _ => raise ERR "get_axiom" ("Trying to retrieve axiom (" ^ axname ^ ")")
  1343       | _ => raise ERR "get_axiom" ("Trying to retrieve axiom (" ^ axname ^ ")")
  1352 
  1344 
  1353 fun intern_store_thm gen_output thyname thmname hth thy =
  1345 fun intern_store_thm gen_output thyname thmname hth thy =
  1354     let
  1346     let
  1355 	val sg = sign_of thy
  1347 	val (hth' as HOLThm (args as (_,th))) = norm_hthm thy hth
  1356 	val (hth' as HOLThm (args as (_,th))) = norm_hthm sg hth
       
  1357 	val rew = rewrite_hol4_term (concl_of th) thy
  1348 	val rew = rewrite_hol4_term (concl_of th) thy
  1358 	val th  = equal_elim rew th
  1349 	val th  = equal_elim rew th
  1359 	val thy' = add_hol4_pending thyname thmname args thy
  1350 	val thy' = add_hol4_pending thyname thmname args thy
  1360         val th = disambiguate_frees th
  1351         val th = disambiguate_frees th
  1361 	val thy2 = if gen_output
  1352 	val thy2 = if gen_output
  1378 
  1369 
  1379 fun REFL tm thy =
  1370 fun REFL tm thy =
  1380     let
  1371     let
  1381 	val _ = message "REFL:"
  1372 	val _ = message "REFL:"
  1382 	val (info,tm') = disamb_term tm
  1373 	val (info,tm') = disamb_term tm
  1383 	val sg = sign_of thy
  1374 	val ctm = Thm.cterm_of thy tm'
  1384 	val ctm = Thm.cterm_of sg tm'
       
  1385 	val res = HOLThm(rens_of info,mk_REFL ctm)
  1375 	val res = HOLThm(rens_of info,mk_REFL ctm)
  1386 	val _ = if_debug pth res
  1376 	val _ = if_debug pth res
  1387     in
  1377     in
  1388 	(thy,res)
  1378 	(thy,res)
  1389     end
  1379     end
  1390 
  1380 
  1391 fun ASSUME tm thy =
  1381 fun ASSUME tm thy =
  1392     let
  1382     let
  1393 	val _ = message "ASSUME:"
  1383 	val _ = message "ASSUME:"
  1394 	val (info,tm') = disamb_term tm
  1384 	val (info,tm') = disamb_term tm
  1395 	val sg = sign_of thy
  1385 	val ctm = Thm.cterm_of thy (HOLogic.mk_Trueprop tm')
  1396 	val ctm = Thm.cterm_of sg (HOLogic.mk_Trueprop tm')
       
  1397 	val th = Thm.trivial ctm
  1386 	val th = Thm.trivial ctm
  1398 	val res = HOLThm(rens_of info,th)
  1387 	val res = HOLThm(rens_of info,th)
  1399 	val _ = if_debug pth res
  1388 	val _ = if_debug pth res
  1400     in
  1389     in
  1401 	(thy,res)
  1390 	(thy,res)
  1403 
  1392 
  1404 fun INST_TYPE lambda (hth as HOLThm(rens,th)) thy =
  1393 fun INST_TYPE lambda (hth as HOLThm(rens,th)) thy =
  1405     let
  1394     let
  1406 	val _ = message "INST_TYPE:"
  1395 	val _ = message "INST_TYPE:"
  1407 	val _ = if_debug pth hth
  1396 	val _ = if_debug pth hth
  1408 	val sg = sign_of thy
       
  1409 	val tys_before = add_term_tfrees (prop_of th,[])
  1397 	val tys_before = add_term_tfrees (prop_of th,[])
  1410 	val th1 = varifyT th
  1398 	val th1 = varifyT th
  1411 	val tys_after = add_term_tvars (prop_of th1,[])
  1399 	val tys_after = add_term_tvars (prop_of th1,[])
  1412 	val tyinst = map (fn (bef, iS) =>
  1400 	val tyinst = map (fn (bef, iS) =>
  1413 			     (case try (Lib.assoc (TFree bef)) lambda of
  1401 			     (case try (Lib.assoc (TFree bef)) lambda of
  1414 				  SOME ty => (ctyp_of sg (TVar iS), ctyp_of sg ty)
  1402 				  SOME ty => (ctyp_of thy (TVar iS), ctyp_of thy ty)
  1415 				| NONE => (ctyp_of sg (TVar iS), ctyp_of sg (TFree bef))
  1403 				| NONE => (ctyp_of thy (TVar iS), ctyp_of thy (TFree bef))
  1416 			     ))
  1404 			     ))
  1417 			 (zip tys_before tys_after)
  1405 			 (zip tys_before tys_after)
  1418 	val res = Drule.instantiate (tyinst,[]) th1
  1406 	val res = Drule.instantiate (tyinst,[]) th1
  1419 	val hth = HOLThm([],res)
  1407 	val hth = HOLThm([],res)
  1420 	val res = norm_hthm sg hth
  1408 	val res = norm_hthm thy hth
  1421 	val _ = message "RESULT:"
  1409 	val _ = message "RESULT:"
  1422 	val _ = if_debug pth res
  1410 	val _ = if_debug pth res
  1423     in
  1411     in
  1424 	(thy,res)
  1412 	(thy,res)
  1425     end
  1413     end
  1427 fun INST sigma hth thy =
  1415 fun INST sigma hth thy =
  1428     let
  1416     let
  1429 	val _ = message "INST:"
  1417 	val _ = message "INST:"
  1430 	val _ = if_debug (app (fn (x,y) => (prin x; prin y))) sigma
  1418 	val _ = if_debug (app (fn (x,y) => (prin x; prin y))) sigma
  1431 	val _ = if_debug pth hth
  1419 	val _ = if_debug pth hth
  1432 	val sg = sign_of thy
       
  1433 	val (sdom,srng) = ListPair.unzip (rev sigma)
  1420 	val (sdom,srng) = ListPair.unzip (rev sigma)
  1434 	val th = hthm2thm hth
  1421 	val th = hthm2thm hth
  1435 	val th1 = mk_INST (map (cterm_of sg) sdom) (map (cterm_of sg) srng) th
  1422 	val th1 = mk_INST (map (cterm_of thy) sdom) (map (cterm_of thy) srng) th
  1436 	val res = HOLThm([],th1)
  1423 	val res = HOLThm([],th1)
  1437 	val _ = message "RESULT:"
  1424 	val _ = message "RESULT:"
  1438 	val _ = if_debug pth res
  1425 	val _ = if_debug pth res
  1439     in
  1426     in
  1440 	(thy,res)
  1427 	(thy,res)
  1464 	val _ = if_debug pth res
  1451 	val _ = if_debug pth res
  1465     in
  1452     in
  1466 	(thy,res)
  1453 	(thy,res)
  1467     end
  1454     end
  1468 
  1455 
  1469 fun mk_COMB th1 th2 sg =
  1456 fun mk_COMB th1 th2 thy =
  1470     let
  1457     let
  1471 	val (f,g) = case concl_of th1 of
  1458 	val (f,g) = case concl_of th1 of
  1472 			_ $ (Const("op =",_) $ f $ g) => (f,g)
  1459 			_ $ (Const("op =",_) $ f $ g) => (f,g)
  1473 		      | _ => raise ERR "mk_COMB" "First theorem not an equality"
  1460 		      | _ => raise ERR "mk_COMB" "First theorem not an equality"
  1474 	val (x,y) = case concl_of th2 of
  1461 	val (x,y) = case concl_of th2 of
  1475 			_ $ (Const("op =",_) $ x $ y) => (x,y)
  1462 			_ $ (Const("op =",_) $ x $ y) => (x,y)
  1476 		      | _ => raise ERR "mk_COMB" "Second theorem not an equality"
  1463 		      | _ => raise ERR "mk_COMB" "Second theorem not an equality"
  1477 	val fty = type_of f
  1464 	val fty = type_of f
  1478 	val (fd,fr) = dom_rng fty
  1465 	val (fd,fr) = dom_rng fty
  1479 	val comb_thm' = Drule.instantiate'
  1466 	val comb_thm' = Drule.instantiate'
  1480 			    [SOME (ctyp_of sg fd),SOME (ctyp_of sg fr)]
  1467 			    [SOME (ctyp_of thy fd),SOME (ctyp_of thy fr)]
  1481 			    [SOME (cterm_of sg f),SOME (cterm_of sg g),
  1468 			    [SOME (cterm_of thy f),SOME (cterm_of thy g),
  1482 			     SOME (cterm_of sg x),SOME (cterm_of sg y)] comb_thm
  1469 			     SOME (cterm_of thy x),SOME (cterm_of thy y)] comb_thm
  1483     in
  1470     in
  1484 	[th1,th2] MRS comb_thm'
  1471 	[th1,th2] MRS comb_thm'
  1485     end
  1472     end
  1486 
  1473 
  1487 fun SUBST rews ctxt hth thy =
  1474 fun SUBST rews ctxt hth thy =
  1492 	val _ = if_debug pth hth
  1479 	val _ = if_debug pth hth
  1493 	val (info,th) = disamb_thm hth
  1480 	val (info,th) = disamb_thm hth
  1494 	val (info1,ctxt') = disamb_term_from info ctxt
  1481 	val (info1,ctxt') = disamb_term_from info ctxt
  1495 	val (info2,rews') = disamb_thms_from info1 rews
  1482 	val (info2,rews') = disamb_thms_from info1 rews
  1496 
  1483 
  1497 	val sg = sign_of thy
  1484 	val cctxt = cterm_of thy ctxt'
  1498 	val cctxt = cterm_of sg ctxt'
       
  1499 	fun subst th [] = th
  1485 	fun subst th [] = th
  1500 	  | subst th (rew::rews) = subst (mk_COMB th rew sg) rews
  1486 	  | subst th (rew::rews) = subst (mk_COMB th rew thy) rews
  1501 	val res = HOLThm(rens_of info2,mk_EQ_MP (subst (mk_REFL cctxt) rews') th)
  1487 	val res = HOLThm(rens_of info2,mk_EQ_MP (subst (mk_REFL cctxt) rews') th)
  1502 	val _ = message "RESULT:"
  1488 	val _ = message "RESULT:"
  1503 	val _ = if_debug pth res
  1489 	val _ = if_debug pth res
  1504     in
  1490     in
  1505 	(thy,res)
  1491 	(thy,res)
  1510 	val _ = message "DISJ_CASES:"
  1496 	val _ = message "DISJ_CASES:"
  1511 	val _ = if_debug (app pth) [hth,hth1,hth2]
  1497 	val _ = if_debug (app pth) [hth,hth1,hth2]
  1512 	val (info,th) = disamb_thm hth
  1498 	val (info,th) = disamb_thm hth
  1513 	val (info1,th1) = disamb_thm_from info hth1
  1499 	val (info1,th1) = disamb_thm_from info hth1
  1514 	val (info2,th2) = disamb_thm_from info1 hth2
  1500 	val (info2,th2) = disamb_thm_from info1 hth2
  1515 	val sg = sign_of thy
       
  1516 	val th1 = norm_hyps th1
  1501 	val th1 = norm_hyps th1
  1517 	val th2 = norm_hyps th2
  1502 	val th2 = norm_hyps th2
  1518 	val (l,r) = case concl_of th of
  1503 	val (l,r) = case concl_of th of
  1519 			_ $ (Const("op |",_) $ l $ r) => (l,r)
  1504 			_ $ (Const("op |",_) $ l $ r) => (l,r)
  1520 		      | _ => raise ERR "DISJ_CASES" "Conclusion not a disjunction"
  1505 		      | _ => raise ERR "DISJ_CASES" "Conclusion not a disjunction"
  1521 	val th1' = rearrange sg (HOLogic.mk_Trueprop l) th1
  1506 	val th1' = rearrange thy (HOLogic.mk_Trueprop l) th1
  1522 	val th2' = rearrange sg (HOLogic.mk_Trueprop r) th2
  1507 	val th2' = rearrange thy (HOLogic.mk_Trueprop r) th2
  1523 	val res1 = th RS disj_cases_thm
  1508 	val res1 = th RS disj_cases_thm
  1524 	val res2 = uniq_compose ((nprems_of th1')-1) th1' ((nprems_of th)+1) res1
  1509 	val res2 = uniq_compose ((nprems_of th1')-1) th1' ((nprems_of th)+1) res1
  1525 	val res3 = uniq_compose ((nprems_of th2')-1) th2' (nprems_of res2) res2
  1510 	val res3 = uniq_compose ((nprems_of th2')-1) th2' (nprems_of res2) res2
  1526 	val res  = HOLThm(rens_of info2,res3)
  1511 	val res  = HOLThm(rens_of info2,res3)
  1527 	val _ = message "RESULT:"
  1512 	val _ = message "RESULT:"
  1535 	val _ = message "DISJ1:"
  1520 	val _ = message "DISJ1:"
  1536 	val _ = if_debug pth hth
  1521 	val _ = if_debug pth hth
  1537 	val _ = if_debug prin tm
  1522 	val _ = if_debug prin tm
  1538 	val (info,th) = disamb_thm hth
  1523 	val (info,th) = disamb_thm hth
  1539 	val (info',tm') = disamb_term_from info tm
  1524 	val (info',tm') = disamb_term_from info tm
  1540 	val sg = sign_of thy
  1525 	val ct = Thm.cterm_of thy tm'
  1541 	val ct = Thm.cterm_of sg tm'
       
  1542 	val disj1_thm' = Drule.instantiate' [] [NONE,SOME ct] disj1_thm
  1526 	val disj1_thm' = Drule.instantiate' [] [NONE,SOME ct] disj1_thm
  1543 	val res = HOLThm(rens_of info',th RS disj1_thm')
  1527 	val res = HOLThm(rens_of info',th RS disj1_thm')
  1544 	val _ = message "RESULT:"
  1528 	val _ = message "RESULT:"
  1545 	val _ = if_debug pth res
  1529 	val _ = if_debug pth res
  1546     in
  1530     in
  1552 	val _ = message "DISJ1:"
  1536 	val _ = message "DISJ1:"
  1553 	val _ = if_debug prin tm
  1537 	val _ = if_debug prin tm
  1554 	val _ = if_debug pth hth
  1538 	val _ = if_debug pth hth
  1555 	val (info,th) = disamb_thm hth
  1539 	val (info,th) = disamb_thm hth
  1556 	val (info',tm') = disamb_term_from info tm
  1540 	val (info',tm') = disamb_term_from info tm
  1557 	val sg = sign_of thy
  1541 	val ct = Thm.cterm_of thy tm'
  1558 	val ct = Thm.cterm_of sg tm'
       
  1559 	val disj2_thm' = Drule.instantiate' [] [NONE,SOME ct] disj2_thm
  1542 	val disj2_thm' = Drule.instantiate' [] [NONE,SOME ct] disj2_thm
  1560 	val res = HOLThm(rens_of info',th RS disj2_thm')
  1543 	val res = HOLThm(rens_of info',th RS disj2_thm')
  1561 	val _ = message "RESULT:"
  1544 	val _ = message "RESULT:"
  1562 	val _ = if_debug pth res
  1545 	val _ = if_debug pth res
  1563     in
  1546     in
  1646 	val _ = if_debug prin ex
  1629 	val _ = if_debug prin ex
  1647 	val _ = if_debug prin wit
  1630 	val _ = if_debug prin wit
  1648 	val _ = if_debug pth hth
  1631 	val _ = if_debug pth hth
  1649 	val (info,th) = disamb_thm hth
  1632 	val (info,th) = disamb_thm hth
  1650 	val (info',[ex',wit']) = disamb_terms_from info [ex,wit]
  1633 	val (info',[ex',wit']) = disamb_terms_from info [ex,wit]
  1651 	val sg = sign_of thy
  1634 	val cwit = cterm_of thy wit'
  1652 	val cwit = cterm_of sg wit'
       
  1653 	val cty = ctyp_of_term cwit
  1635 	val cty = ctyp_of_term cwit
  1654 	val a = case ex' of
  1636 	val a = case ex' of
  1655 		    (Const("Ex",_) $ a) => a
  1637 		    (Const("Ex",_) $ a) => a
  1656 		  | _ => raise ERR "EXISTS" "Argument not existential"
  1638 		  | _ => raise ERR "EXISTS" "Argument not existential"
  1657 	val ca = cterm_of sg a
  1639 	val ca = cterm_of thy a
  1658 	val exists_thm' = beta_eta_thm (Drule.instantiate' [SOME cty] [SOME ca,SOME cwit] exists_thm)
  1640 	val exists_thm' = beta_eta_thm (Drule.instantiate' [SOME cty] [SOME ca,SOME cwit] exists_thm)
  1659 	val th1 = beta_eta_thm th
  1641 	val th1 = beta_eta_thm th
  1660 	val th2 = implies_elim_all th1
  1642 	val th2 = implies_elim_all th1
  1661 	val th3 = th2 COMP exists_thm'
  1643 	val th3 = th2 COMP exists_thm'
  1662 	val th  = implies_intr_hyps th3
  1644 	val th  = implies_intr_hyps th3
  1677 	val (info',v') = disamb_term_from info v
  1659 	val (info',v') = disamb_term_from info v
  1678 	fun strip 0 _ th = th
  1660 	fun strip 0 _ th = th
  1679 	  | strip n (p::ps) th =
  1661 	  | strip n (p::ps) th =
  1680 	    strip (n-1) ps (implies_elim th (assume p))
  1662 	    strip (n-1) ps (implies_elim th (assume p))
  1681 	  | strip _ _ _ = raise ERR "CHOOSE" "strip error"
  1663 	  | strip _ _ _ = raise ERR "CHOOSE" "strip error"
  1682 	val sg = sign_of thy
  1664 	val cv = cterm_of thy v'
  1683 	val cv = cterm_of sg v'
       
  1684 	val th2 = norm_hyps th2
  1665 	val th2 = norm_hyps th2
  1685 	val cvty = ctyp_of_term cv
  1666 	val cvty = ctyp_of_term cv
  1686 	val c = HOLogic.dest_Trueprop (concl_of th2)
  1667 	val c = HOLogic.dest_Trueprop (concl_of th2)
  1687 	val cc = cterm_of sg c
  1668 	val cc = cterm_of thy c
  1688 	val a = case concl_of th1 of
  1669 	val a = case concl_of th1 of
  1689 		    _ $ (Const("Ex",_) $ a) => a
  1670 		    _ $ (Const("Ex",_) $ a) => a
  1690 		  | _ => raise ERR "CHOOSE" "Conclusion not existential"
  1671 		  | _ => raise ERR "CHOOSE" "Conclusion not existential"
  1691 	val ca = cterm_of (sign_of_thm th1) a
  1672 	val ca = cterm_of (theory_of_thm th1) a
  1692 	val choose_thm' = beta_eta_thm (Drule.instantiate' [SOME cvty] [SOME ca,SOME cc] choose_thm)
  1673 	val choose_thm' = beta_eta_thm (Drule.instantiate' [SOME cvty] [SOME ca,SOME cc] choose_thm)
  1693 	val th21 = rearrange sg (HOLogic.mk_Trueprop (a $ v')) th2
  1674 	val th21 = rearrange thy (HOLogic.mk_Trueprop (a $ v')) th2
  1694 	val th22 = strip ((nprems_of th21)-1) (cprems_of th21) th21
  1675 	val th22 = strip ((nprems_of th21)-1) (cprems_of th21) th21
  1695 	val th23 = beta_eta_thm (forall_intr cv th22)
  1676 	val th23 = beta_eta_thm (forall_intr cv th22)
  1696 	val th11 = implies_elim_all (beta_eta_thm th1)
  1677 	val th11 = implies_elim_all (beta_eta_thm th1)
  1697 	val th' = th23 COMP (th11 COMP choose_thm')
  1678 	val th' = th23 COMP (th11 COMP choose_thm')
  1698 	val th = implies_intr_hyps th'
  1679 	val th = implies_intr_hyps th'
  1708       val _ = message "GEN:"
  1689       val _ = message "GEN:"
  1709 	val _ = if_debug prin v
  1690 	val _ = if_debug prin v
  1710 	val _ = if_debug pth hth
  1691 	val _ = if_debug pth hth
  1711 	val (info,th) = disamb_thm hth
  1692 	val (info,th) = disamb_thm hth
  1712 	val (info',v') = disamb_term_from info v
  1693 	val (info',v') = disamb_term_from info v
  1713 	val res = HOLThm(rens_of info',mk_GEN v' th (sign_of thy))
  1694 	val res = HOLThm(rens_of info',mk_GEN v' th thy)
  1714 	val _ = message "RESULT:"
  1695 	val _ = message "RESULT:"
  1715 	val _ = if_debug pth res
  1696 	val _ = if_debug pth res
  1716     in
  1697     in
  1717 	(thy,res)
  1698 	(thy,res)
  1718     end
  1699     end
  1722 	val _ = message "SPEC:"
  1703 	val _ = message "SPEC:"
  1723 	val _ = if_debug prin tm
  1704 	val _ = if_debug prin tm
  1724 	val _ = if_debug pth hth
  1705 	val _ = if_debug pth hth
  1725 	val (info,th) = disamb_thm hth
  1706 	val (info,th) = disamb_thm hth
  1726 	val (info',tm') = disamb_term_from info tm
  1707 	val (info',tm') = disamb_term_from info tm
  1727 	val sg = sign_of thy
  1708 	val ctm = Thm.cterm_of thy tm'
  1728 	val ctm = Thm.cterm_of sg tm'
       
  1729 	val cty = Thm.ctyp_of_term ctm
  1709 	val cty = Thm.ctyp_of_term ctm
  1730 	val spec' = Drule.instantiate' [SOME cty] [NONE,SOME ctm] spec_thm
  1710 	val spec' = Drule.instantiate' [SOME cty] [NONE,SOME ctm] spec_thm
  1731 	val th = th RS spec'
  1711 	val th = th RS spec'
  1732 	val res = HOLThm(rens_of info',th)
  1712 	val res = HOLThm(rens_of info',th)
  1733 	val _ = message "RESULT:"
  1713 	val _ = message "RESULT:"
  1740     let
  1720     let
  1741 	val _ = message "COMB:"
  1721 	val _ = message "COMB:"
  1742 	val _ = if_debug pth hth1
  1722 	val _ = if_debug pth hth1
  1743 	val _ = if_debug pth hth2
  1723 	val _ = if_debug pth hth2
  1744 	val (info,[th1,th2]) = disamb_thms [hth1,hth2]
  1724 	val (info,[th1,th2]) = disamb_thms [hth1,hth2]
  1745 	val sg = sign_of thy
  1725 	val res = HOLThm(rens_of info,mk_COMB th1 th2 thy)
  1746 	val res = HOLThm(rens_of info,mk_COMB th1 th2 sg)
       
  1747 	val _ = message "RESULT:"
  1726 	val _ = message "RESULT:"
  1748 	val _ = if_debug pth res
  1727 	val _ = if_debug pth res
  1749     in
  1728     in
  1750 	(thy,res)
  1729 	(thy,res)
  1751     end
  1730     end
  1771 	val _ = if_debug prin tm
  1750 	val _ = if_debug prin tm
  1772 	val _ = if_debug pth hth
  1751 	val _ = if_debug pth hth
  1773 	val (info,th) = disamb_thm hth
  1752 	val (info,th) = disamb_thm hth
  1774 	val (info',tm') = disamb_term_from info tm
  1753 	val (info',tm') = disamb_term_from info tm
  1775 	val th = norm_hyps th
  1754 	val th = norm_hyps th
  1776 	val sg = sign_of thy
  1755 	val ct = cterm_of thy tm'
  1777 	val ct = cterm_of sg tm'
  1756 	val th1 = rearrange thy (HOLogic.mk_Trueprop (Const("Not",boolT-->boolT) $ tm')) th
  1778 	val th1 = rearrange sg (HOLogic.mk_Trueprop (Const("Not",boolT-->boolT) $ tm')) th
       
  1779 	val ccontr_thm' = Drule.instantiate' [] [SOME ct] ccontr_thm
  1757 	val ccontr_thm' = Drule.instantiate' [] [SOME ct] ccontr_thm
  1780 	val res1 = uniq_compose ((nprems_of th1) - 1) th1 1 ccontr_thm'
  1758 	val res1 = uniq_compose ((nprems_of th1) - 1) th1 1 ccontr_thm'
  1781 	val res = HOLThm(rens_of info',res1)
  1759 	val res = HOLThm(rens_of info',res1)
  1782 	val _ = message "RESULT:"
  1760 	val _ = message "RESULT:"
  1783 	val _ = if_debug pth res
  1761 	val _ = if_debug pth res
  1784     in
  1762     in
  1785 	(thy,res)
  1763 	(thy,res)
  1786     end
  1764     end
  1787 
  1765 
  1788 fun mk_ABS v th sg =
  1766 fun mk_ABS v th thy =
  1789     let
  1767     let
  1790 	val cv = cterm_of sg v
  1768 	val cv = cterm_of thy v
  1791 	val th1 = implies_elim_all (beta_eta_thm th)
  1769 	val th1 = implies_elim_all (beta_eta_thm th)
  1792 	val (f,g) = case concl_of th1 of
  1770 	val (f,g) = case concl_of th1 of
  1793 			_ $ (Const("op =",_) $ f $ g) => (Term.lambda v f,Term.lambda v g)
  1771 			_ $ (Const("op =",_) $ f $ g) => (Term.lambda v f,Term.lambda v g)
  1794 		      | _ => raise ERR "mk_ABS" "Bad conclusion"
  1772 		      | _ => raise ERR "mk_ABS" "Bad conclusion"
  1795 	val (fd,fr) = dom_rng (type_of f)
  1773 	val (fd,fr) = dom_rng (type_of f)
  1796 	val abs_thm' = Drule.instantiate' [SOME (ctyp_of sg fd), SOME (ctyp_of sg fr)] [SOME (cterm_of sg f), SOME (cterm_of sg g)] abs_thm
  1774 	val abs_thm' = Drule.instantiate' [SOME (ctyp_of thy fd), SOME (ctyp_of thy fr)] [SOME (cterm_of thy f), SOME (cterm_of thy g)] abs_thm
  1797 	val th2 = forall_intr cv th1
  1775 	val th2 = forall_intr cv th1
  1798 	val th3 = th2 COMP abs_thm'
  1776 	val th3 = th2 COMP abs_thm'
  1799 	val res = implies_intr_hyps th3
  1777 	val res = implies_intr_hyps th3
  1800     in
  1778     in
  1801 	res
  1779 	res
  1806 	val _ = message "ABS:"
  1784 	val _ = message "ABS:"
  1807 	val _ = if_debug prin v
  1785 	val _ = if_debug prin v
  1808 	val _ = if_debug pth hth
  1786 	val _ = if_debug pth hth
  1809 	val (info,th) = disamb_thm hth
  1787 	val (info,th) = disamb_thm hth
  1810 	val (info',v') = disamb_term_from info v
  1788 	val (info',v') = disamb_term_from info v
  1811 	val sg = sign_of thy
  1789 	val res = HOLThm(rens_of info',mk_ABS v' th thy)
  1812 	val res = HOLThm(rens_of info',mk_ABS v' th sg)
       
  1813 	val _ = message "RESULT:"
  1790 	val _ = message "RESULT:"
  1814 	val _ = if_debug pth res
  1791 	val _ = if_debug pth res
  1815     in
  1792     in
  1816 	(thy,res)
  1793 	(thy,res)
  1817     end
  1794     end
  1824 		  | NONE => ()
  1801 		  | NONE => ()
  1825 	val _ = if_debug (app prin) vlist
  1802 	val _ = if_debug (app prin) vlist
  1826 	val _ = if_debug pth hth
  1803 	val _ = if_debug pth hth
  1827 	val (info,th) = disamb_thm hth
  1804 	val (info,th) = disamb_thm hth
  1828 	val (info',vlist') = disamb_terms_from info vlist
  1805 	val (info',vlist') = disamb_terms_from info vlist
  1829 	val sg = sign_of thy
       
  1830 	val th1 =
  1806 	val th1 =
  1831 	    case copt of
  1807 	    case copt of
  1832 		SOME (c as Const(cname,cty)) =>
  1808 		SOME (c as Const(cname,cty)) =>
  1833 		let
  1809 		let
  1834 		    fun inst_type ty1 ty2 (TVar _) = raise ERR "GEN_ABS" "Type variable found!"
  1810 		    fun inst_type ty1 ty2 (TVar _) = raise ERR "GEN_ABS" "Type variable found!"
  1841 		    foldr (fn (v,th) =>
  1817 		    foldr (fn (v,th) =>
  1842 			      let
  1818 			      let
  1843 				  val cdom = fst (dom_rng (fst (dom_rng cty)))
  1819 				  val cdom = fst (dom_rng (fst (dom_rng cty)))
  1844 				  val vty  = type_of v
  1820 				  val vty  = type_of v
  1845 				  val newcty = inst_type cdom vty cty
  1821 				  val newcty = inst_type cdom vty cty
  1846 				  val cc = cterm_of sg (Const(cname,newcty))
  1822 				  val cc = cterm_of thy (Const(cname,newcty))
  1847 			      in
  1823 			      in
  1848 				  mk_COMB (mk_REFL cc) (mk_ABS v th sg) sg
  1824 				  mk_COMB (mk_REFL cc) (mk_ABS v th thy) thy
  1849 			      end) th vlist'
  1825 			      end) th vlist'
  1850 		end
  1826 		end
  1851 	      | SOME _ => raise ERR "GEN_ABS" "Bad constant"
  1827 	      | SOME _ => raise ERR "GEN_ABS" "Bad constant"
  1852 	      | NONE => 
  1828 	      | NONE => 
  1853 		foldr (fn (v,th) => mk_ABS v th sg) th vlist'
  1829 		foldr (fn (v,th) => mk_ABS v th thy) th vlist'
  1854 	val res = HOLThm(rens_of info',th1)
  1830 	val res = HOLThm(rens_of info',th1)
  1855 	val _ = message "RESULT:"
  1831 	val _ = message "RESULT:"
  1856 	val _ = if_debug pth res
  1832 	val _ = if_debug pth res
  1857     in
  1833     in
  1858 	(thy,res)
  1834 	(thy,res)
  1860 
  1836 
  1861 fun NOT_INTRO (hth as HOLThm(rens,th)) thy =
  1837 fun NOT_INTRO (hth as HOLThm(rens,th)) thy =
  1862     let
  1838     let
  1863 	val _ = message "NOT_INTRO:"
  1839 	val _ = message "NOT_INTRO:"
  1864 	val _ = if_debug pth hth
  1840 	val _ = if_debug pth hth
  1865 	val sg = sign_of thy
       
  1866 	val th1 = implies_elim_all (beta_eta_thm th)
  1841 	val th1 = implies_elim_all (beta_eta_thm th)
  1867 	val a = case concl_of th1 of
  1842 	val a = case concl_of th1 of
  1868 		    _ $ (Const("op -->",_) $ a $ Const("False",_)) => a
  1843 		    _ $ (Const("op -->",_) $ a $ Const("False",_)) => a
  1869 		  | _ => raise ERR "NOT_INTRO" "Conclusion of bad form"
  1844 		  | _ => raise ERR "NOT_INTRO" "Conclusion of bad form"
  1870 	val ca = cterm_of sg a
  1845 	val ca = cterm_of thy a
  1871 	val th2 = equal_elim (Drule.instantiate' [] [SOME ca] not_intro_thm) th1
  1846 	val th2 = equal_elim (Drule.instantiate' [] [SOME ca] not_intro_thm) th1
  1872 	val res = HOLThm(rens,implies_intr_hyps th2)
  1847 	val res = HOLThm(rens,implies_intr_hyps th2)
  1873 	val _ = message "RESULT:"
  1848 	val _ = message "RESULT:"
  1874 	val _ = if_debug pth res
  1849 	val _ = if_debug pth res
  1875     in
  1850     in
  1878 
  1853 
  1879 fun NOT_ELIM (hth as HOLThm(rens,th)) thy =
  1854 fun NOT_ELIM (hth as HOLThm(rens,th)) thy =
  1880     let
  1855     let
  1881 	val _ = message "NOT_INTRO:"
  1856 	val _ = message "NOT_INTRO:"
  1882 	val _ = if_debug pth hth
  1857 	val _ = if_debug pth hth
  1883 	val sg = sign_of thy
       
  1884 	val th1 = implies_elim_all (beta_eta_thm th)
  1858 	val th1 = implies_elim_all (beta_eta_thm th)
  1885 	val a = case concl_of th1 of
  1859 	val a = case concl_of th1 of
  1886 		    _ $ (Const("Not",_) $ a) => a
  1860 		    _ $ (Const("Not",_) $ a) => a
  1887 		  | _ => raise ERR "NOT_ELIM" "Conclusion of bad form"
  1861 		  | _ => raise ERR "NOT_ELIM" "Conclusion of bad form"
  1888 	val ca = cterm_of sg a
  1862 	val ca = cterm_of thy a
  1889 	val th2 = equal_elim (Drule.instantiate' [] [SOME ca] not_elim_thm) th1
  1863 	val th2 = equal_elim (Drule.instantiate' [] [SOME ca] not_elim_thm) th1
  1890 	val res = HOLThm(rens,implies_intr_hyps th2)
  1864 	val res = HOLThm(rens,implies_intr_hyps th2)
  1891 	val _ = message "RESULT:"
  1865 	val _ = message "RESULT:"
  1892 	val _ = if_debug pth res
  1866 	val _ = if_debug pth res
  1893     in
  1867     in
  1900 	val _ = if_debug prin tm
  1874 	val _ = if_debug prin tm
  1901 	val _ = if_debug pth hth
  1875 	val _ = if_debug pth hth
  1902 	val (info,th) = disamb_thm hth
  1876 	val (info,th) = disamb_thm hth
  1903 	val (info',tm') = disamb_term_from info tm
  1877 	val (info',tm') = disamb_term_from info tm
  1904 	val prems = prems_of th
  1878 	val prems = prems_of th
  1905 	val sg = sign_of thy
       
  1906 	val th1 = beta_eta_thm th
  1879 	val th1 = beta_eta_thm th
  1907 	val th2 = implies_elim_all th1
  1880 	val th2 = implies_elim_all th1
  1908 	val th3 = implies_intr (cterm_of sg (HOLogic.mk_Trueprop tm')) th2
  1881 	val th3 = implies_intr (cterm_of thy (HOLogic.mk_Trueprop tm')) th2
  1909 	val th4 = th3 COMP disch_thm
  1882 	val th4 = th3 COMP disch_thm
  1910 	val res = HOLThm(rens_of info',implies_intr_hyps th4)
  1883 	val res = HOLThm(rens_of info',implies_intr_hyps th4)
  1911 	val _ = message "RESULT:"
  1884 	val _ = message "RESULT:"
  1912 	val _ = if_debug pth res
  1885 	val _ = if_debug pth res
  1913     in
  1886     in
  1917 val spaces = String.concat o separate " "
  1890 val spaces = String.concat o separate " "
  1918 
  1891 
  1919 fun new_definition thyname constname rhs thy =
  1892 fun new_definition thyname constname rhs thy =
  1920     let
  1893     let
  1921 	val constname = rename_const thyname thy constname
  1894 	val constname = rename_const thyname thy constname
  1922         val sg = sign_of thy
  1895         val redeclared = isSome (Sign.const_type thy (Sign.intern_const thy constname));
  1923         val redeclared = isSome (Sign.const_type sg (Sign.intern_const sg constname));
       
  1924 	val _ = warning ("Introducing constant " ^ constname)
  1896 	val _ = warning ("Introducing constant " ^ constname)
  1925 	val (thmname,thy) = get_defname thyname constname thy
  1897 	val (thmname,thy) = get_defname thyname constname thy
  1926 	val (info,rhs') = disamb_term rhs
  1898 	val (info,rhs') = disamb_term rhs
  1927 	val ctype = type_of rhs'
  1899 	val ctype = type_of rhs'
  1928 	val csyn = mk_syn thy constname
  1900 	val csyn = mk_syn thy constname
  1932 	val eq = mk_defeq constname rhs' thy1
  1904 	val eq = mk_defeq constname rhs' thy1
  1933 	val (thy2,thms) = PureThy.add_defs_i false [((thmname,eq),[])] thy1
  1905 	val (thy2,thms) = PureThy.add_defs_i false [((thmname,eq),[])] thy1
  1934 	val def_thm = hd thms
  1906 	val def_thm = hd thms
  1935 	val thm' = def_thm RS meta_eq_to_obj_eq_thm
  1907 	val thm' = def_thm RS meta_eq_to_obj_eq_thm
  1936 	val (thy',th) = (thy2, thm')
  1908 	val (thy',th) = (thy2, thm')
  1937 	val fullcname = Sign.intern_const (sign_of thy') constname
  1909 	val fullcname = Sign.intern_const thy' constname
  1938 	val thy'' = add_hol4_const_mapping thyname constname true fullcname thy'
  1910 	val thy'' = add_hol4_const_mapping thyname constname true fullcname thy'
  1939 	val (linfo,tm24) = disamb_term (mk_teq constname rhs' thy'')
  1911 	val (linfo,tm24) = disamb_term (mk_teq constname rhs' thy'')
  1940 	val sg = sign_of thy''
       
  1941 	val rew = rewrite_hol4_term eq thy''
  1912 	val rew = rewrite_hol4_term eq thy''
  1942 	val crhs = cterm_of sg (#2 (Logic.dest_equals (prop_of rew)))
  1913 	val crhs = cterm_of thy'' (#2 (Logic.dest_equals (prop_of rew)))
  1943 	val thy22 = if (def_name constname) = thmname andalso not redeclared andalso csyn = NoSyn
  1914 	val thy22 = if (def_name constname) = thmname andalso not redeclared andalso csyn = NoSyn
  1944 		    then
  1915 		    then
  1945 			add_dump ("constdefs\n  " ^ (quotename constname) ^ " :: \"" ^ string_of_ctyp (ctyp_of sg ctype) ^ "\" " ^ (Syntax.string_of_mixfix csyn) ^ "\n  " ^ (smart_string_of_cterm crhs)) thy''
  1916 			add_dump ("constdefs\n  " ^ (quotename constname) ^ " :: \"" ^ string_of_ctyp (ctyp_of thy'' ctype) ^ "\" " ^ (Syntax.string_of_mixfix csyn) ^ "\n  " ^ (smart_string_of_cterm crhs)) thy''
  1946 		    else
  1917 		    else
  1947 			add_dump ("consts\n  " ^ (quotename constname) ^ " :: \"" ^ string_of_ctyp (ctyp_of sg ctype) ^
  1918 			add_dump ("consts\n  " ^ (quotename constname) ^ " :: \"" ^ string_of_ctyp (ctyp_of thy'' ctype) ^
  1948 				  "\" " ^ (Syntax.string_of_mixfix csyn) ^ "\n\ndefs\n  " ^ (quotename thmname) ^ ": " ^ (smart_string_of_cterm crhs))
  1919 				  "\" " ^ (Syntax.string_of_mixfix csyn) ^ "\n\ndefs\n  " ^ (quotename thmname) ^ ": " ^ (smart_string_of_cterm crhs))
  1949 				 thy''
  1920 				 thy''
  1950 
  1921 
  1951 	val hth = case Shuffler.set_prop thy22 (HOLogic.mk_Trueprop tm24) [("",th)] of
  1922 	val hth = case Shuffler.set_prop thy22 (HOLogic.mk_Trueprop tm24) [("",th)] of
  1952 		     SOME (_,res) => HOLThm(rens_of linfo,res)
  1923 		     SOME (_,res) => HOLThm(rens_of linfo,res)
  1953 		   | NONE => raise ERR "new_definition" "Bad conclusion"
  1924 		   | NONE => raise ERR "new_definition" "Bad conclusion"
  1954 	val fullname = Sign.full_name sg thmname
  1925 	val fullname = Sign.full_name thy22 thmname
  1955 	val thy22' = case opt_get_output_thy thy22 of
  1926 	val thy22' = case opt_get_output_thy thy22 of
  1956 			 "" => add_hol4_mapping thyname thmname fullname thy22
  1927 			 "" => add_hol4_mapping thyname thmname fullname thy22
  1957 		       | output_thy =>
  1928 		       | output_thy =>
  1958 			 let
  1929 			 let
  1959 			     val moved_thmname = output_thy ^ "." ^ thyname ^ "." ^ thmname
  1930 			     val moved_thmname = output_thy ^ "." ^ thyname ^ "." ^ thmname
  1980 	let
  1951 	let
  1981 	    val _ = message "NEW_SPEC:"
  1952 	    val _ = message "NEW_SPEC:"
  1982 	    val _ = if_debug pth hth
  1953 	    val _ = if_debug pth hth
  1983 	    val names = map (rename_const thyname thy) names
  1954 	    val names = map (rename_const thyname thy) names
  1984 	    val _ = warning ("Introducing constants " ^ (commafy names))
  1955 	    val _ = warning ("Introducing constants " ^ (commafy names))
  1985 	    val (HOLThm(rens,th)) = norm_hthm (sign_of thy) hth
  1956 	    val (HOLThm(rens,th)) = norm_hthm thy hth
  1986 	    val thy1 = case HOL4DefThy.get thy of
  1957 	    val thy1 = case HOL4DefThy.get thy of
  1987 			   Replaying _ => thy
  1958 			   Replaying _ => thy
  1988 			 | _ =>
  1959 			 | _ =>
  1989 			   let
  1960 			   let
  1990 			       fun dest_eta_abs (Abs(x,xT,body)) = (x,xT,body)
  1961 			       fun dest_eta_abs (Abs(x,xT,body)) = (x,xT,body)
  2004 							  let
  1975 							  let
  2005 							      val (_,cT,p) = dest_exists ex
  1976 							      val (_,cT,p) = dest_exists ex
  2006 							  in
  1977 							  in
  2007 							      ((cname,cT,mk_syn thy cname)::cs,p)
  1978 							      ((cname,cT,mk_syn thy cname)::cs,p)
  2008 							  end) (([],HOLogic.dest_Trueprop (concl_of th)),names)
  1979 							  end) (([],HOLogic.dest_Trueprop (concl_of th)),names)
  2009 			       val sg = sign_of thy
       
  2010 			       val str = Library.foldl (fn (acc,(c,T,csyn)) =>
  1980 			       val str = Library.foldl (fn (acc,(c,T,csyn)) =>
  2011 						   acc ^ "\n  " ^ (quotename c) ^ " :: \"" ^ string_of_ctyp (ctyp_of sg T) ^ "\" " ^ (Syntax.string_of_mixfix csyn)) ("consts",consts)
  1981 						   acc ^ "\n  " ^ (quotename c) ^ " :: \"" ^ string_of_ctyp (ctyp_of thy T) ^ "\" " ^ (Syntax.string_of_mixfix csyn)) ("consts",consts)
  2012 			       val thy' = add_dump str thy
  1982 			       val thy' = add_dump str thy
  2013 			   in
  1983 			   in
  2014 			       Theory.add_consts_i consts thy'
  1984 			       Theory.add_consts_i consts thy'
  2015 			   end
  1985 			   end
  2016 
  1986 
  2053 			   
  2023 			   
  2054 fun new_axiom name tm thy = raise ERR "new_axiom" ("Oh, no you don't! (" ^ name ^ ")")
  2024 fun new_axiom name tm thy = raise ERR "new_axiom" ("Oh, no you don't! (" ^ name ^ ")")
  2055 				      
  2025 				      
  2056 fun to_isa_thm (hth as HOLThm(_,th)) =
  2026 fun to_isa_thm (hth as HOLThm(_,th)) =
  2057     let
  2027     let
  2058 	val (HOLThm args) = norm_hthm (sign_of_thm th) hth
  2028 	val (HOLThm args) = norm_hthm (theory_of_thm th) hth
  2059     in
  2029     in
  2060 	apsnd strip_shyps args
  2030 	apsnd strip_shyps args
  2061     end
  2031     end
  2062 
  2032 
  2063 fun to_isa_term tm = tm
  2033 fun to_isa_term tm = tm
  2074       | _ => 
  2044       | _ => 
  2075 	let
  2045 	let
  2076 	    val _ = message "TYPE_DEF:"
  2046 	    val _ = message "TYPE_DEF:"
  2077 	    val _ = if_debug pth hth
  2047 	    val _ = if_debug pth hth
  2078 	    val _ = warning ("Introducing type " ^ tycname)
  2048 	    val _ = warning ("Introducing type " ^ tycname)
  2079 	    val (HOLThm(rens,td_th)) = norm_hthm (sign_of thy) hth
  2049 	    val (HOLThm(rens,td_th)) = norm_hthm thy hth
  2080 	    val th2 = beta_eta_thm (td_th RS ex_imp_nonempty)
  2050 	    val th2 = beta_eta_thm (td_th RS ex_imp_nonempty)
  2081 	    val c = case concl_of th2 of
  2051 	    val c = case concl_of th2 of
  2082 			_ $ (Const("Ex",_) $ Abs(_,_,Const("op :",_) $ _ $ c)) => c
  2052 			_ $ (Const("Ex",_) $ Abs(_,_,Const("op :",_) $ _ $ c)) => c
  2083 		      | _ => raise ERR "new_type_definition" "Bad type definition theorem"
  2053 		      | _ => raise ERR "new_type_definition" "Bad type definition theorem"
  2084 	    val tfrees = term_tfrees c
  2054 	    val tfrees = term_tfrees c
  2087 	    val typ = (tycname,tnames,tsyn)
  2057 	    val typ = (tycname,tnames,tsyn)
  2088 	    val (thy',typedef_info) = TypedefPackage.add_typedef_i false (SOME thmname) typ c NONE (rtac th2 1) thy
  2058 	    val (thy',typedef_info) = TypedefPackage.add_typedef_i false (SOME thmname) typ c NONE (rtac th2 1) thy
  2089 				      
  2059 				      
  2090 	    val th3 = (#type_definition typedef_info) RS typedef_hol2hol4
  2060 	    val th3 = (#type_definition typedef_info) RS typedef_hol2hol4
  2091 
  2061 
  2092 	    val fulltyname = Sign.intern_type (sign_of thy') tycname
  2062 	    val fulltyname = Sign.intern_type thy' tycname
  2093 	    val thy'' = add_hol4_type_mapping thyname tycname true fulltyname thy'
  2063 	    val thy'' = add_hol4_type_mapping thyname tycname true fulltyname thy'
  2094 
  2064 
  2095 	    val sg = sign_of thy''
  2065 	    val (hth' as HOLThm args) = norm_hthm thy'' (HOLThm(rens,th3))
  2096 	    val (hth' as HOLThm args) = norm_hthm sg (HOLThm(rens,th3))
       
  2097 	    val _ = if has_ren hth' then warning ("Theorem " ^ thmname ^ " needs variable-disambiguating")
  2066 	    val _ = if has_ren hth' then warning ("Theorem " ^ thmname ^ " needs variable-disambiguating")
  2098 		    else ()
  2067 		    else ()
  2099 	    val thy4 = add_hol4_pending thyname thmname args thy''
  2068 	    val thy4 = add_hol4_pending thyname thmname args thy''
  2100 
  2069 
  2101 	    val sg = sign_of thy4
       
  2102 	    val rew = rewrite_hol4_term (concl_of td_th) thy4
  2070 	    val rew = rewrite_hol4_term (concl_of td_th) thy4
  2103 	    val th  = equal_elim rew (Thm.transfer sg td_th)
  2071 	    val th  = equal_elim rew (Thm.transfer thy4 td_th)
  2104 	    val c   = case HOLogic.dest_Trueprop (prop_of th) of
  2072 	    val c   = case HOLogic.dest_Trueprop (prop_of th) of
  2105 			  Const("Ex",exT) $ P =>
  2073 			  Const("Ex",exT) $ P =>
  2106 			  let
  2074 			  let
  2107 			      val PT = domain_type exT
  2075 			      val PT = domain_type exT
  2108 			  in
  2076 			  in
  2113 				then ""
  2081 				then ""
  2114 				else "(" ^ (commafy tnames) ^ ") "
  2082 				else "(" ^ (commafy tnames) ^ ") "
  2115 	    val proc_prop = if null tnames
  2083 	    val proc_prop = if null tnames
  2116 			    then smart_string_of_cterm
  2084 			    then smart_string_of_cterm
  2117 			    else Library.setmp show_all_types true smart_string_of_cterm
  2085 			    else Library.setmp show_all_types true smart_string_of_cterm
  2118 	    val thy5 = add_dump ("typedef (open) " ^ tnames_string ^ (quotename tycname) ^ " = " ^ (proc_prop (cterm_of sg c)) ^ " " 
  2086 	    val thy5 = add_dump ("typedef (open) " ^ tnames_string ^ (quotename tycname) ^ " = " ^ (proc_prop (cterm_of thy4 c)) ^ " " 
  2119 				 ^ (Syntax.string_of_mixfix tsyn) ^ "\n  by (rule typedef_helper,import " ^ thyname ^ " " ^ thmname ^ ")") thy4
  2087 				 ^ (Syntax.string_of_mixfix tsyn) ^ "\n  by (rule typedef_helper,import " ^ thyname ^ " " ^ thmname ^ ")") thy4
  2120 	    
  2088 	    
  2121 	    val thy6 = add_dump ("lemmas " ^ thmname ^ " = typedef_hol2hol4 [OF type_definition_" ^ tycname ^ "]") thy5
  2089 	    val thy6 = add_dump ("lemmas " ^ thmname ^ " = typedef_hol2hol4 [OF type_definition_" ^ tycname ^ "]") thy5
  2122               
  2090               
  2123 	    val _ = message "RESULT:"
  2091 	    val _ = message "RESULT:"
  2160       | _ => 
  2128       | _ => 
  2161 	let
  2129 	let
  2162             val _ = message "TYPE_INTRO:"
  2130             val _ = message "TYPE_INTRO:"
  2163 	    val _ = if_debug pth hth
  2131 	    val _ = if_debug pth hth
  2164 	    val _ = warning ("Introducing type " ^ tycname ^ " (with morphisms " ^ abs_name ^ " and " ^ rep_name ^ ")")
  2132 	    val _ = warning ("Introducing type " ^ tycname ^ " (with morphisms " ^ abs_name ^ " and " ^ rep_name ^ ")")
  2165 	    val (HOLThm(rens,td_th)) = norm_hthm (sign_of thy) hth
  2133 	    val (HOLThm(rens,td_th)) = norm_hthm thy hth
  2166 	    val sg = sign_of thy
       
  2167 	    val tT = type_of t
  2134 	    val tT = type_of t
  2168 	    val light_nonempty' =
  2135 	    val light_nonempty' =
  2169 		Drule.instantiate' [SOME (ctyp_of sg tT)]
  2136 		Drule.instantiate' [SOME (ctyp_of thy tT)]
  2170 				   [SOME (cterm_of sg P),
  2137 				   [SOME (cterm_of thy P),
  2171 				    SOME (cterm_of sg t)] light_nonempty
  2138 				    SOME (cterm_of thy t)] light_nonempty
  2172 	    val th2 = beta_eta_thm (td_th RS (beta_eta_thm light_nonempty'))
  2139 	    val th2 = beta_eta_thm (td_th RS (beta_eta_thm light_nonempty'))
  2173 	    val c = case concl_of th2 of
  2140 	    val c = case concl_of th2 of
  2174 			_ $ (Const("Ex",_) $ Abs(_,_,Const("op :",_) $ _ $ c)) => c
  2141 			_ $ (Const("Ex",_) $ Abs(_,_,Const("op :",_) $ _ $ c)) => c
  2175 		      | _ => raise ERR "type_introduction" "Bad type definition theorem"
  2142 		      | _ => raise ERR "type_introduction" "Bad type definition theorem"
  2176 	    val tfrees = term_tfrees c
  2143 	    val tfrees = term_tfrees c
  2177 	    val tnames = sort string_ord (map fst tfrees)
  2144 	    val tnames = sort string_ord (map fst tfrees)
  2178 	    val tsyn = mk_syn thy tycname
  2145 	    val tsyn = mk_syn thy tycname
  2179 	    val typ = (tycname,tnames,tsyn)
  2146 	    val typ = (tycname,tnames,tsyn)
  2180 	    val (thy',typedef_info) = TypedefPackage.add_typedef_i false NONE typ c (SOME(rep_name,abs_name)) (rtac th2 1) thy
  2147 	    val (thy',typedef_info) = TypedefPackage.add_typedef_i false NONE typ c (SOME(rep_name,abs_name)) (rtac th2 1) thy
  2181 	    val fulltyname = Sign.intern_type (sign_of thy') tycname
  2148 	    val fulltyname = Sign.intern_type thy' tycname
  2182 	    val aty = Type (fulltyname, map mk_vartype tnames)
  2149 	    val aty = Type (fulltyname, map mk_vartype tnames)
  2183 	    val abs_ty = tT --> aty
  2150 	    val abs_ty = tT --> aty
  2184 	    val rep_ty = aty --> tT
  2151 	    val rep_ty = aty --> tT
  2185             val typedef_hol2hollight' = 
  2152             val typedef_hol2hollight' = 
  2186 		Drule.instantiate' 
  2153 		Drule.instantiate' 
  2191             val _ = if Drule.tvars_of th4 = [] then () else raise ERR "type_introduction" "no type variables expected any more" 
  2158             val _ = if Drule.tvars_of th4 = [] then () else raise ERR "type_introduction" "no type variables expected any more" 
  2192             val _ = if Drule.vars_of th4 = [] then () else raise ERR "type_introduction" "no term variables expected any more"
  2159             val _ = if Drule.vars_of th4 = [] then () else raise ERR "type_introduction" "no term variables expected any more"
  2193 	    val _ = message ("step 3: thyname="^thyname^", tycname="^tycname^", fulltyname="^fulltyname)
  2160 	    val _ = message ("step 3: thyname="^thyname^", tycname="^tycname^", fulltyname="^fulltyname)
  2194             val thy'' = add_hol4_type_mapping thyname tycname true fulltyname thy'
  2161             val thy'' = add_hol4_type_mapping thyname tycname true fulltyname thy'
  2195             val _ = message "step 4"
  2162             val _ = message "step 4"
  2196 	    val sg = sign_of thy''
  2163 	    val (hth' as HOLThm args) = norm_hthm thy'' (HOLThm(rens,th4))
  2197 	    val (hth' as HOLThm args) = norm_hthm sg (HOLThm(rens,th4))
       
  2198 	    val thy4 = add_hol4_pending thyname thmname args thy''
  2164 	    val thy4 = add_hol4_pending thyname thmname args thy''
  2199 	   
  2165 	   
  2200 	    val sg = sign_of thy4
       
  2201 	    val P' = P (* why !? #2 (Logic.dest_equals (concl_of (rewrite_hol4_term P thy4))) *)
  2166 	    val P' = P (* why !? #2 (Logic.dest_equals (concl_of (rewrite_hol4_term P thy4))) *)
  2202 	    val c   =
  2167 	    val c   =
  2203 		let
  2168 		let
  2204 		    val PT = type_of P'
  2169 		    val PT = type_of P'
  2205 		in
  2170 		in
  2211 				else "(" ^ (commafy tnames) ^ ") "
  2176 				else "(" ^ (commafy tnames) ^ ") "
  2212 	    val proc_prop = if null tnames
  2177 	    val proc_prop = if null tnames
  2213 			    then smart_string_of_cterm
  2178 			    then smart_string_of_cterm
  2214 			    else Library.setmp show_all_types true smart_string_of_cterm
  2179 			    else Library.setmp show_all_types true smart_string_of_cterm
  2215 	    val thy = add_dump ("typedef (open) " ^ tnames_string ^ (quotename tycname) ^ 
  2180 	    val thy = add_dump ("typedef (open) " ^ tnames_string ^ (quotename tycname) ^ 
  2216               " = " ^ (proc_prop (cterm_of sg c)) ^ " " ^ 
  2181               " = " ^ (proc_prop (cterm_of thy4 c)) ^ " " ^ 
  2217 	      (Syntax.string_of_mixfix tsyn) ^ " morphisms "^
  2182 	      (Syntax.string_of_mixfix tsyn) ^ " morphisms "^
  2218               (quote rep_name)^" "^(quote abs_name)^"\n"^ 
  2183               (quote rep_name)^" "^(quote abs_name)^"\n"^ 
  2219 	      ("  apply (rule light_ex_imp_nonempty[where t="^
  2184 	      ("  apply (rule light_ex_imp_nonempty[where t="^
  2220               (proc_prop (cterm_of sg t))^"])\n"^              
  2185               (proc_prop (cterm_of thy4 t))^"])\n"^              
  2221 	      ("  by (import " ^ thyname ^ " " ^ (quotename thmname) ^ ")"))) thy4
  2186 	      ("  by (import " ^ thyname ^ " " ^ (quotename thmname) ^ ")"))) thy4
  2222 	    val str_aty = string_of_ctyp (ctyp_of thy aty)
  2187 	    val str_aty = string_of_ctyp (ctyp_of thy aty)
  2223             val thy = add_dump_syntax thy rep_name 
  2188             val thy = add_dump_syntax thy rep_name 
  2224             val thy = add_dump_syntax thy abs_name
  2189             val thy = add_dump_syntax thy abs_name
  2225 	    val thy = add_dump ("lemmas " ^ (quote (thmname^"_@intern")) ^ 
  2190 	    val thy = add_dump ("lemmas " ^ (quote (thmname^"_@intern")) ^