src/HOL/Tools/function_package/fundef_datatype.ML
author krauss
Mon, 16 Jul 2007 21:22:43 +0200
changeset 23819 2040846d1bbe
parent 23351 3702086a15a3
child 24170 33f055a0f3a1
permissions -rw-r--r--
some interface cleanup
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
    ID:         $Id$
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     3
    Author:     Alexander Krauss, TU Muenchen
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     4
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
     5
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
     6
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
     7
*)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     8
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
     9
signature FUNDEF_DATATYPE =
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    10
sig
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    11
    val pat_complete_tac: int -> tactic
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    12
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    13
    val pat_completeness : method
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    14
    val setup : theory -> theory
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    15
end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    16
23351
3702086a15a3 Method.Basic: include position;
wenzelm
parents: 23203
diff changeset
    17
structure FundefDatatype: FUNDEF_DATATYPE =
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    18
struct
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    19
21051
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20999
diff changeset
    20
open FundefLib
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20999
diff changeset
    21
open FundefCommon
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents: 19564
diff changeset
    22
23189
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 23056
diff changeset
    23
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 23056
diff changeset
    24
fun check_pats ctxt geq =
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 23056
diff changeset
    25
    let 
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    26
      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
    27
                                      str ^ " not allowed in sequential mode.",
