src/HOL/Tools/typedef_package.ML
author wenzelm
Thu Jan 19 21:22:08 2006 +0100 (2006-01-19 ago)
changeset 18708 4b3dadb4fe33
parent 18678 dd0c569fa43d
child 18728 6790126ab5f6
permissions -rw-r--r--
setup: theory -> theory;
wenzelm@4866
     1
(*  Title:      HOL/Tools/typedef_package.ML
wenzelm@4866
     2
    ID:         $Id$
wenzelm@16458
     3
    Author:     Markus Wenzel and Stefan Berghofer, TU Muenchen
wenzelm@4866
     4
wenzelm@6357
     5
Gordon/HOL-style type definitions.
wenzelm@4866
     6
*)
wenzelm@4866
     7
wenzelm@4866
     8
signature TYPEDEF_PACKAGE =
wenzelm@4866
     9
sig
wenzelm@5697
    10
  val quiet_mode: bool ref
wenzelm@5104
    11
  val add_typedecls: (bstring * string list * mixfix) list -> theory -> theory
wenzelm@11827
    12
  val add_typedef: bool -> string option -> bstring * string list * mixfix ->
wenzelm@11822
    13
    string -> (bstring * bstring) option -> tactic -> theory -> theory *
wenzelm@11822
    14
    {type_definition: thm, set_def: thm option, Rep: thm, Rep_inverse: thm,
wenzelm@11822
    15
      Abs_inverse: thm, Rep_inject: thm, Abs_inject: thm, Rep_cases: thm, Abs_cases: thm,
wenzelm@11822
    16
      Rep_induct: thm, Abs_induct: thm}
wenzelm@11827
    17
  val add_typedef_i: bool -> string option -> bstring * string list * mixfix ->
wenzelm@11822
    18
    term -> (bstring * bstring) option -> tactic -> theory -> theory *
wenzelm@11822
    19
    {type_definition: thm, set_def: thm option, Rep: thm, Rep_inverse: thm,
wenzelm@11822
    20
      Abs_inverse: thm, Rep_inject: thm, Abs_inject: thm, Rep_cases: thm, Abs_cases: thm,
wenzelm@11822
    21
      Rep_induct: thm, Abs_induct: thm}
wenzelm@17339
    22
  val typedef: (bool * string) * (bstring * string list * mixfix) * string
wenzelm@17339
    23
    * (string * string) option -> theory -> Proof.state
wenzelm@17339
    24
  val typedef_i: (bool * string) * (bstring * string list * mixfix) * term
wenzelm@17339
    25
    * (string * string) option -> theory -> Proof.state
wenzelm@18708
    26
  val setup: theory -> theory
wenzelm@4866
    27
end;
wenzelm@4866
    28
wenzelm@4866
    29
structure TypedefPackage: TYPEDEF_PACKAGE =
wenzelm@4866
    30
struct
wenzelm@4866
    31
wenzelm@4866
    32
wenzelm@10280
    33
(** theory context references **)
wenzelm@10280
    34
wenzelm@11608
    35
val type_definitionN = "Typedef.type_definition";
wenzelm@10280
    36
wenzelm@13413
    37
val Rep = thm "type_definition.Rep";
wenzelm@13413
    38
val Rep_inverse = thm "type_definition.Rep_inverse";
wenzelm@13413
    39
val Abs_inverse = thm "type_definition.Abs_inverse";
wenzelm@13413
    40
val Rep_inject = thm "type_definition.Rep_inject";
wenzelm@13413
    41
val Abs_inject = thm "type_definition.Abs_inject";
wenzelm@13413
    42
val Rep_cases = thm "type_definition.Rep_cases";
wenzelm@13413
    43
val Abs_cases = thm "type_definition.Abs_cases";
wenzelm@13413
    44
val Rep_induct = thm "type_definition.Rep_induct";
wenzelm@13413
    45
val Abs_induct = thm "type_definition.Abs_induct";
wenzelm@10280
    46
wenzelm@10280
    47
wenzelm@10280
    48
wenzelm@17922
    49
(** type declarations **)
wenzelm@17922
    50
wenzelm@17922
    51
fun add_typedecls decls thy =
wenzelm@17922
    52
  let
wenzelm@17922
    53
    fun arity_of (raw_name, args, mx) =
