Nominal/Ex/LamTest.thy
changeset 2650 e5fa8de0e4bd
parent 2649 a8ebcb368a15
child 2651 4aa72a88b2c1
equal deleted inserted replaced
2649:a8ebcb368a15 2650:e5fa8de0e4bd
    10 | Lam x::"name" l::"lam"  bind x in l
    10 | Lam x::"name" l::"lam"  bind x in l
    11 
    11 
    12 definition
    12 definition
    13  "eqvt x \<equiv> \<forall>p. p \<bullet> x = x"
    13  "eqvt x \<equiv> \<forall>p. p \<bullet> x = x"
    14 
    14 
       
    15 lemma eqvtI:
       
    16   "(\<And>p. p \<bullet> x \<equiv> x) \<Longrightarrow> eqvt x"
       
    17 apply(auto simp add: eqvt_def)
       
    18 done
    15 
    19 
    16 ML {*
    20 ML {*
    17 
    21 
    18 
    22 
    19 val trace = Unsynchronized.ref false
    23 val trace = Unsynchronized.ref false
    94   in
    98   in
    95     rev (Function_Ctx_Tree.traverse_tree add_Ri tree [])
    99     rev (Function_Ctx_Tree.traverse_tree add_Ri tree [])
    96   end
   100   end
    97 *}
   101 *}
    98 
   102 
       
   103 ML {*
       
   104 fun mk_eqvt trm =
       
   105   let
       
   106     val ty = fastype_of trm
       
   107   in
       
   108     Const (@{const_name eqvt}, ty --> @{typ bool}) $ trm
       
   109     |> HOLogic.mk_Trueprop
       
   110   end
       
   111 *}
       
   112 
    99 
   113 
   100 ML {*
   114 ML {*
   101 (** building proof obligations *)
   115 (** building proof obligations *)
   102 
   116 
   103 fun mk_compat_proof_obligations domT ranT fvar f glrs =
   117 fun mk_compat_proof_obligations domT ranT fvar f glrs =
   112       in
   126       in
   113         Logic.mk_implies
   127         Logic.mk_implies
   114           (HOLogic.mk_Trueprop (HOLogic.eq_const domT $ shift lhs $ lhs'),
   128           (HOLogic.mk_Trueprop (HOLogic.eq_const domT $ shift lhs $ lhs'),
   115             HOLogic.mk_Trueprop (HOLogic.eq_const ranT $ shift rhs $ rhs'))
   129             HOLogic.mk_Trueprop (HOLogic.eq_const ranT $ shift rhs $ rhs'))
   116         |> fold_rev (curry Logic.mk_implies) (map shift gs @ gs')
   130         |> fold_rev (curry Logic.mk_implies) (map shift gs @ gs')
       
   131         (* HERE |> (curry Logic.mk_implies) (mk_eqvt fvar) *)
   117         |> (curry Logic.mk_implies) @{term "Trueprop True"}
   132         |> (curry Logic.mk_implies) @{term "Trueprop True"}
   118         |> fold_rev (fn (n,T) => fn b => Term.all T $ Abs(n,T,b)) (qs @ qs')
   133         |> fold_rev (fn (n,T) => fn b => Term.all T $ Abs(n,T,b)) (qs @ qs')
   119         |> curry abstract_over fvar
   134         |> curry abstract_over fvar
   120         |> curry subst_bound f
   135         |> curry subst_bound f
   121       end
   136       end
   226   in
   241   in
   227     (thms1 :: store_compat_thms (n - 1) thms2)
   242     (thms1 :: store_compat_thms (n - 1) thms2)
   228   end
   243   end
   229 *}
   244 *}
   230 
   245 
   231 
       
   232 ML {*
   246 ML {*
   233 (* expects i <= j *)
   247 (* expects i <= j *)
   234 fun lookup_compat_thm i j cts =
   248 fun lookup_compat_thm i j cts =
   235   nth (nth cts (i - 1)) (j - i)
   249   nth (nth cts (i - 1)) (j - i)
   236 
   250 
   242     val ClauseContext {cqs=cqsj,ags=agsj,lhs=lhsj,...} = ctxj
   256     val ClauseContext {cqs=cqsj,ags=agsj,lhs=lhsj,...} = ctxj
   243 
   257 
   244     val lhsi_eq_lhsj = cterm_of thy (HOLogic.mk_Trueprop (mk_eq (lhsi, lhsj)))
   258     val lhsi_eq_lhsj = cterm_of thy (HOLogic.mk_Trueprop (mk_eq (lhsi, lhsj)))
   245   in if j < i then
   259   in if j < i then
   246     let
   260     let
   247       val _ = tracing "then"
       
   248       val compat = lookup_compat_thm j i cts
   261       val compat = lookup_compat_thm j i cts
   249     in
   262     in
   250       compat         (* "!!qj qi. Gsj => Gsi => lhsj = lhsi ==> rhsj = rhsi" *)
   263       compat         (* "!!qj qi. Gsj => Gsi => lhsj = lhsi ==> rhsj = rhsi" *)
   251       |> fold Thm.forall_elim (cqsj @ cqsi) (* "Gsj => Gsi => lhsj = lhsi ==> rhsj = rhsi" *)
   264       |> fold Thm.forall_elim (cqsj @ cqsi) (* "Gsj => Gsi => lhsj = lhsi ==> rhsj = rhsi" *)
   252       |> Thm.elim_implies @{thm TrueI}
   265       |> Thm.elim_implies @{thm TrueI}
   254       |> fold Thm.elim_implies agsi
   267       |> fold Thm.elim_implies agsi
   255       |> Thm.elim_implies ((Thm.assume lhsi_eq_lhsj) RS sym) (* "Gsj, Gsi, lhsi = lhsj |-- rhsj = rhsi" *)
   268       |> Thm.elim_implies ((Thm.assume lhsi_eq_lhsj) RS sym) (* "Gsj, Gsi, lhsi = lhsj |-- rhsj = rhsi" *)
   256     end
   269     end
   257     else
   270     else
   258     let
   271     let
   259       val _ = tracing "else"
       
   260       val compat = lookup_compat_thm i j cts
   272       val compat = lookup_compat_thm i j cts
   261 
       
   262       fun pp s (th:thm) = tracing (s ^ " compat thm: " ^ @{make_string} th)
       
   263     in
   273     in
   264       compat        (* "!!qi qj. Gsi => Gsj => lhsi = lhsj ==> rhsi = rhsj" *)
   274       compat        (* "!!qi qj. Gsi => Gsj => lhsi = lhsj ==> rhsi = rhsj" *)
   265       |> (tap (fn th => pp "*0" th))
       
   266       |> fold Thm.forall_elim (cqsi @ cqsj) (* "Gsi => Gsj => lhsi = lhsj ==> rhsi = rhsj" *)
   275       |> fold Thm.forall_elim (cqsi @ cqsj) (* "Gsi => Gsj => lhsi = lhsj ==> rhsi = rhsj" *)
   267       |> (tap (fn th => pp "*1" th))
       
   268       |> Thm.elim_implies @{thm TrueI}
   276       |> Thm.elim_implies @{thm TrueI}
   269       |> fold Thm.elim_implies agsi
   277       |> fold Thm.elim_implies agsi
   270       |> (tap (fn th => pp "*2" th))
       
   271       |> fold Thm.elim_implies agsj
   278       |> fold Thm.elim_implies agsj
   272       |> (tap (fn th => pp "*3" th))
       
   273       |> Thm.elim_implies (Thm.assume lhsi_eq_lhsj)
   279       |> Thm.elim_implies (Thm.assume lhsi_eq_lhsj)
   274       |> (tap (fn th => pp "*4" th))
       
   275       |> (fn thm => thm RS sym) (* "Gsi, Gsj, lhsi = lhsj |-- rhsj = rhsi" *)
   280       |> (fn thm => thm RS sym) (* "Gsi, Gsj, lhsi = lhsj |-- rhsj = rhsi" *)
   276     end
   281     end
   277   end
   282   end
   278 *}
   283 *}
   279 
   284 
   318     val ClauseInfo {no=j, qglr=cdescj, RCs=RCsj, ...} = clausej
   323     val ClauseInfo {no=j, qglr=cdescj, RCs=RCsj, ...} = clausej
   319 
   324 
   320     val cctxj as ClauseContext {ags = agsj', lhs = lhsj', rhs = rhsj', qs = qsj', cqs = cqsj', ...} =
   325     val cctxj as ClauseContext {ags = agsj', lhs = lhsj', rhs = rhsj', qs = qsj', cqs = cqsj', ...} =
   321       mk_clause_context x ctxti cdescj
   326       mk_clause_context x ctxti cdescj
   322 
   327 
   323     val _ = tracing "1"
       
   324 
       
   325     val rhsj'h = Pattern.rewrite_term thy [(fvar,h)] [] rhsj'
   328     val rhsj'h = Pattern.rewrite_term thy [(fvar,h)] [] rhsj'
   326    
       
   327     val _ = tracing "1A"
       
   328 
       
   329     val compat = get_compat_thm thy compat_store i j cctxi cctxj
   329     val compat = get_compat_thm thy compat_store i j cctxi cctxj
   330    
       
   331     val _ = tracing "1B"
       
   332 
   330 
   333     val Ghsj' = map 
   331     val Ghsj' = map 
   334       (fn RCInfo {h_assum, ...} => Thm.assume (cterm_of thy (subst_bounds (rev qsj', h_assum)))) RCsj
   332       (fn RCInfo {h_assum, ...} => Thm.assume (cterm_of thy (subst_bounds (rev qsj', h_assum)))) RCsj
   335 
       
   336     val _ = tracing "2"
       
   337 
   333 
   338     val RLj_import = RLj
   334     val RLj_import = RLj
   339       |> fold Thm.forall_elim cqsj'
   335       |> fold Thm.forall_elim cqsj'
   340       |> fold Thm.elim_implies agsj'
   336       |> fold Thm.elim_implies agsj'
   341       |> fold Thm.elim_implies Ghsj'
   337       |> fold Thm.elim_implies Ghsj'
   342 
   338 
   343     val _ = tracing "3"
       
   344 
       
   345     val y_eq_rhsj'h = Thm.assume (cterm_of thy (HOLogic.mk_Trueprop (mk_eq (y, rhsj'h))))
   339     val y_eq_rhsj'h = Thm.assume (cterm_of thy (HOLogic.mk_Trueprop (mk_eq (y, rhsj'h))))
   346     val lhsi_eq_lhsj' = Thm.assume (cterm_of thy (HOLogic.mk_Trueprop (mk_eq (lhsi, lhsj'))))
   340     val lhsi_eq_lhsj' = Thm.assume (cterm_of thy (HOLogic.mk_Trueprop (mk_eq (lhsi, lhsj'))))
   347        (* lhs_i = lhs_j' |-- lhs_i = lhs_j' *)
   341        (* lhs_i = lhs_j' |-- lhs_i = lhs_j' *)
   348 
       
   349     val _ = tracing "4"
       
   350   in
   342   in
   351     (trans OF [case_hyp, lhsi_eq_lhsj']) (* lhs_i = lhs_j' |-- x = lhs_j' *)
   343     (trans OF [case_hyp, lhsi_eq_lhsj']) (* lhs_i = lhs_j' |-- x = lhs_j' *)
   352     |> Thm.implies_elim RLj_import
   344     |> Thm.implies_elim RLj_import
   353       (* Rj1' ... Rjk', lhs_i = lhs_j' |-- rhs_j'_h = rhs_j'_f *)
   345       (* Rj1' ... Rjk', lhs_i = lhs_j' |-- rhs_j'_h = rhs_j'_f *)
   354     |> (fn it => trans OF [it, compat])
   346     |> (fn it => trans OF [it, compat])
   373     val ClauseInfo {cdata = ClauseContext {lhs, rhs, cqs, ags, case_hyp, ...}, lGI, RCs, ...} = clausei
   365     val ClauseInfo {cdata = ClauseContext {lhs, rhs, cqs, ags, case_hyp, ...}, lGI, RCs, ...} = clausei
   374     val rhsC = Pattern.rewrite_term thy [(fvar, f)] [] rhs
   366     val rhsC = Pattern.rewrite_term thy [(fvar, f)] [] rhs
   375 
   367 
   376     val ih_intro_case = full_simplify (HOL_basic_ss addsimps [case_hyp]) ih_intro
   368     val ih_intro_case = full_simplify (HOL_basic_ss addsimps [case_hyp]) ih_intro
   377 
   369 
   378     val _ = tracing "A"
       
   379 
       
   380     fun prep_RC (RCInfo {llRI, RIvs, CCas, ...}) = (llRI RS ih_intro_case)
   370     fun prep_RC (RCInfo {llRI, RIvs, CCas, ...}) = (llRI RS ih_intro_case)
   381       |> fold_rev (Thm.implies_intr o cprop_of) CCas
   371       |> fold_rev (Thm.implies_intr o cprop_of) CCas
   382       |> fold_rev (Thm.forall_intr o cterm_of thy o Free) RIvs
   372       |> fold_rev (Thm.forall_intr o cterm_of thy o Free) RIvs
   383 
   373 
   384     val existence = fold (curry op COMP o prep_RC) RCs lGI
   374     val existence = fold (curry op COMP o prep_RC) RCs lGI
   385 
   375 
   386     val _ = tracing "B"
       
   387 
       
   388     val P = cterm_of thy (mk_eq (y, rhsC))
   376     val P = cterm_of thy (mk_eq (y, rhsC))
   389     val G_lhs_y = Thm.assume (cterm_of thy (HOLogic.mk_Trueprop (G $ lhs $ y)))
   377     val G_lhs_y = Thm.assume (cterm_of thy (HOLogic.mk_Trueprop (G $ lhs $ y)))
   390 
       
   391     val _ = tracing "B2"
       
   392 
   378 
   393     val unique_clauses =
   379     val unique_clauses =
   394       map2 (mk_uniqueness_clause thy globals compat_store clausei) clauses rep_lemmas
   380       map2 (mk_uniqueness_clause thy globals compat_store clausei) clauses rep_lemmas
   395   
   381   
   396     val _ = tracing "C"
       
   397 
       
   398     fun elim_implies_eta A AB =
   382     fun elim_implies_eta A AB =
   399       Thm.compose_no_flatten true (A, 0) 1 AB |> Seq.list_of |> the_single
   383       Thm.compose_no_flatten true (A, 0) 1 AB |> Seq.list_of |> the_single
   400 
       
   401     val _ = tracing "D"
       
   402 
   384 
   403     val uniqueness = G_cases
   385     val uniqueness = G_cases
   404       |> Thm.forall_elim (cterm_of thy lhs)
   386       |> Thm.forall_elim (cterm_of thy lhs)
   405       |> Thm.forall_elim (cterm_of thy y)
   387       |> Thm.forall_elim (cterm_of thy y)
   406       |> Thm.forall_elim P
   388       |> Thm.forall_elim P
   407       |> Thm.elim_implies G_lhs_y
   389       |> Thm.elim_implies G_lhs_y
   408       |> fold elim_implies_eta unique_clauses
   390       |> fold elim_implies_eta unique_clauses
   409       |> Thm.implies_intr (cprop_of G_lhs_y)
   391       |> Thm.implies_intr (cprop_of G_lhs_y)
   410       |> Thm.forall_intr (cterm_of thy y)
   392       |> Thm.forall_intr (cterm_of thy y)
   411 
   393 
   412     val _ = tracing "E"
       
   413 
       
   414     val P2 = cterm_of thy (lambda y (G $ lhs $ y)) (* P2 y := (lhs, y): G *)
   394     val P2 = cterm_of thy (lambda y (G $ lhs $ y)) (* P2 y := (lhs, y): G *)
   415 
   395 
   416     val exactly_one =
   396     val exactly_one =
   417       ex1I |> instantiate' [SOME (ctyp_of thy ranT)] [SOME P2, SOME (cterm_of thy rhsC)]
   397       ex1I |> instantiate' [SOME (ctyp_of thy ranT)] [SOME P2, SOME (cterm_of thy rhsC)]
   418       |> curry (op COMP) existence
   398       |> curry (op COMP) existence
   420       |> simplify (HOL_basic_ss addsimps [case_hyp RS sym])
   400       |> simplify (HOL_basic_ss addsimps [case_hyp RS sym])
   421       |> Thm.implies_intr (cprop_of case_hyp)
   401       |> Thm.implies_intr (cprop_of case_hyp)
   422       |> fold_rev (Thm.implies_intr o cprop_of) ags
   402       |> fold_rev (Thm.implies_intr o cprop_of) ags
   423       |> fold_rev Thm.forall_intr cqs
   403       |> fold_rev Thm.forall_intr cqs
   424 
   404 
   425     val _ = tracing "F"
       
   426 
       
   427     val function_value =
   405     val function_value =
   428       existence
   406       existence
   429       |> Thm.implies_intr ihyp
   407       |> Thm.implies_intr ihyp
   430       |> Thm.implies_intr (cprop_of case_hyp)
   408       |> Thm.implies_intr (cprop_of case_hyp)
   431       |> Thm.forall_intr (cterm_of thy x)
   409       |> Thm.forall_intr (cterm_of thy x)
   432       |> Thm.forall_elim (cterm_of thy lhs)
   410       |> Thm.forall_elim (cterm_of thy lhs)
   433       |> curry (op RS) refl
   411       |> curry (op RS) refl
   434 
       
   435     val _ = tracing "G" 
       
   436   in
   412   in
   437     (exactly_one, function_value)
   413     (exactly_one, function_value)
   438   end
   414   end
   439 *}
   415 *}
   440 
   416 
   480       |> fold_rev (Thm.implies_intr o cprop_of) compat
   456       |> fold_rev (Thm.implies_intr o cprop_of) compat
   481       |> Thm.implies_intr (cprop_of complete)
   457       |> Thm.implies_intr (cprop_of complete)
   482   in
   458   in
   483     (goalstate, values)
   459     (goalstate, values)
   484   end
   460   end
   485 
   461 *}
       
   462 
       
   463 ML {* 
       
   464 Inductive.add_inductive_i;
       
   465 Local_Theory.conceal
       
   466 *}
       
   467 
       
   468 ML {*
   486 (* wrapper -- restores quantifiers in rule specifications *)
   469 (* wrapper -- restores quantifiers in rule specifications *)
   487 fun inductive_def (binding as ((R, T), _)) intrs lthy =
   470 fun inductive_def (binding as ((R, T), _)) intrs lthy =
   488   let
   471   let
   489     val ({intrs = intrs_gen, elims = [elim_gen], preds = [ Rdef ], induct, ...}, lthy) =
   472     val ({intrs = intrs_gen, elims = [elim_gen], preds = [ Rdef ], induct, raw_induct, ...}, lthy) =
   490       lthy
   473       lthy
   491       |> Local_Theory.conceal
   474       |> Local_Theory.conceal 
   492       |> Inductive.add_inductive_i
   475       |> Inductive.add_inductive_i
   493           {quiet_mode = true,
   476           {quiet_mode = true,
   494             verbose = ! trace,
   477             verbose = ! trace,
   495             alt_name = Binding.empty,
   478             alt_name = Binding.empty,
   496             coind = false,
   479             coind = false,
   502           [] (* no parameters *)
   485           [] (* no parameters *)
   503           (map (fn t => (Attrib.empty_binding, t)) intrs) (* intro rules *)
   486           (map (fn t => (Attrib.empty_binding, t)) intrs) (* intro rules *)
   504           [] (* no special monos *)
   487           [] (* no special monos *)
   505       ||> Local_Theory.restore_naming lthy
   488       ||> Local_Theory.restore_naming lthy
   506 
   489 
       
   490     val ([eqvt_thm], lthy) = Nominal_Eqvt.raw_equivariance false [Rdef] raw_induct intrs_gen lthy
       
   491     val eqvt_thm' = (Nominal_ThmDecls.eqvt_transform lthy eqvt_thm) RS @{thm eqvtI}
       
   492 
   507     val cert = cterm_of (ProofContext.theory_of lthy)
   493     val cert = cterm_of (ProofContext.theory_of lthy)
   508     fun requantify orig_intro thm =
   494     fun requantify orig_intro thm =
   509       let
   495       let
   510         val (qs, t) = dest_all_all orig_intro
   496         val (qs, t) = dest_all_all orig_intro
   511         val frees = frees_in_term lthy t |> remove (op =) (Binding.name_of R, T)
   497         val frees = frees_in_term lthy t |> remove (op =) (Binding.name_of R, T)
   515       in
   501       in
   516         fold_rev (fn Free (n, T) =>
   502         fold_rev (fn Free (n, T) =>
   517           forall_intr_rename (n, cert (Var (varmap (n, T), T)))) qs thm
   503           forall_intr_rename (n, cert (Var (varmap (n, T), T)))) qs thm
   518       end
   504       end
   519   in
   505   in
   520     ((Rdef, map2 requantify intrs intrs_gen, forall_intr_vars elim_gen, induct), lthy)
   506     ((Rdef, map2 requantify intrs intrs_gen, forall_intr_vars elim_gen, induct, eqvt_thm'), lthy)
   521   end
   507   end
   522 *}
   508 *}
   523 
   509 
   524 ML {*
   510 ML {*
   525 fun define_graph Gname fvar domT ranT clauses RCss lthy =
   511 fun define_graph Gname fvar domT ranT clauses RCss lthy =
   572       |> fold_rev mk_forall_rename (map fst oqs ~~ qs)
   558       |> fold_rev mk_forall_rename (map fst oqs ~~ qs)
   573       (* "!!qs xs. CS ==> G => (r, lhs) : R" *)
   559       (* "!!qs xs. CS ==> G => (r, lhs) : R" *)
   574 
   560 
   575     val R_intross = map2 (map o mk_RIntro) (clauses ~~ qglrs) RCss
   561     val R_intross = map2 (map o mk_RIntro) (clauses ~~ qglrs) RCss
   576 
   562 
   577     val ((R, RIntro_thms, R_elim, _), lthy) =
   563     val ((R, RIntro_thms, R_elim, _, R_eqvt), lthy) =
   578       inductive_def ((Binding.name n, T), NoSyn) (flat R_intross) lthy
   564       inductive_def ((Binding.name n, T), NoSyn) (flat R_intross) lthy
   579   in
   565   in
   580     ((R, Library.unflat R_intross RIntro_thms, R_elim), lthy)
   566     ((R, Library.unflat R_intross RIntro_thms, R_elim, R_eqvt), lthy)
   581   end
   567   end
   582 
   568 
   583 
   569 
   584 fun fix_globals domT ranT fvar ctxt =
   570 fun fix_globals domT ranT fvar ctxt =
   585   let
   571   let
   926         |> fold_rev forall_intr_rename (map fst oqs ~~ cqs)
   912         |> fold_rev forall_intr_rename (map fst oqs ~~ cqs)
   927       end
   913       end
   928   in
   914   in
   929     map2 mk_trsimp clauses psimps
   915     map2 mk_trsimp clauses psimps
   930   end
   916   end
   931 
   917 *}
   932 
   918 
       
   919 ML {*
   933 fun prepare_function config defname [((fname, fT), mixfix)] abstract_qglrs lthy =
   920 fun prepare_function config defname [((fname, fT), mixfix)] abstract_qglrs lthy =
   934   let
   921   let
   935     val FunctionConfig {domintros, tailrec, default=default_opt, ...} = config
   922     val FunctionConfig {domintros, tailrec, default=default_opt, ...} = config
   936 
   923 
   937     val default_str = the_default "%x. undefined" default_opt (*FIXME dynamic scoping*)
   924     val default_str = the_default "%x. undefined" default_opt (*FIXME dynamic scoping*)
   954        Function_Ctx_Tree.mk_tree (fname, fT) h ctxt rhs
   941        Function_Ctx_Tree.mk_tree (fname, fT) h ctxt rhs
   955 
   942 
   956     val trees = map build_tree clauses
   943     val trees = map build_tree clauses
   957     val RCss = map find_calls trees
   944     val RCss = map find_calls trees
   958 
   945 
   959     val ((G, GIntro_thms, G_elim, G_induct), lthy) =
   946     val ((G, GIntro_thms, G_elim, G_induct, G_eqvt), lthy) =
   960       PROFILE "def_graph" (define_graph (graph_name defname) fvar domT ranT clauses RCss) lthy
   947       PROFILE "def_graph" (define_graph (graph_name defname) fvar domT ranT clauses RCss) lthy
   961 
   948 
   962     val _ = tracing ("Graph - name: " ^ @{make_string} G)
   949     val _ = tracing ("Graph - name: " ^ @{make_string} G)
   963     val _ = tracing ("Graph intros:\n" ^ cat_lines (map @{make_string} GIntro_thms))
   950     val _ = tracing ("Graph intros:\n" ^ cat_lines (map @{make_string} GIntro_thms))
       
   951     val _ = tracing ("Graph Equivariance" ^ @{make_string} G_eqvt)
       
   952     
   964 
   953 
   965     val ((f, (_, f_defthm)), lthy) =
   954     val ((f, (_, f_defthm)), lthy) =
   966       PROFILE "def_fun" (define_function (defname ^ "_sumC_def") (fname, mixfix) domT ranT G default) lthy
   955       PROFILE "def_fun" (define_function (defname ^ "_sumC_def") (fname, mixfix) domT ranT G default) lthy
   967 
   956 
   968     val _ = tracing ("f - name: " ^ @{make_string} f)
   957     val _ = tracing ("f - name: " ^ @{make_string} f)
   969     val _ = tracing ("f_defthm:\n" ^ @{make_string} f_defthm)
   958     val _ = tracing ("f_defthm:\n" ^ @{make_string} f_defthm)
   970 
   959 
   971     val RCss = map (map (inst_RC (ProofContext.theory_of lthy) fvar f)) RCss
   960     val RCss = map (map (inst_RC (ProofContext.theory_of lthy) fvar f)) RCss
   972     val trees = map (Function_Ctx_Tree.inst_tree (ProofContext.theory_of lthy) fvar f) trees
   961     val trees = map (Function_Ctx_Tree.inst_tree (ProofContext.theory_of lthy) fvar f) trees
   973 
   962 
   974     val ((R, RIntro_thmss, R_elim), lthy) =
   963     val ((R, RIntro_thmss, R_elim, R_eqvt), lthy) =
   975       PROFILE "def_rel" (define_recursion_relation (rel_name defname) domT abstract_qglrs clauses RCss) lthy
   964       PROFILE "def_rel" (define_recursion_relation (rel_name defname) domT abstract_qglrs clauses RCss) lthy
   976 
   965 
   977     val _ = tracing ("Relation - name: " ^ @{make_string} R) 
   966     val _ = tracing ("Relation - name: " ^ @{make_string} R) 
   978     val _ = tracing ("Relation intros:\n" ^ cat_lines (map @{make_string} RIntro_thmss))
   967     val _ = tracing ("Relation intros:\n" ^ cat_lines (map @{make_string} RIntro_thmss))
       
   968     val _ = tracing ("Relation Equivariance" ^ @{make_string} R_eqvt)
   979 
   969 
   980     val (_, lthy) =
   970     val (_, lthy) =
   981       Local_Theory.abbrev Syntax.mode_default ((Binding.name (dom_name defname), NoSyn), mk_acc domT R) lthy
   971       Local_Theory.abbrev Syntax.mode_default ((Binding.name (dom_name defname), NoSyn), mk_acc domT R) lthy
   982 
   972 
   983     val newthy = ProofContext.theory_of lthy
   973     val newthy = ProofContext.theory_of lthy
   996       mk_compat_proof_obligations domT ranT fvar f abstract_qglrs
   986       mk_compat_proof_obligations domT ranT fvar f abstract_qglrs
   997       |> map (cert #> Thm.assume)
   987       |> map (cert #> Thm.assume)
   998 
   988 
   999     val compat_store = store_compat_thms n compat
   989     val compat_store = store_compat_thms n compat
  1000 
   990 
       
   991     (*
  1001     val _ = tracing ("globals:\n" ^ @{make_string} globals)
   992     val _ = tracing ("globals:\n" ^ @{make_string} globals)
  1002     val _ = tracing ("complete:\n" ^ @{make_string} complete)
   993     val _ = tracing ("complete:\n" ^ @{make_string} complete)
  1003     val _ = tracing ("compat:\n" ^ @{make_string} compat)
   994     val _ = tracing ("compat:\n" ^ @{make_string} compat)
  1004     val _ = tracing ("compat_store:\n" ^ @{make_string} compat_store)
   995     val _ = tracing ("compat_store:\n" ^ @{make_string} compat_store)
       
   996     *)
  1005 
   997 
  1006     val (goalstate, values) = PROFILE "prove_stuff"
   998     val (goalstate, values) = PROFILE "prove_stuff"
  1007       (prove_stuff lthy globals G f R xclauses complete compat
   999       (prove_stuff lthy globals G f R xclauses complete compat
  1008          compat_store G_elim) f_defthm
  1000          compat_store G_elim) f_defthm
  1009      
  1001      
  1499   depth :: "lam \<Rightarrow> nat"
  1491   depth :: "lam \<Rightarrow> nat"
  1500 where
  1492 where
  1501   "depth (Var x) = 1"
  1493   "depth (Var x) = 1"
  1502 | "depth (App t1 t2) = (max (depth t1) (depth t2)) + 1"
  1494 | "depth (App t1 t2) = (max (depth t1) (depth t2)) + 1"
  1503 | "depth (Lam x t) = (depth t) + 1"
  1495 | "depth (Lam x t) = (depth t) + 1"
       
  1496 thm depth_graph.intros
  1504 apply(rule_tac y="x" in lam.exhaust)
  1497 apply(rule_tac y="x" in lam.exhaust)
  1505 apply(simp_all)[3]
  1498 apply(simp_all)[3]
  1506 apply(simp_all only: lam.distinct)
  1499 apply(simp_all only: lam.distinct)
  1507 apply(simp add: lam.eq_iff)
  1500 apply(simp add: lam.eq_iff)
  1508 apply(simp add: lam.eq_iff)
  1501 apply(simp add: lam.eq_iff)