src/HOL/thy_syntax.ML
author paulson
Tue, 29 Mar 2005 12:30:48 +0200
changeset 15635 8408a06590a6
parent 15570 8d8c70b41bab
child 15906 9cb0a8ffa40d
permissions -rw-r--r--
converted HOL-Subst to tactic scripts
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     1
(*  Title:      HOL/thy_syntax.ML
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     2
    ID:         $Id$
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     3
    Author:     Markus Wenzel and Lawrence C Paulson and Carsten Clasohm
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     4
12050
6934c005aec4 tuned comment;
wenzelm
parents: 11822
diff changeset
     5
Additional sections for *old-style* theory files in HOL.
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     6
*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     7
3622
85898be702b2 use ThySyn.add_syntax;
wenzelm
parents: 3617
diff changeset
     8
local
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     9
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    10
open ThyParse;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    11
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    12
1475
7f5a4cd08209 expanded tabs; renamed subtype to typedef;
clasohm
parents: 1465
diff changeset
    13
(** typedef **)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    14
1475
7f5a4cd08209 expanded tabs; renamed subtype to typedef;
clasohm
parents: 1465
diff changeset
    15
fun mk_typedef_decl (((((opt_name, vs), t), mx), rhs), wt) =
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    16
  let
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
    17
    val name' = getOpt (opt_name,t);
6642
732af87c0650 strip_quotes replaced by unenclose;
wenzelm
parents: 6576
diff changeset
    18
    val name = unenclose name';
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    19
  in
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    20
    (cat_lines [name', mk_triple (t, mk_list vs, mx), rhs, wt],
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    21
      [name ^ "_def", "Rep_" ^ name, "Rep_" ^ name ^ "_inverse",
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    22
        "Abs_" ^ name ^ "_inverse"])
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    23
  end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    24
1475
7f5a4cd08209 expanded tabs; renamed subtype to typedef;
clasohm
parents: 1465
diff changeset
    25
val typedef_decl =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14700
diff changeset
    26
  optional ("(" $$-- name --$$ ")" >> SOME) NONE --
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    27
  type_args -- name -- opt_infix --$$ "=" -- string -- opt_witness
1475
7f5a4cd08209 expanded tabs; renamed subtype to typedef;
clasohm
parents: 1465
diff changeset
    28
  >> mk_typedef_decl;
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    29
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    30
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    31
3980
21ef91734970 added record section;
wenzelm
parents: 3945
diff changeset
    32
(** record **)
21ef91734970 added record section;
wenzelm
parents: 3945
diff changeset
    33
21ef91734970 added record section;
wenzelm
parents: 3945
diff changeset
    34
val record_decl =
4225
3d9e551bc5a6 improved record syntax;
wenzelm
parents: 4204
diff changeset
    35
  (type_args -- name >> (mk_pair o apfst mk_list)) --$$ "="
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14700
diff changeset
    36
    -- optional (typ --$$ "+" >> (parens o cat "SOME")) "NONE"
12505
46bfc675015a record: mixfix;
wenzelm
parents: 12180
diff changeset
    37
    -- repeat1 ((name --$$ "::" -- !! (typ -- opt_mixfix)) >> mk_triple2)
4001
b6a3c2665c45 record: tuned output;
wenzelm
parents: 3980
diff changeset
    38
  >> (fn ((x, y), zs) => cat_lines [x, y, mk_big_list zs]);
3980
21ef91734970 added record section;
wenzelm
parents: 3945
diff changeset
    39
21ef91734970 added record section;
wenzelm
parents: 3945
diff changeset
    40
6477
e36581d04eee recdef adapted to RecdefPackage.add_recdef;
wenzelm
parents: 6426
diff changeset
    41
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    42
(** (co)inductive **)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    43
8813
abc0f3722aed added scan_to_id (used to be in Pure/section_utils.ML);
wenzelm
parents: 8657
diff changeset
    44
(*Skipping initial blanks, find the first identifier*)  (* FIXME slightly broken! *)
abc0f3722aed added scan_to_id (used to be in Pure/section_utils.ML);
wenzelm
parents: 8657
diff changeset
    45
fun scan_to_id s =
abc0f3722aed added scan_to_id (used to be in Pure/section_utils.ML);
wenzelm
parents: 8657
diff changeset
    46
    s |> Symbol.explode
abc0f3722aed added scan_to_id (used to be in Pure/section_utils.ML);
wenzelm
parents: 8657
diff changeset
    47
    |> Scan.error (Scan.finite Symbol.stopper
abc0f3722aed added scan_to_id (used to be in Pure/section_utils.ML);
wenzelm
parents: 8657
diff changeset
    48
      (Scan.!! (fn _ => "Expected to find an identifier in " ^ s)
abc0f3722aed added scan_to_id (used to be in Pure/section_utils.ML);
wenzelm
parents: 8657
diff changeset
    49
        (Scan.any Symbol.is_blank |-- Syntax.scan_id)))
abc0f3722aed added scan_to_id (used to be in Pure/section_utils.ML);
wenzelm
parents: 8657
diff changeset
    50
    |> #1;
abc0f3722aed added scan_to_id (used to be in Pure/section_utils.ML);
wenzelm
parents: 8657
diff changeset
    51
5097
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    52
fun inductive_decl coind =
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    53
  let
6426
9a2ace82b68e intrs: names and atts;
wenzelm
parents: 6381
diff changeset
    54
    val no_atts = map (mk_pair o rpair "[]");
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    55
    fun mk_intr_name (s, _) =   (*the "op" cancels any infix status*)
14672
79bac83b2c27 use ThmDatabase.is_ml_identifier;
wenzelm
parents: 14598
diff changeset
    56
      if ThmDatabase.is_ml_identifier s then "op " ^ s else "_";
12180
91c9f661b183 inductive: removed con_defs;
wenzelm
parents: 12050
diff changeset
    57
    fun mk_params ((recs, ipairs), monos) =
6642
732af87c0650 strip_quotes replaced by unenclose;
wenzelm
parents: 6576
diff changeset
    58
      let val big_rec_name = space_implode "_" (map (scan_to_id o unenclose) recs)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    59
          and srec_tms = mk_list recs
14598
7009f59711e3 Replaced quote by Library.quote, since quote now refers to Symbol.quote
berghofe
parents: 12505
diff changeset
    60
          and sintrs   = mk_big_list (no_atts (map (mk_pair o apfst Library.quote) ipairs))
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    61
      in
5097
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    62
        ";\n\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    63
        \local\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    64
        \val (thy, {defs, mono, unfold, intrs, elims, mk_cases, induct, ...}) =\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    65
        \  InductivePackage.add_inductive true " ^
11628
e57a6e51715e inductive: no collective atts;
wenzelm
parents: 9857
diff changeset
    66
        (if coind then "true " else "false ") ^ srec_tms ^
12180
91c9f661b183 inductive: removed con_defs;
wenzelm
parents: 12050
diff changeset
    67
         sintrs ^ " " ^ mk_list (no_atts monos) ^ " thy;\n\
5097
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    68
        \in\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    69
        \structure " ^ big_rec_name ^ " =\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    70
        \struct\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    71
        \  val defs = defs;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    72
        \  val mono = mono;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    73
        \  val unfold = unfold;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    74
        \  val intrs = intrs;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    75
        \  val elims = elims;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    76
        \  val elim = hd elims;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    77
        \  val " ^ (if coind then "co" else "") ^ "induct = induct;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    78
        \  val mk_cases = mk_cases;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    79
        \  val " ^ mk_list (map mk_intr_name ipairs) ^ " = intrs;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    80
        \end;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    81
        \val thy = thy;\nend;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    82
        \val thy = thy\n"
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    83
      end
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    84
    val ipairs = "intrs" $$-- repeat1 (ident -- !! string)
5716
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
    85
    fun optlist s = optional (s $$-- list1 name) []
12180
91c9f661b183 inductive: removed con_defs;
wenzelm
parents: 12050
diff changeset
    86
  in repeat1 name -- ipairs -- optlist "monos" >> mk_params end;
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    87
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    88
6477
e36581d04eee recdef adapted to RecdefPackage.add_recdef;
wenzelm
parents: 6426
diff changeset
    89
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    90
(** datatype **)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    91
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    92
local
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
    93
  (*** generate string for calling add_datatype ***)
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
    94
  (*** and bindig theorems to ML identifiers    ***)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    95
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
    96
  fun mk_bind_thms_string names =
14672
79bac83b2c27 use ThmDatabase.is_ml_identifier;
wenzelm
parents: 14598
diff changeset
    97
   (case find_first (not o ThmDatabase.is_ml_identifier) names of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14700
diff changeset
    98
      SOME id => (warning (id ^ " is not a valid identifier"); "")
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14700
diff changeset
    99
    | NONE =>
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   100
        let
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   101
          fun mk_dt_struct (s, (id, i)) =
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   102
            s ^ "structure " ^ id ^ " =\n\
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   103
            \struct\n\
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   104
            \  val distinct = List.nth (distinct, " ^ i ^ ");\n\
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   105
            \  val inject = List.nth (inject, " ^ i ^ ");\n\
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   106
            \  val exhaust = List.nth (exhaustion, " ^ i ^ ");\n\
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   107
            \  val cases = List.nth (case_thms, " ^ i ^ ");\n\
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   108
            \  val (split, split_asm) = List.nth (split_thms, " ^ i ^ ");\n" ^
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   109
              (if length names = 1 then
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   110
                 "  val induct = induction;\n\
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   111
                 \  val recs = rec_thms;\n\
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   112
                 \  val simps = simps;\n\
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   113
                 \  val size = size;\n"
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   114
               else "") ^
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   115
            "end;\n";
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   116
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   117
          val structs = Library.foldl mk_dt_struct
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   118
            ("", (names ~~ (map string_of_int (0 upto length names - 1))));
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   119
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   120
        in
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   121
          (if length names > 1 then
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   122
             "structure " ^ (space_implode "_" names) ^ " =\n\
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   123
             \struct\n\
5658
082debccf486 Changed structure of name spaces for datatypes.
berghofe
parents: 5222
diff changeset
   124
             \  val induct = induction;\n\
082debccf486 Changed structure of name spaces for datatypes.
berghofe
parents: 5222
diff changeset
   125
             \  val recs = rec_thms;\n\
082debccf486 Changed structure of name spaces for datatypes.
berghofe
parents: 5222
diff changeset
   126
             \  val simps = simps;\n\
082debccf486 Changed structure of name spaces for datatypes.
berghofe
parents: 5222
diff changeset
   127
             \  val size = size;\nend;\n"
082debccf486 Changed structure of name spaces for datatypes.
berghofe
parents: 5222
diff changeset
   128
           else "") ^ structs
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   129
        end);
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   130
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   131
  fun mk_dt_string dts =
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   132
    let
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   133
      val names = map (fn ((((alt_name, _), name), _), _) =>
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   134
        unenclose (getOpt (alt_name,name))) dts;
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   135
14598
7009f59711e3 Replaced quote by Library.quote, since quote now refers to Symbol.quote
berghofe
parents: 12505
diff changeset
   136
      val add_datatype_args = brackets (commas (map Library.quote names)) ^ " " ^
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   137
        brackets (commas (map (fn ((((_, vs), name), mx), cs) =>
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   138
          parens (brackets (commas vs) ^ ", " ^ name ^ ", " ^ mx ^ ", " ^
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   139
            brackets (commas cs))) dts));
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   140
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   141
    in
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   142
      ";\nlocal\n\
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   143
      \val (thy, {distinct, inject, exhaustion, rec_thms,\n\
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   144
      \  case_thms, split_thms, induction, size, simps}) =\n\
6103
36f272ea9413 get_tthms witness theorems;
wenzelm
parents: 6035
diff changeset
   145
      \ DatatypePackage.add_datatype false " ^ add_datatype_args ^ " thy;\n\
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   146
      \in\n" ^ mk_bind_thms_string names ^
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   147
      "val thy = thy;\nend;\nval thy = thy\n"
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   148
    end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   149
6381
ed0c7b4a325d adapted rep_datatype;
wenzelm
parents: 6356
diff changeset
   150
  fun mk_thmss namess = mk_list (map (mk_list o map (mk_pair o rpair "[]")) namess);
ed0c7b4a325d adapted rep_datatype;
wenzelm
parents: 6356
diff changeset
   151
  fun mk_thm name = mk_pair (name, "[]");
6103
36f272ea9413 get_tthms witness theorems;
wenzelm
parents: 6035
diff changeset
   152
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   153
  fun mk_rep_dt_string (((names, distinct), inject), induct) =
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   154
    ";\nlocal\n\
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   155
    \val (thy, {distinct, inject, exhaustion, rec_thms,\n\
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   156
    \  case_thms, split_thms, induction, size, simps}) =\n\
6103
36f272ea9413 get_tthms witness theorems;
wenzelm
parents: 6035
diff changeset
   157
    \ DatatypePackage.rep_datatype " ^
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   158
    (case names of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14700
diff changeset
   159
        SOME names => "(SOME [" ^ Library.commas_quote names ^ "])\n " ^
6103
36f272ea9413 get_tthms witness theorems;
wenzelm
parents: 6035
diff changeset
   160
          mk_thmss distinct ^ "\n " ^ mk_thmss inject ^
36f272ea9413 get_tthms witness theorems;
wenzelm
parents: 6035
diff changeset
   161
            "\n " ^ mk_thm induct ^ " thy;\nin\n" ^ mk_bind_thms_string names
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14700
diff changeset
   162
      | NONE => "NONE\n " ^ mk_thmss distinct ^ "\n " ^ mk_thmss inject ^
6103
36f272ea9413 get_tthms witness theorems;
wenzelm
parents: 6035
diff changeset
   163
            "\n " ^ mk_thm induct ^ " thy;\nin\n") ^
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   164
    "val thy = thy;\nend;\nval thy = thy\n";
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   165
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   166
  (*** parsers ***)
4106
wenzelm
parents: 4091
diff changeset
   167
6642
732af87c0650 strip_quotes replaced by unenclose;
wenzelm
parents: 6576
diff changeset
   168
  val simple_typ = ident || (type_var >> unenclose);
1316
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   169
1251
81fc4d8e3eda added nested types on right hand side of datatype definitions
clasohm
parents: 977
diff changeset
   170
  fun complex_typ toks =
1316
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   171
    let val typ = simple_typ || "(" $$-- complex_typ --$$ ")";
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   172
        val typ2 = complex_typ || "(" $$-- complex_typ --$$ ")";
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   173
    in
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   174
     (typ ^^ (repeat ident >> (cat "" o space_implode " ")) ||
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   175
      "(" $$-- !! (list1 typ2 >> (parens o commas)) --$$ ")" ^^ !!
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   176
        (repeat1 ident >> (cat "" o space_implode " "))) toks
1316
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   177
    end;
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   178
6642
732af87c0650 strip_quotes replaced by unenclose;
wenzelm
parents: 6576
diff changeset
   179
  val opt_typs = repeat ((string >> unenclose) ||
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   180
    simple_typ || ("(" $$-- complex_typ --$$ ")"));
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   181
  val constructor = name -- opt_typs -- opt_mixfix >> (fn ((n, Ts), mx) =>
14598
7009f59711e3 Replaced quote by Library.quote, since quote now refers to Symbol.quote
berghofe
parents: 12505
diff changeset
   182
    parens (n ^ ", " ^ brackets (Library.commas_quote Ts) ^ ", " ^ mx));
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14700
diff changeset
   183
  val opt_name = optional ("(" $$-- name --$$ ")" >> SOME) NONE
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   184
5716
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   185
  fun optlistlist s = optional (s $$-- enum "and" (list name)) [[]]
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   186
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   187
in
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   188
  val datatype_decl =
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   189
    enum1 "and" (opt_name -- type_args -- name -- opt_infix --$$ "=" --
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   190
      enum1 "|" constructor) >> mk_dt_string;
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   191
  val rep_datatype_decl =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14700
diff changeset
   192
    ((optional ((repeat1 (name >> unenclose)) >> SOME) NONE) --
5716
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   193
      optlistlist "distinct" -- optlistlist "inject" --
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   194
        ("induct" $$-- name)) >> mk_rep_dt_string;
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   195
end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   196
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   197
6477
e36581d04eee recdef adapted to RecdefPackage.add_recdef;
wenzelm
parents: 6426
diff changeset
   198
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   199
(** primrec **)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   200
8657
b9475dad85ed recdef: admit names/atts;
wenzelm
parents: 8623
diff changeset
   201
fun mk_patterns eqns = mk_list (map (fn (s, _) => if s = "" then "_" else s) eqns);
14598
7009f59711e3 Replaced quote by Library.quote, since quote now refers to Symbol.quote
berghofe
parents: 12505
diff changeset
   202
fun mk_eqns eqns = mk_list (map (fn (x, y) => mk_pair (mk_pair (Library.quote x, y), "[]")) eqns);
8657
b9475dad85ed recdef: admit names/atts;
wenzelm
parents: 8623
diff changeset
   203
5716
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   204
fun mk_primrec_decl (alt_name, eqns) =
8657
b9475dad85ed recdef: admit names/atts;
wenzelm
parents: 8623
diff changeset
   205
  ";\nval (thy, " ^ mk_patterns eqns ^ ") = PrimrecPackage.add_primrec " ^ alt_name ^ " "
b9475dad85ed recdef: admit names/atts;
wenzelm
parents: 8623
diff changeset
   206
  ^ mk_eqns eqns ^ " " ^ " thy;\n\
b9475dad85ed recdef: admit names/atts;
wenzelm
parents: 8623
diff changeset
   207
  \val thy = thy\n"
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   208
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   209
(* either names and axioms or just axioms *)
8657
b9475dad85ed recdef: admit names/atts;
wenzelm
parents: 8623
diff changeset
   210
val primrec_decl = optional ("(" $$-- name --$$ ")") "\"\"" --
b9475dad85ed recdef: admit names/atts;
wenzelm
parents: 8623
diff changeset
   211
  repeat1 (ident -- string || (string >> pair "")) >> mk_primrec_decl;
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   212
2922
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   213
6497
120ca2bb27e1 Now for recdefs that omit the WF relation
paulson
parents: 6477
diff changeset
   214
(*** recdef: interface to Slind's TFL ***)
2922
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   215
6497
120ca2bb27e1 Now for recdefs that omit the WF relation
paulson
parents: 6477
diff changeset
   216
(** TFL with explicit WF relations **)
2922
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   217
3194
36bfceef1800 TFL theory section
paulson
parents: 2930
diff changeset
   218
(*fname: name of function being defined; rel: well-founded relation*)
8657
b9475dad85ed recdef: admit names/atts;
wenzelm
parents: 8623
diff changeset
   219
fun mk_recdef_decl ((((fname, rel), congs), ss), eqns) =
9857
2f994c499bef RecdefPackage.add_recdef_old;
wenzelm
parents: 8813
diff changeset
   220
  let val fid = unenclose fname in
6477
e36581d04eee recdef adapted to RecdefPackage.add_recdef;
wenzelm
parents: 6426
diff changeset
   221
    ";\n\n\
e36581d04eee recdef adapted to RecdefPackage.add_recdef;
wenzelm
parents: 6426
diff changeset
   222
    \local\n\
8623
5668aaf41c36 mod in recdef allows to access the correct simpset via simpset().
nipkow
parents: 6642
diff changeset
   223
    \fun simpset() = Simplifier.simpset_of thy;\n\
14598
7009f59711e3 Replaced quote by Library.quote, since quote now refers to Symbol.quote
berghofe
parents: 12505
diff changeset
   224
    \val (thy, result) = thy |> RecdefPackage.add_recdef_old " ^ Library.quote fid ^ " " ^ rel ^ "\n" ^
9857
2f994c499bef RecdefPackage.add_recdef_old;
wenzelm
parents: 8813
diff changeset
   225
    mk_eqns eqns ^ "\n(" ^ ss ^ ",\n " ^ mk_list congs ^ ");\n\
6477
e36581d04eee recdef adapted to RecdefPackage.add_recdef;
wenzelm
parents: 6426
diff changeset
   226
    \in\n\
e36581d04eee recdef adapted to RecdefPackage.add_recdef;
wenzelm
parents: 6426
diff changeset
   227
    \structure " ^ fid ^ " =\n\
e36581d04eee recdef adapted to RecdefPackage.add_recdef;
wenzelm
parents: 6426
diff changeset
   228
    \struct\n\
8657
b9475dad85ed recdef: admit names/atts;
wenzelm
parents: 8623
diff changeset
   229
    \  val {simps, rules = " ^ mk_patterns eqns ^ ", induct, tcs} = result;\n\
6477
e36581d04eee recdef adapted to RecdefPackage.add_recdef;
wenzelm
parents: 6426
diff changeset
   230
    \end;\n\
e36581d04eee recdef adapted to RecdefPackage.add_recdef;
wenzelm
parents: 6426
diff changeset
   231
    \val thy = thy;\n\
e36581d04eee recdef adapted to RecdefPackage.add_recdef;
wenzelm
parents: 6426
diff changeset
   232
    \end;\n\
e36581d04eee recdef adapted to RecdefPackage.add_recdef;
wenzelm
parents: 6426
diff changeset
   233
    \val thy = thy\n"
2922
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   234
  end;
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   235
6555
17b7b88a8e3c separated recdef / defer_recdef;
wenzelm
parents: 6523
diff changeset
   236
val recdef_decl =
8657
b9475dad85ed recdef: admit names/atts;
wenzelm
parents: 8623
diff changeset
   237
  name -- string --
9857
2f994c499bef RecdefPackage.add_recdef_old;
wenzelm
parents: 8813
diff changeset
   238
    optional ("congs" $$-- list1 ident) [] --
6642
732af87c0650 strip_quotes replaced by unenclose;
wenzelm
parents: 6576
diff changeset
   239
    optional ("simpset" $$-- string >> unenclose) "simpset()" --
8657
b9475dad85ed recdef: admit names/atts;
wenzelm
parents: 8623
diff changeset
   240
    repeat1 (ident -- string || (string >> pair "")) >> mk_recdef_decl;
6555
17b7b88a8e3c separated recdef / defer_recdef;
wenzelm
parents: 6523
diff changeset
   241
6497
120ca2bb27e1 Now for recdefs that omit the WF relation
paulson
parents: 6477
diff changeset
   242
120ca2bb27e1 Now for recdefs that omit the WF relation
paulson
parents: 6477
diff changeset
   243
(** TFL with no WF relation supplied **)
120ca2bb27e1 Now for recdefs that omit the WF relation
paulson
parents: 6477
diff changeset
   244
120ca2bb27e1 Now for recdefs that omit the WF relation
paulson
parents: 6477
diff changeset
   245
(*fname: name of function being defined; rel: well-founded relation*)
6555
17b7b88a8e3c separated recdef / defer_recdef;
wenzelm
parents: 6523
diff changeset
   246
fun mk_defer_recdef_decl ((fname, congs), axms) =
17b7b88a8e3c separated recdef / defer_recdef;
wenzelm
parents: 6523
diff changeset
   247
  let
6642
732af87c0650 strip_quotes replaced by unenclose;
wenzelm
parents: 6576
diff changeset
   248
    val fid = unenclose fname;
6555
17b7b88a8e3c separated recdef / defer_recdef;
wenzelm
parents: 6523
diff changeset
   249
    val congs_text = mk_list (map (fn c => mk_pair (c, "[]")) congs);
17b7b88a8e3c separated recdef / defer_recdef;
wenzelm
parents: 6523
diff changeset
   250
    val axms_text = mk_big_list axms;
6497
120ca2bb27e1 Now for recdefs that omit the WF relation
paulson
parents: 6477
diff changeset
   251
  in
6555
17b7b88a8e3c separated recdef / defer_recdef;
wenzelm
parents: 6523
diff changeset
   252
    ";\n\n\
17b7b88a8e3c separated recdef / defer_recdef;
wenzelm
parents: 6523
diff changeset
   253
    \local\n\
14598
7009f59711e3 Replaced quote by Library.quote, since quote now refers to Symbol.quote
berghofe
parents: 12505
diff changeset
   254
    \val (thy, result) = thy |> RecdefPackage.defer_recdef " ^ Library.quote fid ^ "\n" ^
6555
17b7b88a8e3c separated recdef / defer_recdef;
wenzelm
parents: 6523
diff changeset
   255
    axms_text ^ "\n" ^ congs_text ^ ";\n\
17b7b88a8e3c separated recdef / defer_recdef;
wenzelm
parents: 6523
diff changeset
   256
    \in\n\
17b7b88a8e3c separated recdef / defer_recdef;
wenzelm
parents: 6523
diff changeset
   257
    \structure " ^ fid ^ " =\n\
17b7b88a8e3c separated recdef / defer_recdef;
wenzelm
parents: 6523
diff changeset
   258
    \struct\n\
17b7b88a8e3c separated recdef / defer_recdef;
wenzelm
parents: 6523
diff changeset
   259
    \  val {induct_rules} = result;\n\
17b7b88a8e3c separated recdef / defer_recdef;
wenzelm
parents: 6523
diff changeset
   260
    \end;\n\
17b7b88a8e3c separated recdef / defer_recdef;
wenzelm
parents: 6523
diff changeset
   261
    \val thy = thy;\n\
17b7b88a8e3c separated recdef / defer_recdef;
wenzelm
parents: 6523
diff changeset
   262
    \end;\n\
17b7b88a8e3c separated recdef / defer_recdef;
wenzelm
parents: 6523
diff changeset
   263
    \val thy = thy\n"
6497
120ca2bb27e1 Now for recdefs that omit the WF relation
paulson
parents: 6477
diff changeset
   264
  end;
120ca2bb27e1 Now for recdefs that omit the WF relation
paulson
parents: 6477
diff changeset
   265
6555
17b7b88a8e3c separated recdef / defer_recdef;
wenzelm
parents: 6523
diff changeset
   266
val defer_recdef_decl =
17b7b88a8e3c separated recdef / defer_recdef;
wenzelm
parents: 6523
diff changeset
   267
  (name --
17b7b88a8e3c separated recdef / defer_recdef;
wenzelm
parents: 6523
diff changeset
   268
    optional ("congs" $$-- list1 name) [] --
17b7b88a8e3c separated recdef / defer_recdef;
wenzelm
parents: 6523
diff changeset
   269
    repeat1 string >> mk_defer_recdef_decl);
2922
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   270
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   271
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   272
3622
85898be702b2 use ThySyn.add_syntax;
wenzelm
parents: 3617
diff changeset
   273
(** augment thy syntax **)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   274
3622
85898be702b2 use ThySyn.add_syntax;
wenzelm
parents: 3617
diff changeset
   275
in
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   276
3622
85898be702b2 use ThySyn.add_syntax;
wenzelm
parents: 3617
diff changeset
   277
val _ = ThySyn.add_syntax
12180
91c9f661b183 inductive: removed con_defs;
wenzelm
parents: 12050
diff changeset
   278
 ["intrs", "monos", "congs", "simpset", "|", "and", "distinct", "inject", "induct"]
11822
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11804
diff changeset
   279
 [axm_section "typedef" "|> TypedefPackage.add_typedef_x" typedef_decl,
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14672
diff changeset
   280
  section "record" "|> RecordPackage.add_record" record_decl,
6497
120ca2bb27e1 Now for recdefs that omit the WF relation
paulson
parents: 6477
diff changeset
   281
  section "inductive" 	"" (inductive_decl false),
120ca2bb27e1 Now for recdefs that omit the WF relation
paulson
parents: 6477
diff changeset
   282
  section "coinductive"	"" (inductive_decl true),
120ca2bb27e1 Now for recdefs that omit the WF relation
paulson
parents: 6477
diff changeset
   283
  section "datatype" 	"" datatype_decl,
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   284
  section "rep_datatype" "" rep_datatype_decl,
6497
120ca2bb27e1 Now for recdefs that omit the WF relation
paulson
parents: 6477
diff changeset
   285
  section "primrec" 	"" primrec_decl,
6555
17b7b88a8e3c separated recdef / defer_recdef;
wenzelm
parents: 6523
diff changeset
   286
  section "recdef" 	"" recdef_decl,
17b7b88a8e3c separated recdef / defer_recdef;
wenzelm
parents: 6523
diff changeset
   287
  section "defer_recdef" "" defer_recdef_decl];
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   288
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   289
end;