src/HOL/Tools/function_package/fundef_datatype.ML
author krauss
Tue, 21 Apr 2009 09:53:24 +0200
changeset 30906 3c7a76e79898
parent 30790 350bb108406d
child 31723 f5cafe803b55
permissions -rw-r--r--
simplify computation and consistency checks of argument counts in the input
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     1
(*  Title:      HOL/Tools/function_package/fundef_datatype.ML
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     2
    Author:     Alexander Krauss, TU Muenchen
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     3
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
     4
A package for general recursive function definitions.
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     5
A tactic to prove completeness of datatype patterns.
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     6
*)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     7
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
     8
signature FUNDEF_DATATYPE =
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     9
sig
30493
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 29387
diff changeset
    10
    val pat_completeness_tac: Proof.context -> int -> tactic
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 29387
diff changeset
    11
    val pat_completeness: Proof.context -> Proof.method
27271
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
    12
    val prove_completeness : theory -> term list -> term -> term list list -> term list list -> thm
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    13
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    14
    val setup : theory -> theory
30493
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 29387
diff changeset
    15
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 29387
diff changeset
    16
    val add_fun : FundefCommon.fundef_config ->
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 29387
diff changeset
    17
      (binding * typ option * mixfix) list -> (Attrib.binding * term) list ->
30787
5b7a5a05c7aa remove "otherwise" feature from function package which was never really documented or used
krauss
parents: 30515
diff changeset
    18
      bool -> local_theory -> Proof.context
30493
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 29387
diff changeset
    19
    val add_fun_cmd : FundefCommon.fundef_config ->
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 29387
diff changeset
    20
      (binding * string option * mixfix) list -> (Attrib.binding * string) list ->
30787
5b7a5a05c7aa remove "otherwise" feature from function package which was never really documented or used
krauss
parents: 30515
diff changeset
    21
      bool -> local_theory -> Proof.context
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    22
end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    23
27271
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
    24
structure FundefDatatype : FUNDEF_DATATYPE =
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    25
struct
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    26
21051
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20999
diff changeset
    27
open FundefLib
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20999
diff changeset
    28
open FundefCommon
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents: 19564
diff changeset
    29
23189
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 23056
diff changeset
    30
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 23056
diff changeset
    31
fun check_pats ctxt geq =
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 23056
diff changeset
    32
    let 
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    33
      fun err str = error (cat_lines ["Malformed definition:",
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    34
                                      str ^ " not allowed in sequential mode.",
24920
2a45e400fdad generic Syntax.pretty/string_of operations;
wenzelm
parents: 24867
diff changeset
    35
                                      Syntax.string_of_term ctxt geq])
23189
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 23056
diff changeset
    36
      val thy = ProofContext.theory_of ctxt
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    37
                
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    38
      fun check_constr_pattern (Bound _) = ()
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    39
        | check_constr_pattern t =
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    40
          let
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    41
            val (hd, args) = strip_comb t
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    42
          in
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    43
            (((case DatatypePackage.datatype_of_constr thy (fst (dest_Const hd)) of
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    44
                 SOME _ => ()
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    45
               | NONE => err "Non-constructor pattern")
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    46
              handle TERM ("dest_Const", _) => err "Non-constructor patterns");
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    47
             map check_constr_pattern args; 
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    48
             ())
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    49
          end
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    50
          
24170
33f055a0f3a1 more error handling
krauss
parents: 23819
diff changeset
    51
      val (fname, qs, gs, args, rhs) = split_def ctxt geq 
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    52
                                       
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    53
      val _ = if not (null gs) then err "Conditional equations" else ()
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    54
      val _ = map check_constr_pattern args
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    55
                  
23189
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 23056
diff changeset
    56
                  (* just count occurrences to check linearity *)
24170
33f055a0f3a1 more error handling
krauss
parents: 23819
diff changeset
    57
      val _ = if fold (fold_aterms (fn Bound _ => curry (op +) 1 | _ => I)) args 0 > length qs
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    58
              then err "Nonlinear patterns" else ()
23189
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 23056
diff changeset
    59
    in
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 23056
diff changeset
    60
      ()
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 23056
diff changeset
    61
    end
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    62
    
23189
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 23056
diff changeset
    63
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    64
fun mk_argvar i T = Free ("_av" ^ (string_of_int i), T)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    65
fun mk_patvar i T = Free ("_pv" ^ (string_of_int i), T)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    66
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    67
fun inst_free var inst thm =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    68
    forall_elim inst (forall_intr var thm)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    69
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    70
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    71
fun inst_case_thm thy x P thm =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    72
    let
