src/HOL/Tools/typedef_package.ML
author wenzelm
Thu, 10 Jan 2002 01:11:43 +0100
changeset 12694 9950c1ce9d24
parent 12624 9ed65232429c
child 12876 a70df1e5bf10
permissions -rw-r--r--
simplified IsarThy.theorem_i;
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
11822
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
    13
  val add_typedef_x: string -> bstring * string list * mixfix ->
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    14
    string -> string list -> thm list -> tactic option -> theory -> theory
11827
16ef206e6648 sane internal interface for add_typedef(_i);
wenzelm
parents: 11822
diff changeset
    15
  val add_typedef: bool -> string option -> bstring * string list * mixfix ->
11822
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
    16
    string -> (bstring * bstring) option -> tactic -> theory -> theory *
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
    17
    {type_definition: thm, set_def: thm option, Rep: thm, Rep_inverse: thm,
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
    18
      Abs_inverse: thm, Rep_inject: thm, Abs_inject: thm, Rep_cases: thm, Abs_cases: thm,
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
    19
      Rep_induct: thm, Abs_induct: thm}
11827
16ef206e6648 sane internal interface for add_typedef(_i);
wenzelm
parents: 11822
diff changeset
    20
  val add_typedef_i: bool -> string option -> bstring * string list * mixfix ->
11822
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
    21
    term -> (bstring * bstring) option -> tactic -> theory -> theory *
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
    22
    {type_definition: thm, set_def: thm option, Rep: thm, Rep_inverse: thm,
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
    23
      Abs_inverse: thm, Rep_inject: thm, Abs_inject: thm, Rep_cases: thm, Abs_cases: thm,
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
    24
      Rep_induct: thm, Abs_induct: thm}
11744
3a4625eaead0 'morphisms' spec;
wenzelm
parents: 11740
diff changeset
    25
  val typedef_proof:
3a4625eaead0 'morphisms' spec;
wenzelm
parents: 11740
diff changeset
    26
    (string * (bstring * string list * mixfix) * string * (string * string) option) * Comment.text
6690
acbcf8085166 typedef_proof: pass interactive flag;
wenzelm
parents: 6383
diff changeset
    27
    -> bool -> theory -> ProofHistory.T
11744
3a4625eaead0 'morphisms' spec;
wenzelm
parents: 11740
diff changeset
    28
  val typedef_proof_i:
3a4625eaead0 'morphisms' spec;
wenzelm
parents: 11740
diff changeset
    29
    (string * (bstring * string list * mixfix) * term * (string * string) option) * Comment.text
6690
acbcf8085166 typedef_proof: pass interactive flag;
wenzelm
parents: 6383
diff changeset
    30
    -> bool -> theory -> ProofHistory.T
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    31
end;
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    32
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    33
structure TypedefPackage: TYPEDEF_PACKAGE =
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    34
struct
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    35
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    36
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    37
(** theory context references **)
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    38
11608
c760ea8154ee renamed theory "subset" to "Typedef";
wenzelm
parents: 11426
diff changeset
    39
val type_definitionN = "Typedef.type_definition";
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    40
val type_definition_def = thm "type_definition_def";
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    41
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    42
val Rep = thm "Rep";
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    43
val Rep_inverse = thm "Rep_inverse";
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    44
val Abs_inverse = thm "Abs_inverse";
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    45
val Rep_inject = thm "Rep_inject";
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    46
val Abs_inject = thm "Abs_inject";
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    47
val Rep_cases = thm "Rep_cases";
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    48
val Abs_cases = thm "Abs_cases";
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    49
val Rep_induct = thm "Rep_induct";
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    50
val Abs_induct = thm "Abs_induct";
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    51
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    52
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
    53
5104
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    54
(** type declarations **)
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    55
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    56
fun add_typedecls decls thy =
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    57
  let
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    58
    val full = Sign.full_name (Theory.sign_of thy);
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    59
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    60
    fun arity_of (raw_name, args, mx) =
12338
de0f4a63baa5 renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents: 12043
diff changeset
    61
      (full (Syntax.type_name raw_name mx), replicate (length args) HOLogic.typeS, HOLogic.typeS);
5104
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    62
  in
8141
d6d896e81cd7 fallback on PureThy version;
wenzelm
parents: 8100
diff changeset
    63
    if can (Theory.assert_super HOL.thy) thy then
