src/HOL/Tools/typedef_package.ML
author wenzelm
Tue Oct 20 16:37:02 1998 +0200 (1998-10-20)
changeset 5697 e816c4f1a396
parent 5180 d82a70766af0
child 6092 d9db67970c73
permissions -rw-r--r--
quiet_mode, message;
wenzelm@4866
     1
(*  Title:      HOL/Tools/typedef_package.ML
wenzelm@4866
     2
    ID:         $Id$
wenzelm@4866
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@4866
     4
wenzelm@4866
     5
Gordon/HOL style type definitions.
wenzelm@4866
     6
*)
wenzelm@4866
     7
wenzelm@4866
     8
signature TYPEDEF_PACKAGE =
wenzelm@4866
     9
sig
wenzelm@5697
    10
  val quiet_mode: bool ref
wenzelm@5104
    11
  val add_typedecls: (bstring * string list * mixfix) list -> theory -> theory
wenzelm@4866
    12
  val prove_nonempty: cterm -> thm list -> tactic option -> thm
wenzelm@4866
    13
  val add_typedef: string -> bstring * string list * mixfix ->
wenzelm@4866
    14
    string -> string list -> thm list -> tactic option -> theory -> theory
wenzelm@4866
    15
  val add_typedef_i: string -> bstring * string list * mixfix ->
wenzelm@4866
    16
    term -> string list -> thm list -> tactic option -> theory -> theory
berghofe@5180
    17
  val add_typedef_i_no_def: string -> bstring * string list * mixfix ->
berghofe@5180
    18
    term -> string list -> thm list -> tactic option -> theory -> theory
wenzelm@4866
    19
end;
wenzelm@4866
    20
wenzelm@4866
    21
structure TypedefPackage: TYPEDEF_PACKAGE =
wenzelm@4866
    22
struct
wenzelm@4866
    23
wenzelm@4866
    24
wenzelm@5104
    25
(** type declarations **)
wenzelm@5104
    26
wenzelm@5104
    27
fun add_typedecls decls thy =
wenzelm@5104
    28
  let
wenzelm@5104
    29
    val full = Sign.full_name (Theory.sign_of thy);
wenzelm@5104
    30
wenzelm@5104
    31
    fun arity_of (raw_name, args, mx) =
wenzelm@5104
    32
      (full (Syntax.type_name raw_name mx), replicate (length args) HOLogic.termS, HOLogic.termS);
wenzelm@5104
    33
  in
wenzelm@5104
    34
    thy
wenzelm@5104
    35
    |> PureThy.add_typedecls decls
wenzelm@5104
    36
    |> Theory.add_arities_i (map arity_of decls)
wenzelm@5104
    37
  end;
wenzelm@5104
    38
wenzelm@5104
    39
wenzelm@5104
    40
wenzelm@5104
    41
(** type definitions **)
wenzelm@5104
    42
wenzelm@5697
    43
(* messages *)
wenzelm@5697
    44
wenzelm@5697
    45
val quiet_mode = ref false;
wenzelm@5697
    46
fun message s = if ! quiet_mode then () else writeln s;
wenzelm@5697
    47
wenzelm@5697
    48
wenzelm@4866
    49
(* prove non-emptyness of a set *)   (*exception ERROR*)
wenzelm@4866
    50
wenzelm@4866
    51
val is_def = Logic.is_equals o #prop o rep_thm;
wenzelm@4866
    52
wenzelm@4866
    53
fun prove_nonempty cset thms usr_tac =
wenzelm@4866
    54
  let
wenzelm@4866
    55
    val {T = setT, t = set, maxidx, sign} = rep_cterm cset;
wenzelm@4866
    56
    val T = HOLogic.dest_setT setT;
wenzelm@4866
    57
    val goal = cterm_of sign
wenzelm@4866
    58
      (HOLogic.mk_Trueprop (HOLogic.mk_mem (Var (("x", maxidx + 1), T), set)));
wenzelm@4866
    59
    val tac =
