src/HOL/Tools/typedef_package.ML
author wenzelm
Tue, 19 Dec 2000 13:06:49 +0100
changeset 10697 ec197510165a
parent 10675 0b40c19f09f3
child 11426 f280d4b29a2c
permissions -rw-r--r--
improved errors;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/Tools/typedef_package.ML
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
9230
wenzelm
parents: 8428
diff changeset
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
     5
6357
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
     6
Gordon/HOL-style type definitions.
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
     7
*)
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
     8
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
     9
signature TYPEDEF_PACKAGE =
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    10
sig
5697
e816c4f1a396 quiet_mode, message;
wenzelm
parents: 5180
diff changeset
    11
  val quiet_mode: bool ref
5104
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    12
  val add_typedecls: (bstring * string list * mixfix) list -> theory -> theory
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    13
  val add_typedef: string -> bstring * string list * mixfix ->
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    14
    string -> string list -> thm list -> tactic option -> theory -> theory
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    15
  val add_typedef_i: string -> bstring * string list * mixfix ->
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    16
    term -> string list -> thm list -> tactic option -> theory -> theory
5180
d82a70766af0 Added new function add_typedef_i_no_def which doesn't add
berghofe
parents: 5104
diff changeset
    17
  val add_typedef_i_no_def: string -> bstring * string list * mixfix ->
d82a70766af0 Added new function add_typedef_i_no_def which doesn't add
berghofe
parents: 5104
diff changeset
    18
    term -> string list -> thm list -> tactic option -> theory -> theory
6729
b6e167580a32 formal comments (still dummy);
wenzelm
parents: 6723
diff changeset
    19
  val typedef_proof: (string * (bstring * string list * mixfix) * string) * Comment.text
6690
acbcf8085166 typedef_proof: pass interactive flag;
wenzelm
parents: 6383
diff changeset
    20
    -> bool -> theory -> ProofHistory.T
6729
b6e167580a32 formal comments (still dummy);
wenzelm
parents: 6723
diff changeset
    21
  val typedef_proof_i: (string * (bstring * string list * mixfix) * term) * Comment.text
6690
acbcf8085166 typedef_proof: pass interactive flag;
wenzelm
parents: 6383
diff changeset
    22
    -> bool -> theory -> ProofHistory.T
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    23
end;
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    24
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    25
structure TypedefPackage: TYPEDEF_PACKAGE =
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    26
struct
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    27
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    28
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    29
(** theory context references **)
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    30
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    31
val type_definitionN = "subset.type_definition";
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    32
val type_definition_def = thm "type_definition_def";
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    33
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    34
val Rep = thm "Rep";
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    35
val Rep_inverse = thm "Rep_inverse";
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    36
val Abs_inverse = thm "Abs_inverse";
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    37
val Rep_inject = thm "Rep_inject";
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    38
val Abs_inject = thm "Abs_inject";
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    39
val Rep_cases = thm "Rep_cases";
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    40
val Abs_cases = thm "Abs_cases";
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    41
val Rep_induct = thm "Rep_induct";
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    42
val Abs_induct = thm "Abs_induct";
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    43
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    44
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    45
5104
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    46
(** type declarations **)
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    47
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    48
fun add_typedecls decls thy =
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    49
  let
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    50
    val full = Sign.full_name (Theory.sign_of thy);
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    51
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    52
    fun arity_of (raw_name, args, mx) =
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    53
      (full (Syntax.type_name raw_name mx), replicate (length args) HOLogic.termS, HOLogic.termS);
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    54
  in
8141
d6d896e81cd7 fallback on PureThy version;
wenzelm
parents: 8100
diff changeset
    55
    if can (Theory.assert_super HOL.thy) thy then
d6d896e81cd7 fallback on PureThy version;
wenzelm
parents: 8100
diff changeset
    56
      thy
d6d896e81cd7 fallback on PureThy version;
wenzelm
parents: 8100
diff changeset
    57
      |> PureThy.add_typedecls decls
d6d896e81cd7 fallback on PureThy version;
wenzelm
parents: 8100
diff changeset
    58
      |> Theory.add_arities_i (map arity_of decls)
