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