src/HOL/thy_data.ML
author nipkow
Thu, 22 May 1997 18:29:17 +0200
changeset 3307 a106a557d704
parent 3282 c31e6239d4c9
child 3511 da4dd8b7ced4
permissions -rw-r--r--
exhaust_tac can now deal with whole terms rather than just variables.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
     1
(*  Title:      HOL/thy_data.ML
1455
52a0271621f3 changed the way simpsets and information about datatypes are stored
clasohm
parents:
diff changeset
     2
    ID:         $Id$
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
     3
    Author:     Carsten Clasohm
1455
52a0271621f3 changed the way simpsets and information about datatypes are stored
clasohm
parents:
diff changeset
     4
    Copyright   1995 TU Muenchen
52a0271621f3 changed the way simpsets and information about datatypes are stored
clasohm
parents:
diff changeset
     5
52a0271621f3 changed the way simpsets and information about datatypes are stored
clasohm
parents:
diff changeset
     6
Definitions that have to be reread after init_thy_reader has been invoked
52a0271621f3 changed the way simpsets and information about datatypes are stored
clasohm
parents:
diff changeset
     7
*)
52a0271621f3 changed the way simpsets and information about datatypes are stored
clasohm
parents:
diff changeset
     8
52a0271621f3 changed the way simpsets and information about datatypes are stored
clasohm
parents:
diff changeset
     9
fun simpset_of tname =
1657
a7a6c3fb3cdd changed first parameter of add_thydata and get_thydata
clasohm
parents: 1465
diff changeset
    10
  case get_thydata tname "simpset" of
1455
52a0271621f3 changed the way simpsets and information about datatypes are stored
clasohm
parents:
diff changeset
    11
      None => empty_ss
52a0271621f3 changed the way simpsets and information about datatypes are stored
clasohm
parents:
diff changeset
    12
    | Some (SS_DATA ss) => ss;
52a0271621f3 changed the way simpsets and information about datatypes are stored
clasohm
parents:
diff changeset
    13
1726
cbea219f5e5a Added function claset_of.
berghofe
parents: 1668
diff changeset
    14
fun claset_of tname =
cbea219f5e5a Added function claset_of.
berghofe
parents: 1668
diff changeset
    15
  case get_thydata tname "claset" of
cbea219f5e5a Added function claset_of.
berghofe
parents: 1668
diff changeset
    16
      None => empty_cs
cbea219f5e5a Added function claset_of.
berghofe
parents: 1668
diff changeset
    17
    | Some (CS_DATA cs) => cs;
cbea219f5e5a Added function claset_of.
berghofe
parents: 1668
diff changeset
    18
1668
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    19
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    20
(** Information about datatypes **)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    21
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    22
(* Retrieve information for a specific datatype *)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    23
fun datatype_info thy tname =
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    24
  case get_thydata (thyname_of_sign (sign_of thy)) "datatypes" of
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    25
      None => None
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    26
    | Some (DT_DATA ds) => assoc (ds, tname);
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    27
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    28
fun match_info thy tname =
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    29
  case datatype_info thy tname of
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    30
      Some {case_const, constructors, ...} =>
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    31
        {case_const=case_const, constructors=constructors}
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    32
    | None => error ("No datatype \"" ^ tname ^ "\" defined in this theory.");
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    33
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    34
fun induct_info thy tname =
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    35
  case datatype_info thy tname of
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    36
      Some {constructors, nchotomy, ...} =>
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    37
        {constructors=constructors, nchotomy=nchotomy}
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    38
    | None => error ("No datatype \"" ^ tname ^ "\" defined in this theory.");
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    39
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    40
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    41
(* Retrieve information for all datatypes defined in a theory and its
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    42
   ancestors *)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    43