wenzelm@17922
    54
      (Sign.full_name thy (Syntax.type_name raw_name mx),
wenzelm@17922
    55
        replicate (length args) HOLogic.typeS, HOLogic.typeS);
wenzelm@17922
    56
  in
wenzelm@17922
    57
    thy
wenzelm@17922
    58
    |> Theory.add_typedecls decls
wenzelm@17922
    59
    |> can (Theory.assert_super HOL.thy) ? Theory.add_arities_i (map arity_of decls)
wenzelm@17922
    60
  end;
wenzelm@17922
    61
wenzelm@17922
    62
wenzelm@17922
    63
wenzelm@17922
    64
(** type definitions **)
wenzelm@17922
    65
wenzelm@17922
    66
(* messages *)
wenzelm@17922
    67
wenzelm@17922
    68
val quiet_mode = ref false;
wenzelm@17922
    69
fun message s = if ! quiet_mode then () else writeln s;
wenzelm@17922
    70
wenzelm@17922
    71
wenzelm@17922
    72
(* theory data *)
berghofe@15259
    73
wenzelm@16458
    74
structure TypedefData = TheoryDataFun
wenzelm@16458
    75
(struct
berghofe@15259
    76
  val name = "HOL/typedef";
berghofe@15259
    77
  type T = (typ * typ * string * string) Symtab.table;
berghofe@15259
    78
  val empty = Symtab.empty;
berghofe@15259
    79
  val copy = I;
wenzelm@16458
    80
  val extend = I;
wenzelm@16458
    81
  fun merge _ (tabs: T * T) = Symtab.merge (op =) tabs;
wenzelm@16458
    82
  fun print _ _ = ();
wenzelm@16458
    83
end);
berghofe@15259
    84
wenzelm@17280
    85
fun put_typedef newT oldT Abs_name Rep_name =
wenzelm@17412
    86
  TypedefData.map (Symtab.update_new (fst (dest_Type newT), (newT, oldT, Abs_name, Rep_name)));
berghofe@15259
    87
berghofe@15259
    88
wenzelm@6383
    89
(* prepare_typedef *)
wenzelm@6383
    90
wenzelm@16458
    91
fun read_term thy used s =
wenzelm@16458
    92
  #1 (Thm.read_def_cterm (thy, K NONE, K NONE) used true (s, HOLogic.typeT));
wenzelm@4866
    93
wenzelm@16458
    94
fun cert_term thy _ t = Thm.cterm_of thy t handle TERM (msg, _) => error msg;
wenzelm@6383
    95
wenzelm@18678
    96
fun err_in_typedef msg name =
wenzelm@18678
    97
  cat_error msg ("The error(s) above occurred in typedef " ^ quote name);
wenzelm@6383
    98
wenzelm@11822
    99
fun prepare_typedef prep_term def name (t, vs, mx) raw_set opt_morphs thy =
wenzelm@4866
   100
  let
wenzelm@11608
   101
    val _ = Theory.requires thy "Typedef" "typedefs";
wenzelm@16458
   102
    val full = Sign.full_name thy;
wenzelm@4866
   103
wenzelm@4866
   104
    (*rhs*)
wenzelm@10280
   105
    val full_name = full name;
wenzelm@16458
   106
    val cset = prep_term thy vs raw_set;
wenzelm@6383
   107
    val {T = setT, t = set, ...} = Thm.rep_cterm cset;
wenzelm@17280
   108
    val rhs_tfrees = Term.add_tfrees set [];
wenzelm@17280
   109
    val rhs_tfreesT = Term.add_tfreesT setT [];
wenzelm@4866
   110
    val oldT = HOLogic.dest_setT setT handle TYPE _ =>
wenzelm@16458
   111
      error ("Not a set type: " ^ quote (Sign.string_of_typ thy setT));
wenzelm@11426
   112
    fun mk_nonempty A =
wenzelm@11426
   113
      HOLogic.mk_Trueprop (HOLogic.mk_exists ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), A)));
wenzelm@11426
   114
    val goal = mk_nonempty set;
wenzelm@17280
   115
    val goal_pat = mk_nonempty (Var (if_none (Syntax.read_variable name) (name, 0), setT));
wenzelm@4866
   116
wenzelm@4866
   117
    (*lhs*)
wenzelm@17280
   118
    val defS = Sign.defaultS thy;
