src/HOL/Tools/function_package/mutual.ML
author krauss
Fri, 20 Apr 2007 10:06:11 +0200
changeset 22733 0b14bb35be90
parent 22623 5fcee5b319a2
child 23189 4574ab8f3b21
permissions -rw-r--r--
definition lookup via terms, not names. Methods "relation" and "lexicographic_order" do not depend on "termination" setup.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
     1
(*  Title:      HOL/Tools/function_package/mutual.ML
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
     2
    ID:         $Id$
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
     3
    Author:     Alexander Krauss, TU Muenchen
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
     4
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
     5
A package for general recursive function definitions.
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
     6
Tools for mutual recursive definitions.
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
     7
*)
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
     8
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
     9
signature FUNDEF_MUTUAL =
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
    10
sig
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
    11
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    12
  val prepare_fundef_mutual : FundefCommon.fundef_config
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    13
                              -> string (* defname *)
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    14
                              -> ((string * typ) * mixfix) list
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
    15
                              -> term list
20654
d80502f0d701 1. Function package accepts a parameter (default "some_term"), which specifies the functions
krauss
parents: 20534
diff changeset
    16
                              -> string (* default, unparsed term *)
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
    17
                              -> local_theory
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    18
                              -> ((thm (* goalstate *)
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    19
                                   * (thm -> FundefCommon.fundef_result) (* proof continuation *)
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    20
                                   * (thm list -> thm list list)         (* sorting continuation *)
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    21
                                  ) * local_theory)
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
    22
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
    23
end
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
    24
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
    25
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
    26
structure FundefMutual: FUNDEF_MUTUAL =
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
    27
struct
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
    28
21051
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20949
diff changeset
    29
open FundefLib
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
    30
open FundefCommon
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
    31
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
    32
(* Theory dependencies *)
22622
25693088396b Moving "FunDef" up in the HOL development graph, since it is independent from "Recdef" and "Datatype" now.
krauss
parents: 22620
diff changeset
    33
val sum_case_rules = thms "Sum_Type.sum_cases"
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
    34
val split_apply = thm "Product_Type.split"
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
    35
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    36
type qgar = string * (string * typ) list * term list * term list * term
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    37
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    38
fun name_of_fqgar (f, _, _, _, _) = f
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    39
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    40
datatype mutual_part =
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    41
  MutualPart of 
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    42
   {
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    43
    fvar : string * typ,
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    44
    cargTs: typ list,
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    45
    pthA: SumTools.sum_path,
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    46
    pthR: SumTools.sum_path,
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    47
    f_def: term,
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    48
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    49
    f: term option,
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    50
    f_defthm : thm option
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    51
   }
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    52
   
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    53
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    54
datatype mutual_info =
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    55
  Mutual of 
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    56
   { 
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    57
    fsum_var : string * typ,
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    58
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    59
    ST: typ,
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    60
    RST: typ,
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    61
    streeA: SumTools.sum_tree,
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    62
    streeR: SumTools.sum_tree,
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    63
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    64
    parts: mutual_part list,
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    65
    fqgars: qgar list,
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    66
    qglrs: ((string * typ) list * term list * term * term) list,
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    67
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    68
    fsum : term option
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    69
   }
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
    70
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
    71
fun mutual_induct_Pnames n =
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
    72
    if n < 5 then fst (chop n ["P","Q","R","S"])
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
    73
    else map (fn i => "P" ^ string_of_int i) (1 upto n)
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
    74
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
    75
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
    76
fun open_all_all (Const ("all", _) $ Abs (n, T, b)) = apfst (cons (n, T)) (open_all_all b)
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
    77
  | open_all_all t = ([], t)
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
    78
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
    79
(* Builds a curried clause description in abstracted form *)
20851
bf80cb83f8be Improved error reporting
krauss
parents: 20797
diff changeset
    80
fun split_def ctxt fnames geq arities =
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
    81
    let
21319
cf814e36f788 replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents: 21255
diff changeset
    82
      fun input_error msg = cat_lines [msg, ProofContext.string_of_term ctxt geq]
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    83
                            
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
    84
      val (qs, imp) = open_all_all geq
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
    85
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
    86
      val gs = Logic.strip_imp_prems imp
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
    87
      val eq = Logic.strip_imp_concl imp
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
    88
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
    89
      val (f_args, rhs) = HOLogic.dest_eq (HOLogic.dest_Trueprop eq)
