src/HOL/Tools/typedef_package.ML
author wenzelm
Wed Apr 29 11:39:52 1998 +0200 (1998-04-29)
changeset 4866 72a46bd00c8d
child 4932 c90411dde8e8
permissions -rw-r--r--
renamed from typedef.ML;
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@4866
    10
  val prove_nonempty: cterm -> thm list -> tactic option -> thm
wenzelm@4866
    11
  val add_typedef: string -> bstring * string list * mixfix ->
wenzelm@4866
    12
    string -> string list -> thm list -> tactic option -> theory -> theory
wenzelm@4866
    13
  val add_typedef_i: string -> bstring * string list * mixfix ->
wenzelm@4866
    14
    term -> string list -> thm list -> tactic option -> theory -> theory
wenzelm@4866
    15
end;
wenzelm@4866
    16
wenzelm@4866
    17
structure TypedefPackage: TYPEDEF_PACKAGE =
wenzelm@4866
    18
struct
wenzelm@4866
    19
wenzelm@4866
    20
wenzelm@4866
    21
(* prove non-emptyness of a set *)   (*exception ERROR*)
wenzelm@4866
    22
wenzelm@4866
    23
val is_def = Logic.is_equals o #prop o rep_thm;
wenzelm@4866
    24
wenzelm@4866
    25
fun prove_nonempty cset thms usr_tac =
wenzelm@4866
    26
  let
wenzelm@4866
    27
    val {T = setT, t = set, maxidx, sign} = rep_cterm cset;
wenzelm@4866
    28
    val T = HOLogic.dest_setT setT;
wenzelm@4866
    29
    val goal = cterm_of sign
wenzelm@4866
    30
      (HOLogic.mk_Trueprop (HOLogic.mk_mem (Var (("x", maxidx + 1), T), set)));
wenzelm@4866
    31
    val tac =
wenzelm@4866
    32
      TRY (rewrite_goals_tac (filter is_def thms)) THEN
wenzelm@4866
    33
      TRY (REPEAT_FIRST (resolve_tac (filter_out is_def thms))) THEN