29329
e02b3a32f34f removed references to OldTerm.*
krauss
parents: 29265
diff changeset
    73
        val [Pv, xv] = Term.add_vars (prop_of thm) []
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    74
    in
29329
e02b3a32f34f removed references to OldTerm.*
krauss
parents: 29265
diff changeset
    75
        cterm_instantiate [(cterm_of thy (Var xv), cterm_of thy x), 
e02b3a32f34f removed references to OldTerm.*
krauss
parents: 29265
diff changeset
    76
                           (cterm_of thy (Var Pv), cterm_of thy P)] thm
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    77
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    78
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    79
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    80
fun invent_vars constr i =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    81
    let
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    82
        val Ts = binder_types (fastype_of constr)
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    83
        val j = i + length Ts
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    84
        val is = i upto (j - 1)
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    85
        val avs = map2 mk_argvar is Ts
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    86
        val pvs = map2 mk_patvar is Ts
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    87
    in
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    88
        (avs, pvs, j)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    89
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    90
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    91
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    92
fun filter_pats thy cons pvars [] = []
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    93
  | filter_pats thy cons pvars (([], thm) :: pts) = raise Match
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    94
  | filter_pats thy cons pvars ((pat :: pats, thm) :: pts) =
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    95
    case pat of
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    96
        Free _ => let val inst = list_comb (cons, pvars)
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    97
                 in (inst :: pats, inst_free (cterm_of thy pat) (cterm_of thy inst) thm)
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    98
                    :: (filter_pats thy cons pvars pts) end
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    99
      | _ => if fst (strip_comb pat) = cons
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   100
             then (pat :: pats, thm) :: (filter_pats thy cons pvars pts)
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   101
             else filter_pats thy cons pvars pts
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   102
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   103
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   104
fun inst_constrs_of thy (T as Type (name, _)) =
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   105
        map (fn (Cn,CT) => Envir.subst_TVars (Sign.typ_match thy (body_type CT, T) Vartab.empty) (Const (Cn, CT)))
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   106
            (the (DatatypePackage.get_datatype_constrs thy name))
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   107
  | inst_constrs_of thy _ = raise Match
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   108
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   109
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   110
fun transform_pat thy avars c_assum ([] , thm) = raise Match
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   111
  | transform_pat thy avars c_assum (pat :: pats, thm) =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   112
    let
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   113
        val (_, subps) = strip_comb pat
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   114
        val eqs = map (cterm_of thy o HOLogic.mk_Trueprop o HOLogic.mk_eq) (avars ~~ subps)
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   115
        val a_eqs = map assume eqs
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   116
        val c_eq_pat = simplify (HOL_basic_ss addsimps a_eqs) c_assum
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   117
    in
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   118
        (subps @ pats, fold_rev implies_intr eqs
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   119
                                (implies_elim thm c_eq_pat))
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   120
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   121
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   122
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   123
exception COMPLETENESS
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   124
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   125
fun constr_case thy P idx (v :: vs) pats cons =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   126
    let
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   127
        val (avars, pvars, newidx) = invent_vars cons idx
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   128
        val c_hyp = cterm_of thy (HOLogic.mk_Trueprop (HOLogic.mk_eq (v, list_comb (cons, avars))))
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   129
        val c_assum = assume c_hyp
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   130
        val newpats = map (transform_pat thy avars c_assum) (filter_pats thy cons pvars pats)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   131
    in
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   132
        o_alg thy P newidx (avars @ vs) newpats
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   133
              |> implies_intr c_hyp
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   134
              |> fold_rev (forall_intr o cterm_of thy) avars
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   135
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   136
  | constr_case _ _ _ _ _ _ = raise Match
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   137
and o_alg thy P idx [] (([], Pthm) :: _)  = Pthm
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   138
  | o_alg thy P idx (v :: vs) [] = raise COMPLETENESS
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   139
  | o_alg thy P idx (v :: vs) pts =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   140
    if forall (is_Free o hd o fst) pts (* Var case *)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   141
    then o_alg thy P idx vs (map (fn (pv :: pats, thm) =>
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   142
                               (pats, refl RS (inst_free (cterm_of thy pv) (cterm_of thy v) thm))) pts)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   143
    else (* Cons case *)
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   144
         let
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   145
             val T = fastype_of v
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   146
             val (tname, _) = dest_Type T
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   147
             val {exhaustion=case_thm, ...} = DatatypePackage.the_datatype thy tname
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   148
             val constrs = inst_constrs_of thy T
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   149
             val c_cases = map (constr_case thy P idx (v :: vs) pts) constrs
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   150
         in
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   151
             inst_case_thm thy v P case_thm
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   152
                           |> fold (curry op COMP) c_cases
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   153
         end
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   154
  | o_alg _ _ _ _ _ = raise Match
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   155
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   156
27271
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
   157
