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