23189
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 23056
diff changeset
    28
                                      ProofContext.string_of_term ctxt geq])
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 23056
diff changeset
    29
      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
    30
                
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    31
      fun check_constr_pattern (Bound _) = ()
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    32
        | check_constr_pattern t =
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    33
          let
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    34
            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
    35
          in
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    36
            (((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
    37
                 SOME _ => ()
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    38
               | NONE => err "Non-constructor pattern")
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    39
              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
    40
             map check_constr_pattern args; 
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    41
             ())
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    42
          end
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    43
          
23189
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 23056
diff changeset
    44
      val (fname, qs, gs, args, rhs) = split_def geq 
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    45
                                       
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    46
      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
    47
      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
    48
                  
23189
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 23056
diff changeset
    49
                  (* just count occurrences to check linearity *)
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 23056
diff changeset
    50
      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
    51
              then err "Nonlinear patterns" else ()
23189
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 23056
diff changeset
    52
    in
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 23056
diff changeset
    53
      ()
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 23056
diff changeset
    54
    end
23203
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
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    57
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
    58
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
    59
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    60
fun inst_free var inst thm =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    61
    forall_elim inst (forall_intr var thm)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    62
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    63
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    64
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
    65
    let
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    66
        val [Pv, xv] = term_vars (prop_of thm)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    67
    in
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    68
        cterm_instantiate [(cterm_of thy xv, cterm_of thy x), (cterm_of thy Pv, cterm_of thy P)] thm
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    69
    end
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
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    72
fun invent_vars constr i =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    73
    let
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    74
        val Ts = binder_types (fastype_of constr)
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    75
        val j = i + length Ts
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    76
        val is = i upto (j - 1)
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    77
        val avs = map2 mk_argvar is Ts
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    78
        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
    79
    in
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    80
        (avs, pvs, j)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    81
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    82
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    83
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    84
fun filter_pats thy cons pvars [] = []
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    85
  | filter_pats thy cons pvars (([], thm) :: pts) = raise Match
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    86
  | 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
    87
    case pat of
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    88
        Free _ => let val inst = list_comb (cons, pvars)
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    89
                 in (inst :: pats, inst_free (cterm_of thy pat) (cterm_of thy inst) thm)
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    90
                    :: (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
    91
      | _ => if fst (strip_comb pat) = cons
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    92
             then (pat :: pats, thm) :: (filter_pats thy cons pvars pts)
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    93
             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
    94
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    95
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    96
fun inst_constrs_of thy (T as Type (name, _)) =
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
    97
        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
    98
            (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
    99
  | inst_constrs_of thy _ = raise Match
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   100
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   101
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   102
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
   103
  | 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
   104
    let
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   105
        val (_, subps) = strip_comb pat
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   106
        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
   107
        val a_eqs = map assume eqs
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   108
        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
   109
    in
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   110
        (subps @ pats, fold_rev implies_intr eqs
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   111
                                (implies_elim thm c_eq_pat))
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   112
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   113
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   114
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   115
exception COMPLETENESS
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   116
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   117
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
   118
    let
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   119
        val (avars, pvars, newidx) = invent_vars cons idx
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   120
        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
   121
        val c_assum = assume c_hyp
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   122
        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
   123
    in
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   124
        o_alg thy P newidx (avars @ vs) newpats
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   125
              |> implies_intr c_hyp
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   126
              |> 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
   127
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   128
  | constr_case _ _ _ _ _ _ = raise Match
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   129
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
   130
  | 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
   131
  | 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
   132
    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
   133
    then o_alg thy P idx vs (map (fn (pv :: pats, thm) =>
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   134
                               (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
   135
    else (* Cons case *)
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   136
         let
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   137
             val T = fastype_of v
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   138
             val (tname, _) = dest_Type T
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   139
             val {exhaustion=case_thm, ...} = DatatypePackage.the_datatype thy tname
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   140
             val constrs = inst_constrs_of thy T
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   141
             val c_cases = map (constr_case thy P idx (v :: vs) pts) constrs
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   142
         in
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   143
             inst_case_thm thy v P case_thm
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   144
                           |> fold (curry op COMP) c_cases
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   145
         end
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   146
  | o_alg _ _ _ _ _ = raise Match
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   147
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   148
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   149
fun prove_completeness thy x P qss pats =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   150
    let
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   151
        fun mk_assum qs pat = Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (x,pat)),
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   152
                                                HOLogic.mk_Trueprop P)
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   153
                                               |> fold_rev mk_forall qs
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   154
                                               |> cterm_of thy
19564
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
        val hyps = map2 mk_assum qss pats
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   157
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   158
        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
   159
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   160
        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
   161
    in
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   162
        o_alg thy P 2 [x] (map2 (pair o single) pats assums)
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   163
              |> fold_rev implies_intr hyps
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   164
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   165
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   166
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   167
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   168
fun pat_complete_tac i thm =
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   169
    let
19922
984ae977f7aa Fixed name clash.
krauss
parents: 19770
diff changeset
   170
      val thy = theory_of_thm thm
984ae977f7aa Fixed name clash.
krauss
parents: 19770
diff changeset
   171
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   172
        val subgoal = nth (prems_of thm) (i - 1)   (* FIXME SUBGOAL tactical *)
19922
984ae977f7aa Fixed name clash.
krauss
parents: 19770
diff changeset
   173
984ae977f7aa Fixed name clash.
krauss
parents: 19770
diff changeset
   174
        val ([P, x], subgf) = dest_all_all subgoal
984ae977f7aa Fixed name clash.
krauss
parents: 19770
diff changeset
   175
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   176
        val assums = Logic.strip_imp_prems subgf
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   177
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   178
        fun pat_of assum =
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   179
            let
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   180
                val (qs, imp) = dest_all_all assum
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   181
            in
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   182
                case Logic.dest_implies imp of
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   183
                    (_ $ (_ $ _ $ pat), _) => (qs, pat)
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   184
                  | _ => raise COMPLETENESS
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   185
            end
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   186
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   187
        val (qss, pats) = split_list (map pat_of assums)
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   188
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   189
        val complete_thm = prove_completeness thy x P qss pats
19922
984ae977f7aa Fixed name clash.
krauss
parents: 19770
diff changeset
   190
                                              |> forall_intr (cterm_of thy x)
984ae977f7aa Fixed name clash.
krauss
parents: 19770
diff changeset
   191
                                              |> forall_intr (cterm_of thy P)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   192
    in
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   193
        Seq.single (Drule.compose_single(complete_thm, i, thm))
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   194
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   195
    handle COMPLETENESS => Seq.empty
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   196
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   197
21588
cd0dc678a205 simplified method setup;
wenzelm
parents: 21319
diff changeset
   198
val pat_completeness = Method.SIMPLE_METHOD' pat_complete_tac
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   199
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   200
val by_pat_completeness_simp =
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   201
    Proof.global_terminal_proof
23351
3702086a15a3 Method.Basic: include position;
wenzelm
parents: 23203
diff changeset
   202
      (Method.Basic (K 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
   203
       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
   204
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22498
diff changeset
   205
val termination_by_lexicographic_order =
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22498
diff changeset
   206
    FundefPackage.setup_termination_proof NONE
23351
3702086a15a3 Method.Basic: include position;
wenzelm
parents: 23203
diff changeset
   207
    #> Proof.global_terminal_proof
3702086a15a3 Method.Basic: include position;
wenzelm
parents: 23203
diff changeset
   208
      (Method.Basic (LexicographicOrder.lexicographic_order [], Position.none), NONE)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   209
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   210
fun mk_catchall fixes arities =
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   211
    let
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   212
      fun mk_eqn ((fname, fT), _) =
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   213
          let 
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   214
            val n = the (Symtab.lookup arities fname)
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   215
            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
   216
                                   |> 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
   217
                              
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   218
            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
   219
          in
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   220
            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
   221
                          Const ("HOL.undefined", rT))
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   222
              |> HOLogic.mk_Trueprop
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   223
              |> fold_rev mk_forall qs
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   224
          end
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   225
    in
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   226
      map mk_eqn fixes
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   227
    end
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   228
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   229
fun add_catchall fixes spec =
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   230
    let 
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   231
      val catchalls = mk_catchall fixes (mk_arities (map split_def (map snd spec)))
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   232
    in
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   233
      spec @ map (pair true) catchalls
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   234
    end
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   235
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   236
fun sequential_preproc (config as FundefConfig {sequential, ...}) flags ctxt fixes spec =
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   237
    let
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   238
      val enabled = sequential orelse exists I flags
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   239
    in 
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   240
      if enabled then
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   241
        let
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   242
          val flags' = if sequential then map (K true) flags else flags
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   243
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   244
          val (nas, eqss) = split_list spec
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   245
                            
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   246
          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
   247
                    
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   248
          val spliteqs = eqs
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   249
                           |> tap (check_defs ctxt fixes) (* Standard checks *)
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   250
                           |> tap (map (check_pats ctxt))    (* More checks for sequential mode *)
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   251
                           |> curry op ~~ flags'
23819
2040846d1bbe some interface cleanup
krauss
parents: 23351
diff changeset
   252
                           |> add_catchall fixes   (* Completion *)
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   253
                           |> FundefSplit.split_some_equations ctxt
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   254
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   255
          fun restore_spec thms =
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   256
              nas ~~ Library.take (length nas, Library.unflat spliteqs thms)
23819
2040846d1bbe some interface cleanup
krauss
parents: 23351
diff changeset
   257
              
2040846d1bbe some interface cleanup
krauss
parents: 23351
diff changeset
   258
          val spliteqs' = flat (Library.take (length nas, spliteqs))
2040846d1bbe some interface cleanup
krauss
parents: 23351
diff changeset
   259
          val fnames = map (fst o fst) fixes
2040846d1bbe some interface cleanup
krauss
parents: 23351
diff changeset
   260
          val indices = map (fn eq => find_index (curry op = (fname_of eq)) fnames) spliteqs'
2040846d1bbe some interface cleanup
krauss
parents: 23351
diff changeset
   261
2040846d1bbe some interface cleanup
krauss
parents: 23351
diff changeset
   262
          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
   263
                                       |> map (map snd)
2040846d1bbe some interface cleanup
krauss
parents: 23351
diff changeset
   264
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   265
        in
23819
2040846d1bbe some interface cleanup
krauss
parents: 23351
diff changeset
   266
          (flat spliteqs, restore_spec, sort)
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   267
        end
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   268
      else
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   269
        FundefCommon.empty_preproc check_defs config flags ctxt fixes spec
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   270
    end
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   271
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   272
val setup =
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   273
    Method.add_methods [("pat_completeness", Method.no_args pat_completeness, 
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   274
                         "Completeness prover for datatype patterns")]
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   275
    #> 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
   276
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   277
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   278
val fun_config = FundefConfig { sequential=true, default="%x. arbitrary", 
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   279
                                target=NONE, domintros=false, tailrec=false }
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   280
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   281
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   282
local structure P = OuterParse and K = OuterKeyword in
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   283
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   284
fun fun_cmd config fixes statements flags lthy =
21211
5370cfbf3070 Preparations for making "lexicographic_order" part of "fun"
krauss
parents: 21051
diff changeset
   285
    lthy
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   286
      |> FundefPackage.add_fundef fixes statements config flags
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22498
diff changeset
   287
      |> by_pat_completeness_simp
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22498
diff changeset
   288
      |> termination_by_lexicographic_order
21211
5370cfbf3070 Preparations for making "lexicographic_order" part of "fun"
krauss
parents: 21051
diff changeset
   289
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   290
val funP =
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   291
  OuterSyntax.command "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
   292
  (fundef_parser fun_config
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   293
     >> (fn ((config, fixes), (flags, statements)) =>
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   294
            (Toplevel.local_theory (target_of config) (fun_cmd config fixes statements flags))));
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   295
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   296
val _ = OuterSyntax.add_parsers [funP];
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 19922
diff changeset
   297
end
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   298
20875
95d24e8d117e TheoryTarget.init;
wenzelm
parents: 20654
diff changeset
   299
end