fun prove_completeness thy xs P qss patss =
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   158
    let
27271
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
   159
        fun mk_assum qs pats = 
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
   160
            HOLogic.mk_Trueprop P
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
   161
            |> fold_rev (curry Logic.mk_implies o HOLogic.mk_Trueprop o HOLogic.mk_eq) (xs ~~ pats)
27330
1af2598b5f7d Logic.all/mk_equals/mk_implies;
wenzelm
parents: 27271
diff changeset
   162
            |> fold_rev Logic.all qs
27271
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
   163
            |> cterm_of thy
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   164
27271
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
   165
        val hyps = map2 mk_assum qss patss
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   166
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   167
        fun inst_hyps hyp qs = fold (forall_elim o cterm_of thy) qs (assume hyp)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   168
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   169
        val assums = map2 inst_hyps hyps qss
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   170
    in
27271
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
   171
        o_alg thy P 2 xs (patss ~~ assums)
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   172
              |> fold_rev implies_intr hyps
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   173
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   174
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   175
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   176
30493
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 29387
diff changeset
   177
fun pat_completeness_tac ctxt = SUBGOAL (fn (subgoal, i) =>
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   178
    let
27271
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
   179
      val thy = ProofContext.theory_of ctxt
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
   180
      val (vs, subgf) = dest_all_all subgoal
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
   181
      val (cases, _ $ thesis) = Logic.strip_horn subgf
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
   182
          handle Bind => raise COMPLETENESS
19922
984ae977f7aa Fixed name clash.
krauss
parents: 19770
diff changeset
   183
27271
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
   184
      fun pat_of assum =
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   185
            let
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   186
                val (qs, imp) = dest_all_all assum
27271
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
   187
                val prems = Logic.strip_imp_prems imp
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   188
            in
27271
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
   189
              (qs, map (HOLogic.dest_eq o HOLogic.dest_Trueprop) prems)
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   190
            end
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   191
27271
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
   192
        val (qss, x_pats) = split_list (map pat_of cases)
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
   193
        val xs = map fst (hd x_pats)
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
   194
                 handle Empty => raise COMPLETENESS
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
   195
                 
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
   196
        val patss = map (map snd) x_pats 
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   197
27271
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
   198
        val complete_thm = prove_completeness thy xs thesis qss patss
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
   199
             |> fold_rev (forall_intr o cterm_of thy) vs
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   200
    in
27271
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
   201
      PRIMITIVE (fn st => Drule.compose_single(complete_thm, i, st))
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   202
    end
27271
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
   203
    handle COMPLETENESS => no_tac)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   204
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   205
30510
4120fc59dd85 unified type Proof.method and pervasive METHOD combinators;
wenzelm
parents: 30493
diff changeset
   206
fun pat_completeness ctxt = SIMPLE_METHOD' (pat_completeness_tac ctxt)
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   207
30493
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 29387
diff changeset
   208
val by_pat_completeness_auto =
29387
d23fdfa46b6a Proof.global_future_terminal_proof;
wenzelm
parents: 29382
diff changeset
   209
    Proof.global_future_terminal_proof
27271
ba2a00d35df1 generalized induct_scheme method to prove conditional induction schemes.
krauss
parents: 26989
diff changeset
   210
      (Method.Basic (pat_completeness, Position.none),
22899
5ea718c68123 "fun" command: Changed pattern compatibility proof back from "simp_all" to the slower but more robust "auto"
krauss
parents: 22733
diff changeset
   211
       SOME (Method.Source_i (Args.src (("HOL.auto", []), Position.none))))
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   212
29382
9f6e2658a868 Proof.future_terminal_proof: no fork for interactive mode -- proofs need to be checked immediately here;
wenzelm
parents: 29351
diff changeset
   213