d6d896e81cd7 fallback on PureThy version;
wenzelm
parents: 8100
diff changeset
    64
      thy
d6d896e81cd7 fallback on PureThy version;
wenzelm
parents: 8100
diff changeset
    65
      |> PureThy.add_typedecls decls
d6d896e81cd7 fallback on PureThy version;
wenzelm
parents: 8100
diff changeset
    66
      |> Theory.add_arities_i (map arity_of decls)
d6d896e81cd7 fallback on PureThy version;
wenzelm
parents: 8100
diff changeset
    67
    else thy |> PureThy.add_typedecls decls
5104
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    68
  end;
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    69
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    70
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    71
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    72
(** type definitions **)
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    73
5697
e816c4f1a396 quiet_mode, message;
wenzelm
parents: 5180
diff changeset
    74
(* messages *)
e816c4f1a396 quiet_mode, message;
wenzelm
parents: 5180
diff changeset
    75
e816c4f1a396 quiet_mode, message;
wenzelm
parents: 5180
diff changeset
    76
val quiet_mode = ref false;
e816c4f1a396 quiet_mode, message;
wenzelm
parents: 5180
diff changeset
    77
fun message s = if ! quiet_mode then () else writeln s;
e816c4f1a396 quiet_mode, message;
wenzelm
parents: 5180
diff changeset
    78
e816c4f1a396 quiet_mode, message;
wenzelm
parents: 5180
diff changeset
    79
11426
f280d4b29a2c abtract non-emptiness statements (no longer use Eps);
wenzelm
parents: 10697
diff changeset
    80
(* prove_nonempty -- tactical version *)        (*exception ERROR*)
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    81
11827
16ef206e6648 sane internal interface for add_typedef(_i);
wenzelm
parents: 11822
diff changeset
    82
fun prove_nonempty thy cset goal (witn1_tac, witn_names, witn_thms, witn2_tac) =
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    83
  let
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    84
    val is_def = Logic.is_equals o #prop o Thm.rep_thm;
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    85
    val thms = PureThy.get_thmss thy witn_names @ witn_thms;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    86
    val tac =
11827
16ef206e6648 sane internal interface for add_typedef(_i);
wenzelm
parents: 11822
diff changeset
    87
      witn1_tac THEN
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    88
      TRY (rewrite_goals_tac (filter is_def thms)) THEN
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    89
      TRY (REPEAT_FIRST (resolve_tac (filter_out is_def thms))) THEN
11827
16ef206e6648 sane internal interface for add_typedef(_i);
wenzelm
parents: 11822
diff changeset
    90
      if_none witn2_tac (TRY (ALLGOALS (CLASET' blast_tac)));
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    91
  in
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    92
    message ("Proving non-emptiness of set " ^ quote (string_of_cterm cset) ^ " ...");
11968
859a141085d0 use Tactic.prove;
wenzelm
parents: 11827
diff changeset
    93
    Tactic.prove (Theory.sign_of thy) [] [] goal (K tac)
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    94
  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
    95
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    96
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    97
(* prepare_typedef *)
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    98
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    99
fun read_term sg used s =
12338
de0f4a63baa5 renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents: 12043
diff changeset
   100
  #1 (Thm.read_def_cterm (sg, K None, K None) used true (s, HOLogic.typeT));
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   101
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   102
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
   103
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   104
fun err_in_typedef name =
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   105
  error ("The error(s) above occurred in typedef " ^ quote name);
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   106
11822
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   107
fun prepare_typedef prep_term def name (t, vs, mx) raw_set opt_morphs thy =
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   108
  let
11608
c760ea8154ee renamed theory "subset" to "Typedef";
wenzelm
parents: 11426
diff changeset
   109
    val _ = Theory.requires thy "Typedef" "typedefs";
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   110
    val sign = Theory.sign_of thy;
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   111
    val full = Sign.full_name sign;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   112
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   113
    (*rhs*)
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   114
    val full_name = full name;
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   115
    val cset = prep_term sign vs raw_set;
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   116
    val {T = setT, t = set, ...} = Thm.rep_cterm cset;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   117
    val rhs_tfrees = term_tfrees set;
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   118
    val oldT = HOLogic.dest_setT setT handle TYPE _ =>
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   119
      error ("Not a set type: " ^ quote (Sign.string_of_typ sign setT));
11426
f280d4b29a2c abtract non-emptiness statements (no longer use Eps);
wenzelm
parents: 10697
diff changeset
   120
    fun mk_nonempty A =
f280d4b29a2c abtract non-emptiness statements (no longer use Eps);
wenzelm
parents: 10697
diff changeset
   121
      HOLogic.mk_Trueprop (HOLogic.mk_exists ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), A)));
