src/HOL/Tools/typedef_package.ML
author wenzelm
Tue, 25 May 1999 20:24:10 +0200
changeset 6729 b6e167580a32
parent 6723 f342449d73ca
child 6935 a3f3f4128cab
permissions -rw-r--r--
formal comments (still dummy);
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
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
     4
6357
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
     5
Gordon/HOL-style type definitions.
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
     6
*)
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
     7
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
     8
signature TYPEDEF_PACKAGE =
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
     9
sig
5697
e816c4f1a396 quiet_mode, message;
wenzelm
parents: 5180
diff changeset
    10
  val quiet_mode: bool ref
5104
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    11
  val add_typedecls: (bstring * string list * mixfix) list -> theory -> theory
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    12
  val add_typedef: string -> bstring * string list * mixfix ->
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    13
    string -> string list -> thm list -> tactic option -> theory -> theory
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    14
  val add_typedef_i: string -> bstring * string list * mixfix ->
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    15
    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
    16
  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
    17
    term -> string list -> thm list -> tactic option -> theory -> theory
6729
b6e167580a32 formal comments (still dummy);
wenzelm
parents: 6723
diff changeset
    18
  val typedef_proof: (string * (bstring * string list * mixfix) * string) * Comment.text
6690
acbcf8085166 typedef_proof: pass interactive flag;
wenzelm
parents: 6383
diff changeset
    19
    -> bool -> theory -> ProofHistory.T
6729
b6e167580a32 formal comments (still dummy);
wenzelm
parents: 6723
diff changeset
    20
  val typedef_proof_i: (string * (bstring * string list * mixfix) * term) * Comment.text
6690
acbcf8085166 typedef_proof: pass interactive flag;
wenzelm
parents: 6383
diff changeset
    21
    -> bool -> theory -> ProofHistory.T
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    22
end;
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    23
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    24
structure TypedefPackage: TYPEDEF_PACKAGE =
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    25
struct
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    26
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    27
5104
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    28
(** type declarations **)
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    29
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    30
fun add_typedecls decls thy =
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    31
  let
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    32
    val full = Sign.full_name (Theory.sign_of thy);
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    33
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    34
    fun arity_of (raw_name, args, mx) =
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    35
      (full (Syntax.type_name raw_name mx), replicate (length args) HOLogic.termS, HOLogic.termS);
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    36
  in
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    37
    thy
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    38
    |> PureThy.add_typedecls decls
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    39
    |> Theory.add_arities_i (map arity_of decls)
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    40
  end;
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    41
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    42
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    43
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    44
(** type definitions **)
230cca8452c7 added add_typedecls;
wenzelm
parents: 4970
diff changeset
    45
5697
e816c4f1a396 quiet_mode, message;
wenzelm
parents: 5180
diff changeset
    46
(* messages *)
e816c4f1a396 quiet_mode, message;
wenzelm
parents: 5180
diff changeset
    47
e816c4f1a396 quiet_mode, message;
wenzelm
parents: 5180
diff changeset
    48
val quiet_mode = ref false;
e816c4f1a396 quiet_mode, message;
wenzelm
parents: 5180
diff changeset
    49
fun message s = if ! quiet_mode then () else writeln s;
e816c4f1a396 quiet_mode, message;
wenzelm
parents: 5180
diff changeset
    50
