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