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