wenzelm@4866
    34
      if_none usr_tac (TRY (ALLGOALS (CLASET' blast_tac)));
wenzelm@4866
    35
  in
wenzelm@4866
    36
    prove_goalw_cterm [] goal (K [tac])
wenzelm@4866
    37
  end
wenzelm@4866
    38
  handle ERROR =>
wenzelm@4866
    39
    error ("Failed to prove nonemptiness of " ^ quote (string_of_cterm cset));
wenzelm@4866
    40
wenzelm@4866
    41
wenzelm@4866
    42
(* gen_add_typedef *)
wenzelm@4866
    43
wenzelm@4866
    44
fun gen_add_typedef prep_term name (t, vs, mx) raw_set axms thms usr_tac thy =
wenzelm@4866
    45
  let
wenzelm@4866
    46
    val _ = Theory.require thy "Set" "typedefs";
wenzelm@4866
    47
    val sign = sign_of thy;
wenzelm@4866
    48
    val full_name = Sign.full_name sign;
wenzelm@4866
    49
wenzelm@4866
    50
    (*rhs*)
wenzelm@4866
    51
    val cset = prep_term sign raw_set;
wenzelm@4866
    52
    val {T = setT, t = set, ...} = rep_cterm cset;
wenzelm@4866
    53
    val rhs_tfrees = term_tfrees set;
wenzelm@4866
    54
    val oldT = HOLogic.dest_setT setT handle TYPE _ =>
wenzelm@4866
    55
      error ("Not a set type: " ^ quote (Sign.string_of_typ sign setT));
wenzelm@4866
    56
wenzelm@4866
    57
    (*lhs*)
wenzelm@4866
    58
    val lhs_tfrees =
wenzelm@4866
    59
      map (fn v => (v, if_none (assoc (rhs_tfrees, v)) HOLogic.termS)) vs;
wenzelm@4866
    60
wenzelm@4866
    61
    val tname = Syntax.type_name t mx;
wenzelm@4866
    62
    val tlen = length vs;
wenzelm@4866
    63
    val newT = Type (full_name tname, map TFree lhs_tfrees);
wenzelm@4866
    64
wenzelm@4866
    65
    val Rep_name = "Rep_" ^ name;
wenzelm@4866
    66
    val Abs_name = "Abs_" ^ name;
wenzelm@4866
    67
    val setC = Const (full_name name, setT);
wenzelm@4866
    68
    val RepC = Const (full_name Rep_name, newT --> oldT);
wenzelm@4866
    69
    val AbsC = Const (full_name Abs_name, oldT --> newT);
wenzelm@4866
    70
    val x_new = Free ("x", newT);
wenzelm@4866
    71
    val y_old = Free ("y", oldT);
wenzelm@4866
    72
wenzelm@4866
    73
    (*axioms*)
wenzelm@4866
    74
    val rep_type = HOLogic.mk_Trueprop (HOLogic.mk_mem (RepC $ x_new, setC));
wenzelm@4866
    75
    val rep_type_inv = HOLogic.mk_Trueprop (HOLogic.mk_eq (AbsC $ (RepC $ x_new), x_new));
wenzelm@4866
    76
    val abs_type_inv = Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_mem (y_old, setC)),
wenzelm@4866
    77
      HOLogic.mk_Trueprop (HOLogic.mk_eq (RepC $ (AbsC $ y_old), y_old)));
wenzelm@4866
    78
wenzelm@4866
    79
wenzelm@4866
    80
    (* errors *)
wenzelm@4866
    81
wenzelm@4866
    82
    fun show_names pairs = commas_quote (map fst pairs);
wenzelm@4866
    83
wenzelm@4866
    84
    val illegal_vars =
wenzelm@4866
    85
      if null (term_vars set) andalso null (term_tvars set) then []
wenzelm@4866
    86
      else ["Illegal schematic variable(s) on rhs"];
wenzelm@4866
    87
wenzelm@4866
    88
    val dup_lhs_tfrees =
wenzelm@4866
    89
      (case duplicates lhs_tfrees of [] => []
wenzelm@4866
    90
      | dups => ["Duplicate type variables on lhs: " ^ show_names dups]);
wenzelm@4866
    91
wenzelm@4866
    92
    val extra_rhs_tfrees =
wenzelm@4866
    93
      (case gen_rems (op =) (rhs_tfrees, lhs_tfrees) of [] => []
wenzelm@4866
    94
      | extras => ["Extra type variables on rhs: " ^ show_names extras]);
wenzelm@4866
    95
wenzelm@4866
    96
    val illegal_frees =
wenzelm@4866
    97
      (case term_frees set of [] => []
wenzelm@4866
    98
      | xs => ["Illegal variables on rhs: " ^ show_names (map dest_Free xs)]);
wenzelm@4866
    99
wenzelm@4866
   100
    val errs = illegal_vars @ dup_lhs_tfrees @ extra_rhs_tfrees @ illegal_frees;
wenzelm@4866
   101
  in
wenzelm@4866
   102
    if null errs then ()
wenzelm@4866
   103
    else error (cat_lines errs);
wenzelm@4866
   104
wenzelm@4866
   105
    writeln("Proving nonemptiness of " ^ quote name ^ " ...");
wenzelm@4866
   106
    prove_nonempty cset (map (get_axiom thy) axms @ thms) usr_tac;
wenzelm@4866
   107
wenzelm@4866
   108
    thy
wenzelm@4866
   109
    |> Theory.add_types [(t, tlen, mx)]
wenzelm@4866
   110
    |> Theory.add_arities_i
wenzelm@4866
   111
     [(full_name tname, replicate tlen logicS, logicS),
wenzelm@4866
   112
      (full_name tname, replicate tlen HOLogic.termS, HOLogic.termS)]
wenzelm@4866
   113
    |> Theory.add_consts_i
wenzelm@4866
   114
     [(name, setT, NoSyn),
wenzelm@4866
   115
      (Rep_name, newT --> oldT, NoSyn),
wenzelm@4866
   116
      (Abs_name, oldT --> newT, NoSyn)]
wenzelm@4866
   117
    |> (PureThy.add_defs_i o map Attribute.none)
wenzelm@4866
   118
     [(name ^ "_def", Logic.mk_equals (setC, set))]
wenzelm@4866
   119
    |> (PureThy.add_axioms_i o map Attribute.none)
wenzelm@4866
   120
     [(Rep_name, rep_type),
wenzelm@4866
   121
      (Rep_name ^ "_inverse", rep_type_inv),
wenzelm@4866
   122
      (Abs_name ^ "_inverse", abs_type_inv)]
wenzelm@4866
   123
  end
wenzelm@4866
   124
  handle ERROR =>
wenzelm@4866
   125
    error ("The error(s) above occurred in typedef " ^ quote name);
wenzelm@4866
   126
wenzelm@4866
   127
wenzelm@4866
   128
(* external interfaces *)
wenzelm@4866
   129
wenzelm@4866
   130
fun read_term sg str =
wenzelm@4866
   131
  read_cterm sg (str, HOLogic.termTVar);
wenzelm@4866
   132
wenzelm@4866
   133
fun cert_term sg tm =
wenzelm@4866
   134
  cterm_of sg tm handle TERM (msg, _) => error msg;
wenzelm@4866
   135
wenzelm@4866
   136
val add_typedef = gen_add_typedef read_term;
wenzelm@4866
   137
val add_typedef_i = gen_add_typedef cert_term;
wenzelm@4866
   138
wenzelm@4866
   139
wenzelm@4866
   140
end;