fun termination_by method int =
30493
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 29387
diff changeset
   214
    FundefPackage.termination_proof NONE
29387
d23fdfa46b6a Proof.global_future_terminal_proof;
wenzelm
parents: 29382
diff changeset
   215
    #> Proof.global_future_terminal_proof
29382
9f6e2658a868 Proof.future_terminal_proof: no fork for interactive mode -- proofs need to be checked immediately here;
wenzelm
parents: 29351
diff changeset
   216
      (Method.Basic (method, Position.none), NONE) int
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   217
30906
3c7a76e79898 simplify computation and consistency checks of argument counts in the input
krauss
parents: 30790
diff changeset
   218
fun mk_catchall fixes arity_of =
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   219
    let
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   220
      fun mk_eqn ((fname, fT), _) =
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   221
          let 
30906
3c7a76e79898 simplify computation and consistency checks of argument counts in the input
krauss
parents: 30790
diff changeset
   222
            val n = arity_of fname
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   223
            val (argTs, rT) = chop n (binder_types fT)
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   224
                                   |> apsnd (fn Ts => Ts ---> body_type fT) 
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   225
                              
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   226
            val qs = map Free (Name.invent_list [] "a" n ~~ argTs)
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   227
          in
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   228
            HOLogic.mk_eq(list_comb (Free (fname, fT), qs),
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   229
                          Const ("HOL.undefined", rT))
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   230
              |> HOLogic.mk_Trueprop
27330
1af2598b5f7d Logic.all/mk_equals/mk_implies;
wenzelm
parents: 27271
diff changeset
   231
              |> fold_rev Logic.all qs
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   232
          end
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   233
    in
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   234
      map mk_eqn fixes
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   235
    end
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   236
24170
33f055a0f3a1 more error handling
krauss
parents: 23819
diff changeset
   237
fun add_catchall ctxt fixes spec =
30906
3c7a76e79898 simplify computation and consistency checks of argument counts in the input
krauss
parents: 30790
diff changeset
   238
  let val fqgars = map (split_def ctxt) spec
3c7a76e79898 simplify computation and consistency checks of argument counts in the input
krauss
parents: 30790
diff changeset
   239
      val arity_of = map (fn (fname,_,_,args,_) => (fname, length args)) fqgars
3c7a76e79898 simplify computation and consistency checks of argument counts in the input
krauss
parents: 30790
diff changeset
   240
                     |> AList.lookup (op =) #> the
3c7a76e79898 simplify computation and consistency checks of argument counts in the input
krauss
parents: 30790
diff changeset
   241
  in
3c7a76e79898 simplify computation and consistency checks of argument counts in the input
krauss
parents: 30790
diff changeset
   242
    spec @ mk_catchall fixes arity_of
3c7a76e79898 simplify computation and consistency checks of argument counts in the input
krauss
parents: 30790
diff changeset
   243
  end
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   244
24356
65fd09a7243f issue a warning, when encountering redundant equations (covered by prece3ding clauses)
krauss
parents: 24170
diff changeset
   245
fun warn_if_redundant ctxt origs tss =
65fd09a7243f issue a warning, when encountering redundant equations (covered by prece3ding clauses)
krauss
parents: 24170
diff changeset
   246
    let
24920
2a45e400fdad generic Syntax.pretty/string_of operations;
wenzelm
parents: 24867
diff changeset
   247
        fun msg t = "Ignoring redundant equation: " ^ quote (Syntax.string_of_term ctxt t)
24466
619f78b717cb fixed pattern comnpletion; untabified
krauss
parents: 24356
diff changeset
   248
                    
