src/HOL/Tools/Function/pat_completeness.ML
author haftmann
Thu, 08 Jul 2010 16:19:24 +0200
changeset 37744 3daaf23b9ab4
parent 36945 9bec62c10714
child 42361 23f352990944
permissions -rw-r--r--
tuned titles
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37744
3daaf23b9ab4 tuned titles
haftmann
parents: 36945
diff changeset
     1
(*  Title:      HOL/Tools/Function/pat_completeness.ML
33083
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
     2
    Author:     Alexander Krauss, TU Muenchen
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
     3
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
     4
Method "pat_completeness" to prove completeness of datatype patterns.
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
     5
*)
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
     6
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
     7
signature PAT_COMPLETENESS =
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
     8
sig
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
     9
    val pat_completeness_tac: Proof.context -> int -> tactic
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    10
    val pat_completeness: Proof.context -> Proof.method
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    11
    val prove_completeness : theory -> term list -> term -> term list list ->
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    12
      term list list -> thm
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    13
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    14
    val setup : theory -> theory
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    15
end
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    16
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    17
structure Pat_Completeness : PAT_COMPLETENESS =
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    18
struct
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    19
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents: 33083
diff changeset
    20
open Function_Lib
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents: 33083
diff changeset
    21
open Function_Common
33083
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    22
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    23
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    24
fun mk_argvar i T = Free ("_av" ^ (string_of_int i), T)
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    25
fun mk_patvar i T = Free ("_pv" ^ (string_of_int i), T)
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    26
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 33099
diff changeset
    27
fun inst_free var inst = Thm.forall_elim inst o Thm.forall_intr var
33083
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    28
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    29
fun inst_case_thm thy x P thm =
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    30
  let val [Pv, xv] = Term.add_vars (prop_of thm) []
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    31
  in
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    32
    thm |> cterm_instantiate (map (pairself (cterm_of thy))
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    33
      [(Var xv, x), (Var Pv, P)])
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    34
  end
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    35
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    36
fun invent_vars constr i =
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    37
  let
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    38
    val Ts = binder_types (fastype_of constr)
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    39
    val j = i + length Ts
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    40
    val is = i upto (j - 1)
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    41
    val avs = map2 mk_argvar is Ts
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    42
    val pvs = map2 mk_patvar is Ts
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    43
 in
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    44
   (avs, pvs, j)
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    45
 end
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    46
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    47
fun filter_pats thy cons pvars [] = []
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    48
  | filter_pats thy cons pvars (([], thm) :: pts) = raise Match
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    49
  | filter_pats thy cons pvars (((pat as Free _) :: pats, thm) :: pts) =
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    50
    let val inst = list_comb (cons, pvars)
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    51
    in (inst :: pats, inst_free (cterm_of thy pat) (cterm_of thy inst) thm)
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    52
       :: (filter_pats thy cons pvars pts)
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    53
    end
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    54
  | filter_pats thy cons pvars ((pat :: pats, thm) :: pts) =
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    55
    if fst (strip_comb pat) = cons
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    56
    then (pat :: pats, thm) :: (filter_pats thy cons pvars pts)
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    57
    else filter_pats thy cons pvars pts
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    58
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    59
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    60
fun inst_constrs_of thy (T as Type (name, _)) =
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    61
  map (fn (Cn,CT) =>
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    62
          Envir.subst_term_types (Sign.typ_match thy (body_type CT, T) Vartab.empty) (Const (Cn, CT)))
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    63
      (the (Datatype.get_constrs thy name))
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    64
  | inst_constrs_of thy _ = raise Match
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    65
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    66
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    67
fun transform_pat thy avars c_assum ([] , thm) = raise Match
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    68
  | transform_pat thy avars c_assum (pat :: pats, thm) =
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    69
  let
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    70
    val (_, subps) = strip_comb pat
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    71
    val eqs = map (cterm_of thy o HOLogic.mk_Trueprop o HOLogic.mk_eq) (avars ~~ subps)
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 33099
diff changeset
    72
    val c_eq_pat = simplify (HOL_basic_ss addsimps (map Thm.assume eqs)) c_assum
33083
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    73
  in
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    74
    (subps @ pats,
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 33099
diff changeset
    75
     fold_rev Thm.implies_intr eqs (Thm.implies_elim thm c_eq_pat))
