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