20851
bf80cb83f8be Improved error reporting
krauss
parents: 20797
diff changeset
    90
      val (head, args) = strip_comb f_args
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
    91
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
    92
      val invalid_head_msg = "Head symbol of left hand side must be " ^ plural "" "one out of " fnames ^ commas_quote fnames
20851
bf80cb83f8be Improved error reporting
krauss
parents: 20797
diff changeset
    93
      val fname = fst (dest_Free head)
21319
cf814e36f788 replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents: 21255
diff changeset
    94
          handle TERM _ => error (input_error invalid_head_msg)
20851
bf80cb83f8be Improved error reporting
krauss
parents: 20797
diff changeset
    95
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22526
diff changeset
    96
      val _ = fname mem fnames orelse error (input_error invalid_head_msg)
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
    97
20534
b147d0c13f6e Fixed Subscript Exception occurring with Higher-Order recursion
krauss
parents: 20523
diff changeset
    98
      fun add_bvs t is = add_loose_bnos (t, 0, is)
20851
bf80cb83f8be Improved error reporting
krauss
parents: 20797
diff changeset
    99
      val rvs = (add_bvs rhs [] \\ fold add_bvs args [])
bf80cb83f8be Improved error reporting
krauss
parents: 20797
diff changeset
   100
                  |> map (fst o nth (rev qs))
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   101
                
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22526
diff changeset
   102
      val _ = null rvs orelse error (input_error ("Variable" ^ plural " " "s " rvs ^ commas_quote rvs
21319
cf814e36f788 replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents: 21255
diff changeset
   103
                                              ^ " occur" ^ plural "s" "" rvs ^ " on right hand side only:"))
20851
bf80cb83f8be Improved error reporting
krauss
parents: 20797
diff changeset
   104
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22526
diff changeset
   105
      val _ = forall (forall_aterms (fn Free (n, _) => not (n mem fnames) | _ => true)) gs orelse
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22526
diff changeset
   106
                     error (input_error "Recursive Calls not allowed in premises")
20654
d80502f0d701 1. Function package accepts a parameter (default "some_term"), which specifies the functions
krauss
parents: 20534
diff changeset
   107
d80502f0d701 1. Function package accepts a parameter (default "some_term"), which specifies the functions
krauss
parents: 20534
diff changeset
   108
      val k = length args
d80502f0d701 1. Function package accepts a parameter (default "some_term"), which specifies the functions
krauss
parents: 20534
diff changeset
   109
d80502f0d701 1. Function package accepts a parameter (default "some_term"), which specifies the functions
krauss
parents: 20534
diff changeset
   110
      val arities' = case Symtab.lookup arities fname of
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   111
                       NONE => Symtab.update (fname, k) arities