33083
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    76
  end
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    77
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    78
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    79
exception COMPLETENESS
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    80
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    81
fun constr_case thy P idx (v :: vs) pats cons =
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    82
  let
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    83
    val (avars, pvars, newidx) = invent_vars cons idx
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    84
    val c_hyp = cterm_of thy (HOLogic.mk_Trueprop (HOLogic.mk_eq (v, list_comb (cons, avars))))
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 33099
diff changeset
    85
    val c_assum = Thm.assume c_hyp
33083
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    86
    val newpats = map (transform_pat thy avars c_assum) (filter_pats thy cons pvars pats)
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    87
  in
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    88
    o_alg thy P newidx (avars @ vs) newpats
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 33099
diff changeset
    89
    |> Thm.implies_intr c_hyp
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 33099
diff changeset
    90
    |> fold_rev (Thm.forall_intr o cterm_of thy) avars
33083
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    91
  end
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    92
  | constr_case _ _ _ _ _ _ = raise Match
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    93
and o_alg thy P idx [] (([], Pthm) :: _)  = Pthm
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    94
  | o_alg thy P idx (v :: vs) [] = raise COMPLETENESS
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    95
  | o_alg thy P idx (v :: vs) pts =
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    96
  if forall (is_Free o hd o fst) pts (* Var case *)
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    97
  then o_alg thy P idx vs
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    98
         (map (fn (pv :: pats, thm) =>
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
    99
           (pats, refl RS (inst_free (cterm_of thy pv) (cterm_of thy v) thm))) pts)
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   100
  else (* Cons case *)
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   101
    let
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   102
      val T = fastype_of v
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   103
      val (tname, _) = dest_Type T
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   104
      val {exhaust=case_thm, ...} = Datatype.the_info thy tname
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   105
      val constrs = inst_constrs_of thy T
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   106
      val c_cases = map (constr_case thy P idx (v :: vs) pts) constrs
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   107
    in
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   108
      inst_case_thm thy v P case_thm
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   109
      |> fold (curry op COMP) c_cases
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   110
    end
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   111
  | o_alg _ _ _ _ _ = raise Match
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   112
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   113
fun prove_completeness thy xs P qss patss =
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   114
  let
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   115
    fun mk_assum qs pats =
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   116
      HOLogic.mk_Trueprop P
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   117
      |> fold_rev (curry Logic.mk_implies o HOLogic.mk_Trueprop o HOLogic.mk_eq) (xs ~~ pats)
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   118
      |> fold_rev Logic.all qs
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   119
      |> cterm_of thy
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   120
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   121
    val hyps = map2 mk_assum qss patss
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 33099
diff changeset
   122
    fun inst_hyps hyp qs = fold (Thm.forall_elim o cterm_of thy) qs (Thm.assume hyp)
33083
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   123
    val assums = map2 inst_hyps hyps qss
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   124
    in
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   125
      o_alg thy P 2 xs (patss ~~ assums)
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 33099
diff changeset
   126
      |> fold_rev Thm.implies_intr hyps
33083
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   127
    end
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   128
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   129
fun pat_completeness_tac ctxt = SUBGOAL (fn (subgoal, i) =>
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   130
  let
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   131
    val thy = ProofContext.theory_of ctxt
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   132
    val (vs, subgf) = dest_all_all subgoal
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   133
    val (cases, _ $ thesis) = Logic.strip_horn subgf
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   134
      handle Bind => raise COMPLETENESS
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   135
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   136
    fun pat_of assum =
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   137
      let
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   138
        val (qs, imp) = dest_all_all assum
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   139
        val prems = Logic.strip_imp_prems imp
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   140
      in
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   141
        (qs, map (HOLogic.dest_eq o HOLogic.dest_Trueprop) prems)
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   142
      end
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   143
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   144
    val (qss, x_pats) = split_list (map pat_of cases)
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   145
    val xs = map fst (hd x_pats)
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   146
      handle Empty => raise COMPLETENESS
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   147
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   148
    val patss = map (map snd) x_pats
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   149
    val complete_thm = prove_completeness thy xs thesis qss patss
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 33099
diff changeset
   150
      |> fold_rev (Thm.forall_intr o cterm_of thy) vs
33083
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   151
    in
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   152
      PRIMITIVE (fn st => Drule.compose_single(complete_thm, i, st))
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   153
  end
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   154
  handle COMPLETENESS => no_tac)
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   155
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   156
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   157
val pat_completeness = SIMPLE_METHOD' o pat_completeness_tac
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   158
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   159
val setup =
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   160
  Method.setup @{binding pat_completeness} (Scan.succeed pat_completeness)
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   161
    "Completeness prover for datatype patterns"
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   162
1fad3160d873 pat_completeness gets its own file
krauss
parents:
diff changeset
   163
end