f280d4b29a2c abtract non-emptiness statements (no longer use Eps);
wenzelm
parents: 10697
diff changeset
   122
    val goal = mk_nonempty set;
11744
3a4625eaead0 'morphisms' spec;
wenzelm
parents: 11740
diff changeset
   123
    val vname = take_suffix Symbol.is_digit (Symbol.explode name)
3a4625eaead0 'morphisms' spec;
wenzelm
parents: 11740
diff changeset
   124
      |> apfst implode |> apsnd (#1 o Term.read_int);
3a4625eaead0 'morphisms' spec;
wenzelm
parents: 11740
diff changeset
   125
    val goal_pat = mk_nonempty (Var (vname, setT));
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   126
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   127
    (*lhs*)
12338
de0f4a63baa5 renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents: 12043
diff changeset
   128
    val lhs_tfrees = map (fn v => (v, if_none (assoc (rhs_tfrees, v)) HOLogic.typeS)) vs;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   129
    val tname = Syntax.type_name t mx;
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   130
    val full_tname = full tname;
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   131
    val newT = Type (full_tname, map TFree lhs_tfrees);
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   132
11744
3a4625eaead0 'morphisms' spec;
wenzelm
parents: 11740
diff changeset
   133
    val (Rep_name, Abs_name) = if_none opt_morphs ("Rep_" ^ name, "Abs_" ^ name);
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   134
    val setC = Const (full_name, setT);
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   135
    val RepC = Const (full Rep_name, newT --> oldT);
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   136
    val AbsC = Const (full Abs_name, oldT --> newT);
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   137
    val x_new = Free ("x", newT);
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   138
    val y_old = Free ("y", oldT);
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   139
11822
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   140
    val set' = if def then setC else set;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   141
10280
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   142
    val typedef_name = "type_definition_" ^ name;
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   143
    val typedefC =
2626d4e37341 provide more theorems (see subset.thy);
wenzelm
parents: 9969
diff changeset
   144
      Const (type_definitionN, (newT --> oldT) --> (oldT --> newT) --> setT --> HOLogic.boolT);
11426
f280d4b29a2c abtract non-emptiness statements (no longer use Eps);
wenzelm
parents: 10697
diff changeset
   145
    val typedef_prop =
f280d4b29a2c abtract non-emptiness statements (no longer use Eps);
wenzelm
parents: 10697
diff changeset
   146
      Logic.mk_implies (goal, HOLogic.mk_Trueprop (typedefC $ RepC $ AbsC $ set'));
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   147
11822
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   148
    fun typedef_result (theory, nonempty) =
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   149
      theory
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   150
      |> add_typedecls [(t, vs, mx)]
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   151
      |> Theory.add_consts_i
11822
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   152
       ((if def then [(name, setT, NoSyn)] else []) @
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   153
        [(Rep_name, newT --> oldT, NoSyn),
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   154
         (Abs_name, oldT --> newT, NoSyn)])
11822
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   155
      |> (if def then (apsnd (Some o hd) oo (PureThy.add_defs_i false o map Thm.no_attributes))
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   156
           [Logic.mk_defpair (setC, set)]
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   157
          else rpair None)
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   158
      |>> PureThy.add_axioms_i [((typedef_name, typedef_prop),
11727
a27150cc8fa5 test: use SkipProof.make_thm instead of Thm.assume;
wenzelm
parents: 11608
diff changeset
   159
          [apsnd (fn cond_axm => Drule.standard (nonempty RS cond_axm))])]
11822
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   160
      |> (fn ((theory', [type_definition]), set_def) =>
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   161
        let
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   162
          fun make th = Drule.standard (th OF [type_definition]);
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   163
          val (theory'', [Rep, Rep_inverse, Abs_inverse, Rep_inject, Abs_inject,
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   164
              Rep_cases, Abs_cases, Rep_induct, Abs_induct]) =
12338
de0f4a63baa5 renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents: 12043
diff changeset
   165
            theory'
de0f4a63baa5 renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents: 12043
diff changeset
   166
            |> Theory.add_path name
de0f4a63baa5 renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents: 12043
diff changeset
   167
            |> PureThy.add_thms
11822
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   168
              ([((Rep_name, make Rep), []),
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   169
                ((Rep_name ^ "_inverse", make Rep_inverse), []),
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   170
                ((Abs_name ^ "_inverse", make Abs_inverse), []),
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   171
                ((Rep_name ^ "_inject", make Rep_inject), []),
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   172
                ((Abs_name ^ "_inject", make Abs_inject), []),
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   173
                ((Rep_name ^ "_cases", make Rep_cases),
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   174
                  [RuleCases.case_names [Rep_name], InductAttrib.cases_set_global full_name]),
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   175
                ((Abs_name ^ "_cases", make Abs_cases),
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   176
                  [RuleCases.case_names [Abs_name], InductAttrib.cases_type_global full_tname]),
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   177
                ((Rep_name ^ "_induct", make Rep_induct),
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   178
                  [RuleCases.case_names [Rep_name], InductAttrib.induct_set_global full_name]),
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   179
                ((Abs_name ^ "_induct", make Abs_induct),
12338
de0f4a63baa5 renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents: 12043
diff changeset
   180
                  [RuleCases.case_names [Abs_name], InductAttrib.induct_type_global full_tname])])
de0f4a63baa5 renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents: 12043
diff changeset
   181
            |>> Theory.parent_path;
11822
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   182
          val result = {type_definition = type_definition, set_def = set_def,
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   183
            Rep = Rep, Rep_inverse = Rep_inverse, Abs_inverse = Abs_inverse,
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   184
            Rep_inject = Rep_inject, Abs_inject = Abs_inject, Rep_cases = Rep_cases,
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   185
            Abs_cases = Abs_cases, Rep_induct = Rep_induct, Abs_induct = Abs_induct};
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   186
        in ((theory'', type_definition), result) end);
6383
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;
11426
f280d4b29a2c abtract non-emptiness statements (no longer use Eps);
wenzelm
parents: 10697
diff changeset
   210
    val _ = if null errs then () else error (cat_lines errs);
f280d4b29a2c abtract non-emptiness statements (no longer use Eps);
wenzelm
parents: 10697
diff changeset
   211
f280d4b29a2c abtract non-emptiness statements (no longer use Eps);
wenzelm
parents: 10697
diff changeset
   212
    (*test theory errors now!*)
f280d4b29a2c abtract non-emptiness statements (no longer use Eps);
wenzelm
parents: 10697
diff changeset
   213
    val test_thy = Theory.copy thy;
11727
a27150cc8fa5 test: use SkipProof.make_thm instead of Thm.assume;
wenzelm
parents: 11608
diff changeset
   214
    val _ = (test_thy,
11822
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   215
      setmp quick_and_dirty true (SkipProof.make_thm test_thy) goal) |> typedef_result;
11426
f280d4b29a2c abtract non-emptiness statements (no longer use Eps);
wenzelm
parents: 10697
diff changeset
   216
11822
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   217
  in (cset, goal, goal_pat, typedef_result) end
11426
f280d4b29a2c abtract non-emptiness statements (no longer use Eps);
wenzelm
parents: 10697
diff changeset
   218
  handle ERROR => err_in_typedef name;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   219
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   220
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   221
(* add_typedef interfaces *)
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   222
11827
16ef206e6648 sane internal interface for add_typedef(_i);
wenzelm
parents: 11822
diff changeset
   223
fun gen_typedef prep_term def name typ set opt_morphs tac1 names thms tac2 thy =
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   224
  let
11822
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   225
    val (cset, goal, _, typedef_result) =
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   226
      prepare_typedef prep_term def name typ set opt_morphs thy;
11827
16ef206e6648 sane internal interface for add_typedef(_i);
wenzelm
parents: 11822
diff changeset
   227
    val non_empty = prove_nonempty thy cset goal (tac1, names, thms, tac2);
11822
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   228
    val ((thy', _), result) = (thy, non_empty) |> typedef_result;
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   229
  in (thy', result) end;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   230
11827
16ef206e6648 sane internal interface for add_typedef(_i);
wenzelm
parents: 11822
diff changeset
   231
fun sane_typedef prep_term def opt_name typ set opt_morphs tac =
16ef206e6648 sane internal interface for add_typedef(_i);
wenzelm
parents: 11822
diff changeset
   232
  gen_typedef prep_term def
16ef206e6648 sane internal interface for add_typedef(_i);
wenzelm
parents: 11822
diff changeset
   233
    (if_none opt_name (#1 typ)) typ set opt_morphs all_tac [] [] (Some tac);
11822
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   234
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   235
fun add_typedef_x name typ set names thms tac =
11827
16ef206e6648 sane internal interface for add_typedef(_i);
wenzelm
parents: 11822
diff changeset
   236
  #1 o gen_typedef read_term true name typ set None (Tactic.rtac exI 1) names thms tac;
11822
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   237
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   238
val add_typedef = sane_typedef read_term;
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   239
val add_typedef_i = sane_typedef cert_term;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   240
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   241
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   242
(* typedef_proof interface *)
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   243
11744
3a4625eaead0 'morphisms' spec;
wenzelm
parents: 11740
diff changeset
   244
fun gen_typedef_proof prep_term ((name, typ, set, opt_morphs), comment) int thy =
11822
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   245
  let
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   246
    val (_, goal, goal_pat, att_result) =
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   247
      prepare_typedef prep_term true name typ set opt_morphs thy;
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   248
    val att = #1 o att_result;
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   249
  in
12694
9950c1ce9d24 simplified IsarThy.theorem_i;
wenzelm
parents: 12624
diff changeset
   250
    thy
9950c1ce9d24 simplified IsarThy.theorem_i;
wenzelm
parents: 12624
diff changeset
   251
    |> IsarThy.theorem_i Drule.internalK ((("", [att]), (goal, ([goal_pat], []))), comment) int
11740
86ac4189a1c1 IsarThy.theorem_i Drule.internalK;
wenzelm
parents: 11727
diff changeset
   252
  end;
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   253
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   254
val typedef_proof = gen_typedef_proof read_term;
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   255
val typedef_proof_i = gen_typedef_proof cert_term;
6357
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   256
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   257
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   258
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   259
(** outer syntax **)
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   260
6723
f342449d73ca outer syntax keyword classification;
wenzelm
parents: 6690
diff changeset
   261
local structure P = OuterParse and K = OuterSyntax.Keyword in
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   262
6357
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   263
val typedeclP =
12624
9ed65232429c tuned msg;
wenzelm
parents: 12338
diff changeset
   264
  OuterSyntax.command "typedecl" "type declaration (HOL)" K.thy_decl
7152
44d46a112127 tuned outer syntax;
wenzelm
parents: 6935
diff changeset
   265
    (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
   266
      Toplevel.theory (add_typedecls [(t, vs, mx)])));
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   267
6723
f342449d73ca outer syntax keyword classification;
wenzelm
parents: 6690
diff changeset
   268
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   269
val typedef_proof_decl =
6723
f342449d73ca outer syntax keyword classification;
wenzelm
parents: 6690
diff changeset
   270
  Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") --
11744
3a4625eaead0 'morphisms' spec;
wenzelm
parents: 11740
diff changeset
   271
    (P.type_args -- P.name) -- P.opt_infix -- (P.$$$ "=" |-- P.term) --
3a4625eaead0 'morphisms' spec;
wenzelm
parents: 11740
diff changeset
   272
    Scan.option (P.$$$ "morphisms" |-- P.!!! (P.name -- P.name)) --
3a4625eaead0 'morphisms' spec;
wenzelm
parents: 11740
diff changeset
   273
    P.marg_comment;
6357
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   274
11744
3a4625eaead0 'morphisms' spec;
wenzelm
parents: 11740
diff changeset
   275
fun mk_typedef_proof (((((opt_name, (vs, t)), mx), A), morphs), comment) =
3a4625eaead0 'morphisms' spec;
wenzelm
parents: 11740
diff changeset
   276
  typedef_proof ((if_none opt_name (Syntax.type_name t mx), (t, vs, mx), A, morphs), comment);
6357
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   277
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   278
val typedefP =
6723
f342449d73ca outer syntax keyword classification;
wenzelm
parents: 6690
diff changeset
   279
  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
   280
    (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
   281
6723
f342449d73ca outer syntax keyword classification;
wenzelm
parents: 6690
diff changeset
   282
11744
3a4625eaead0 'morphisms' spec;
wenzelm
parents: 11740
diff changeset
   283
val _ = OuterSyntax.add_keywords ["morphisms"];
6357
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   284
val _ = OuterSyntax.add_parsers [typedeclP, typedefP];
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   285
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   286
end;
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   287
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   288
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   289
end;