e816c4f1a396 quiet_mode, message;
wenzelm
parents: 5180
diff changeset
    51
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    52
(* non-emptiness of set *)              (*exception ERROR*)
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    53
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    54
fun check_nonempty cset thm =
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    55
  let
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    56
    val {t, sign, maxidx, ...} = Thm.rep_cterm cset;
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    57
    val {prop, ...} = Thm.rep_thm (Thm.transfer_sg sign (Drule.standard thm));
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    58
    val matches = Pattern.matches (Sign.tsig_of sign);
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    59
  in
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    60
    (case try (HOLogic.dest_mem o HOLogic.dest_Trueprop) prop of
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    61
      None => raise ERROR
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    62
    | 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
    63
  end handle ERROR => error ("Bad non-emptiness theorem " ^ Display.string_of_thm thm ^
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    64
    "\nfor set " ^ quote (Display.string_of_cterm cset));
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    65
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    66
fun goal_nonempty cset =
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    67
  let
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    68
    val {T = setT, t = A, maxidx, sign} = Thm.rep_cterm cset;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    69
    val T = HOLogic.dest_setT setT;
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    70
  in Thm.cterm_of sign (HOLogic.mk_Trueprop (HOLogic.mk_mem (Var (("x", maxidx + 1), T), A))) end;
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    71
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    72
fun prove_nonempty thy cset (witn_names, witn_thms, witn_tac) =
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    73
  let
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    74
    val is_def = Logic.is_equals o #prop o Thm.rep_thm;
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    75
    val thms = PureThy.get_thmss thy witn_names @ witn_thms;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    76
    val tac =
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    77
      TRY (rewrite_goals_tac (filter is_def thms)) THEN
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    78
      TRY (REPEAT_FIRST (resolve_tac (filter_out is_def thms))) THEN
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    79
      if_none witn_tac (TRY (ALLGOALS (CLASET' blast_tac)));
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    80
  in
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    81
    message ("Proving non-emptiness of set " ^ quote (string_of_cterm cset) ^ " ...");
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    82
    prove_goalw_cterm [] (goal_nonempty cset) (K [tac])
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    83
  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
    84
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    85
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    86
(* prepare_typedef *)
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    87
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    88
fun read_term sg used s =
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    89
  #1 (Thm.read_def_cterm (sg, K None, K None) used true (s, HOLogic.termTVar));
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    90
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    91
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
    92
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    93
fun err_in_typedef name =
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    94
  error ("The error(s) above occurred in typedef " ^ quote name);
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    95
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    96
fun prepare_typedef prep_term no_def name (t, vs, mx) raw_set thy =
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    97
  let
4970
8b65444edbb0 Changed require to requires for MLWorks
paulson
parents: 4932
diff changeset
    98
    val _ = Theory.requires thy "Set" "typedefs";
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
    99
    val sign = Theory.sign_of thy;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   100
    val full_name = Sign.full_name sign;
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   101
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   102
    (*rhs*)
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   103
    val cset = prep_term sign vs raw_set;
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   104
    val {T = setT, t = set, ...} = Thm.rep_cterm cset;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   105
    val rhs_tfrees = term_tfrees set;
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   106
    val oldT = HOLogic.dest_setT setT handle TYPE _ =>
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   107
      error ("Not a set type: " ^ quote (Sign.string_of_typ sign setT));
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   108
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   109
    (*lhs*)
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   110
    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
   111
    val tname = Syntax.type_name t mx;
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   112
    val newT = Type (full_name tname, map TFree lhs_tfrees);
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   113
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   114
    val Rep_name = "Rep_" ^ name;
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   115
    val Abs_name = "Abs_" ^ name;
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   116
    val setC = Const (full_name name, setT);
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   117
    val RepC = Const (full_name Rep_name, newT --> oldT);
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   118
    val AbsC = Const (full_name Abs_name, oldT --> newT);
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   119
    val x_new = Free ("x", newT);
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   120
    val y_old = Free ("y", oldT);
5180
d82a70766af0 Added new function add_typedef_i_no_def which doesn't add
berghofe
parents: 5104
diff changeset
   121
    val set' = if no_def then set else setC;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   122
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   123
    (*axioms*)
5180
d82a70766af0 Added new function add_typedef_i_no_def which doesn't add
berghofe
parents: 5104
diff changeset
   124
    val rep_type = HOLogic.mk_Trueprop (HOLogic.mk_mem (RepC $ x_new, set'));
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   125
    val rep_type_inv = HOLogic.mk_Trueprop (HOLogic.mk_eq (AbsC $ (RepC $ x_new), x_new));
5180
d82a70766af0 Added new function add_typedef_i_no_def which doesn't add
berghofe
parents: 5104
diff changeset
   126
    val abs_type_inv = Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_mem (y_old, set')),
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   127
      HOLogic.mk_Trueprop (HOLogic.mk_eq (RepC $ (AbsC $ y_old), y_old)));
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   128
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   129
    (*theory extender*)
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   130
    fun do_typedef theory =
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   131
      theory
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   132
      |> Theory.assert_super thy
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   133
      |> add_typedecls [(t, vs, mx)]
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   134
      |> Theory.add_consts_i
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   135
       ((if no_def then [] else [(name, setT, NoSyn)]) @
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   136
        [(Rep_name, newT --> oldT, NoSyn),
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   137
         (Abs_name, oldT --> newT, NoSyn)])
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   138
      |> (if no_def then I else (PureThy.add_defs_i o map Thm.no_attributes)
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   139
       [Logic.mk_defpair (setC, set)])
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   140
      |> (PureThy.add_axioms_i o map Thm.no_attributes)
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   141
       [(Rep_name, rep_type),
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   142
        (Rep_name ^ "_inverse", rep_type_inv),
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   143
        (Abs_name ^ "_inverse", abs_type_inv)]
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   144
      handle ERROR => err_in_typedef name;
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   145
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   146
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   147
    (* errors *)
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   148
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   149
    fun show_names pairs = commas_quote (map fst pairs);
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   150
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   151
    val illegal_vars =
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   152
      if null (term_vars set) andalso null (term_tvars set) then []
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   153
      else ["Illegal schematic variable(s) on rhs"];
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   154
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   155
    val dup_lhs_tfrees =
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   156
      (case duplicates lhs_tfrees of [] => []
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   157
      | dups => ["Duplicate type variables on lhs: " ^ show_names dups]);
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   158
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   159
    val extra_rhs_tfrees =
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   160
      (case gen_rems (op =) (rhs_tfrees, lhs_tfrees) of [] => []
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   161
      | extras => ["Extra type variables on rhs: " ^ show_names extras]);
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   162
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   163
    val illegal_frees =
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   164
      (case term_frees set of [] => []
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   165
      | xs => ["Illegal variables on rhs: " ^ show_names (map dest_Free xs)]);
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   166
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   167
    val errs = illegal_vars @ dup_lhs_tfrees @ extra_rhs_tfrees @ illegal_frees;
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   168
  in
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   169
    if null errs then () else error (cat_lines errs);
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   170
    (cset, do_typedef)
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   171
  end handle ERROR => err_in_typedef name;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   172
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   173
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   174
(* add_typedef interfaces *)
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   175
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   176
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
   177
  let
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   178
    val (cset, do_typedef) = prepare_typedef prep_term no_def name typ set thy;
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   179
    val result = prove_nonempty thy cset (names, thms, tac);
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   180
  in check_nonempty cset result; thy |> do_typedef end;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   181
5180
d82a70766af0 Added new function add_typedef_i_no_def which doesn't add
berghofe
parents: 5104
diff changeset
   182
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
   183
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
   184
val add_typedef_i_no_def = gen_add_typedef cert_term true;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   185
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   186
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   187
(* typedef_proof interface *)
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   188
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   189
fun typedef_attribute cset do_typedef (thy, thm) =
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   190
  (check_nonempty cset thm; (thy |> do_typedef, thm));
6357
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   191
6729
b6e167580a32 formal comments (still dummy);
wenzelm
parents: 6723
diff changeset
   192
fun gen_typedef_proof prep_term ((name, typ, set), comment) int thy =
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   193
  let
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   194
    val (cset, do_typedef) = prepare_typedef prep_term false name typ set thy;
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   195
    val goal = Thm.term_of (goal_nonempty cset);
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   196
  in
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   197
    thy
6729
b6e167580a32 formal comments (still dummy);
wenzelm
parents: 6723
diff changeset
   198
    |> IsarThy.theorem_i (("", [typedef_attribute cset do_typedef], (goal, [])), comment) int
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   199
  end;
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   200
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   201
val typedef_proof = gen_typedef_proof read_term;
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   202
val typedef_proof_i = gen_typedef_proof cert_term;
6357
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   203
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   204
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   205
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   206
(** outer syntax **)
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   207
6723
f342449d73ca outer syntax keyword classification;
wenzelm
parents: 6690
diff changeset
   208
local structure P = OuterParse and K = OuterSyntax.Keyword in
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   209
6357
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   210
val typedeclP =
6723
f342449d73ca outer syntax keyword classification;
wenzelm
parents: 6690
diff changeset
   211
  OuterSyntax.command "typedecl" "HOL type declaration" K.thy_decl
6729
b6e167580a32 formal comments (still dummy);
wenzelm
parents: 6723
diff changeset
   212
    (P.type_args -- P.name -- P.opt_mixfix --| P.marg_comment >> (fn ((vs, t), mx) =>
6357
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   213
      Toplevel.theory (add_typedecls [(t, vs, mx)])));
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   214
6723
f342449d73ca outer syntax keyword classification;
wenzelm
parents: 6690
diff changeset
   215
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   216
val typedef_proof_decl =
6723
f342449d73ca outer syntax keyword classification;
wenzelm
parents: 6690
diff changeset
   217
  Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") --
6729
b6e167580a32 formal comments (still dummy);
wenzelm
parents: 6723
diff changeset
   218
    (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
   219
6729
b6e167580a32 formal comments (still dummy);
wenzelm
parents: 6723
diff changeset
   220
fun mk_typedef_proof ((((opt_name, (vs, t)), mx), A), comment) =
b6e167580a32 formal comments (still dummy);
wenzelm
parents: 6723
diff changeset
   221
  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
   222
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   223
val typedefP =
6723
f342449d73ca outer syntax keyword classification;
wenzelm
parents: 6690
diff changeset
   224
  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
   225
    (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
   226
6723
f342449d73ca outer syntax keyword classification;
wenzelm
parents: 6690
diff changeset
   227
6357
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   228
val _ = OuterSyntax.add_parsers [typedeclP, typedefP];
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   229
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   230
end;
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   231
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   232
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   233
end;