d6d896e81cd7 fallback on PureThy version;
wenzelm
parents: 8100
diff changeset
    59
    else thy |> PureThy.add_typedecls decls
5104
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    60
  end;
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    61
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    62
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    63
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    64
(** type definitions **)
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    65
5697
e816c4f1a396 quiet_mode, message;
wenzelm
parents: 5180
diff changeset
    66
(* messages *)
e816c4f1a396 quiet_mode, message;
wenzelm
parents: 5180
diff changeset
    67
e816c4f1a396 quiet_mode, message;
wenzelm
parents: 5180
diff changeset
    68
val quiet_mode = ref false;
e816c4f1a396 quiet_mode, message;
wenzelm
parents: 5180
diff changeset
    69
fun message s = if ! quiet_mode then () else writeln s;
e816c4f1a396 quiet_mode, message;
wenzelm
parents: 5180
diff changeset
    70
e816c4f1a396 quiet_mode, message;
wenzelm
parents: 5180
diff changeset
    71
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    72
(* non-emptiness of set *)              (*exception ERROR*)
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    73
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    74
fun check_nonempty cset thm =
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    75
  let
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    76
    val {t, sign, maxidx, ...} = Thm.rep_cterm cset;
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    77
    val {prop, ...} = Thm.rep_thm (Thm.transfer_sg sign (Drule.standard thm));
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    78
    val matches = Pattern.matches (Sign.tsig_of sign);
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    79
  in
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    80
    (case try (HOLogic.dest_mem o HOLogic.dest_Trueprop) prop of
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    81
      None => raise ERROR
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    82
    | Some (_, A) => if matches (Logic.incr_indexes ([], maxidx) A, t) then () else raise ERROR)
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    83
  end handle ERROR => error ("Bad non-emptiness theorem " ^ Display.string_of_thm thm ^
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    84
    "\nfor set " ^ quote (Display.string_of_cterm cset));
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    85
7481
d44c77be268c goal_nonempty: Ex goal for new-style version;
wenzelm
parents: 7152
diff changeset
    86
fun goal_nonempty ex cset =
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    87
  let
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    88
    val {T = setT, t = A, maxidx, sign} = Thm.rep_cterm cset;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    89
    val T = HOLogic.dest_setT setT;
7481
d44c77be268c goal_nonempty: Ex goal for new-style version;
wenzelm
parents: 7152
diff changeset
    90
    val tm =
d44c77be268c goal_nonempty: Ex goal for new-style version;
wenzelm
parents: 7152
diff changeset
    91
      if ex then HOLogic.mk_exists ("x", T, HOLogic.mk_mem (Free ("x", T), A))
d44c77be268c goal_nonempty: Ex goal for new-style version;
wenzelm
parents: 7152
diff changeset
    92
      else HOLogic.mk_mem (Var (("x", maxidx + 1), T), A);   (*old-style version*)
