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