src/HOL/Nominal/Examples/Fsub.thy
changeset 18410 73bb08d2823c
parent 18353 4dd468ccfdf7
child 18412 9f6b3e1da352
equal deleted inserted replaced
18409:080094128a09 18410:73bb08d2823c
   682 
   682 
   683 (* helper lemma to calculate the measure of the induction *)
   683 (* helper lemma to calculate the measure of the induction *)
   684 lemma measure_eq [simp]: "(x, y) \<in> measure f = (f x < f y)"
   684 lemma measure_eq [simp]: "(x, y) \<in> measure f = (f x < f y)"
   685   by (simp add: measure_def inv_image_def)
   685   by (simp add: measure_def inv_image_def)
   686 
   686 
       
   687 lemma
       
   688   fixes n :: nat
       
   689   shows "!!x::'a. A n x ==> P n x"
       
   690   and "!!y::'b. B n y ==> Q n y"
       
   691 proof (induct n rule: less_induct)
       
   692   case (less n)
       
   693   show ?case sorry
       
   694 qed
       
   695 
       
   696 text {* I want to do an induction over the size(!) of Q 
       
   697         therefore the example above does not help *}
       
   698 
   687 lemma transitivity_and_narrowing:
   699 lemma transitivity_and_narrowing:
   688   shows "(\<forall>\<Gamma> S T. \<Gamma> \<turnstile> S <: Q \<longrightarrow> \<Gamma> \<turnstile> Q <: T \<longrightarrow> \<Gamma> \<turnstile> S <: T) \<and>
   700   shows "(\<forall>\<Gamma> S T. \<Gamma> \<turnstile> S <: Q \<longrightarrow> \<Gamma> \<turnstile> Q <: T \<longrightarrow> \<Gamma> \<turnstile> S <: T) \<and>
   689          (\<forall>\<Delta> \<Gamma> X P M N. (\<Delta>@(X,Q)#\<Gamma>) \<turnstile> M <: N \<longrightarrow> \<Gamma> \<turnstile> P <: Q \<longrightarrow> (\<Delta>@(X,P)#\<Gamma>) \<turnstile> M <: N)"
   701          (\<forall>\<Delta> \<Gamma> X P M N. (\<Delta>@(X,Q)#\<Gamma>) \<turnstile> M <: N \<longrightarrow> \<Gamma> \<turnstile> P <: Q \<longrightarrow> (\<Delta>@(X,P)#\<Gamma>) \<turnstile> M <: N)"
   690 proof (rule wf_induct [of "measure size_ty" _ Q, rule_format])
   702 proof (rule wf_induct [of "measure size_ty" _ Q, rule_format])
   691   show "wf (measure size_ty)" by simp
   703   show "wf (measure size_ty)" by simp
   815       ultimately show "\<Gamma>1 \<turnstile> \<forall>[X<:S1].S2 <: T" using Q_inst top_case[of "\<Gamma>1" "\<forall>[X<:S1].S2" _ "T'"] 
   827       ultimately show "\<Gamma>1 \<turnstile> \<forall>[X<:S1].S2 <: T" using Q_inst top_case[of "\<Gamma>1" "\<forall>[X<:S1].S2" _ "T'"] 
   816 	by auto
   828 	by auto
   817     qed
   829     qed
   818   qed
   830   qed
   819 
   831 
   820   (* HERE *)
   832   (* HERE how should I state this lemma with the context? I want to quantify over all \<Delta>*)
   821   have narrowing:
   833   have narrowing:
   822     "\<And>\<Delta> \<Gamma> X M N P. (\<Delta>@(X,Q)#\<Gamma>) \<turnstile> M <: N \<Longrightarrow> \<Gamma> \<turnstile> P<:Q \<Longrightarrow> (\<Delta>@(X,Q)#\<Gamma>) \<turnstile> M <: N"
   834     "\<And>\<Delta> \<Gamma> X M N P. (\<Delta>@(X,Q)#\<Gamma>) \<turnstile> M <: N \<Longrightarrow> \<Gamma> \<turnstile> P<:Q \<Longrightarrow> (\<Delta>@(X,P)#\<Gamma>) \<turnstile> M <: N"
   823   proof -
   835   proof -
   824     fix \<Delta> \<Gamma> X M N P
   836     fix \<Delta> \<Gamma> X M N P
   825     assume a: "(\<Delta>@(X,Q)#\<Gamma>) \<turnstile> M <: N"
   837     assume a: "(\<Delta>@(X,Q)#\<Gamma>) \<turnstile> M <: N"
   826     assume b: "\<Gamma> \<turnstile> P<:Q"
   838     assume b: "\<Gamma> \<turnstile> P<:Q"
   827     show "(\<Delta>@(X,Q)#\<Gamma>) \<turnstile> M <: N"
   839     show "(\<Delta>@(X,P)#\<Gamma>) \<turnstile> M <: N"
   828       using a b 
   840       using a b 
   829     proof (nominal_induct \<Gamma>\<equiv>"\<Delta>@(X,Q)#\<Gamma>" M N avoiding: \<Gamma> rule: subtype_of_rel_induct) 
   841     proof (nominal_induct \<Gamma>\<equiv>"\<Delta>@(X,Q)#\<Gamma>" M N avoiding: \<Delta> \<Gamma> X rule: subtype_of_rel_induct) 
   830       case (S_Top \<Gamma>1 S1 \<Gamma>2)
   842       case (S_Top \<Gamma>1 S1 \<Delta> \<Gamma>2 X)
   831       have lh_drv_prem1: "\<turnstile> \<Gamma>1 ok" by fact
   843       have lh_drv_prem1: "\<turnstile> \<Gamma>1 ok" by fact
   832       have lh_drv_prem2: "S1 closed_in \<Gamma>1" by fact
   844       have lh_drv_prem2: "S1 closed_in \<Gamma>1" by fact
   833       have \<Gamma>1_inst: "\<Gamma>1=\<Delta>@(X,Q)#\<Gamma>2" by fact
   845       have \<Gamma>1_inst: "\<Gamma>1=\<Delta>@(X,Q)#\<Gamma>2" by fact
   834       have rh_drv: "\<Gamma>2 \<turnstile> P <: Q" by fact
   846       have rh_drv: "\<Gamma>2 \<turnstile> P <: Q" by fact
   835       hence a1: "P closed_in \<Gamma>2" by (simp add: subtype_implies_closed)
   847       hence a1: "P closed_in \<Gamma>2" by (simp add: subtype_implies_closed)
   836       hence a2: "\<turnstile> (\<Delta>@(X,P)#\<Gamma>2) ok" using lh_drv_prem1 a1 \<Gamma>1_inst by (simp add: replace_type)
   848       hence a2: "\<turnstile> (\<Delta>@(X,P)#\<Gamma>2) ok" using lh_drv_prem1 a1 \<Gamma>1_inst by (simp add: replace_type)
   837       show ?case
       
   838       show "(\<Delta>@(X,P)#\<Gamma>2) \<turnstile> S1 <: Top"
   849       show "(\<Delta>@(X,P)#\<Gamma>2) \<turnstile> S1 <: Top"
   839 	  using a1 a3 lh_drv_prem2 by (force simp add: closed_in_def domain_append)
   850 	  using a1 a2 \<Gamma>1_inst lh_drv_prem2 by (force simp add: closed_in_def domain_append)
       
   851     next
       
   852       case (S_Var \<Gamma>1 X1 S1 T1 \<Delta> \<Gamma>2 X)
       
   853       have lh_drv_prem1: "\<turnstile> \<Gamma>1 ok" by fact
       
   854       have lh_drv_prem2: "(X1,S1)\<in>set \<Gamma>1" by fact
       
   855       have lh_drv_prem3: "\<Gamma>1 \<turnstile> S1 <: T1" by fact
       
   856       have IH_inner: "\<And>\<Gamma>. \<Gamma> \<turnstile> P <: Q \<Longrightarrow> \<Gamma>1=\<Delta>@(X,Q)#\<Gamma> \<Longrightarrow> (\<Delta>@(X,P)#\<Gamma>) \<turnstile> S1 <: T1" by fact
       
   857       have \<Gamma>1_inst: "\<Gamma>1=\<Delta>@(X,Q)#\<Gamma>2" by fact
       
   858       have rh_drv: "\<Gamma>2 \<turnstile> P <: Q" by fact
       
   859       hence "P closed_in \<Gamma>2" by (simp add: subtype_implies_closed)
       
   860       hence a3: "\<turnstile> (\<Delta>@(X,P)#\<Gamma>2) ok" using lh_drv_prem1 \<Gamma>1_inst by (simp add: replace_type)
       
   861       show "(\<Delta>@(X,P)#\<Gamma>2) \<turnstile> TyVar X1 <: T1"
       
   862       proof (cases "X=X1")
       
   863 	case False
       
   864 	have b0: "X\<noteq>X1" by fact
       
   865 	from lh_drv_prem3 \<Gamma>1_inst rh_drv IH_inner 
       
   866 	have b1: "(\<Delta>@(X,P)#\<Gamma>2) \<turnstile> S1 <: T1" by simp
       
   867 	from lh_drv_prem2 \<Gamma>1_inst b0 have b2: "(X1,S1)\<in>set (\<Delta>@(X,P)#\<Gamma>2)" by simp
       
   868 	show "(\<Delta>@(X,P)#\<Gamma>2) \<turnstile> TyVar X1 <: T1" using a3 b2 b1 by (rule subtype_of_rel.S_Var)
       
   869       next 
       
   870 	case True
       
   871 	have b0: "X=X1" by fact
       
   872 	have a4: "S1=Q"
       
   873 	proof -
       
   874 	  have "(X1,Q)\<in>set \<Gamma>1" using b0 \<Gamma>1_inst by simp
       
   875 	  with lh_drv_prem1 lh_drv_prem2 show "S1=Q" by (simp add: uniqueness_of_ctxt)
       
   876 	qed
       
   877 	have a5: "(\<Delta>@(X,P)#\<Gamma>2) extends \<Gamma>2" by (force simp add: extends_def)
       
   878 	have a6: "(\<Delta>@(X1,P)#\<Gamma>2) \<turnstile> P <: Q" using b0 a5 rh_drv a3 by (simp add: weakening)	
       
   879 	have a7: "(\<Delta>@(X1,P)#\<Gamma>2) \<turnstile> Q <: T1" using b0 IH_inner \<Gamma>1_inst lh_drv_prem3 rh_drv a4 by simp
       
   880 	have a8: "(\<Delta>@(X1,P)#\<Gamma>2) \<turnstile> P <: T1" using a6 a7 transitivity by blast
       
   881 	have a9: "(X1,P)\<in>set (\<Delta>@(X,P)#\<Gamma>2)" using b0 by simp
       
   882 	show "(\<Delta>@(X,P)#\<Gamma>2) \<turnstile> TyVar X1 <: T1" using a3 b0 a8 a9 by force
   840       qed
   883       qed
   841     next
   884     next
   842       case (goal2 \<Delta>1 \<Gamma>1 X1 \<Gamma>2 X2 S1 T1)
   885       case (S_Refl \<Gamma>1 X1 \<Delta> \<Gamma>2 X)
   843       have lh_drv_prem1: "\<turnstile> \<Gamma>2 ok" by fact
   886       have lh_drv_prem1: "\<turnstile> \<Gamma>1 ok" by fact
   844       have lh_drv_prem2: "(X2,S1)\<in>set \<Gamma>2" by fact
   887       have lh_drv_prem2: "X1 \<in> domain \<Gamma>1" by fact
   845       have lh_drv_prem3: "\<Gamma>2 \<turnstile> S1 <: T1" by fact
   888       have \<Gamma>1_inst: "\<Gamma>1 = \<Delta>@(X,Q)#\<Gamma>2" by fact
   846       have IH_inner: 
   889       have "\<Gamma>2 \<turnstile> P <: Q" by fact
   847 	"\<forall>\<Delta>1 \<Gamma>1 X1.  \<Gamma>2 = \<Delta>1@(X1,Q)#\<Gamma>1 \<longrightarrow> (\<forall>P. \<Gamma>1 \<turnstile> P <: Q \<longrightarrow> (\<Delta>1@(X1,P)#\<Gamma>1) \<turnstile> S1 <: T1)" by fact
   890       hence "P closed_in \<Gamma>2" by (simp add: subtype_implies_closed)
   848       show "\<Gamma>2 = (\<Delta>1@(X1,Q)#\<Gamma>1) \<longrightarrow> (\<forall>P. \<Gamma>1 \<turnstile> P <: Q \<longrightarrow> (\<Delta>1@(X1,P)#\<Gamma>1) \<turnstile> TyVar X2 <: T1)"
   891       hence a3: "\<turnstile> (\<Delta>@(X,P)#\<Gamma>2) ok" using lh_drv_prem1 \<Gamma>1_inst by (simp add: replace_type)
   849       proof (intro strip)
   892       have b0: "X1 \<in> domain (\<Delta>@(X,P)#\<Gamma>2)" using lh_drv_prem2 \<Gamma>1_inst by (simp add: domain_append)
   850 	fix P
   893       show "(\<Delta>@(X,P)#\<Gamma>2) \<turnstile> TyVar X1 <: TyVar X1" using a3 b0 by (rule subtype_of_rel.S_Refl)
   851         assume a1: "\<Gamma>2 = \<Delta>1@(X1,Q)#\<Gamma>1"
       
   852 	and    a2: "\<Gamma>1 \<turnstile> P <: Q"
       
   853 	from a2 have "P closed_in \<Gamma>1" by (simp add: subtype_implies_closed)
       
   854 	hence a3: "\<turnstile> (\<Delta>1@(X1,P)#\<Gamma>1) ok" 
       
   855 	  using lh_drv_prem1 a1 by (rule_tac replace_type, simp_all)
       
   856 	show "(\<Delta>1@(X1,P)#\<Gamma>1) \<turnstile> TyVar X2 <: T1"
       
   857  	proof (cases "X1=X2")
       
   858 	  case False
       
   859 	  have b0: "X1\<noteq>X2" by fact
       
   860 	  from lh_drv_prem3 a1 a2 IH_inner 
       
   861 	  have b1: "(\<Delta>1@(X1,P)#\<Gamma>1) \<turnstile> S1 <: T1" by simp
       
   862 	  from lh_drv_prem2 a1 b0 have b2: "(X2,S1)\<in>set (\<Delta>1@(X1,P)#\<Gamma>1)" by simp
       
   863 	  show "(\<Delta>1@(X1,P)#\<Gamma>1) \<turnstile> TyVar X2 <: T1" using a3 b2 b1 by (rule S_Var)
       
   864 	next 
       
   865 	  case True
       
   866 	  have b0: "X1=X2" by fact
       
   867 	  have a4: "S1=Q"
       
   868 	  proof -
       
   869 	    have c0: "(X2,Q)\<in>set \<Gamma>2" using b0 a1 by simp
       
   870 	    with lh_drv_prem1 lh_drv_prem2 show "S1=Q" by (simp add: uniqueness_of_ctxt)
       
   871 	  qed
       
   872 	  have a5: "(\<Delta>1@(X1,P)#\<Gamma>1) extends \<Gamma>1" by (force simp add: extends_def)
       
   873 	  have a6: "(\<Delta>1@(X2,P)#\<Gamma>1) \<turnstile> P <: Q" using b0 a5 a2 a3 by (simp add: weakening)
       
   874 	  have a7: "(\<Delta>1@(X2,P)#\<Gamma>1) \<turnstile> Q <: T1" using b0 IH_inner a1 lh_drv_prem3 a2 a4 by simp
       
   875 	  have a8: "(\<Delta>1@(X2,P)#\<Gamma>1) \<turnstile> P <: T1" using a6 a7 transitivity by blast
       
   876 	  have a9: "(X2,P)\<in>set (\<Delta>1@(X1,P)#\<Gamma>1)" using b0 by simp
       
   877 	  show "(\<Delta>1@(X1,P)#\<Gamma>1) \<turnstile> TyVar X2 <: T1" using a3 b0 a8 a9 by (force simp add: S_Var)
       
   878 	qed
       
   879       qed
       
   880     next
   894     next
   881       case (goal3 \<Delta>1 \<Gamma>1 X1 \<Gamma>2 X2)
   895       case S_Arrow thus ?case by blast
   882       have lh_drv_prem1: "\<turnstile> \<Gamma>2 ok" by fact
       
   883       have lh_drv_prem2: "X2 \<in> domain \<Gamma>2" by fact
       
   884       show "\<Gamma>2 = (\<Delta>1@(X1,Q)#\<Gamma>1) \<longrightarrow> (\<forall>P. \<Gamma>1 \<turnstile> P <: Q \<longrightarrow> (\<Delta>1@(X1,P)#\<Gamma>1) \<turnstile> TyVar X2 <: TyVar X2)"
       
   885       proof (intro strip)
       
   886 	fix P
       
   887 	assume a1: "\<Gamma>2 = \<Delta>1@(X1,Q)#\<Gamma>1"
       
   888 	and    a2: "\<Gamma>1 \<turnstile> P <: Q"
       
   889 	from a2 have "P closed_in \<Gamma>1" by (simp add: subtype_implies_closed)
       
   890 	hence a3: "\<turnstile> (\<Delta>1@(X1,P)#\<Gamma>1) ok" 
       
   891 	  using lh_drv_prem1 a1 by (rule_tac replace_type, simp_all)
       
   892 	have b0: "X2 \<in> domain (\<Delta>1@(X1,P)#\<Gamma>1)" using lh_drv_prem2 a1 by (simp add: domain_append)
       
   893 	show "(\<Delta>1@(X1,P)#\<Gamma>1) \<turnstile> TyVar X2 <: TyVar X2" using a3 b0 by (rule S_Refl)
       
   894       qed
       
   895     next
   896     next
   896       case goal4 thus ?case by blast
   897       case (S_All \<Gamma>1 X1 S1 S2 T1 T2 \<Delta> \<Gamma>2 X)
   897     next
   898       have IH_inner1: "\<And>\<Delta> \<Gamma> X. \<Gamma> \<turnstile> P <: Q \<Longrightarrow> \<Gamma>1 = \<Delta>@(X,Q)#\<Gamma> \<Longrightarrow> (\<Delta>@(X,P)#\<Gamma>) \<turnstile> T1 <: S1" by fact
   898       case (goal5 \<Delta>1 \<Gamma>1 X1 \<Gamma>2 X2 S1 S2 T1 T2)
   899       have IH_inner2: "\<And>\<Delta> \<Gamma> X. \<Gamma> \<turnstile> P <: Q \<Longrightarrow> (X1,T1)#\<Gamma>1 = \<Delta>@(X,Q)#\<Gamma> \<Longrightarrow> (\<Delta>@(X,P)#\<Gamma>) \<turnstile> S2 <: T2" 
   899       have IH_inner1: 
       
   900 	"\<forall>\<Delta>1 \<Gamma>1 X1.  \<Gamma>2 = \<Delta>1@(X1,Q)#\<Gamma>1 \<longrightarrow> (\<forall>P. \<Gamma>1 \<turnstile> P <: Q \<longrightarrow> (\<Delta>1@(X1,P)#\<Gamma>1) \<turnstile> T1 <: S1)" by fact
       
   901       have IH_inner2: 
       
   902 	"\<forall>\<Delta>1 \<Gamma>1 X1. (X2,T1)#\<Gamma>2 = \<Delta>1@(X1,Q)#\<Gamma>1 \<longrightarrow> (\<forall>P. \<Gamma>1 \<turnstile> P <: Q \<longrightarrow> (\<Delta>1@(X1,P)#\<Gamma>1) \<turnstile> S2 <: T2)" 
       
   903 	by fact
   900 	by fact
   904       have lh_drv_prem1: "\<Gamma>2 \<turnstile> T1 <: S1" by fact
   901       have lh_drv_prem1: "\<Gamma>1 \<turnstile> T1 <: S1" by fact
   905       have lh_drv_prem2: "X2 \<sharp> (\<Gamma>2,S1, T1)" by fact
   902       have lh_drv_prem2: "X1\<sharp>\<Gamma>1" "X1\<sharp>S1" "X1\<sharp>T1" by fact (* check this whether this is the lh_drv_p2 *)
   906       have lh_drv_prem3: "((X2,T1) # \<Gamma>2) \<turnstile> S2 <: T2" by fact
   903       have lh_drv_prem3: "((X1,T1)#\<Gamma>1) \<turnstile> S2 <: T2" by fact
   907       have freshness: "X2\<sharp>(\<Delta>1, \<Gamma>1, X1)" by fact
   904       have \<Gamma>1_inst: "\<Gamma>1 = \<Delta>@(X,Q)#\<Gamma>2" by fact
   908       show "\<Gamma>2 = (\<Delta>1@(X1,Q)#\<Gamma>1) \<longrightarrow> 
   905       have a2: "\<Gamma>2 \<turnstile> P <: Q" by fact
   909             (\<forall>P. \<Gamma>1 \<turnstile> P <: Q \<longrightarrow> (\<Delta>1@(X1,P)#\<Gamma>1) \<turnstile> \<forall> [X2<:S1].S2 <: \<forall> [X2<:T1].T2)"
   906       have b0: "(\<Delta>@(X,P)#\<Gamma>2) \<turnstile> T1 <: S1" using \<Gamma>1_inst a2 lh_drv_prem1 IH_inner1 by simp
   910       proof (intro strip)
   907       have b1: "(((X1,T1)#\<Delta>)@(X,P)#\<Gamma>2) \<turnstile> S2 <: T2" using \<Gamma>1_inst a2 lh_drv_prem3 IH_inner2
   911 	fix P
   908 	apply(auto)
   912 	assume a1: "\<Gamma>2 = \<Delta>1@(X1,Q)#\<Gamma>1"
   909 	apply(drule_tac x="\<Gamma>2" in meta_spec)
   913 	and    a2: "\<Gamma>1 \<turnstile> P <: Q"
   910 	apply(drule_tac x="(X1,T1)#\<Delta>" in meta_spec)
   914 	have b0: "(\<Delta>1@(X1,P)#\<Gamma>1) \<turnstile> T1 <: S1" using a1 a2 lh_drv_prem1 IH_inner1 by simp
   911 	apply(auto)
   915 	have b1: "(((X2,T1)#\<Delta>1)@(X1,P)#\<Gamma>1) \<turnstile> S2 <: T2" using a1 a2 lh_drv_prem3 IH_inner2
   912 	done
   916 	  apply auto
   913       have b3: "X1\<sharp>(\<Delta>@(X,P)#\<Gamma>2)" using lh_drv_prem2 \<Gamma>1_inst a2
   917 	  apply (drule_tac x="(X2,T1)#\<Delta>1" in spec)
   914 	by (auto simp add: fresh_prod fresh_list_append fresh_list_cons dest: subtype_implies_fresh)
   918 	  apply(simp)
   915       show "(\<Delta>@(X,P)#\<Gamma>2) \<turnstile> \<forall> [X1<:S1].S2 <: \<forall> [X1<:T1].T2" using b0 b3 b1 by force 
   919 	  done
       
   920 	have b3: "X2\<sharp>(\<Delta>1@(X1,P)#\<Gamma>1)" using lh_drv_prem2 freshness a2
       
   921 	  by (auto simp add: fresh_prod fresh_list_append fresh_list_cons dest: subtype_implies_fresh)
       
   922 	show "(\<Delta>1@(X1,P)#\<Gamma>1) \<turnstile> \<forall> [X2<:S1].S2 <: \<forall> [X2<:T1].T2" using b0 b3 b1 by force 
       
   923       qed
       
   924     qed
   916     qed
   925   qed
   917   qed
   926   from transitivity narrowing show ?case by blast 
   918   from transitivity narrowing show ?case by blast 
   927 qed
   919 qed
   928 
   920