d44c77be268c goal_nonempty: Ex goal for new-style version;
wenzelm
parents: 7152
diff changeset
    93
  in Thm.cterm_of sign (HOLogic.mk_Trueprop tm) end;
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    94
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    95
fun prove_nonempty thy cset (witn_names, witn_thms, witn_tac) =
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    96
  let
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    97
    val is_def = Logic.is_equals o #prop o Thm.rep_thm;
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    98
    val thms = PureThy.get_thmss thy witn_names @ witn_thms;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    99
    val tac =
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   100
      TRY (rewrite_goals_tac (filter is_def thms)) THEN
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   101
      TRY (REPEAT_FIRST (resolve_tac (filter_out is_def thms))) THEN
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   102
      if_none witn_tac (TRY (ALLGOALS (CLASET' blast_tac)));
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   103
  in
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   104
    message ("Proving non-emptiness of set " ^ quote (string_of_cterm cset) ^ " ...");
7481
d44c77be268c goal_nonempty: Ex goal for new-style version;
wenzelm
parents: 7152
diff changeset
   105
    prove_goalw_cterm [] (goal_nonempty false cset) (K [tac])
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   106
  end handle ERROR => error ("Failed to prove non-emptiness of " ^ quote (string_of_cterm cset));
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   107
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   108
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   109
(* prepare_typedef *)
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   110
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   111
fun read_term sg used s =
8100
6186ee807f2e replaced HOLogic.termTVar by HOLogic.termT;
wenzelm
parents: 7481
diff changeset
   112
  #1 (Thm.read_def_cterm (sg, K None, K None) used true (s, HOLogic.termT));
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   113
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   114
fun cert_term sg _ t = Thm.cterm_of sg t handle TERM (msg, _) => error msg;
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   115
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   116
fun err_in_typedef name =
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   117
  error ("The error(s) above occurred in typedef " ^ quote name);
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   118
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   119
fun prepare_typedef prep_term no_def name (t, vs, mx) raw_set thy =
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   120
  let
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   121
    val _ = Theory.requires thy "subset" "typedefs";
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   122
    val sign = Theory.sign_of thy;
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   123
    val full = Sign.full_name sign;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   124
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   125
    (*rhs*)
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   126
    val full_name = full name;
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   127
    val cset = prep_term sign vs raw_set;
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   128
    val {T = setT, t = set, ...} = Thm.rep_cterm cset;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   129
    val rhs_tfrees = term_tfrees set;
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   130
    val oldT = HOLogic.dest_setT setT handle TYPE _ =>
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   131
      error ("Not a set type: " ^ quote (Sign.string_of_typ sign setT));
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   132
    val cset_pat = Thm.cterm_of sign (Var ((name, 0), setT));
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   133
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   134
    (*lhs*)
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   135
    val lhs_tfrees = map (fn v => (v, if_none (assoc (rhs_tfrees, v)) HOLogic.termS)) vs;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   136
    val tname = Syntax.type_name t mx;
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   137
    val full_tname = full tname;
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   138
    val newT = Type (full_tname, map TFree lhs_tfrees);
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   139
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   140
    val Rep_name = "Rep_" ^ name;
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   141
    val Abs_name = "Abs_" ^ name;
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   142
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   143
    val setC = Const (full_name, setT);
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   144
    val RepC = Const (full Rep_name, newT --> oldT);
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   145
    val AbsC = Const (full Abs_name, oldT --> newT);
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   146
    val x_new = Free ("x", newT);
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   147
    val y_old = Free ("y", oldT);
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   148
10615
163b265d3d83 less rude treatment of "no_def";
wenzelm
parents: 10463
diff changeset
   149
    val set' = if no_def then set else setC;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   150
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   151
    val typedef_name = "type_definition_" ^ name;
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   152
    val typedefC =
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   153
      Const (type_definitionN, (newT --> oldT) --> (oldT --> newT) --> setT --> HOLogic.boolT);
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   154
    val typedef_prop = HOLogic.mk_Trueprop (typedefC $ RepC $ AbsC $ set');
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   155
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   156
    (*theory extender*)
10697
ec197510165a improved errors;
wenzelm
parents: 10675
diff changeset
   157
    fun do_typedef super_theory theory =
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   158
      theory
10697
ec197510165a improved errors;
wenzelm
parents: 10675
diff changeset
   159
      |> Theory.assert_super super_theory
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   160
      |> add_typedecls [(t, vs, mx)]
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   161
      |> Theory.add_consts_i
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   162
       ((if no_def then [] else [(name, setT, NoSyn)]) @
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   163
        [(Rep_name, newT --> oldT, NoSyn),
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   164
         (Abs_name, oldT --> newT, NoSyn)])
9315
f793f05024f6 adapted PureThy.add_defs_i;
wenzelm
parents: 9230
diff changeset
   165
      |> (if no_def then I else (#1 oo (PureThy.add_defs_i false o map Thm.no_attributes))
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   166
       [Logic.mk_defpair (setC, set)])
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   167
      |> PureThy.add_axioms_i [((typedef_name, typedef_prop), [])]
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   168
      |> (fn (theory', typedef_ax) =>
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   169
        let fun make th = standard (th OF typedef_ax) in
10675
0b40c19f09f3 'typedef': present result theorem "type_definition Rep Abs A";
wenzelm
parents: 10615
diff changeset
   170
          rpair (hd typedef_ax) (theory'
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   171
          |> (#1 oo PureThy.add_thms)
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   172
            ([((Rep_name, make Rep), []),
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   173
              ((Rep_name ^ "_inverse", make Rep_inverse), []),
10615
163b265d3d83 less rude treatment of "no_def";
wenzelm
parents: 10463
diff changeset
   174
              ((Abs_name ^ "_inverse", make Abs_inverse), []),
163b265d3d83 less rude treatment of "no_def";
wenzelm
parents: 10463
diff changeset
   175
              ((Rep_name ^ "_inject", make Rep_inject), []),
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   176
              ((Abs_name ^ "_inject", make Abs_inject), []),
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   177
              ((Rep_name ^ "_cases", make Rep_cases),
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   178
                [RuleCases.case_names [Rep_name], InductAttrib.cases_set_global full_name]),
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   179
              ((Abs_name ^ "_cases", make Abs_cases),
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   180
                [RuleCases.case_names [Abs_name], InductAttrib.cases_type_global full_tname]),
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   181
              ((Rep_name ^ "_induct", make Rep_induct),
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   182
                [RuleCases.case_names [Rep_name], InductAttrib.induct_set_global full_name]),
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   183
              ((Abs_name ^ "_induct", make Abs_induct),
10675
0b40c19f09f3 'typedef': present result theorem "type_definition Rep Abs A";
wenzelm
parents: 10615
diff changeset
   184
                [RuleCases.case_names [Abs_name], InductAttrib.induct_type_global full_tname])]))
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   185
        end)
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   186
      handle ERROR => err_in_typedef name;
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   187
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   188
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   189
    (* errors *)
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   190
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   191
    fun show_names pairs = commas_quote (map fst pairs);
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   192
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   193
    val illegal_vars =
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   194
      if null (term_vars set) andalso null (term_tvars set) then []
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   195
      else ["Illegal schematic variable(s) on rhs"];
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   196
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   197
    val dup_lhs_tfrees =
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   198
      (case duplicates lhs_tfrees of [] => []
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   199
      | dups => ["Duplicate type variables on lhs: " ^ show_names dups]);
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   200
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   201
    val extra_rhs_tfrees =
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   202
      (case gen_rems (op =) (rhs_tfrees, lhs_tfrees) of [] => []
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   203
      | extras => ["Extra type variables on rhs: " ^ show_names extras]);
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   204
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   205
    val illegal_frees =
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   206
      (case term_frees set of [] => []
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   207
      | xs => ["Illegal variables on rhs: " ^ show_names (map dest_Free xs)]);
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   208
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   209
    val errs = illegal_vars @ dup_lhs_tfrees @ extra_rhs_tfrees @ illegal_frees;
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   210
  in
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   211
    if null errs then () else error (cat_lines errs);
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   212
    (cset, cset_pat, do_typedef)
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   213
  end handle ERROR => err_in_typedef name;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   214
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   215
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   216
(* add_typedef interfaces *)
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   217
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   218
fun gen_add_typedef prep_term no_def name typ set names thms tac thy =
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   219
  let
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   220
    val (cset, _, do_typedef) = prepare_typedef prep_term no_def name typ set thy;
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   221
    val result = prove_nonempty thy cset (names, thms, tac);
10697
ec197510165a improved errors;
wenzelm
parents: 10675
diff changeset
   222
  in check_nonempty cset result; thy |> do_typedef thy |> #1 end;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   223
5180
d82a70766af0 Added new function add_typedef_i_no_def which doesn't add
berghofe
parents: 5104
diff changeset
   224
val add_typedef = gen_add_typedef read_term false;
d82a70766af0 Added new function add_typedef_i_no_def which doesn't add
berghofe
parents: 5104
diff changeset
   225
val add_typedef_i = gen_add_typedef cert_term false;
d82a70766af0 Added new function add_typedef_i_no_def which doesn't add
berghofe
parents: 5104
diff changeset
   226
val add_typedef_i_no_def = gen_add_typedef cert_term true;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   227
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   228
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   229
(* typedef_proof interface *)
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   230
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   231
fun typedef_attribute cset do_typedef (thy, thm) =
10675
0b40c19f09f3 'typedef': present result theorem "type_definition Rep Abs A";
wenzelm
parents: 10615
diff changeset
   232
  (check_nonempty cset (thm RS (some_eq_ex RS iffD2)); (thy |> do_typedef));
6357
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   233
6729
b6e167580a32 formal comments (still dummy);
wenzelm
parents: 6723
diff changeset
   234
fun gen_typedef_proof prep_term ((name, typ, set), comment) int thy =
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   235
  let
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   236
    val (cset, cset_pat, do_typedef) = prepare_typedef prep_term false name typ set thy;
7481
d44c77be268c goal_nonempty: Ex goal for new-style version;
wenzelm
parents: 7152
diff changeset
   237
    val goal = Thm.term_of (goal_nonempty true cset);
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   238
    val goal_pat = Thm.term_of (goal_nonempty true cset_pat);
10697
ec197510165a improved errors;
wenzelm
parents: 10675
diff changeset
   239
    val test_thy = Theory.copy thy;
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   240
  in
10697
ec197510165a improved errors;
wenzelm
parents: 10675
diff changeset
   241
    test_thy |> do_typedef test_thy;  (*preview errors!*)
ec197510165a improved errors;
wenzelm
parents: 10675
diff changeset
   242
    thy |> IsarThy.theorem_i ((("", [typedef_attribute cset (do_typedef thy)]),
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   243
      (goal, ([goal_pat], []))), comment) int
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   244
  end;
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   245
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   246
val typedef_proof = gen_typedef_proof read_term;
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   247
val typedef_proof_i = gen_typedef_proof cert_term;
6357
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   248
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   249
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   250
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   251
(** outer syntax **)
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   252
6723
f342449d73ca outer syntax keyword classification;
wenzelm
parents: 6690
diff changeset
   253
local structure P = OuterParse and K = OuterSyntax.Keyword in
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   254
6357
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   255
val typedeclP =
6723
f342449d73ca outer syntax keyword classification;
wenzelm
parents: 6690
diff changeset
   256
  OuterSyntax.command "typedecl" "HOL type declaration" K.thy_decl
7152
44d46a112127 tuned outer syntax;
wenzelm
parents: 6935
diff changeset
   257
    (P.type_args -- P.name -- P.opt_infix --| P.marg_comment >> (fn ((vs, t), mx) =>
6357
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   258
      Toplevel.theory (add_typedecls [(t, vs, mx)])));
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   259
6723
f342449d73ca outer syntax keyword classification;
wenzelm
parents: 6690
diff changeset
   260
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   261
val typedef_proof_decl =
6723
f342449d73ca outer syntax keyword classification;
wenzelm
parents: 6690
diff changeset
   262
  Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") --
6729
b6e167580a32 formal comments (still dummy);
wenzelm
parents: 6723
diff changeset
   263
    (P.type_args -- P.name) -- P.opt_infix -- (P.$$$ "=" |-- P.term) -- P.marg_comment;
6357
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   264
6729
b6e167580a32 formal comments (still dummy);
wenzelm
parents: 6723
diff changeset
   265
fun mk_typedef_proof ((((opt_name, (vs, t)), mx), A), comment) =
b6e167580a32 formal comments (still dummy);
wenzelm
parents: 6723
diff changeset
   266
  typedef_proof ((if_none opt_name (Syntax.type_name t mx), (t, vs, mx), A), comment);
6357
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   267
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   268
val typedefP =
6723
f342449d73ca outer syntax keyword classification;
wenzelm
parents: 6690
diff changeset
   269
  OuterSyntax.command "typedef" "HOL type definition (requires non-emptiness proof)" K.thy_goal
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   270
    (typedef_proof_decl >> (Toplevel.print oo (Toplevel.theory_to_proof o mk_typedef_proof)));
6357
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   271
6723
f342449d73ca outer syntax keyword classification;
wenzelm
parents: 6690
diff changeset
   272
6357
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   273
val _ = OuterSyntax.add_parsers [typedeclP, typedefP];
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   274
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   275
end;
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   276
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   277
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   278
end;