wenzelm@17280
   119
    val lhs_tfrees = map (fn v => (v, if_none (AList.lookup (op =) rhs_tfrees v) defS)) vs;
wenzelm@17280
   120
    val args_setT = lhs_tfrees
wenzelm@17280
   121
      |> filter (member (op =) rhs_tfrees andf (not o member (op =) rhs_tfreesT))
wenzelm@17280
   122
      |> map TFree;
wenzelm@17280
   123
wenzelm@4866
   124
    val tname = Syntax.type_name t mx;
wenzelm@10280
   125
    val full_tname = full tname;
wenzelm@10280
   126
    val newT = Type (full_tname, map TFree lhs_tfrees);
wenzelm@4866
   127
wenzelm@17280
   128
    val (Rep_name, Abs_name) = if_none opt_morphs ("Rep_" ^ name, "Abs_" ^ name);
wenzelm@17280
   129
    val setT' = map itselfT args_setT ---> setT;
wenzelm@17280
   130
    val setC = Term.list_comb (Const (full_name, setT'), map Logic.mk_type args_setT);
wenzelm@10280
   131
    val RepC = Const (full Rep_name, newT --> oldT);
wenzelm@10280
   132
    val AbsC = Const (full Abs_name, oldT --> newT);
wenzelm@4866
   133
    val x_new = Free ("x", newT);
wenzelm@4866
   134
    val y_old = Free ("y", oldT);
wenzelm@10280
   135
wenzelm@11822
   136
    val set' = if def then setC else set;
wenzelm@4866
   137
wenzelm@10280
   138
    val typedef_name = "type_definition_" ^ name;
wenzelm@10280
   139
    val typedefC =
wenzelm@10280
   140
      Const (type_definitionN, (newT --> oldT) --> (oldT --> newT) --> setT --> HOLogic.boolT);
wenzelm@11426
   141
    val typedef_prop =
wenzelm@11426
   142
      Logic.mk_implies (goal, HOLogic.mk_Trueprop (typedefC $ RepC $ AbsC $ set'));
wenzelm@4866
   143
haftmann@18358
   144
    fun add_def def def' thy =
haftmann@18358
   145
      if def
haftmann@18358
   146
      then
haftmann@18358
   147
        thy
haftmann@18358
   148
        |> PureThy.add_defs_i false [Thm.no_attributes def']
haftmann@18358
   149
        |-> (fn [def'] => pair (SOME def'))
haftmann@18358
   150
      else
haftmann@18358
   151
        (NONE, thy);
haftmann@18358
   152
wenzelm@11822
   153
    fun typedef_result (theory, nonempty) =
wenzelm@6383
   154
      theory
berghofe@15259
   155
      |> put_typedef newT oldT (full Abs_name) (full Rep_name)
wenzelm@6383
   156
      |> add_typedecls [(t, vs, mx)]
wenzelm@6383
   157
      |> Theory.add_consts_i
wenzelm@17280
   158
       ((if def then [(name, setT', NoSyn)] else []) @
wenzelm@6383
   159
        [(Rep_name, newT --> oldT, NoSyn),
wenzelm@6383
   160
         (Abs_name, oldT --> newT, NoSyn)])
haftmann@18358
   161
      |> add_def def (Logic.mk_defpair (setC, set))
haftmann@18377
   162
      ||>> PureThy.add_axioms_i [((typedef_name, typedef_prop),
haftmann@18377
   163
          [apsnd (fn cond_axm => Drule.standard (nonempty RS cond_axm))])]
haftmann@18358
   164
      ||> Theory.add_finals_i false [RepC, AbsC]
haftmann@18358
   165
      |-> (fn (set_def, [type_definition]) => fn theory' =>
wenzelm@11822
   166
        let
wenzelm@11822
   167
          fun make th = Drule.standard (th OF [type_definition]);
haftmann@18377
   168
          val ([Rep, Rep_inverse, Abs_inverse, Rep_inject, Abs_inject,
haftmann@18377
   169
              Rep_cases, Abs_cases, Rep_induct, Abs_induct], theory'') =
wenzelm@12338
   170
            theory'
wenzelm@12338
   171
            |> Theory.add_path name
wenzelm@12338
   172
            |> PureThy.add_thms
wenzelm@11822
   173
              ([((Rep_name, make Rep), []),
wenzelm@11822
   174
                ((Rep_name ^ "_inverse", make Rep_inverse), []),
wenzelm@11822
   175
                ((Abs_name ^ "_inverse", make Abs_inverse), []),
wenzelm@11822
   176
                ((Rep_name ^ "_inject", make Rep_inject), []),
wenzelm@11822
   177
                ((Abs_name ^ "_inject", make Abs_inject), []),
wenzelm@11822
   178
                ((Rep_name ^ "_cases", make Rep_cases),
wenzelm@18643
   179
                  [RuleCases.case_names [Rep_name],
wenzelm@18643
   180
                    Attrib.theory (InductAttrib.cases_set full_name)]),
wenzelm@11822
   181
                ((Abs_name ^ "_cases", make Abs_cases),
wenzelm@18643
   182
                  [RuleCases.case_names [Abs_name],
wenzelm@18643
   183
                    Attrib.theory (InductAttrib.cases_type full_tname)]),
wenzelm@11822
   184
                ((Rep_name ^ "_induct", make Rep_induct),
wenzelm@18643
   185
                  [RuleCases.case_names [Rep_name],
wenzelm@18643
   186
                    Attrib.theory (InductAttrib.induct_set full_name)]),
wenzelm@11822
   187
                ((Abs_name ^ "_induct", make Abs_induct),
wenzelm@18643
   188
                  [RuleCases.case_names [Abs_name],
wenzelm@18643
   189
                    Attrib.theory (InductAttrib.induct_type full_tname)])])
haftmann@18377
   190
            ||> Theory.parent_path;
wenzelm@11822
   191
          val result = {type_definition = type_definition, set_def = set_def,
wenzelm@11822
   192
            Rep = Rep, Rep_inverse = Rep_inverse, Abs_inverse = Abs_inverse,
wenzelm@11822
   193
            Rep_inject = Rep_inject, Abs_inject = Abs_inject, Rep_cases = Rep_cases,
wenzelm@11822
   194
            Abs_cases = Abs_cases, Rep_induct = Rep_induct, Abs_induct = Abs_induct};
wenzelm@11822
   195
        in ((theory'', type_definition), result) end);
wenzelm@6383
   196
wenzelm@4866
   197
wenzelm@4866
   198
    (* errors *)
wenzelm@4866
   199
wenzelm@4866
   200
    fun show_names pairs = commas_quote (map fst pairs);
wenzelm@4866
   201
wenzelm@4866
   202
    val illegal_vars =
wenzelm@4866
   203
      if null (term_vars set) andalso null (term_tvars set) then []
wenzelm@4866
   204
      else ["Illegal schematic variable(s) on rhs"];
wenzelm@4866
   205
wenzelm@4866
   206
    val dup_lhs_tfrees =
wenzelm@4866
   207
      (case duplicates lhs_tfrees of [] => []
wenzelm@4866
   208
      | dups => ["Duplicate type variables on lhs: " ^ show_names dups]);
wenzelm@4866
   209
wenzelm@4866
   210
    val extra_rhs_tfrees =
wenzelm@17280
   211
      (case fold (remove (op =)) lhs_tfrees rhs_tfrees of [] => []
wenzelm@4866
   212
      | extras => ["Extra type variables on rhs: " ^ show_names extras]);
wenzelm@4866
   213
wenzelm@4866
   214
    val illegal_frees =
wenzelm@4866
   215
      (case term_frees set of [] => []
wenzelm@4866
   216
      | xs => ["Illegal variables on rhs: " ^ show_names (map dest_Free xs)]);
wenzelm@4866
   217
wenzelm@4866
   218
    val errs = illegal_vars @ dup_lhs_tfrees @ extra_rhs_tfrees @ illegal_frees;
wenzelm@11426
   219
    val _ = if null errs then () else error (cat_lines errs);
wenzelm@11426
   220
wenzelm@11426
   221
    (*test theory errors now!*)
wenzelm@11426
   222
    val test_thy = Theory.copy thy;
wenzelm@11727
   223
    val _ = (test_thy,
wenzelm@11822
   224
      setmp quick_and_dirty true (SkipProof.make_thm test_thy) goal) |> typedef_result;
wenzelm@11426
   225
wenzelm@11822
   226
  in (cset, goal, goal_pat, typedef_result) end
wenzelm@18678
   227
  handle ERROR msg => err_in_typedef msg name;
wenzelm@4866
   228
wenzelm@4866
   229
wenzelm@6383
   230
(* add_typedef interfaces *)
wenzelm@4866
   231
wenzelm@17339
   232
local
wenzelm@17339
   233
wenzelm@17922
   234
fun gen_typedef prep_term def opt_name typ set opt_morphs tac thy =
wenzelm@6383
   235
  let
wenzelm@17922
   236
    val name = the_default (#1 typ) opt_name;
wenzelm@11822
   237
    val (cset, goal, _, typedef_result) =
wenzelm@11822
   238
      prepare_typedef prep_term def name typ set opt_morphs thy;
wenzelm@17922
   239
    val _ = message ("Proving non-emptiness of set " ^ quote (string_of_cterm cset) ^ " ...");
wenzelm@18678
   240
    val non_empty = Goal.prove thy [] [] goal (K tac) handle ERROR msg =>
wenzelm@18678
   241
      cat_error msg ("Failed to prove non-emptiness of " ^ quote (string_of_cterm cset));
wenzelm@11822
   242
    val ((thy', _), result) = (thy, non_empty) |> typedef_result;
wenzelm@11822
   243
  in (thy', result) end;
wenzelm@4866
   244
wenzelm@17339
   245
in
wenzelm@17339
   246
wenzelm@17922
   247
val add_typedef = gen_typedef read_term;
wenzelm@17922
   248
val add_typedef_i = gen_typedef cert_term;
wenzelm@4866
   249
wenzelm@17339
   250
end;
wenzelm@4866
   251
wenzelm@17339
   252
wenzelm@17339
   253
(* Isar typedef interface *)
wenzelm@6383
   254
wenzelm@17339
   255
local
wenzelm@17339
   256
wenzelm@17339
   257
fun gen_typedef prep_term ((def, name), typ, set, opt_morphs) thy =
wenzelm@11822
   258
  let
wenzelm@11822
   259
    val (_, goal, goal_pat, att_result) =
wenzelm@13443
   260
      prepare_typedef prep_term def name typ set opt_morphs thy;
wenzelm@11822
   261
    val att = #1 o att_result;
wenzelm@17339
   262
  in IsarThy.theorem_i Drule.internalK ("", [att]) (goal, ([goal_pat], [])) thy end;
wenzelm@17339
   263
wenzelm@17339
   264
in
wenzelm@6383
   265
wenzelm@17339
   266
val typedef = gen_typedef read_term;
wenzelm@17339
   267
val typedef_i = gen_typedef cert_term;
wenzelm@17339
   268
wenzelm@17339
   269
end;
wenzelm@6357
   270
wenzelm@6357
   271
wenzelm@6383
   272
berghofe@15259
   273
(** trivial code generator **)
berghofe@15259
   274
berghofe@17144
   275
fun typedef_codegen thy defs gr dep module brack t =
berghofe@15259
   276
  let
berghofe@16645
   277
    fun get_name (Type (tname, _)) = tname
berghofe@16645
   278
      | get_name _ = "";
berghofe@15259
   279
    fun mk_fun s T ts =
berghofe@15259
   280
      let
berghofe@17144
   281
        val (gr', _) = Codegen.invoke_tycodegen thy defs dep module false (gr, T);
berghofe@15259
   282
        val (gr'', ps) =
berghofe@17144
   283
          foldl_map (Codegen.invoke_codegen thy defs dep module true) (gr', ts);
berghofe@17144
   284
        val id = Codegen.mk_qual_id module (Codegen.get_const_id s gr'')
skalberg@15531
   285
      in SOME (gr'', Codegen.mk_app brack (Pretty.str id) ps) end;
wenzelm@17261
   286
    fun lookup f T =
wenzelm@17412
   287
      (case Symtab.lookup (TypedefData.get thy) (get_name T) of
wenzelm@17261
   288
        NONE => ""
wenzelm@17261
   289
      | SOME s => f s);
berghofe@15259
   290
  in
berghofe@15259
   291
    (case strip_comb t of
berghofe@15259
   292
       (Const (s, Type ("fun", [T, U])), ts) =>
berghofe@15259
   293
         if lookup #4 T = s andalso
berghofe@15259
   294
           is_none (Codegen.get_assoc_type thy (get_name T))
berghofe@15259
   295
         then mk_fun s T ts
berghofe@15259
   296
         else if lookup #3 U = s andalso
berghofe@15259
   297
           is_none (Codegen.get_assoc_type thy (get_name U))
berghofe@15259
   298
         then mk_fun s U ts
skalberg@15531
   299
         else NONE
skalberg@15531
   300
     | _ => NONE)
berghofe@15259
   301
  end;
berghofe@15259
   302
berghofe@15259
   303
fun mk_tyexpr [] s = Pretty.str s
berghofe@15259
   304
  | mk_tyexpr [p] s = Pretty.block [p, Pretty.str (" " ^ s)]
berghofe@15259
   305
  | mk_tyexpr ps s = Pretty.list "(" (") " ^ s) ps;
berghofe@15259
   306
berghofe@17144
   307
fun typedef_tycodegen thy defs gr dep module brack (Type (s, Ts)) =
wenzelm@17412
   308
      (case Symtab.lookup (TypedefData.get thy) s of
skalberg@15531
   309
         NONE => NONE
skalberg@15531
   310
       | SOME (newT as Type (tname, Us), oldT, Abs_name, Rep_name) =>
wenzelm@17280
   311
           if is_some (Codegen.get_assoc_type thy tname) then NONE else
berghofe@15259
   312
           let
berghofe@17144
   313
             val module' = Codegen.if_library
berghofe@17144
   314
               (Codegen.thyname_of_type tname thy) module;
berghofe@17144
   315
             val node_id = tname ^ " (type)";
berghofe@17144
   316
             val (gr', (((qs, (_, Abs_id)), (_, Rep_id)), ty_id)) = foldl_map
berghofe@17144
   317
                 (Codegen.invoke_tycodegen thy defs dep module (length Ts = 1))
berghofe@17144
   318
                   (gr, Ts) |>>>
berghofe@17144
   319
               Codegen.mk_const_id module' Abs_name |>>>
berghofe@17144
   320
               Codegen.mk_const_id module' Rep_name |>>>
berghofe@17144
   321
               Codegen.mk_type_id module' s;
berghofe@17144
   322
             val tyexpr = mk_tyexpr qs (Codegen.mk_qual_id module ty_id)
berghofe@17144
   323
           in SOME (case try (Codegen.get_node gr') node_id of
berghofe@17144
   324
               NONE =>
berghofe@15259
   325
               let
berghofe@15259
   326
                 val (gr'', p :: ps) = foldl_map
berghofe@17144
   327
                   (Codegen.invoke_tycodegen thy defs node_id module' false)
berghofe@17144
   328
                   (Codegen.add_edge (node_id, dep)
berghofe@17144
   329
                      (Codegen.new_node (node_id, (NONE, "", "")) gr'), oldT :: Us);
berghofe@15259
   330
                 val s =
berghofe@15259
   331
                   Pretty.string_of (Pretty.block [Pretty.str "datatype ",
berghofe@17144
   332
                     mk_tyexpr ps (snd ty_id),
berghofe@15259
   333
                     Pretty.str " =", Pretty.brk 1, Pretty.str (Abs_id ^ " of"),
berghofe@15259
   334
                     Pretty.brk 1, p, Pretty.str ";"]) ^ "\n\n" ^
berghofe@15259
   335
                   Pretty.string_of (Pretty.block [Pretty.str ("fun " ^ Rep_id),
berghofe@15259
   336
                     Pretty.brk 1, Pretty.str ("(" ^ Abs_id), Pretty.brk 1,
berghofe@15259
   337
                     Pretty.str "x) = x;"]) ^ "\n\n" ^
berghofe@15259
   338
                   (if "term_of" mem !Codegen.mode then
berghofe@15259
   339
                      Pretty.string_of (Pretty.block [Pretty.str "fun ",
berghofe@17144
   340
                        Codegen.mk_term_of gr'' module' false newT, Pretty.brk 1,
berghofe@15259
   341
                        Pretty.str ("(" ^ Abs_id), Pretty.brk 1,
berghofe@15259
   342
                        Pretty.str "x) =", Pretty.brk 1,
berghofe@15259
   343
                        Pretty.block [Pretty.str ("Const (\"" ^ Abs_name ^ "\","),
berghofe@15259
   344
                          Pretty.brk 1, Codegen.mk_type false (oldT --> newT),
berghofe@15259
   345
                          Pretty.str ")"], Pretty.str " $", Pretty.brk 1,
berghofe@17144
   346
                        Codegen.mk_term_of gr'' module' false oldT, Pretty.brk 1,
berghofe@15259
   347
                        Pretty.str "x;"]) ^ "\n\n"
berghofe@15259
   348
                    else "") ^
berghofe@15259
   349
                   (if "test" mem !Codegen.mode then
berghofe@15259
   350
                      Pretty.string_of (Pretty.block [Pretty.str "fun ",
berghofe@17144
   351
                        Codegen.mk_gen gr'' module' false [] "" newT, Pretty.brk 1,
berghofe@15259
   352
                        Pretty.str "i =", Pretty.brk 1,
berghofe@15259
   353
                        Pretty.block [Pretty.str (Abs_id ^ " ("),
berghofe@17144
   354
                          Codegen.mk_gen gr'' module' false [] "" oldT, Pretty.brk 1,
berghofe@15259
   355
                          Pretty.str "i);"]]) ^ "\n\n"
berghofe@15259
   356
                    else "")
berghofe@17144
   357
               in Codegen.map_node node_id (K (NONE, module', s)) gr'' end
berghofe@17144
   358
             | SOME _ => Codegen.add_edge (node_id, dep) gr', tyexpr)
berghofe@15259
   359
           end)
berghofe@17144
   360
  | typedef_tycodegen thy defs gr dep module brack _ = NONE;
berghofe@15259
   361
berghofe@15259
   362
val setup =
wenzelm@18708
   363
  TypedefData.init #>
wenzelm@18708
   364
  Codegen.add_codegen "typedef" typedef_codegen #>
wenzelm@18708
   365
  Codegen.add_tycodegen "typedef" typedef_tycodegen;
berghofe@15259
   366
berghofe@15259
   367
berghofe@15259
   368
wenzelm@6383
   369
(** outer syntax **)
wenzelm@6383
   370
wenzelm@17057
   371
local structure P = OuterParse and K = OuterKeyword in
wenzelm@6383
   372
wenzelm@6357
   373
val typedeclP =
wenzelm@12624
   374
  OuterSyntax.command "typedecl" "type declaration (HOL)" K.thy_decl
wenzelm@12876
   375
    (P.type_args -- P.name -- P.opt_infix >> (fn ((vs, t), mx) =>
wenzelm@6357
   376
      Toplevel.theory (add_typedecls [(t, vs, mx)])));
wenzelm@6357
   377
wenzelm@6723
   378
wenzelm@17339
   379
val typedef_decl =
wenzelm@16126
   380
  Scan.optional (P.$$$ "(" |--
wenzelm@16126
   381
      ((P.$$$ "open" >> K false) -- Scan.option P.name || P.name >> (fn s => (true, SOME s)))
wenzelm@16126
   382
        --| P.$$$ ")") (true, NONE) --
wenzelm@11744
   383
    (P.type_args -- P.name) -- P.opt_infix -- (P.$$$ "=" |-- P.term) --
wenzelm@12876
   384
    Scan.option (P.$$$ "morphisms" |-- P.!!! (P.name -- P.name));
wenzelm@6357
   385
wenzelm@17339
   386
fun mk_typedef ((((((def, opt_name), (vs, t)), mx), A), morphs)) =
wenzelm@17339
   387
  typedef ((def, if_none opt_name (Syntax.type_name t mx)), (t, vs, mx), A, morphs);
wenzelm@6357
   388
wenzelm@6357
   389
val typedefP =
wenzelm@6723
   390
  OuterSyntax.command "typedef" "HOL type definition (requires non-emptiness proof)" K.thy_goal
wenzelm@17339
   391
    (typedef_decl >> (Toplevel.print oo (Toplevel.theory_to_proof o mk_typedef)));
wenzelm@6357
   392
wenzelm@6723
   393
wenzelm@11744
   394
val _ = OuterSyntax.add_keywords ["morphisms"];
wenzelm@6357
   395
val _ = OuterSyntax.add_parsers [typedeclP, typedefP];
wenzelm@6357
   396
wenzelm@4866
   397
end;
wenzelm@6383
   398
wenzelm@6383
   399
end;