619f78b717cb fixed pattern comnpletion; untabified
krauss
parents: 24356
diff changeset
   249
        val (tss', _) = chop (length origs) tss
30787
5b7a5a05c7aa remove "otherwise" feature from function package which was never really documented or used
krauss
parents: 30515
diff changeset
   250
        fun check (t, []) = (Output.warning (msg t); [])
5b7a5a05c7aa remove "otherwise" feature from function package which was never really documented or used
krauss
parents: 30515
diff changeset
   251
          | check (t, s) = s
24356
65fd09a7243f issue a warning, when encountering redundant equations (covered by prece3ding clauses)
krauss
parents: 24170
diff changeset
   252
    in
24466
619f78b717cb fixed pattern comnpletion; untabified
krauss
parents: 24356
diff changeset
   253
        (map check (origs ~~ tss'); tss)
24356
65fd09a7243f issue a warning, when encountering redundant equations (covered by prece3ding clauses)
krauss
parents: 24170
diff changeset
   254
    end
65fd09a7243f issue a warning, when encountering redundant equations (covered by prece3ding clauses)
krauss
parents: 24170
diff changeset
   255
65fd09a7243f issue a warning, when encountering redundant equations (covered by prece3ding clauses)
krauss
parents: 24170
diff changeset
   256
30787
5b7a5a05c7aa remove "otherwise" feature from function package which was never really documented or used
krauss
parents: 30515
diff changeset
   257
fun sequential_preproc (config as FundefConfig {sequential, ...}) ctxt fixes spec =
5b7a5a05c7aa remove "otherwise" feature from function package which was never really documented or used
krauss
parents: 30515
diff changeset
   258
      if sequential then
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   259
        let
30790
350bb108406d bstring -> binding
krauss
parents: 30787
diff changeset
   260
          val (bnds, eqss) = split_list spec
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   261
                            
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   262
          val eqs = map the_single eqss
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   263
                    
24356
65fd09a7243f issue a warning, when encountering redundant equations (covered by prece3ding clauses)
krauss
parents: 24170
diff changeset
   264
          val feqs = eqs
25222
78943ac46f6d fun/function: generate case names for induction rules
krauss
parents: 25169
diff changeset
   265
                      |> tap (check_defs ctxt fixes) (* Standard checks *)
78943ac46f6d fun/function: generate case names for induction rules
krauss
parents: 25169
diff changeset
   266
                      |> tap (map (check_pats ctxt))    (* More checks for sequential mode *)
24356
65fd09a7243f issue a warning, when encountering redundant equations (covered by prece3ding clauses)
krauss
parents: 24170
diff changeset
   267
25222
78943ac46f6d fun/function: generate case names for induction rules
krauss
parents: 25169
diff changeset
   268
          val compleqs = add_catchall ctxt fixes feqs   (* Completion *)
24356
65fd09a7243f issue a warning, when encountering redundant equations (covered by prece3ding clauses)
krauss
parents: 24170
diff changeset
   269
25222
78943ac46f6d fun/function: generate case names for induction rules
krauss
parents: 25169
diff changeset
   270
          val spliteqs = warn_if_redundant ctxt feqs
30787
5b7a5a05c7aa remove "otherwise" feature from function package which was never really documented or used
krauss
parents: 30515
diff changeset
   271
                           (FundefSplit.split_all_equations ctxt compleqs)
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   272
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   273
          fun restore_spec thms =
30790
350bb108406d bstring -> binding
krauss
parents: 30787
diff changeset
   274
              bnds ~~ Library.take (length bnds, Library.unflat spliteqs thms)
23819
2040846d1bbe some interface cleanup
krauss
parents: 23351
diff changeset
   275
              
30790
350bb108406d bstring -> binding
krauss
parents: 30787
diff changeset
   276
          val spliteqs' = flat (Library.take (length bnds, spliteqs))
23819
2040846d1bbe some interface cleanup
krauss
parents: 23351
diff changeset
   277
          val fnames = map (fst o fst) fixes
2040846d1bbe some interface cleanup
krauss
parents: 23351
diff changeset
   278
          val indices = map (fn eq => find_index (curry op = (fname_of eq)) fnames) spliteqs'
2040846d1bbe some interface cleanup
krauss
parents: 23351
diff changeset
   279
2040846d1bbe some interface cleanup
krauss
parents: 23351
diff changeset
   280
          fun sort xs = partition_list (fn i => fn (j,_) => i = j) 0 (length fnames - 1) (indices ~~ xs)
2040846d1bbe some interface cleanup
krauss
parents: 23351
diff changeset
   281
                                       |> map (map snd)
2040846d1bbe some interface cleanup
krauss
parents: 23351
diff changeset
   282
25222
78943ac46f6d fun/function: generate case names for induction rules
krauss
parents: 25169
diff changeset
   283
30790
350bb108406d bstring -> binding
krauss
parents: 30787
diff changeset
   284
          val bnds' = bnds @ replicate (length spliteqs - length bnds) Attrib.empty_binding
25222
78943ac46f6d fun/function: generate case names for induction rules
krauss
parents: 25169
diff changeset
   285
25361
1aa441e48496 function package: using the names of the equations as case names turned out to be impractical => disabled
krauss
parents: 25289
diff changeset
   286
          (* using theorem names for case name currently disabled *)
30790
350bb108406d bstring -> binding
krauss
parents: 30787
diff changeset
   287
          val case_names = map_index (fn (i, (_, es)) => mk_case_names i "" (length es)) 
350bb108406d bstring -> binding
krauss
parents: 30787
diff changeset
   288
                                     (bnds' ~~ spliteqs)
25222
78943ac46f6d fun/function: generate case names for induction rules
krauss
parents: 25169
diff changeset
   289
                           |> flat
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   290
        in
25222
78943ac46f6d fun/function: generate case names for induction rules
krauss
parents: 25169
diff changeset
   291
          (flat spliteqs, restore_spec, sort, case_names)
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   292
        end
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   293
      else
30787
5b7a5a05c7aa remove "otherwise" feature from function package which was never really documented or used
krauss
parents: 30515
diff changeset
   294
        FundefCommon.empty_preproc check_defs config ctxt fixes spec
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   295
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   296
val setup =
30515
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   297
    Method.setup @{binding pat_completeness} (Scan.succeed pat_completeness)
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   298
        "Completeness prover for datatype patterns"
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   299
    #> Context.theory_map (FundefCommon.set_preproc sequential_preproc)
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   300
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   301
28524
644b62cf678f arbitrary is undefined
haftmann
parents: 28287
diff changeset
   302
val fun_config = FundefConfig { sequential=true, default="%x. undefined" (*FIXME dynamic scoping*), 
26989
9b2acb536228 uniform treatment of target, not as config;
wenzelm
parents: 26270
diff changeset
   303
                                domintros=false, tailrec=false }
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   304
30787
5b7a5a05c7aa remove "otherwise" feature from function package which was never really documented or used
krauss
parents: 30515
diff changeset
   305
fun gen_fun add config fixes statements int lthy =
26171
5426a823455c more precise handling of "group" for termination;
wenzelm
parents: 26129
diff changeset
   306
  let val group = serial_string () in
21211
5370cfbf3070 Preparations for making "lexicographic_order" part of "fun"
krauss
parents: 21051
diff changeset
   307
    lthy
26171
5426a823455c more precise handling of "group" for termination;
wenzelm
parents: 26129
diff changeset
   308
      |> LocalTheory.set_group group
30787
5b7a5a05c7aa remove "otherwise" feature from function package which was never really documented or used
krauss
parents: 30515
diff changeset
   309
      |> add fixes statements config
30493
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 29387
diff changeset
   310
      |> by_pat_completeness_auto int
26270
73ac6430f5e7 restore instead of init
haftmann
parents: 26171
diff changeset
   311
      |> LocalTheory.restore
26171
5426a823455c more precise handling of "group" for termination;
wenzelm
parents: 26129
diff changeset
   312
      |> LocalTheory.set_group group
30493
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 29387
diff changeset
   313
      |> termination_by (FundefCommon.get_termination_prover lthy) int
26171
5426a823455c more precise handling of "group" for termination;
wenzelm
parents: 26129
diff changeset
   314
  end;
21211
5370cfbf3070 Preparations for making "lexicographic_order" part of "fun"
krauss
parents: 21051
diff changeset
   315
30493
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 29387
diff changeset
   316
val add_fun = gen_fun FundefPackage.add_fundef
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 29387
diff changeset
   317
val add_fun_cmd = gen_fun FundefPackage.add_fundef_cmd
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 29387
diff changeset
   318
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 29387
diff changeset
   319
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 29387
diff changeset
   320
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 29387
diff changeset
   321
local structure P = OuterParse and K = OuterKeyword in
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 29387
diff changeset
   322
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24466
diff changeset
   323
val _ =
29382
9f6e2658a868 Proof.future_terminal_proof: no fork for interactive mode -- proofs need to be checked immediately here;
wenzelm
parents: 29351
diff changeset
   324
  OuterSyntax.local_theory' "fun" "define general recursive functions (short version)" K.thy_decl
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   325
  (fundef_parser fun_config
30787
5b7a5a05c7aa remove "otherwise" feature from function package which was never really documented or used
krauss
parents: 30515
diff changeset
   326
     >> (fn ((config, fixes), statements) => add_fun_cmd config fixes statements));
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   327
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   328
end
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   329
20875
95d24e8d117e TheoryTarget.init;
wenzelm
parents: 20654
diff changeset
   330
end