22567
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22526
diff changeset
   112
                     | SOME i => (i = k orelse
1565d476a9e2 removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
wenzelm
parents: 22526
diff changeset
   113
                                  error (input_error ("Function " ^ quote fname ^ " has different numbers of arguments in different equations"));
21319
cf814e36f788 replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents: 21255
diff changeset
   114
                                  arities)
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   115
    in
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   116
      ((fname, qs, gs, args, rhs), arities')
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   117
    end
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   118
    
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   119
fun get_part fname =
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   120
    the o find_first (fn (MutualPart {fvar=(n,_), ...}) => n = fname)
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   121
                     
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   122
(* FIXME *)
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   123
fun mk_prod_abs e (t1, t2) =
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   124
    let
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   125
      val bTs = rev (map snd e)
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   126
      val T1 = fastype_of1 (bTs, t1)
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   127
      val T2 = fastype_of1 (bTs, t2)
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   128
    in
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   129
      HOLogic.pair_const T1 T2 $ t1 $ t2
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   130
    end;
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   131
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   132
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   133
fun analyze_eqs ctxt defname fs eqs =
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   134
    let
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   135
        val fnames = map fst fs
20851
bf80cb83f8be Improved error reporting
krauss
parents: 20797
diff changeset
   136
        val (fqgars, arities) = fold_map (split_def ctxt fnames) eqs Symtab.empty
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   137
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   138
        fun curried_types (fname, fT) =
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   139
            let
20654
d80502f0d701 1. Function package accepts a parameter (default "some_term"), which specifies the functions
krauss
parents: 20534
diff changeset
   140
              val k = the_default 1 (Symtab.lookup arities fname)
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   141
              val (caTs, uaTs) = chop k (binder_types fT)
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   142
            in
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   143
                (caTs, uaTs ---> body_type fT)
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   144
            end
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   145
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   146
        val (caTss, resultTs) = split_list (map curried_types fs)
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   147
        val argTs = map (foldr1 HOLogic.mk_prodT) caTss
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   148
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   149
        val (RST,streeR, pthsR) = SumTools.mk_tree_distinct resultTs
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   150
        val (ST, streeA, pthsA) = SumTools.mk_tree argTs
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   151
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   152
        val fsum_type = ST --> RST
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   153
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   154
        val ([fsum_var_name], _) = Variable.add_fixes [ defname ^ "_sum" ] ctxt
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   155
        val fsum_var = (fsum_var_name, fsum_type)
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   156
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   157
        fun define (fvar as (n, T)) caTs pthA pthR =
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   158
            let
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   159
                val vars = map_index (fn (i,T) => Free ("x" ^ string_of_int i, T)) caTs (* FIXME: Bind xs properly *)
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   160
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   161
                val f_exp = SumTools.mk_proj streeR pthR (Free fsum_var $ SumTools.mk_inj streeA pthA (foldr1 HOLogic.mk_prod vars))
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   162
                val def = Term.abstract_over (Free fsum_var, fold_rev lambda vars f_exp)
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   163
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   164
                val rew = (n, fold_rev lambda vars f_exp)
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   165
            in
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   166
                (MutualPart {fvar=fvar,cargTs=caTs,pthA=pthA,pthR=pthR,f_def=def,f=NONE,f_defthm=NONE}, rew)
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   167
            end
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   168
            
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   169
        val (parts, rews) = split_list (map4 define fs caTss pthsA pthsR)
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   170
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   171
        fun convert_eqs (f, qs, gs, args, rhs) =
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   172
            let
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   173
              val MutualPart {pthA, pthR, ...} = get_part f parts
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   174
            in
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   175
              (qs, gs, SumTools.mk_inj streeA pthA (foldr1 (mk_prod_abs qs) args),
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   176
               SumTools.mk_inj streeR pthR (replace_frees rews rhs)
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   177
                               |> Envir.norm_term (Envir.empty 0))
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   178
            end
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   179
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   180
        val qglrs = map convert_eqs fqgars
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   181
    in
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   182
        Mutual {fsum_var=fsum_var, ST=ST, RST=RST, streeA=streeA, streeR=streeR,
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   183
                parts=parts, fqgars=fqgars, qglrs=qglrs, fsum=NONE}
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   184
    end
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   185
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   186
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   187
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   188
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   189
fun define_projections fixes mutual fsum lthy =
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   190
    let
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   191
      fun def ((MutualPart {fvar=(fname, fT), cargTs, pthA, pthR, f_def, ...}), (_, mixfix)) lthy =
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   192
          let
21436
5313a4cc3823 LocalTheory.notes/defs: proper kind;
wenzelm
parents: 21319
diff changeset
   193
            val ((f, (_, f_defthm)), lthy') =
5313a4cc3823 LocalTheory.notes/defs: proper kind;
wenzelm
parents: 21319
diff changeset
   194
              LocalTheory.def Thm.internalK ((fname, mixfix),
5313a4cc3823 LocalTheory.notes/defs: proper kind;
wenzelm
parents: 21319
diff changeset
   195
                                            ((fname ^ "_def", []), Term.subst_bound (fsum, f_def)))
5313a4cc3823 LocalTheory.notes/defs: proper kind;
wenzelm
parents: 21319
diff changeset
   196
                              lthy
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   197
          in
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   198
            (MutualPart {fvar=(fname, fT), cargTs=cargTs, pthA=pthA, pthR=pthR, f_def=f_def,
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   199
                         f=SOME f, f_defthm=SOME f_defthm },
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   200
             lthy')
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   201
          end
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   202
          
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   203
      val Mutual { fsum_var, ST, RST, streeA, streeR, parts, fqgars, qglrs, ... } = mutual
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   204
      val (parts', lthy') = fold_map def (parts ~~ fixes) lthy
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   205
    in
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   206
      (Mutual { fsum_var=fsum_var, ST=ST, RST=RST, streeA=streeA, streeR=streeR, parts=parts',
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   207
                fqgars=fqgars, qglrs=qglrs, fsum=SOME fsum },
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   208
       lthy')
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   209
    end
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   210
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   211
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   212
fun beta_reduce thm = Thm.equal_elim (Thm.beta_conversion true (cprop_of thm)) thm
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   213
                      
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   214
fun in_context ctxt (f, pre_qs, pre_gs, pre_args, pre_rhs) F =
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   215
    let
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   216
      val thy = ProofContext.theory_of ctxt
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   217
                
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   218
      val oqnames = map fst pre_qs
20797
c1f0bc7e7d80 renamed Variable.invent_fixes to Variable.variant_fixes;
wenzelm
parents: 20654
diff changeset
   219
      val (qs, ctxt') = Variable.variant_fixes oqnames ctxt
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   220
                        |>> map2 (fn (_, T) => fn n => Free (n, T)) pre_qs
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   221
                        
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   222
      fun inst t = subst_bounds (rev qs, t)
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   223
      val gs = map inst pre_gs
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   224
      val args = map inst pre_args
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   225
      val rhs = inst pre_rhs
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   226
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   227
      val cqs = map (cterm_of thy) qs
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   228
      val ags = map (assume o cterm_of thy) gs
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   229
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   230
      val import = fold forall_elim cqs
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   231
                   #> fold implies_elim_swp ags
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   232
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   233
      val export = fold_rev (implies_intr o cprop_of) ags
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   234
                   #> fold_rev forall_intr_rename (oqnames ~~ cqs)
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   235
    in
22497
1fe951654cee fixed problem with the construction of mutual simp rules
krauss
parents: 22323
diff changeset
   236
      F ctxt (f, qs, gs, args, rhs) import export
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   237
    end
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   238
22497
1fe951654cee fixed problem with the construction of mutual simp rules
krauss
parents: 22323
diff changeset
   239
fun recover_mutual_psimp all_orig_fdefs parts ctxt (fname, _, _, args, rhs) import (export : thm -> thm) sum_psimp_eq =
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   240
    let
22497
1fe951654cee fixed problem with the construction of mutual simp rules
krauss
parents: 22323
diff changeset
   241
      val (MutualPart {f=SOME f, f_defthm=SOME f_def, pthR, ...}) = get_part fname parts
1fe951654cee fixed problem with the construction of mutual simp rules
krauss
parents: 22323
diff changeset
   242
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   243
      val psimp = import sum_psimp_eq
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   244
      val (simp, restore_cond) = case cprems_of psimp of
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   245
                                   [] => (psimp, I)
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   246
                                 | [cond] => (implies_elim psimp (assume cond), implies_intr cond)
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   247
                                 | _ => sys_error "Too many conditions"
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   248
    in
22497
1fe951654cee fixed problem with the construction of mutual simp rules
krauss
parents: 22323
diff changeset
   249
      Goal.prove ctxt [] [] 
1fe951654cee fixed problem with the construction of mutual simp rules
krauss
parents: 22323
diff changeset
   250
                 (HOLogic.Trueprop $ HOLogic.mk_eq (list_comb (f, args), rhs))
1fe951654cee fixed problem with the construction of mutual simp rules
krauss
parents: 22323
diff changeset
   251
                 (fn _ => SIMPSET (unfold_tac all_orig_fdefs)
1fe951654cee fixed problem with the construction of mutual simp rules
krauss
parents: 22323
diff changeset
   252
                          THEN EqSubst.eqsubst_tac ctxt [0] [simp] 1
22622
25693088396b Moving "FunDef" up in the HOL development graph, since it is independent from "Recdef" and "Datatype" now.
krauss
parents: 22620
diff changeset
   253
                          THEN SIMPSET' (fn ss => simp_tac (ss addsimps [SumTools.projl_inl, SumTools.projr_inr])) 1)
22497
1fe951654cee fixed problem with the construction of mutual simp rules
krauss
parents: 22323
diff changeset
   254
        |> restore_cond 
1fe951654cee fixed problem with the construction of mutual simp rules
krauss
parents: 22323
diff changeset
   255
        |> export
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   256
    end
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   257
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   258
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   259
(* FIXME HACK *)
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   260
fun mk_applied_form ctxt caTs thm =
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   261
    let
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   262
      val thy = ProofContext.theory_of ctxt
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   263
      val xs = map_index (fn (i,T) => cterm_of thy (Free ("x" ^ string_of_int i, T))) caTs (* FIXME: Bind xs properly *)
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   264
    in
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   265
      fold (fn x => fn thm => combination thm (reflexive x)) xs thm
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   266
           |> beta_reduce
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   267
           |> fold_rev forall_intr xs
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   268
           |> forall_elim_vars 0
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   269
    end
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   270
    
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   271
22623
5fcee5b319a2 proper handling of morphisms
krauss
parents: 22622
diff changeset
   272
fun mutual_induct_rules lthy induct all_f_defs (Mutual {RST, parts, streeA, ...}) =
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   273
    let
22623
5fcee5b319a2 proper handling of morphisms
krauss
parents: 22622
diff changeset
   274
      val cert = cterm_of (ProofContext.theory_of lthy)
20949
f030835fd9e4 Induction rules have schematic variables again.
krauss
parents: 20878
diff changeset
   275
      val newPs = map2 (fn Pname => fn MutualPart {cargTs, ...} => 
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   276
                                       Free (Pname, cargTs ---> HOLogic.boolT))
20949
f030835fd9e4 Induction rules have schematic variables again.
krauss
parents: 20878
diff changeset
   277
                       (mutual_induct_Pnames (length parts))
f030835fd9e4 Induction rules have schematic variables again.
krauss
parents: 20878
diff changeset
   278
                       parts
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   279
                       
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   280
      fun mk_P (MutualPart {cargTs, ...}) P =
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   281
          let
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   282
            val avars = map_index (fn (i,T) => Var (("a", i), T)) cargTs
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   283
            val atup = foldr1 HOLogic.mk_prod avars
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   284
          in
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   285
            tupled_lambda atup (list_comb (P, avars))
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   286
          end
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   287
          
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   288
      val Ps = map2 mk_P parts newPs
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   289
      val case_exp = SumTools.mk_sumcases streeA HOLogic.boolT Ps
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   290
                     
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   291
      val induct_inst =
22623
5fcee5b319a2 proper handling of morphisms
krauss
parents: 22622
diff changeset
   292
          forall_elim (cert case_exp) induct
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   293
                      |> full_simplify (HOL_basic_ss addsimps (split_apply :: sum_case_rules))
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   294
                      |> full_simplify (HOL_basic_ss addsimps all_f_defs)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   295
          
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   296
      fun mk_proj rule (MutualPart {cargTs, pthA, ...}) =
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   297
          let
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   298
            val afs = map_index (fn (i,T) => Free ("a" ^ string_of_int i, T)) cargTs
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   299
            val inj = SumTools.mk_inj streeA pthA (foldr1 HOLogic.mk_prod afs)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   300
          in
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   301
            rule
22623
5fcee5b319a2 proper handling of morphisms
krauss
parents: 22622
diff changeset
   302
              |> forall_elim (cert inj)
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   303
              |> full_simplify (HOL_basic_ss addsimps (split_apply :: sum_case_rules))
22623
5fcee5b319a2 proper handling of morphisms
krauss
parents: 22622
diff changeset
   304
              |> fold_rev (forall_intr o cert) (afs @ newPs)
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   305
          end
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   306
          
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   307
    in
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   308
      map (mk_proj induct_inst) parts
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   309
    end
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   310
    
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   311
22623
5fcee5b319a2 proper handling of morphisms
krauss
parents: 22622
diff changeset
   312
fun mk_partial_rules_mutual lthy inner_cont (m as Mutual {parts, fqgars, ...}) proof =
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   313
    let
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   314
      val result = inner_cont proof
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   315
      val FundefResult {fs=[f], G, R, cases, psimps, trsimps, subset_pinducts=[subset_pinduct],simple_pinducts=[simple_pinduct],
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   316
                        termination,domintros} = result
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   317
                                                                                                               
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   318
      val (all_f_defs, fs) = map (fn MutualPart {f_defthm = SOME f_def, f = SOME f, cargTs, ...} =>
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   319
                                     (mk_applied_form lthy cargTs (symmetric f_def), f))
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   320
                                 parts
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   321
                             |> split_list
22497
1fe951654cee fixed problem with the construction of mutual simp rules
krauss
parents: 22323
diff changeset
   322
1fe951654cee fixed problem with the construction of mutual simp rules
krauss
parents: 22323
diff changeset
   323
      val all_orig_fdefs = map (fn MutualPart {f_defthm = SOME f_def, ...} => f_def) parts
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   324
                           
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   325
      fun mk_mpsimp fqgar sum_psimp =
22497
1fe951654cee fixed problem with the construction of mutual simp rules
krauss
parents: 22323
diff changeset
   326
          in_context lthy fqgar (recover_mutual_psimp all_orig_fdefs parts) sum_psimp
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   327
          
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   328
      val mpsimps = map2 mk_mpsimp fqgars psimps
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   329
      val mtrsimps = map_option (map2 mk_mpsimp fqgars) trsimps
22623
5fcee5b319a2 proper handling of morphisms
krauss
parents: 22622
diff changeset
   330
      val minducts = mutual_induct_rules lthy simple_pinduct all_f_defs m
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   331
      val mtermination = full_simplify (HOL_basic_ss addsimps all_f_defs) termination
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   332
    in
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   333
      FundefResult { fs=fs, G=G, R=R,
22623
5fcee5b319a2 proper handling of morphisms
krauss
parents: 22622
diff changeset
   334
                     psimps=mpsimps, subset_pinducts=[subset_pinduct], simple_pinducts=minducts,
5fcee5b319a2 proper handling of morphisms
krauss
parents: 22622
diff changeset
   335
                     cases=cases, termination=mtermination,
5fcee5b319a2 proper handling of morphisms
krauss
parents: 22622
diff changeset
   336
                     domintros=domintros,
5fcee5b319a2 proper handling of morphisms
krauss
parents: 22622
diff changeset
   337
                     trsimps=mtrsimps}
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   338
    end
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   339
      
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   340
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   341
(* puts an object in the "right bucket" *)
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   342
fun store_grouped P x [] = []
20878
384c5bb713b2 mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents: 20851
diff changeset
   343
  | store_grouped P x ((l, xs)::bs) =
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   344
    if P (x, l) then ((l, x::xs)::bs) else ((l, xs)::store_grouped P x bs)
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   345
                                           
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 19922
diff changeset
   346
fun sort_by_function (Mutual {fqgars, ...}) names xs =
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   347
    fold_rev (store_grouped (eq_str o apfst fst))  (* fill *)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   348
             (map name_of_fqgar fqgars ~~ xs)      (* the name-thm pairs *)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   349
             (map (rpair []) names)                (* in the empty buckets labeled with names *)
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   350
             
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   351
             |> map (snd #> map snd)                     (* and remove the labels afterwards *)
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   352
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   353
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   354
fun prepare_fundef_mutual config defname fixes eqss default lthy =
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   355
    let
22323
b8c227d8ca91 beta_eta_contract specification befor processing. These normal forms avoid unpleasant surprises later on.
krauss
parents: 22244
diff changeset
   356
      val mutual = analyze_eqs lthy defname (map fst fixes) (map Envir.beta_eta_contract eqss)
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   357
      val Mutual {fsum_var=(n, T), qglrs, ...} = mutual
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   358
          
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   359
      val ((fsum, goalstate, cont), lthy') =
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   360
          FundefCore.prepare_fundef config defname (n, T, NoSyn) qglrs default lthy
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   361
          
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   362
      val (mutual', lthy'') = define_projections fixes mutual fsum lthy'
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   363
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   364
      val mutual_cont = mk_partial_rules_mutual lthy'' cont mutual'
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   365
      val sort_cont = sort_by_function mutual' (map (fst o fst) fixes)
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   366
    in
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   367
      ((goalstate, mutual_cont, sort_cont), lthy'')
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   368
    end
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21436
diff changeset
   369
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   370
    
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff changeset
   371
end