fun extract_info thy =
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    44
  let val (congs, rewrites) =
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    45
        case get_thydata (thyname_of_sign (sign_of thy)) "datatypes" of
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    46
            None => ([], [])
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    47
          | Some (DT_DATA ds) =>
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    48
              let val info = map snd ds
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    49
              in (map #case_cong info, flat (map #case_rewrites info)) end;
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1657
diff changeset
    50
  in {case_congs = congs, case_rewrites = rewrites} end;
2562
d571d6660240 Moved qed_spec_mp, etc., from HOL.ML to thy_data.ML so that they work
paulson
parents: 1726
diff changeset
    51
3282
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    52
local
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    53
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    54
fun find_tname var Bi =
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    55
  let val frees = map dest_Free (term_frees Bi)
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    56
      val params = Logic.strip_params Bi;
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    57
  in case assoc (frees@params, var) of
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    58
       None => error("No such variable in subgoal: " ^ quote var)
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    59
     | Some(Type(tn,_)) => tn
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    60
     | _ => error("Cannot induct on type of " ^ quote var)
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    61
  end;
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    62
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    63
fun get_dt_info sign tn =
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    64
      case get_thydata (thyname_of_sign sign) "datatypes" of
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    65
        None => error ("No such datatype: " ^ quote tn)
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    66
      | Some (DT_DATA ds) =>
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    67
          case assoc (ds, tn) of
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    68
            Some info => info
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    69
          | None => error ("Not a datatype: " ^ quote tn)
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    70
3307
a106a557d704 exhaust_tac can now deal with whole terms rather than just variables.
nipkow
parents: 3282
diff changeset
    71
fun infer_tname state sign i aterm =
a106a557d704 exhaust_tac can now deal with whole terms rather than just variables.
nipkow
parents: 3282
diff changeset
    72
let val (_, _, Bi, _) = dest_state (state, i)
a106a557d704 exhaust_tac can now deal with whole terms rather than just variables.
nipkow
parents: 3282
diff changeset
    73
    val params = Logic.strip_params Bi	        (*params of subgoal i*)
a106a557d704 exhaust_tac can now deal with whole terms rather than just variables.
nipkow
parents: 3282
diff changeset
    74
    val params = rev(rename_wrt_term Bi params) (*as they are printed*)
a106a557d704 exhaust_tac can now deal with whole terms rather than just variables.
nipkow
parents: 3282
diff changeset
    75
    val (types,sorts) = types_sorts state
a106a557d704 exhaust_tac can now deal with whole terms rather than just variables.
nipkow
parents: 3282
diff changeset
    76
    fun types'(a,~1) = (case assoc(params,a) of None => types(a,~1) | sm => sm)
a106a557d704 exhaust_tac can now deal with whole terms rather than just variables.
nipkow
parents: 3282
diff changeset
    77
      | types'(ixn) = types ixn;
a106a557d704 exhaust_tac can now deal with whole terms rather than just variables.
nipkow
parents: 3282
diff changeset
    78
    val (ct,_) = read_def_cterm (sign,types',sorts) [] false
a106a557d704 exhaust_tac can now deal with whole terms rather than just variables.
nipkow
parents: 3282
diff changeset
    79
                                (aterm,TVar(("",0),[]));
a106a557d704 exhaust_tac can now deal with whole terms rather than just variables.
nipkow
parents: 3282
diff changeset
    80
in case #T(rep_cterm ct) of
a106a557d704 exhaust_tac can now deal with whole terms rather than just variables.
nipkow
parents: 3282
diff changeset
    81
     Type(tn,_) => tn
a106a557d704 exhaust_tac can now deal with whole terms rather than just variables.
nipkow
parents: 3282
diff changeset
    82
   | _ => error("Cannot induct on type of " ^ quote aterm)
a106a557d704 exhaust_tac can now deal with whole terms rather than just variables.
nipkow
parents: 3282
diff changeset
    83
end;
a106a557d704 exhaust_tac can now deal with whole terms rather than just variables.
nipkow
parents: 3282
diff changeset
    84
3282
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    85
in
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    86
3040
7d48671753da Introduced a generic "induct_tac" which picks up the right induction scheme
nipkow
parents: 2562
diff changeset
    87
(* generic induction tactic for datatypes *)
7d48671753da Introduced a generic "induct_tac" which picks up the right induction scheme
nipkow
parents: 2562
diff changeset
    88
fun induct_tac var i =
3282
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    89
  let fun induct state =
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    90
        let val (_, _, Bi, _) = dest_state (state, i)
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    91
            val sign = #sign(rep_thm state)
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    92
            val tn = find_tname var Bi
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    93
            val ind_tac = #induct_tac(get_dt_info sign tn)
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    94
        in ind_tac var i end
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    95
  in STATE induct end;
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    96
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    97
(* generic exhaustion tactic for datatypes *)
3307
a106a557d704 exhaust_tac can now deal with whole terms rather than just variables.
nipkow
parents: 3282
diff changeset
    98
fun exhaust_tac aterm i =
3282
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
    99
  let fun exhaust state =
3307
a106a557d704 exhaust_tac can now deal with whole terms rather than just variables.
nipkow
parents: 3282
diff changeset
   100
        let val sign = #sign(rep_thm state)
a106a557d704 exhaust_tac can now deal with whole terms rather than just variables.
nipkow
parents: 3282
diff changeset
   101
            val tn = infer_tname state sign i aterm
3282
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
   102
            val exh_tac = #exhaust_tac(get_dt_info sign tn)
3307
a106a557d704 exhaust_tac can now deal with whole terms rather than just variables.
nipkow
parents: 3282
diff changeset
   103
        in exh_tac aterm i end
3282
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
   104
  in STATE exhaust end;
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
   105
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
   106
end;
2562
d571d6660240 Moved qed_spec_mp, etc., from HOL.ML to thy_data.ML so that they work
paulson
parents: 1726
diff changeset
   107
d571d6660240 Moved qed_spec_mp, etc., from HOL.ML to thy_data.ML so that they work
paulson
parents: 1726
diff changeset
   108
(*Must be redefined in order to refer to the new instance of bind_thm
d571d6660240 Moved qed_spec_mp, etc., from HOL.ML to thy_data.ML so that they work
paulson
parents: 1726
diff changeset
   109
  created by init_thy_reader.*)
d571d6660240 Moved qed_spec_mp, etc., from HOL.ML to thy_data.ML so that they work
paulson
parents: 1726
diff changeset
   110
d571d6660240 Moved qed_spec_mp, etc., from HOL.ML to thy_data.ML so that they work
paulson
parents: 1726
diff changeset
   111
fun qed_spec_mp name =
d571d6660240 Moved qed_spec_mp, etc., from HOL.ML to thy_data.ML so that they work
paulson
parents: 1726
diff changeset
   112
  let val thm = normalize_thm [RSspec,RSmp] (result())
d571d6660240 Moved qed_spec_mp, etc., from HOL.ML to thy_data.ML so that they work
paulson
parents: 1726
diff changeset
   113
  in bind_thm(name, thm) end;
d571d6660240 Moved qed_spec_mp, etc., from HOL.ML to thy_data.ML so that they work
paulson
parents: 1726
diff changeset
   114
d571d6660240 Moved qed_spec_mp, etc., from HOL.ML to thy_data.ML so that they work
paulson
parents: 1726
diff changeset
   115
fun qed_goal_spec_mp name thy s p = 
d571d6660240 Moved qed_spec_mp, etc., from HOL.ML to thy_data.ML so that they work
paulson
parents: 1726
diff changeset
   116
	bind_thm (name, normalize_thm [RSspec,RSmp] (prove_goal thy s p));
d571d6660240 Moved qed_spec_mp, etc., from HOL.ML to thy_data.ML so that they work
paulson
parents: 1726
diff changeset
   117
d571d6660240 Moved qed_spec_mp, etc., from HOL.ML to thy_data.ML so that they work
paulson
parents: 1726
diff changeset
   118
fun qed_goalw_spec_mp name thy defs s p = 
d571d6660240 Moved qed_spec_mp, etc., from HOL.ML to thy_data.ML so that they work
paulson
parents: 1726
diff changeset
   119
	bind_thm (name, normalize_thm [RSspec,RSmp] (prove_goalw thy defs s p));