wenzelm@4866
    60
      TRY (rewrite_goals_tac (filter is_def thms)) THEN
wenzelm@4866
    61
      TRY (REPEAT_FIRST (resolve_tac (filter_out is_def thms))) THEN
wenzelm@4866
    62
      if_none usr_tac (TRY (ALLGOALS (CLASET' blast_tac)));
wenzelm@4866
    63
  in
wenzelm@4866
    64
    prove_goalw_cterm [] goal (K [tac])
wenzelm@4866
    65
  end
wenzelm@4866
    66
  handle ERROR =>
wenzelm@4866
    67
    error ("Failed to prove nonemptiness of " ^ quote (string_of_cterm cset));
wenzelm@4866
    68
wenzelm@4866
    69
wenzelm@4866
    70
(* gen_add_typedef *)
wenzelm@4866
    71
berghofe@5180
    72
fun gen_add_typedef prep_term no_def name (t, vs, mx) raw_set axms thms usr_tac thy =
wenzelm@4866
    73
  let
paulson@4970
    74
    val _ = Theory.requires thy "Set" "typedefs";
wenzelm@4866
    75
    val sign = sign_of thy;
wenzelm@4866
    76
    val full_name = Sign.full_name sign;
wenzelm@4866
    77
wenzelm@4866
    78
    (*rhs*)
wenzelm@4866
    79
    val cset = prep_term sign raw_set;
wenzelm@4866
    80
    val {T = setT, t = set, ...} = rep_cterm cset;
wenzelm@4866
    81
    val rhs_tfrees = term_tfrees set;
wenzelm@4866
    82
    val oldT = HOLogic.dest_setT setT handle TYPE _ =>
wenzelm@4866
    83
      error ("Not a set type: " ^ quote (Sign.string_of_typ sign setT));
wenzelm@4866
    84
wenzelm@4866
    85
    (*lhs*)
wenzelm@4866
    86
    val lhs_tfrees =
wenzelm@4866
    87
      map (fn v => (v, if_none (assoc (rhs_tfrees, v)) HOLogic.termS)) vs;
wenzelm@4866
    88
wenzelm@4866
    89
    val tname = Syntax.type_name t mx;
wenzelm@4866
    90
    val newT = Type (full_name tname, map TFree lhs_tfrees);
wenzelm@4866
    91
wenzelm@4866
    92
    val Rep_name = "Rep_" ^ name;
wenzelm@4866
    93
    val Abs_name = "Abs_" ^ name;
wenzelm@4866
    94
    val setC = Const (full_name name, setT);
wenzelm@4866
    95
    val RepC = Const (full_name Rep_name, newT --> oldT);
wenzelm@4866
    96
    val AbsC = Const (full_name Abs_name, oldT --> newT);
wenzelm@4866
    97
    val x_new = Free ("x", newT);
wenzelm@4866
    98
    val y_old = Free ("y", oldT);
berghofe@5180
    99
    val set' = if no_def then set else setC;
wenzelm@4866
   100
wenzelm@4866
   101
    (*axioms*)
berghofe@5180
   102
    val rep_type = HOLogic.mk_Trueprop (HOLogic.mk_mem (RepC $ x_new, set'));
wenzelm@4866
   103
    val rep_type_inv = HOLogic.mk_Trueprop (HOLogic.mk_eq (AbsC $ (RepC $ x_new), x_new));
berghofe@5180
   104
    val abs_type_inv = Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_mem (y_old, set')),
wenzelm@4866
   105
      HOLogic.mk_Trueprop (HOLogic.mk_eq (RepC $ (AbsC $ y_old), y_old)));
wenzelm@4866
   106
wenzelm@4866
   107
wenzelm@4866
   108
    (* errors *)
wenzelm@4866
   109
wenzelm@4866
   110
    fun show_names pairs = commas_quote (map fst pairs);
wenzelm@4866
   111
wenzelm@4866
   112
    val illegal_vars =
wenzelm@4866
   113
      if null (term_vars set) andalso null (term_tvars set) then []
wenzelm@4866
   114
      else ["Illegal schematic variable(s) on rhs"];
wenzelm@4866
   115
wenzelm@4866
   116
    val dup_lhs_tfrees =
wenzelm@4866
   117
      (case duplicates lhs_tfrees of [] => []
wenzelm@4866
   118
      | dups => ["Duplicate type variables on lhs: " ^ show_names dups]);
wenzelm@4866
   119
wenzelm@4866
   120
    val extra_rhs_tfrees =
wenzelm@4866
   121
      (case gen_rems (op =) (rhs_tfrees, lhs_tfrees) of [] => []
wenzelm@4866
   122
      | extras => ["Extra type variables on rhs: " ^ show_names extras]);
wenzelm@4866
   123
wenzelm@4866
   124
    val illegal_frees =
wenzelm@4866
   125
      (case term_frees set of [] => []
wenzelm@4866
   126
      | xs => ["Illegal variables on rhs: " ^ show_names (map dest_Free xs)]);
wenzelm@4866
   127
wenzelm@4866
   128
    val errs = illegal_vars @ dup_lhs_tfrees @ extra_rhs_tfrees @ illegal_frees;
wenzelm@4866
   129
  in
wenzelm@4866
   130
    if null errs then ()
wenzelm@4866
   131
    else error (cat_lines errs);
wenzelm@4866
   132
wenzelm@5697
   133
    message ("Proving nonemptiness of " ^ quote name ^ " ...");
wenzelm@4866
   134
    prove_nonempty cset (map (get_axiom thy) axms @ thms) usr_tac;
wenzelm@4866
   135
wenzelm@4866
   136
    thy
wenzelm@4932
   137
    |> PureThy.add_typedecls [(t, vs, mx)]
wenzelm@4866
   138
    |> Theory.add_arities_i
wenzelm@4932
   139
     [(full_name tname, replicate (length vs) HOLogic.termS, HOLogic.termS)]
wenzelm@4866
   140
    |> Theory.add_consts_i
berghofe@5180
   141
     ((if no_def then [] else [(name, setT, NoSyn)]) @
berghofe@5180
   142
      [(Rep_name, newT --> oldT, NoSyn),
berghofe@5180
   143
       (Abs_name, oldT --> newT, NoSyn)])
berghofe@5180
   144
    |> (if no_def then I else (PureThy.add_defs_i o map Attribute.none)
berghofe@5180
   145
     [(name ^ "_def", Logic.mk_equals (setC, set))])
wenzelm@4866
   146
    |> (PureThy.add_axioms_i o map Attribute.none)
wenzelm@4866
   147
     [(Rep_name, rep_type),
wenzelm@4866
   148
      (Rep_name ^ "_inverse", rep_type_inv),
wenzelm@4866
   149
      (Abs_name ^ "_inverse", abs_type_inv)]
wenzelm@4866
   150
  end
wenzelm@4866
   151
  handle ERROR =>
wenzelm@4866
   152
    error ("The error(s) above occurred in typedef " ^ quote name);
wenzelm@4866
   153
wenzelm@4866
   154
wenzelm@4866
   155
(* external interfaces *)
wenzelm@4866
   156
wenzelm@4866
   157
fun read_term sg str =
wenzelm@4866
   158
  read_cterm sg (str, HOLogic.termTVar);
wenzelm@4866
   159
wenzelm@4866
   160
fun cert_term sg tm =
wenzelm@4866
   161
  cterm_of sg tm handle TERM (msg, _) => error msg;
wenzelm@4866
   162
berghofe@5180
   163
val add_typedef = gen_add_typedef read_term false;
berghofe@5180
   164
val add_typedef_i = gen_add_typedef cert_term false;
berghofe@5180
   165
val add_typedef_i_no_def = gen_add_typedef cert_term true;
wenzelm@4866
   166
wenzelm@4866
   167
wenzelm@4866
   168
end;