src/HOL/Tools/function_package/fundef_prep.ML
changeset 19583 c5fa77b03442
parent 19564 d3e2f532459a
child 19770 be5c23ebe1eb
equal deleted inserted replaced
19582:a669c98b9c24 19583:c5fa77b03442
    85     end
    85     end
    86 
    86 
    87 
    87 
    88 fun mk_clause_info thy (names:naming_context) (no, (qs,gs,lhs,rhs)) (GI,tree) RIs =
    88 fun mk_clause_info thy (names:naming_context) (no, (qs,gs,lhs,rhs)) (GI,tree) RIs =
    89     let
    89     let
    90 	val {domT, G, R, h, f, fvar, used, x, ...} = names
    90 	val Names {domT, G, R, h, f, fvar, used, x, ...} = names
    91 				 
    91 				 
    92 	val zv = Var (("z",0), domT) (* for generating h_assums *)
    92 	val zv = Var (("z",0), domT) (* for generating h_assums *)
    93 	val xv = Var (("x",0), domT)
    93 	val xv = Var (("x",0), domT)
    94 	val rw_RI_to_h_assum = (mk_mem (mk_prod (zv, xv), R),
    94 	val rw_RI_to_h_assum = (mk_mem (mk_prod (zv, xv), R),
    95 				mk_mem (mk_prod (zv, h $ zv), G))
    95 				mk_mem (mk_prod (zv, h $ zv), G))
   128 			  
   128 			  
   129 		val Gh_term = Pattern.rewrite_term thy [rw_RI_to_h_assum, rw_f_to_h] [] (prop_of lRIq)
   129 		val Gh_term = Pattern.rewrite_term thy [rw_RI_to_h_assum, rw_f_to_h] [] (prop_of lRIq)
   130 		val Gh = assume (cterm_of thy Gh_term)
   130 		val Gh = assume (cterm_of thy Gh_term)
   131 		val Gh' = assume (cterm_of thy (rename_vars Gh_term))
   131 		val Gh' = assume (cterm_of thy (rename_vars Gh_term))
   132 	    in
   132 	    in
   133 		{RI=RI, RIvs=RIvs, lRI=lRI, lRIq=lRIq, Gh=Gh, Gh'=Gh'}
   133 		RCInfo {RI=RI, RIvs=RIvs, lRI=lRI, lRIq=lRIq, Gh=Gh, Gh'=Gh'}
   134 	    end
   134 	    end
   135 
   135 
   136 	val case_hyp = assume (cterm_of thy (Trueprop (mk_eq (x, lhs))))
   136 	val case_hyp = assume (cterm_of thy (Trueprop (mk_eq (x, lhs))))
   137     in
   137     in
   138 	{
   138 	ClauseInfo
   139 	 no=no,
   139 	    {
   140 	 qs=qs, gs=gs, lhs=lhs, rhs=rhs,		 
   140 	     no=no,
   141 	 cqs=cqs, cvqs=cvqs, ags=ags,		 
   141 	     qs=qs, gs=gs, lhs=lhs, rhs=rhs,		 
   142 	 cqs'=cqs', ags'=ags', lhs'=lhs', rhs'=rhs', ordcqs' = ordcqs',
   142 	     cqs=cqs, cvqs=cvqs, ags=ags,		 
   143 	 GI=GI, lGI=lGI, RCs=map mk_call_info RIs,
   143 	     cqs'=cqs', ags'=ags', lhs'=lhs', rhs'=rhs', ordcqs' = ordcqs',
   144 	 tree=tree, case_hyp = case_hyp
   144 	     GI=GI, lGI=lGI, RCs=map mk_call_info RIs,
   145 	}
   145 	     tree=tree, case_hyp = case_hyp
       
   146 	    }
   146     end
   147     end
   147 
   148 
   148 
   149 
   149 
   150 
   150 
   151 
   189 				    Const ("The", (ranT --> boolT) --> ranT) $
   190 				    Const ("The", (ranT --> boolT) --> ranT) $
   190 					  Abs ("y", ranT, mk_relmemT domT ranT (x, Bound 0) G))
   191 					  Abs ("y", ranT, mk_relmemT domT ranT (x, Bound 0) G))
   191 
   192 
   192 	val ([f_def], thy) = PureThy.add_defs_i false [((fxname ^ "_def", f_eq), [])] thy
   193 	val ([f_def], thy) = PureThy.add_defs_i false [((fxname ^ "_def", f_eq), [])] thy
   193     in
   194     in
   194 	({f=f, glrs=glrs, glrs'=glrs', fvar=fvar, fvarname=fvarname, domT=domT, ranT=ranT, G=G, R=R, acc_R=acc_R, h=h, x=x, y=y, z=z, a=a, D=D, P=P, Pbool=Pbool, f_def=f_def, used=allused, trees=trees}, thy)
   195 	(Names {f=f, glrs=glrs, glrs'=glrs', fvar=fvar, fvarname=fvarname, domT=domT, ranT=ranT, G=G, R=R, acc_R=acc_R, h=h, x=x, y=y, z=z, a=a, D=D, P=P, Pbool=Pbool, f_def=f_def, used=allused, trees=trees}, thy)
   195     end
   196     end
   196 
   197 
   197 
   198 
   198 (* Gs ==> Gs' ==> lhs = lhs' ==> rhs = rhs' *)
   199 (* Gs ==> Gs' ==> lhs = lhs' ==> rhs = rhs' *)
   199 fun mk_compat_impl ((qs, gs, lhs, rhs),(qs', gs', lhs', rhs')) =
   200 fun mk_compat_impl ((qs, gs, lhs, rhs),(qs', gs', lhs', rhs')) =
   220 	(map (map dest_Free) vRis, preRis, Ris)
   221 	(map (map dest_Free) vRis, preRis, Ris)
   221     end
   222     end
   222 
   223 
   223 fun mk_GIntro thy names (qs, gs, lhs, rhs) Ris =
   224 fun mk_GIntro thy names (qs, gs, lhs, rhs) Ris =
   224     let
   225     let
   225 	val { domT, R, G, f, fvar, h, y, Pbool, ... } = names
   226 	val Names { domT, R, G, f, fvar, h, y, Pbool, ... } = names
   226 
   227 
   227 	val z = Var (("z",0), domT)
   228 	val z = Var (("z",0), domT)
   228 	val x = Var (("x",0), domT)
   229 	val x = Var (("x",0), domT)
   229 
   230 
   230 	val rew1 = (mk_mem (mk_prod (z, x), R),
   231 	val rew1 = (mk_mem (mk_prod (z, x), R),
   239 		  |> fold_rev (curry Logic.mk_implies) gs
   240 		  |> fold_rev (curry Logic.mk_implies) gs
   240     end
   241     end
   241 
   242 
   242 fun mk_completeness names glrs =
   243 fun mk_completeness names glrs =
   243     let
   244     let
   244 	val {domT, x, Pbool, ...} = names
   245 	val Names {domT, x, Pbool, ...} = names
   245 
   246 
   246 	fun mk_case (qs, gs, lhs, _) = Trueprop Pbool
   247 	fun mk_case (qs, gs, lhs, _) = Trueprop Pbool
   247 						|> curry Logic.mk_implies (Trueprop (mk_eq (x, lhs)))
   248 						|> curry Logic.mk_implies (Trueprop (mk_eq (x, lhs)))
   248 						|> fold_rev (curry Logic.mk_implies) gs
   249 						|> fold_rev (curry Logic.mk_implies) gs
   249 						|> fold_rev mk_forall qs
   250 						|> fold_rev mk_forall qs
   253     end
   254     end
   254 
   255 
   255 
   256 
   256 fun extract_conditions thy names trees congs =
   257 fun extract_conditions thy names trees congs =
   257     let
   258     let
   258 	val {f, G, R, acc_R, domT, ranT, f_def, x, z, fvarname, glrs, glrs', ...} = names
   259 	val Names {f, G, R, acc_R, domT, ranT, f_def, x, z, fvarname, glrs, glrs', ...} = names
   259 
   260 
   260 	val (vRiss, preRiss, Riss) = split_list3 (map2 (extract_Ris thy congs f R) trees glrs)
   261 	val (vRiss, preRiss, Riss) = split_list3 (map2 (extract_Ris thy congs f R) trees glrs)
   261 	val Gis = map2 (mk_GIntro thy names) glrs preRiss
   262 	val Gis = map2 (mk_GIntro thy names) glrs preRiss
   262 	val complete = mk_completeness names glrs
   263 	val complete = mk_completeness names glrs
   263 	val compat = mk_compat_proof_obligations glrs glrs'
   264 	val compat = mk_compat_proof_obligations glrs glrs'
   292  * and comatibility conditions and returns everything.
   293  * and comatibility conditions and returns everything.
   293  *)
   294  *)
   294 fun prepare_fundef congs eqs fname thy =
   295 fun prepare_fundef congs eqs fname thy =
   295     let
   296     let
   296 	val (names, thy) = setup_context (analyze_eqs eqs) fname congs thy
   297 	val (names, thy) = setup_context (analyze_eqs eqs) fname congs thy
   297 	val {G, R, glrs, trees, ...} = names
   298 	val Names {G, R, glrs, trees, ...} = names
   298 
   299 
   299 	val {G_intros, vRiss, R_intross, complete, compat} = extract_conditions thy names trees congs
   300 	val {G_intros, vRiss, R_intross, complete, compat} = extract_conditions thy names trees congs
   300 
   301 
   301 	val (G_intro_thms, (thy, _)) = inductive_def G_intros (thy, G)
   302 	val (G_intro_thms, (thy, _)) = inductive_def G_intros (thy, G)
   302 	val (R_intro_thmss, (thy, _)) = fold_burrow inductive_def R_intross (thy, R)
   303 	val (R_intro_thmss, (thy, _)) = fold_burrow inductive_def R_intross (thy, R)
   303 
   304 
   304 	val n = length glrs
   305 	val n = length glrs
   305 	val clauses = map3 (mk_clause_info thy names) ((1 upto n) ~~ glrs) (G_intro_thms ~~ trees) (map2 (curry op ~~) vRiss R_intro_thmss)
   306 	val clauses = map3 (mk_clause_info thy names) ((1 upto n) ~~ glrs) (G_intro_thms ~~ trees) (map2 (curry op ~~) vRiss R_intro_thmss)
   306     in
   307     in
   307 	({names = names, complete=complete, compat=compat, clauses = clauses},
   308 	(Prep {names = names, complete=complete, compat=compat, clauses = clauses},
   308 	 thy) 
   309 	 thy) 
   309     end
   310     end
   310 
   311 
   311 
   312 
   312 
   313 
   344 		val ([f_def], thy) = PureThy.add_defs_i false [((fxname ^ "_def", def), [])] thy
   345 		val ([f_def], thy) = PureThy.add_defs_i false [((fxname ^ "_def", def), [])] thy
   345 				      
   346 				      
   346 		val g_to_f_ss = HOL_basic_ss addsimps [symmetric f_def]
   347 		val g_to_f_ss = HOL_basic_ss addsimps [symmetric f_def]
   347 		val eqs_tupled = map (Pattern.rewrite_term thy [(f, f_lambda)] []) eqs
   348 		val eqs_tupled = map (Pattern.rewrite_term thy [(f, f_lambda)] []) eqs
   348 	    in
   349 	    in
   349 		(SOME {curry_ss = g_to_f_ss, argTs = caTs}, fxname, prepare_fundef congs eqs_tupled fxname thy)
   350 		(SOME (Curry {curry_ss = g_to_f_ss, argTs = caTs}), fxname, prepare_fundef congs eqs_tupled fxname thy)
   350 	    end
   351 	    end
   351     end
   352     end
   352 
   353 
   353 
   354 
   354 
   355