Nominal/Ex/LamTest.thy
changeset 2662 7c5bca978886
parent 2661 16896fd8eff5
child 3104 f7c4b8e6918b
equal deleted inserted replaced
2661:16896fd8eff5 2662:7c5bca978886
     7 nominal_datatype lam =
     7 nominal_datatype lam =
     8   Var "name"
     8   Var "name"
     9 | App "lam" "lam"
     9 | App "lam" "lam"
    10 | Lam x::"name" l::"lam"  bind x in l
    10 | Lam x::"name" l::"lam"  bind x in l
    11 
    11 
    12 definition
       
    13  "eqvt_at f x \<equiv> \<forall>p. p \<bullet> (f x) = f (p \<bullet> x)"
       
    14 
       
    15 lemma supp_eqvt_at:
       
    16   assumes asm: "eqvt_at f x"
       
    17   and     fin: "finite (supp x)"
       
    18   shows "supp (f x) \<subseteq> supp x"
       
    19 apply(rule supp_is_subset)
       
    20 unfolding supports_def
       
    21 unfolding fresh_def[symmetric]
       
    22 using asm
       
    23 apply(simp add: eqvt_at_def)
       
    24 apply(simp add: swap_fresh_fresh)
       
    25 apply(rule fin)
       
    26 done
       
    27 
       
    28 lemma finite_supp_eqvt_at:
       
    29   assumes asm: "eqvt_at f x"
       
    30   and     fin: "finite (supp x)"
       
    31   shows "finite (supp (f x))"
       
    32 apply(rule finite_subset)
       
    33 apply(rule supp_eqvt_at[OF asm fin])
       
    34 apply(rule fin)
       
    35 done
       
    36 
       
    37 lemma fresh_eqvt_at:
       
    38   assumes asm: "eqvt_at f x"
       
    39   and     fin: "finite (supp x)"
       
    40   and     fresh: "as \<sharp>* x"
       
    41   shows "as \<sharp>* f x"
       
    42 using fresh
       
    43 unfolding fresh_star_def
       
    44 unfolding fresh_def
       
    45 using supp_eqvt_at[OF asm fin]
       
    46 by auto
       
    47 
       
    48 definition
       
    49  "eqvt f \<equiv> \<forall>p. p \<bullet> f = f"
       
    50 
       
    51 lemma eqvtI:
       
    52   "(\<And>p. p \<bullet> f \<equiv> f) \<Longrightarrow> eqvt f"
       
    53 apply(auto simp add: eqvt_def)
       
    54 done
       
    55 
       
    56 lemma the_default_eqvt:
       
    57   assumes unique: "\<exists>!x. P x"
       
    58   shows "(p \<bullet> (THE_default d P)) = (THE_default d (p \<bullet> P))"
       
    59   apply(rule THE_default1_equality [symmetric])
       
    60   apply(rule_tac p="-p" in permute_boolE)
       
    61   apply(perm_simp add: permute_minus_cancel)
       
    62   apply(rule unique)
       
    63   apply(rule_tac p="-p" in permute_boolE)
       
    64   apply(perm_simp add: permute_minus_cancel)
       
    65   apply(rule THE_defaultI'[OF unique])
       
    66   done
       
    67 
       
    68 lemma fundef_ex1_eqvt:
       
    69   fixes x::"'a::pt"
       
    70   assumes f_def: "f == (\<lambda>x::'a. THE_default d (G x))"
       
    71   assumes eqvt: "eqvt G"
       
    72   assumes ex1: "\<exists>!y. G x y"
       
    73   shows "(p \<bullet> (f x)) = f (p \<bullet> x)"
       
    74   apply (simp only: f_def)
       
    75   apply(subst the_default_eqvt)
       
    76   apply (rule ex1)
       
    77   apply(perm_simp)
       
    78   using eqvt
       
    79   apply(simp add: eqvt_def)
       
    80   done
       
    81 
       
    82 lemma fundef_ex1_eqvt_at:
       
    83   fixes x::"'a::pt"
       
    84   assumes f_def: "f == (\<lambda>x::'a. THE_default d (G x))"
       
    85   assumes eqvt: "eqvt G"
       
    86   assumes ex1: "\<exists>!y. G x y"
       
    87   shows "eqvt_at f x"
       
    88   unfolding eqvt_at_def
       
    89   using assms
       
    90   apply(auto intro: fundef_ex1_eqvt)
       
    91   done
       
    92 
    12 
    93 ML {*
    13 ML {*
    94 
    14 
    95 val trace = Unsynchronized.ref false
    15 val trace = Unsynchronized.ref false
    96 fun trace_msg msg = if ! trace then tracing (msg ()) else ()
    16 fun trace_msg msg = if ! trace then tracing (msg ()) else ()
   331     (thms1 :: store_compat_thms (n - 1) thms2)
   251     (thms1 :: store_compat_thms (n - 1) thms2)
   332   end
   252   end
   333 *}
   253 *}
   334 
   254 
   335 ML {*
   255 ML {*
   336 fun pp_thm thm =
       
   337   let
       
   338     val hyps = Thm.hyps_of thm
       
   339     val tpairs = Thm.terms_of_tpairs (Thm.tpairs_of thm)
       
   340   in
       
   341       (@{make_string} thm) ^ "\n    hyps: " ^ (commas (map (Syntax.string_of_term @{context}) hyps))
       
   342       ^ "    tpairs: " ^   (commas (map (Syntax.string_of_term @{context}) tpairs))
       
   343   end
       
   344 *}
       
   345 
       
   346 
       
   347 ML {*
       
   348 fun eqvt_at_elim thy (eqvts:thm list) thm =
       
   349   case (prop_of thm) of
       
   350     Const ("==>", _) $ (t as (@{term Trueprop} $ (Const (@{const_name eqvt_at}, _) $ _ $ _))) $ _ => 
       
   351       let
       
   352         val el_thm = Skip_Proof.make_thm thy t
       
   353         val _ = tracing ("NEED TO PROVE  " ^ @{make_string} el_thm)
       
   354         val _ = tracing ("HAVE\n" ^ cat_lines (map @{make_string} eqvts))
       
   355       in
       
   356         Thm.elim_implies el_thm thm 
       
   357         |> eqvt_at_elim thy eqvts 
       
   358       end
       
   359   | _ => thm
       
   360 *}
       
   361 
       
   362 ML {*
       
   363 (* expects i <= j *)
   256 (* expects i <= j *)
   364 fun lookup_compat_thm i j cts =
   257 fun lookup_compat_thm i j cts =
   365   nth (nth cts (i - 1)) (j - i)
   258   nth (nth cts (i - 1)) (j - i)
   366 
   259 
   367 (* Returns "Gsi, Gsj, lhs_i = lhs_j |-- rhs_j_f = rhs_i_f" *)
   260 (* Returns "Gsi, Gsj, lhs_i = lhs_j |-- rhs_j_f = rhs_i_f" *)
   373 
   266 
   374     val lhsi_eq_lhsj = cterm_of thy (HOLogic.mk_Trueprop (mk_eq (lhsi, lhsj)))
   267     val lhsi_eq_lhsj = cterm_of thy (HOLogic.mk_Trueprop (mk_eq (lhsi, lhsj)))
   375   in if j < i then
   268   in if j < i then
   376     let
   269     let
   377       val compat = lookup_compat_thm j i cts
   270       val compat = lookup_compat_thm j i cts
   378       val _ = tracing ("XXXX compat clause if:\n" ^ @{make_string} compat)
       
   379     in
   271     in
   380       compat         (* "!!qj qi. Gsj => Gsi => lhsj = lhsi ==> rhsj = rhsi" *)
   272       compat         (* "!!qj qi. Gsj => Gsi => lhsj = lhsi ==> rhsj = rhsi" *)
   381       |> fold Thm.forall_elim (cqsj @ cqsi) (* "Gsj => Gsi => lhsj = lhsi ==> rhsj = rhsi" *)
   273       |> fold Thm.forall_elim (cqsj @ cqsi) (* "Gsj => Gsi => lhsj = lhsi ==> rhsj = rhsi" *)
   382       |> fold Thm.elim_implies (rev eqvtsj) (* HERE *)
   274       |> fold Thm.elim_implies (rev eqvtsj) (* HERE *)
   383       (*|> eqvt_at_elim thy eqvtsj *)
       
   384       |> fold Thm.elim_implies agsj
   275       |> fold Thm.elim_implies agsj
   385       |> fold Thm.elim_implies agsi
   276       |> fold Thm.elim_implies agsi
   386       |> Thm.elim_implies ((Thm.assume lhsi_eq_lhsj) RS sym) (* "Gsj, Gsi, lhsi = lhsj |-- rhsj = rhsi" *)
   277       |> Thm.elim_implies ((Thm.assume lhsi_eq_lhsj) RS sym) (* "Gsj, Gsi, lhsi = lhsj |-- rhsj = rhsi" *)
   387     end
   278     end
   388     else
   279     else
   389     let
   280     let
   390       val compat = lookup_compat_thm i j cts
   281       val compat = lookup_compat_thm i j cts
   391       (* val _ = tracing ("XXXX compat clause else:\n" ^ @{make_string} compat) *)
       
   392     in
   282     in
   393       compat        (* "!!qi qj. Gsi => Gsj => lhsi = lhsj ==> rhsi = rhsj" *)
   283       compat        (* "!!qi qj. Gsi => Gsj => lhsi = lhsj ==> rhsi = rhsj" *)
   394       |> fold Thm.forall_elim (cqsi @ cqsj) (* "Gsi => Gsj => lhsi = lhsj ==> rhsi = rhsj" *)
   284       |> fold Thm.forall_elim (cqsi @ cqsj) (* "Gsi => Gsj => lhsi = lhsj ==> rhsi = rhsj" *)
   395       |> fold Thm.elim_implies (rev eqvtsi)  (* HERE *)
   285       |> fold Thm.elim_implies (rev eqvtsi)  (* HERE *)
   396       (* |> eqvt_at_elim thy eqvtsi *)
       
   397       |> fold Thm.elim_implies agsi
   286       |> fold Thm.elim_implies agsi
   398       |> fold Thm.elim_implies agsj
   287       |> fold Thm.elim_implies agsj
   399       |> Thm.elim_implies (Thm.assume lhsi_eq_lhsj)
   288       |> Thm.elim_implies (Thm.assume lhsi_eq_lhsj)
   400       |> (fn thm => thm RS sym) (* "Gsi, Gsj, lhsi = lhsj |-- rhsj = rhsi" *)
   289       |> (fn thm => thm RS sym) (* "Gsi, Gsj, lhsi = lhsj |-- rhsj = rhsi" *)
   401     end
   290     end
   460 *}
   349 *}
   461 
   350 
   462 ML {*
   351 ML {*
   463 fun mk_uniqueness_clause thy globals compat_store eqvts clausei clausej RLj =
   352 fun mk_uniqueness_clause thy globals compat_store eqvts clausei clausej RLj =
   464   let
   353   let
   465     val _ = tracing "START"
       
   466 
       
   467     val Globals {h, y, x, fvar, ...} = globals
   354     val Globals {h, y, x, fvar, ...} = globals
   468     val ClauseInfo {no=i, cdata=cctxi as ClauseContext {ctxt=ctxti, lhs=lhsi, case_hyp, cqs = cqsi, ags = agsi, ...}, ...} = clausei
   355     val ClauseInfo {no=i, cdata=cctxi as ClauseContext {ctxt=ctxti, lhs=lhsi, case_hyp, cqs = cqsi, 
       
   356       ags = agsi, ...}, ...} = clausei
   469     val ClauseInfo {no=j, qglr=cdescj, RCs=RCsj, ...} = clausej
   357     val ClauseInfo {no=j, qglr=cdescj, RCs=RCsj, ...} = clausej
   470 
   358 
   471     val cctxj as ClauseContext {ags = agsj', lhs = lhsj', rhs = rhsj', qs = qsj', cqs = cqsj', ...} =
   359     val cctxj as ClauseContext {ags = agsj', lhs = lhsj', rhs = rhsj', qs = qsj', cqs = cqsj', ...} =
   472       mk_clause_context x ctxti cdescj 
   360       mk_clause_context x ctxti cdescj 
   473 
   361 
   477       (fn RCInfo {h_assum, ...} => Thm.assume (cterm_of thy (subst_bounds (rev qsj', h_assum)))) RCsj
   365       (fn RCInfo {h_assum, ...} => Thm.assume (cterm_of thy (subst_bounds (rev qsj', h_assum)))) RCsj
   478 
   366 
   479     val y_eq_rhsj'h = Thm.assume (cterm_of thy (HOLogic.mk_Trueprop (mk_eq (y, rhsj'h))))
   367     val y_eq_rhsj'h = Thm.assume (cterm_of thy (HOLogic.mk_Trueprop (mk_eq (y, rhsj'h))))
   480     val lhsi_eq_lhsj' = Thm.assume (cterm_of thy (HOLogic.mk_Trueprop (mk_eq (lhsi, lhsj'))))
   368     val lhsi_eq_lhsj' = Thm.assume (cterm_of thy (HOLogic.mk_Trueprop (mk_eq (lhsi, lhsj'))))
   481        (* lhs_i = lhs_j' |-- lhs_i = lhs_j' *) 
   369        (* lhs_i = lhs_j' |-- lhs_i = lhs_j' *) 
   482 
       
   483     val _ = tracing "POINT B"
       
   484 
   370 
   485     val case_hypj' = trans OF [case_hyp, lhsi_eq_lhsj']
   371     val case_hypj' = trans OF [case_hyp, lhsi_eq_lhsj']
   486 
   372 
   487     val RLj_import = RLj
   373     val RLj_import = RLj
   488       |> fold Thm.forall_elim cqsj'
   374       |> fold Thm.forall_elim cqsj'
   489       |> fold Thm.elim_implies agsj'
   375       |> fold Thm.elim_implies agsj'
   490       |> fold Thm.elim_implies Ghsj'
   376       |> fold Thm.elim_implies Ghsj'
   491 
   377 
   492     val _ = tracing "POINT C"
       
   493  
       
   494     val eqvtsi = nth eqvts (i - 1)
   378     val eqvtsi = nth eqvts (i - 1)
   495       |> map (fold Thm.forall_elim cqsi)
   379       |> map (fold Thm.forall_elim cqsi)
   496       |> map (fold Thm.elim_implies [case_hyp])
   380       |> map (fold Thm.elim_implies [case_hyp])
   497       |> map (fold Thm.elim_implies agsi)
   381       |> map (fold Thm.elim_implies agsi)
   498 
   382 
   499     val _ = tracing "POINT D"
       
   500 
       
   501     val eqvtsj = nth eqvts (j - 1)
   383     val eqvtsj = nth eqvts (j - 1)
   502       |> tap (fn thms => tracing ("O1:\n" ^ cat_lines (map @{make_string} thms)))
       
   503       |> map (fold Thm.forall_elim cqsj')
   384       |> map (fold Thm.forall_elim cqsj')
   504       |> tap (fn thms => tracing ("O2:\n" ^ cat_lines (map @{make_string} thms)))
       
   505       |> map (fold Thm.elim_implies [case_hypj'])
   385       |> map (fold Thm.elim_implies [case_hypj'])
   506       |> tap (fn thms => tracing ("O3:\n" ^ cat_lines (map @{make_string} thms)))
       
   507       |> map (fold Thm.elim_implies agsj')
   386       |> map (fold Thm.elim_implies agsj')
   508 
   387 
   509     val _ = tracing ("eqvtsi:\n" ^ cat_lines (map @{make_string} eqvtsi))
       
   510     val _ = tracing ("eqvtsj:\n" ^ cat_lines (map @{make_string} eqvtsj))
       
   511     val _ = tracing ("case_hypi:\n" ^ @{make_string} case_hyp)
       
   512     val _ = tracing ("case_hypj:\n" ^ @{make_string} case_hypj')
       
   513 
       
   514     val compat = get_compat_thm thy compat_store eqvtsi eqvtsj i j cctxi cctxj
   388     val compat = get_compat_thm thy compat_store eqvtsi eqvtsj i j cctxi cctxj
   515     
   389 
   516     val _ = tracing ("compats:\n" ^ pp_thm compat) 
       
   517     
       
   518 
       
   519     fun pppp str = tap (fn thm => tracing (str ^ ": " ^ pp_thm thm))
       
   520     fun ppp str = I
       
   521   in
   390   in
   522     (trans OF [case_hyp, lhsi_eq_lhsj']) (* lhs_i = lhs_j' |-- x = lhs_j' *)
   391     (trans OF [case_hyp, lhsi_eq_lhsj']) (* lhs_i = lhs_j' |-- x = lhs_j' *)
   523     |> pppp "A"
       
   524     |> Thm.implies_elim RLj_import
   392     |> Thm.implies_elim RLj_import
   525       (* Rj1' ... Rjk', lhs_i = lhs_j' |-- rhs_j'_h = rhs_j'_f *)
   393       (* Rj1' ... Rjk', lhs_i = lhs_j' |-- rhs_j'_h = rhs_j'_f *)
   526     |> pppp "B"
       
   527     |> (fn it => trans OF [it, compat])
   394     |> (fn it => trans OF [it, compat])
   528       (* lhs_i = lhs_j', Gj', Rj1' ... Rjk' |-- rhs_j'_h = rhs_i_f *)
   395       (* lhs_i = lhs_j', Gj', Rj1' ... Rjk' |-- rhs_j'_h = rhs_i_f *)
   529     |> pppp "C"
       
   530     |> (fn it => trans OF [y_eq_rhsj'h, it])
   396     |> (fn it => trans OF [y_eq_rhsj'h, it])
   531       (* lhs_i = lhs_j', Gj', Rj1' ... Rjk', y = rhs_j_h' |-- y = rhs_i_f *)
   397       (* lhs_i = lhs_j', Gj', Rj1' ... Rjk', y = rhs_j_h' |-- y = rhs_i_f *)
   532     |> pppp "D"
       
   533     |> fold_rev (Thm.implies_intr o cprop_of) Ghsj'
   398     |> fold_rev (Thm.implies_intr o cprop_of) Ghsj'
   534     |> pppp "E"
       
   535     |> fold_rev (Thm.implies_intr o cprop_of) agsj'
   399     |> fold_rev (Thm.implies_intr o cprop_of) agsj'
   536       (* lhs_i = lhs_j' , y = rhs_j_h' |-- Gj', Rj1'...Rjk' ==> y = rhs_i_f *)
   400       (* lhs_i = lhs_j' , y = rhs_j_h' |-- Gj', Rj1'...Rjk' ==> y = rhs_i_f *)
   537     |> pppp "F"
       
   538     |> Thm.implies_intr (cprop_of y_eq_rhsj'h)
   401     |> Thm.implies_intr (cprop_of y_eq_rhsj'h)
   539     |> pppp "G"
       
   540     |> Thm.implies_intr (cprop_of lhsi_eq_lhsj')
   402     |> Thm.implies_intr (cprop_of lhsi_eq_lhsj')
   541     |> pppp "H"
       
   542     |> fold_rev Thm.forall_intr (cterm_of thy h :: cqsj')
   403     |> fold_rev Thm.forall_intr (cterm_of thy h :: cqsj')
   543     |> pppp "I"
   404   end
   544   end
   405 *}
   545 *}
   406 
   546 
   407 
   547 
   408 ML {*
   548 ML {*
   409 fun mk_uniqueness_case thy globals G f ihyp ih_intro G_cases compat_store clauses replems eqvtlems clausei =
   549 fun mk_uniqueness_case thy globals G f ihyp ih_intro G_cases compat_store clauses rep_lemmas eqvt_lemmas clausei =
       
   550   let
   410   let
   551     val Globals {x, y, ranT, fvar, ...} = globals
   411     val Globals {x, y, ranT, fvar, ...} = globals
   552     val ClauseInfo {cdata = ClauseContext {lhs, rhs, cqs, ags, case_hyp, ...}, lGI, RCs, ...} = clausei
   412     val ClauseInfo {cdata = ClauseContext {lhs, rhs, cqs, ags, case_hyp, ...}, lGI, RCs, ...} = clausei
   553     val rhsC = Pattern.rewrite_term thy [(fvar, f)] [] rhs
   413     val rhsC = Pattern.rewrite_term thy [(fvar, f)] [] rhs
   554 
   414 
   563 
   423 
   564     val P = cterm_of thy (mk_eq (y, rhsC))
   424     val P = cterm_of thy (mk_eq (y, rhsC))
   565     val G_lhs_y = Thm.assume (cterm_of thy (HOLogic.mk_Trueprop (G $ lhs $ y)))
   425     val G_lhs_y = Thm.assume (cterm_of thy (HOLogic.mk_Trueprop (G $ lhs $ y)))
   566 
   426 
   567     val unique_clauses =
   427     val unique_clauses =
   568       map2 (mk_uniqueness_clause thy globals compat_store eqvt_lemmas clausei) clauses rep_lemmas
   428       map2 (mk_uniqueness_clause thy globals compat_store eqvtlems clausei) clauses replems
   569 
       
   570     val _ = tracing ("DONE unique clauses") 
       
   571 
   429 
   572     fun elim_implies_eta A AB =
   430     fun elim_implies_eta A AB =
   573       Thm.compose_no_flatten true (A, 0) 1 AB |> Seq.list_of |> the_single
   431       Thm.compose_no_flatten true (A, 0) 1 AB |> Seq.list_of |> the_single
   574 
   432 
   575     val uniqueness = G_cases
   433     val uniqueness = G_cases
   603     (exactly_one, function_value)
   461     (exactly_one, function_value)
   604   end
   462   end
   605 *}
   463 *}
   606 
   464 
   607 
   465 
   608 ML {* (ex1_implies_ex, ex1_implies_un) *}
       
   609 thm fundef_ex1_eqvt_at
       
   610 
       
   611 ML {*
   466 ML {*
   612 fun prove_stuff ctxt globals G f R clauses complete compat compat_store G_elim G_eqvt f_def =
   467 fun prove_stuff ctxt globals G f R clauses complete compat compat_store G_elim G_eqvt f_def =
   613   let
   468   let
   614     val Globals {h, domT, ranT, x, ...} = globals
   469     val Globals {h, domT, ranT, x, ...} = globals
   615     val thy = ProofContext.theory_of ctxt
   470     val thy = ProofContext.theory_of ctxt
   624     val ihyp_thm = Thm.assume ihyp |> Thm.forall_elim_vars 0
   479     val ihyp_thm = Thm.assume ihyp |> Thm.forall_elim_vars 0
   625     val ih_intro = ihyp_thm RS (f_def RS ex1_implies_ex)
   480     val ih_intro = ihyp_thm RS (f_def RS ex1_implies_ex)
   626     val ih_elim = ihyp_thm RS (f_def RS ex1_implies_un)
   481     val ih_elim = ihyp_thm RS (f_def RS ex1_implies_un)
   627       |> instantiate' [] [NONE, SOME (cterm_of thy h)]
   482       |> instantiate' [] [NONE, SOME (cterm_of thy h)]
   628     val ih_eqvt = ihyp_thm RS (G_eqvt RS (f_def RS @{thm fundef_ex1_eqvt_at}))
   483     val ih_eqvt = ihyp_thm RS (G_eqvt RS (f_def RS @{thm fundef_ex1_eqvt_at}))
   629 
       
   630     (*
       
   631     val _ = tracing ("ihyp_thm\n" ^ pp_thm ihyp_thm)
       
   632     val _ = tracing ("ih_intro\n" ^ pp_thm ih_intro)
       
   633     val _ = tracing ("ih_elim\n" ^ pp_thm ih_elim)
       
   634     val _ = tracing ("ih_eqvt\n" ^ pp_thm ih_eqvt)
       
   635     *)
       
   636 
   484 
   637     val _ = trace_msg (K "Proving Replacement lemmas...")
   485     val _ = trace_msg (K "Proving Replacement lemmas...")
   638     val repLemmas = map (mk_replacement_lemma thy h ih_elim) clauses
   486     val repLemmas = map (mk_replacement_lemma thy h ih_elim) clauses
   639 
   487 
   640     val _ = trace_msg (K "Proving Equivariance lemmas...")
   488     val _ = trace_msg (K "Proving Equivariance lemmas...")
   664     (goalstate, values)
   512     (goalstate, values)
   665   end
   513   end
   666 *}
   514 *}
   667 
   515 
   668 
   516 
   669 
       
   670 ML {*
   517 ML {*
   671 (* wrapper -- restores quantifiers in rule specifications *)
   518 (* wrapper -- restores quantifiers in rule specifications *)
   672 fun inductive_def (binding as ((R, T), _)) intrs lthy =
   519 fun inductive_def eqvt_flag (binding as ((R, T), _)) intrs lthy =
   673   let
   520   let
   674     val ({intrs = intrs_gen, elims = [elim_gen], preds = [ Rdef ], induct, raw_induct, ...}, lthy) =
   521     val ({intrs = intrs_gen, elims = [elim_gen], preds = [ Rdef ], induct, raw_induct, ...}, lthy) =
   675       lthy
   522       lthy
   676       |> Local_Theory.conceal 
   523       |> Local_Theory.conceal 
   677       |> Inductive.add_inductive_i
   524       |> Inductive.add_inductive_i
   687           [] (* no parameters *)
   534           [] (* no parameters *)
   688           (map (fn t => (Attrib.empty_binding, t)) intrs) (* intro rules *)
   535           (map (fn t => (Attrib.empty_binding, t)) intrs) (* intro rules *)
   689           [] (* no special monos *)
   536           [] (* no special monos *)
   690       ||> Local_Theory.restore_naming lthy
   537       ||> Local_Theory.restore_naming lthy
   691 
   538 
   692     val ([eqvt_thm], lthy) = Nominal_Eqvt.raw_equivariance false [Rdef] raw_induct intrs_gen lthy
   539     val eqvt_thm' = 
   693     val eqvt_thm' = (Nominal_ThmDecls.eqvt_transform lthy eqvt_thm) RS @{thm eqvtI}
   540       if eqvt_flag = false then NONE
       
   541       else 
       
   542         let
       
   543           val ([eqvt_thm], lthy) = Nominal_Eqvt.raw_equivariance false [Rdef] raw_induct intrs_gen lthy
       
   544         in
       
   545           SOME ((Nominal_ThmDecls.eqvt_transform lthy eqvt_thm) RS @{thm eqvtI})
       
   546         end
   694 
   547 
   695     val cert = cterm_of (ProofContext.theory_of lthy)
   548     val cert = cterm_of (ProofContext.theory_of lthy)
   696     fun requantify orig_intro thm =
   549     fun requantify orig_intro thm =
   697       let
   550       let
   698         val (qs, t) = dest_all_all orig_intro
   551         val (qs, t) = dest_all_all orig_intro
   728         |> fold_rev Logic.all (fvar :: qs)
   581         |> fold_rev Logic.all (fvar :: qs)
   729       end
   582       end
   730 
   583 
   731     val G_intros = map2 mk_GIntro clauses RCss
   584     val G_intros = map2 mk_GIntro clauses RCss
   732   in
   585   in
   733     inductive_def ((Binding.name n, T), NoSyn) G_intros lthy
   586     inductive_def true ((Binding.name n, T), NoSyn) G_intros lthy
   734   end
   587   end
   735 *}
   588 *}
   736 
   589 
   737 ML {*
   590 ML {*
   738 fun define_function fdefname (fname, mixfix) domT ranT G default lthy =
   591 fun define_function fdefname (fname, mixfix) domT ranT G default lthy =
   760       |> fold_rev mk_forall_rename (map fst oqs ~~ qs)
   613       |> fold_rev mk_forall_rename (map fst oqs ~~ qs)
   761       (* "!!qs xs. CS ==> G => (r, lhs) : R" *)
   614       (* "!!qs xs. CS ==> G => (r, lhs) : R" *)
   762 
   615 
   763     val R_intross = map2 (map o mk_RIntro) (clauses ~~ qglrs) RCss
   616     val R_intross = map2 (map o mk_RIntro) (clauses ~~ qglrs) RCss
   764 
   617 
   765     val ((R, RIntro_thms, R_elim, _, R_eqvt), lthy) =
   618     val ((R, RIntro_thms, R_elim, _, _), lthy) =
   766       inductive_def ((Binding.name n, T), NoSyn) (flat R_intross) lthy
   619       inductive_def false ((Binding.name n, T), NoSyn) (flat R_intross) lthy
   767   in
   620   in
   768     ((R, Library.unflat R_intross RIntro_thms, R_elim, R_eqvt), lthy)
   621     ((R, Library.unflat R_intross RIntro_thms, R_elim), lthy)
   769   end
   622   end
   770 
   623 
   771 
   624 
   772 fun fix_globals domT ranT fvar ctxt =
   625 fun fix_globals domT ranT fvar ctxt =
   773   let
   626   let
  1143        Function_Ctx_Tree.mk_tree (fname, fT) h ctxt rhs
   996        Function_Ctx_Tree.mk_tree (fname, fT) h ctxt rhs
  1144 
   997 
  1145     val trees = map build_tree clauses
   998     val trees = map build_tree clauses
  1146     val RCss = map find_calls trees
   999     val RCss = map find_calls trees
  1147 
  1000 
  1148     val ((G, GIntro_thms, G_elim, G_induct, G_eqvt), lthy) =
  1001     val ((G, GIntro_thms, G_elim, G_induct, SOME G_eqvt), lthy) =
  1149       PROFILE "def_graph" (define_graph (graph_name defname) fvar domT ranT clauses RCss) lthy
  1002       PROFILE "def_graph" (define_graph (graph_name defname) fvar domT ranT clauses RCss) lthy
  1150 
       
  1151     (* 
       
  1152     val _ = tracing ("Graph - name: " ^ pp_thm G)
       
  1153     val _ = tracing ("Graph intros:\n" ^ cat_lines (map pp_thm GIntro_thms))
       
  1154     val _ = tracing ("Graph Equivariance " ^ pp_thm G_eqvt)
       
  1155     *)
       
  1156 
  1003 
  1157     val ((f, (_, f_defthm)), lthy) =
  1004     val ((f, (_, f_defthm)), lthy) =
  1158       PROFILE "def_fun" (define_function (defname ^ "_sumC_def") (fname, mixfix) domT ranT G default) lthy
  1005       PROFILE "def_fun" (define_function (defname ^ "_sumC_def") (fname, mixfix) domT ranT G default) lthy
  1159 
  1006 
  1160     (*
       
  1161     val _ = tracing ("f - name: " ^ pp_thm f)
       
  1162     val _ = tracing ("f_defthm:\n" ^ pp_thm f_defthm)
       
  1163     *)
       
  1164 
       
  1165     val RCss = map (map (inst_RC (ProofContext.theory_of lthy) fvar f)) RCss
  1007     val RCss = map (map (inst_RC (ProofContext.theory_of lthy) fvar f)) RCss
  1166     val trees = map (Function_Ctx_Tree.inst_tree (ProofContext.theory_of lthy) fvar f) trees
  1008     val trees = map (Function_Ctx_Tree.inst_tree (ProofContext.theory_of lthy) fvar f) trees
  1167 
  1009 
  1168     (*
  1010     val ((R, RIntro_thmss, R_elim), lthy) =
  1169     val _ = tracing ("recursive calls:\n" ^ cat_lines (map pp_thm RCss))
       
  1170     *)
       
  1171 
       
  1172     val ((R, RIntro_thmss, R_elim, R_eqvt), lthy) =
       
  1173       PROFILE "def_rel" (define_recursion_relation (rel_name defname) domT abstract_qglrs clauses RCss) lthy
  1011       PROFILE "def_rel" (define_recursion_relation (rel_name defname) domT abstract_qglrs clauses RCss) lthy
  1174 
       
  1175     (*
       
  1176     val _ = tracing ("Relation - name: " ^ pp_thm R) 
       
  1177     val _ = tracing ("Relation intros:\n" ^ cat_lines (map pp_thm RIntro_thmss))
       
  1178     val _ = tracing ("Relation Equivariance " ^ pp_thm R_eqvt)
       
  1179     *)    
       
  1180 
  1012 
  1181     val (_, lthy) =
  1013     val (_, lthy) =
  1182       Local_Theory.abbrev Syntax.mode_default ((Binding.name (dom_name defname), NoSyn), mk_acc domT R) lthy
  1014       Local_Theory.abbrev Syntax.mode_default ((Binding.name (dom_name defname), NoSyn), mk_acc domT R) lthy
  1183 
  1015 
  1184     val newthy = ProofContext.theory_of lthy
  1016     val newthy = ProofContext.theory_of lthy
  1196     val compat =
  1028     val compat =
  1197       mk_compat_proof_obligations domT ranT fvar f abstract_qglrs 
  1029       mk_compat_proof_obligations domT ranT fvar f abstract_qglrs 
  1198       |> map (cert #> Thm.assume)
  1030       |> map (cert #> Thm.assume)
  1199 
  1031 
  1200     val compat_store = store_compat_thms n compat
  1032     val compat_store = store_compat_thms n compat
  1201 
       
  1202     (*
       
  1203     val _ = tracing ("globals:\n" ^ pp_thm globals)
       
  1204     val _ = tracing ("complete:\n" ^ pp_thm complete)
       
  1205     val _ = tracing ("compat:\n" ^ pp_thm compat)
       
  1206     val _ = tracing ("compat_store:\n" ^ pp_thm compat_store)
       
  1207     *)
       
  1208 
  1033 
  1209     val (goalstate, values) = PROFILE "prove_stuff"
  1034     val (goalstate, values) = PROFILE "prove_stuff"
  1210       (prove_stuff lthy globals G f R xclauses complete compat
  1035       (prove_stuff lthy globals G f R xclauses complete compat
  1211          compat_store G_elim G_eqvt) f_defthm
  1036          compat_store G_elim G_eqvt) f_defthm
  1212      
  1037      
  1350          SumTree.mk_inj RST n' i' (replace_frees rews rhs)
  1175          SumTree.mk_inj RST n' i' (replace_frees rews rhs)
  1351          |> Envir.beta_norm)
  1176          |> Envir.beta_norm)
  1352       end
  1177       end
  1353 
  1178 
  1354     val qglrs = map convert_eqs fqgars
  1179     val qglrs = map convert_eqs fqgars
  1355    
       
  1356     fun pp_f (f, args, precond, lhs, rhs) =
       
  1357       (tracing ("lhs: " ^ commas 
       
  1358          (map (fn t => Syntax.string_of_term ctxt (subst_bounds (map Free (rev args), t))) lhs));
       
  1359        tracing ("rhs: " ^ Syntax.string_of_term ctxt (subst_bounds (map Free (rev args), rhs))))
       
  1360 
       
  1361     fun pp_q (args, precond, lhs, rhs) =
       
  1362       (tracing ("qlhs: " ^ Syntax.string_of_term ctxt (subst_bounds (map Free (rev args), lhs)));
       
  1363        tracing ("qrhs: " ^ Syntax.string_of_term ctxt (subst_bounds (map Free (rev args), rhs))))
       
  1364 
       
  1365   in
  1180   in
  1366     Mutual {n=num, n'=n', fsum_var=fsum_var, ST=ST, RST=RST,
  1181     Mutual {n=num, n'=n', fsum_var=fsum_var, ST=ST, RST=RST,
  1367       parts=parts, fqgars=fqgars, qglrs=qglrs, fsum=NONE}
  1182       parts=parts, fqgars=fqgars, qglrs=qglrs, fsum=NONE}
  1368   end
  1183   end
  1369 *}
  1184 *}
  1640   in
  1455   in
  1641     ((goal_state, afterqed), lthy')
  1456     ((goal_state, afterqed), lthy')
  1642   end
  1457   end
  1643 
  1458 
  1644 *}
  1459 *}
  1645 ML {*
       
  1646   Proof.theorem;
       
  1647   Proof.refine
       
  1648 *}
       
  1649 
       
  1650 
  1460 
  1651 ML {*
  1461 ML {*
  1652 fun gen_function is_external prep default_constraint fixspec eqns config lthy =
  1462 fun gen_function is_external prep default_constraint fixspec eqns config lthy =
  1653   let
  1463   let
  1654     val ((goal_state, afterqed), lthy') =
  1464     val ((goal_state, afterqed), lthy') =