src/HOL/thy_syntax.ML
author wenzelm
Sat Apr 01 20:21:39 2000 +0200 (2000-04-01)
changeset 8657 b9475dad85ed
parent 8623 5668aaf41c36
child 8813 abc0f3722aed
permissions -rw-r--r--
recdef: admit names/atts;
clasohm@923
     1
(*  Title:      HOL/thy_syntax.ML
clasohm@923
     2
    ID:         $Id$
clasohm@923
     3
    Author:     Markus Wenzel and Lawrence C Paulson and Carsten Clasohm
clasohm@923
     4
clasohm@923
     5
Additional theory file sections for HOL.
clasohm@923
     6
*)
clasohm@923
     7
wenzelm@3622
     8
local
clasohm@923
     9
clasohm@923
    10
open ThyParse;
clasohm@923
    11
clasohm@923
    12
clasohm@1475
    13
(** typedef **)
clasohm@923
    14
clasohm@1475
    15
fun mk_typedef_decl (((((opt_name, vs), t), mx), rhs), wt) =
clasohm@923
    16
  let
clasohm@923
    17
    val name' = if_none opt_name t;
wenzelm@6642
    18
    val name = unenclose name';
clasohm@923
    19
  in
clasohm@923
    20
    (cat_lines [name', mk_triple (t, mk_list vs, mx), rhs, wt],
clasohm@923
    21
      [name ^ "_def", "Rep_" ^ name, "Rep_" ^ name ^ "_inverse",
clasohm@923
    22
        "Abs_" ^ name ^ "_inverse"])
clasohm@923
    23
  end;
clasohm@923
    24
clasohm@1475
    25
val typedef_decl =
clasohm@923
    26
  optional ("(" $$-- name --$$ ")" >> Some) None --
clasohm@923
    27
  type_args -- name -- opt_infix --$$ "=" -- string -- opt_witness
clasohm@1475
    28
  >> mk_typedef_decl;
clasohm@923
    29
clasohm@923
    30
clasohm@923
    31
wenzelm@3980
    32
(** record **)
wenzelm@3980
    33
wenzelm@3980
    34
val record_decl =
wenzelm@4225
    35
  (type_args -- name >> (mk_pair o apfst mk_list)) --$$ "="
wenzelm@4226
    36
    -- optional (typ --$$ "+" >> (parens o cat "Some")) "None"
wenzelm@4225
    37
    -- repeat1 ((name --$$ "::" -- typ) >> mk_pair)
wenzelm@4001
    38
  >> (fn ((x, y), zs) => cat_lines [x, y, mk_big_list zs]);
wenzelm@3980
    39
wenzelm@3980
    40
wenzelm@6477
    41
clasohm@923
    42
(** (co)inductive **)
clasohm@923
    43
berghofe@5097
    44
fun inductive_decl coind =
clasohm@923
    45
  let
wenzelm@6426
    46
    val no_atts = map (mk_pair o rpair "[]");
clasohm@923
    47
    fun mk_intr_name (s, _) =   (*the "op" cancels any infix status*)
clasohm@923
    48
      if Syntax.is_identifier s then "op " ^ s else "_";
clasohm@923
    49
    fun mk_params (((recs, ipairs), monos), con_defs) =
wenzelm@6642
    50
      let val big_rec_name = space_implode "_" (map (scan_to_id o unenclose) recs)
clasohm@923
    51
          and srec_tms = mk_list recs
wenzelm@6426
    52
          and sintrs   = mk_big_list (no_atts (map (mk_pair o apfst quote) ipairs))
clasohm@923
    53
      in
berghofe@5097
    54
        ";\n\n\
berghofe@5097
    55
        \local\n\
berghofe@5097
    56
        \val (thy, {defs, mono, unfold, intrs, elims, mk_cases, induct, ...}) =\n\
berghofe@5097
    57
        \  InductivePackage.add_inductive true " ^
wenzelm@6523
    58
        (if coind then "true " else "false ") ^ srec_tms ^ " [] " ^
wenzelm@6426
    59
         sintrs ^ " " ^ mk_list (no_atts monos) ^ " " ^ mk_list (no_atts con_defs) ^ " thy;\n\
berghofe@5097
    60
        \in\n\
berghofe@5097
    61
        \structure " ^ big_rec_name ^ " =\n\
berghofe@5097
    62
        \struct\n\
berghofe@5097
    63
        \  val defs = defs;\n\
berghofe@5097
    64
        \  val mono = mono;\n\
berghofe@5097
    65
        \  val unfold = unfold;\n\
berghofe@5097
    66
        \  val intrs = intrs;\n\
berghofe@5097
    67
        \  val elims = elims;\n\
berghofe@5097
    68
        \  val elim = hd elims;\n\
berghofe@5097
    69
        \  val " ^ (if coind then "co" else "") ^ "induct = induct;\n\
berghofe@5097
    70
        \  val mk_cases = mk_cases;\n\
berghofe@5097
    71
        \  val " ^ mk_list (map mk_intr_name ipairs) ^ " = intrs;\n\
berghofe@5097
    72
        \end;\n\
berghofe@5097
    73
        \val thy = thy;\nend;\n\
berghofe@5097
    74
        \val thy = thy\n"
clasohm@923
    75
      end
clasohm@923
    76
    val ipairs = "intrs" $$-- repeat1 (ident -- !! string)
berghofe@5716
    77
    fun optlist s = optional (s $$-- list1 name) []
clasohm@923
    78
  in
berghofe@5716
    79
    repeat1 name -- ipairs -- optlist "monos" -- optlist "con_defs"
clasohm@923
    80
      >> mk_params
clasohm@923
    81
  end;
clasohm@923
    82
clasohm@923
    83
wenzelm@6477
    84
clasohm@923
    85
(** datatype **)
clasohm@923
    86
clasohm@923
    87
local
berghofe@5183
    88
  (*** generate string for calling add_datatype ***)
berghofe@5183
    89
  (*** and bindig theorems to ML identifiers    ***)
clasohm@923
    90
berghofe@5183
    91
  fun mk_bind_thms_string names =
berghofe@5183
    92
   (case find_first (not o Syntax.is_identifier) names of
berghofe@5183
    93
      Some id => (warning (id ^ " is not a valid identifier"); "")
berghofe@5183
    94
    | None =>
berghofe@5183
    95
        let
berghofe@5183
    96
          fun mk_dt_struct (s, (id, i)) =
berghofe@5183
    97
            s ^ "structure " ^ id ^ " =\n\
berghofe@5183
    98
            \struct\n\
berghofe@5183
    99
            \  val distinct = nth_elem (" ^ i ^ ", distinct);\n\
berghofe@5183
   100
            \  val inject = nth_elem (" ^ i ^ ", inject);\n\
berghofe@5183
   101
            \  val exhaust = nth_elem (" ^ i ^ ", exhaustion);\n\
berghofe@5183
   102
            \  val cases = nth_elem (" ^ i ^ ", case_thms);\n\
berghofe@5183
   103
            \  val (split, split_asm) = nth_elem (" ^ i ^ ", split_thms);\n" ^
berghofe@5183
   104
              (if length names = 1 then
berghofe@5183
   105
                 "  val induct = induction;\n\
berghofe@5183
   106
                 \  val recs = rec_thms;\n\
berghofe@5183
   107
                 \  val simps = simps;\n\
berghofe@5183
   108
                 \  val size = size;\n"
berghofe@5183
   109
               else "") ^
berghofe@5183
   110
            "end;\n";
berghofe@5183
   111
berghofe@5183
   112
          val structs = foldl mk_dt_struct
berghofe@5183
   113
            ("", (names ~~ (map string_of_int (0 upto length names - 1))));
berghofe@5183
   114
berghofe@5183
   115
        in
berghofe@5183
   116
          (if length names > 1 then
berghofe@5183
   117
             "structure " ^ (space_implode "_" names) ^ " =\n\
berghofe@5183
   118
             \struct\n\
berghofe@5658
   119
             \  val induct = induction;\n\
berghofe@5658
   120
             \  val recs = rec_thms;\n\
berghofe@5658
   121
             \  val simps = simps;\n\
berghofe@5658
   122
             \  val size = size;\nend;\n"
berghofe@5658
   123
           else "") ^ structs
berghofe@5183
   124
        end);
berghofe@5183
   125
berghofe@5183
   126
  fun mk_dt_string dts =
clasohm@923
   127
    let
berghofe@5183
   128
      val names = map (fn ((((alt_name, _), name), _), _) =>
wenzelm@6642
   129
        unenclose (if_none alt_name name)) dts;
berghofe@5183
   130
berghofe@5183
   131
      val add_datatype_args = brackets (commas (map quote names)) ^ " " ^
berghofe@5183
   132
        brackets (commas (map (fn ((((_, vs), name), mx), cs) =>
berghofe@5183
   133
          parens (brackets (commas vs) ^ ", " ^ name ^ ", " ^ mx ^ ", " ^
berghofe@5183
   134
            brackets (commas cs))) dts));
berghofe@5183
   135
clasohm@923
   136
    in
berghofe@5183
   137
      ";\nlocal\n\
berghofe@5183
   138
      \val (thy, {distinct, inject, exhaustion, rec_thms,\n\
berghofe@5183
   139
      \  case_thms, split_thms, induction, size, simps}) =\n\
wenzelm@6103
   140
      \ DatatypePackage.add_datatype false " ^ add_datatype_args ^ " thy;\n\
berghofe@5183
   141
      \in\n" ^ mk_bind_thms_string names ^
berghofe@5183
   142
      "val thy = thy;\nend;\nval thy = thy\n"
clasohm@923
   143
    end;
clasohm@923
   144
wenzelm@6381
   145
  fun mk_thmss namess = mk_list (map (mk_list o map (mk_pair o rpair "[]")) namess);
wenzelm@6381
   146
  fun mk_thm name = mk_pair (name, "[]");
wenzelm@6103
   147
berghofe@5183
   148
  fun mk_rep_dt_string (((names, distinct), inject), induct) =
berghofe@5183
   149
    ";\nlocal\n\
berghofe@5183
   150
    \val (thy, {distinct, inject, exhaustion, rec_thms,\n\
berghofe@5183
   151
    \  case_thms, split_thms, induction, size, simps}) =\n\
wenzelm@6103
   152
    \ DatatypePackage.rep_datatype " ^
berghofe@5183
   153
    (case names of
wenzelm@6103
   154
        Some names => "(Some [" ^ commas_quote names ^ "])\n " ^
wenzelm@6103
   155
          mk_thmss distinct ^ "\n " ^ mk_thmss inject ^
wenzelm@6103
   156
            "\n " ^ mk_thm induct ^ " thy;\nin\n" ^ mk_bind_thms_string names
wenzelm@6103
   157
      | None => "None\n " ^ mk_thmss distinct ^ "\n " ^ mk_thmss inject ^
wenzelm@6103
   158
            "\n " ^ mk_thm induct ^ " thy;\nin\n") ^
berghofe@5183
   159
    "val thy = thy;\nend;\nval thy = thy\n";
clasohm@923
   160
berghofe@5183
   161
  (*** parsers ***)
wenzelm@4106
   162
wenzelm@6642
   163
  val simple_typ = ident || (type_var >> unenclose);
clasohm@1316
   164
clasohm@1251
   165
  fun complex_typ toks =
clasohm@1316
   166
    let val typ = simple_typ || "(" $$-- complex_typ --$$ ")";
clasohm@1316
   167
        val typ2 = complex_typ || "(" $$-- complex_typ --$$ ")";
clasohm@1316
   168
    in
berghofe@5183
   169
     (typ ^^ (repeat ident >> (cat "" o space_implode " ")) ||
berghofe@5183
   170
      "(" $$-- !! (list1 typ2 >> (parens o commas)) --$$ ")" ^^ !!
berghofe@5183
   171
        (repeat1 ident >> (cat "" o space_implode " "))) toks
clasohm@1316
   172
    end;
clasohm@1316
   173
wenzelm@6642
   174
  val opt_typs = repeat ((string >> unenclose) ||
berghofe@5183
   175
    simple_typ || ("(" $$-- complex_typ --$$ ")"));
berghofe@5183
   176
  val constructor = name -- opt_typs -- opt_mixfix >> (fn ((n, Ts), mx) =>
berghofe@5716
   177
    parens (n ^ ", " ^ brackets (commas_quote Ts) ^ ", " ^ mx));
berghofe@5183
   178
  val opt_name = optional ("(" $$-- name --$$ ")" >> Some) None
berghofe@5183
   179
berghofe@5716
   180
  fun optlistlist s = optional (s $$-- enum "and" (list name)) [[]]
berghofe@5716
   181
clasohm@923
   182
in
clasohm@923
   183
  val datatype_decl =
berghofe@5183
   184
    enum1 "and" (opt_name -- type_args -- name -- opt_infix --$$ "=" --
berghofe@5183
   185
      enum1 "|" constructor) >> mk_dt_string;
berghofe@5183
   186
  val rep_datatype_decl =
wenzelm@6642
   187
    ((optional ((repeat1 (name >> unenclose)) >> Some) None) --
berghofe@5716
   188
      optlistlist "distinct" -- optlistlist "inject" --
berghofe@5716
   189
        ("induct" $$-- name)) >> mk_rep_dt_string;
clasohm@923
   190
end;
clasohm@923
   191
clasohm@923
   192
wenzelm@6477
   193
clasohm@923
   194
(** primrec **)
clasohm@923
   195
wenzelm@8657
   196
fun mk_patterns eqns = mk_list (map (fn (s, _) => if s = "" then "_" else s) eqns);
wenzelm@8657
   197
fun mk_eqns eqns = mk_list (map (fn (x, y) => mk_pair (mk_pair (quote x, y), "[]")) eqns);
wenzelm@8657
   198
berghofe@5716
   199
fun mk_primrec_decl (alt_name, eqns) =
wenzelm@8657
   200
  ";\nval (thy, " ^ mk_patterns eqns ^ ") = PrimrecPackage.add_primrec " ^ alt_name ^ " "
wenzelm@8657
   201
  ^ mk_eqns eqns ^ " " ^ " thy;\n\
wenzelm@8657
   202
  \val thy = thy\n"
clasohm@923
   203
berghofe@5183
   204
(* either names and axioms or just axioms *)
wenzelm@8657
   205
val primrec_decl = optional ("(" $$-- name --$$ ")") "\"\"" --
wenzelm@8657
   206
  repeat1 (ident -- string || (string >> pair "")) >> mk_primrec_decl;
clasohm@923
   207
paulson@2922
   208
paulson@6497
   209
(*** recdef: interface to Slind's TFL ***)
paulson@2922
   210
paulson@6497
   211
(** TFL with explicit WF relations **)
paulson@2922
   212
paulson@3194
   213
(*fname: name of function being defined; rel: well-founded relation*)
wenzelm@8657
   214
fun mk_recdef_decl ((((fname, rel), congs), ss), eqns) =
wenzelm@6477
   215
  let
wenzelm@6642
   216
    val fid = unenclose fname;
wenzelm@6477
   217
    val congs_text = mk_list (map (fn c => mk_pair (c, "[]")) congs);
paulson@2922
   218
  in
wenzelm@6477
   219
    ";\n\n\
wenzelm@6477
   220
    \local\n\
nipkow@8623
   221
    \fun simpset() = Simplifier.simpset_of thy;\n\
wenzelm@6477
   222
    \val (thy, result) = thy |> RecdefPackage.add_recdef " ^ quote fid ^ " " ^ rel ^ "\n" ^
wenzelm@8657
   223
    mk_eqns eqns ^ "\n(Some (" ^ ss ^ "))\n" ^ congs_text ^ ";\n\
wenzelm@6477
   224
    \in\n\
wenzelm@6477
   225
    \structure " ^ fid ^ " =\n\
wenzelm@6477
   226
    \struct\n\
wenzelm@8657
   227
    \  val {simps, rules = " ^ mk_patterns eqns ^ ", induct, tcs} = result;\n\
wenzelm@6477
   228
    \end;\n\
wenzelm@6477
   229
    \val thy = thy;\n\
wenzelm@6477
   230
    \end;\n\
wenzelm@6477
   231
    \val thy = thy\n"
paulson@2922
   232
  end;
paulson@2922
   233
wenzelm@6555
   234
val recdef_decl =
wenzelm@8657
   235
  name -- string --
wenzelm@6555
   236
    optional ("congs" $$-- list1 name) [] --
wenzelm@6642
   237
    optional ("simpset" $$-- string >> unenclose) "simpset()" --
wenzelm@8657
   238
    repeat1 (ident -- string || (string >> pair "")) >> mk_recdef_decl;
wenzelm@6555
   239
paulson@6497
   240
paulson@6497
   241
(** TFL with no WF relation supplied **)
paulson@6497
   242
paulson@6497
   243
(*fname: name of function being defined; rel: well-founded relation*)
wenzelm@6555
   244
fun mk_defer_recdef_decl ((fname, congs), axms) =
wenzelm@6555
   245
  let
wenzelm@6642
   246
    val fid = unenclose fname;
wenzelm@6555
   247
    val congs_text = mk_list (map (fn c => mk_pair (c, "[]")) congs);
wenzelm@6555
   248
    val axms_text = mk_big_list axms;
paulson@6497
   249
  in
wenzelm@6555
   250
    ";\n\n\
wenzelm@6555
   251
    \local\n\
wenzelm@6555
   252
    \val (thy, result) = thy |> RecdefPackage.defer_recdef " ^ quote fid ^ "\n" ^
wenzelm@6555
   253
    axms_text ^ "\n" ^ congs_text ^ ";\n\
wenzelm@6555
   254
    \in\n\
wenzelm@6555
   255
    \structure " ^ fid ^ " =\n\
wenzelm@6555
   256
    \struct\n\
wenzelm@6555
   257
    \  val {induct_rules} = result;\n\
wenzelm@6555
   258
    \end;\n\
wenzelm@6555
   259
    \val thy = thy;\n\
wenzelm@6555
   260
    \end;\n\
wenzelm@6555
   261
    \val thy = thy\n"
paulson@6497
   262
  end;
paulson@6497
   263
wenzelm@6555
   264
val defer_recdef_decl =
wenzelm@6555
   265
  (name --
wenzelm@6555
   266
    optional ("congs" $$-- list1 name) [] --
wenzelm@6555
   267
    repeat1 string >> mk_defer_recdef_decl);
paulson@2922
   268
paulson@2922
   269
paulson@2922
   270
wenzelm@3622
   271
(** augment thy syntax **)
clasohm@923
   272
wenzelm@3622
   273
in
clasohm@923
   274
wenzelm@3622
   275
val _ = ThySyn.add_syntax
berghofe@5183
   276
 ["intrs", "monos", "con_defs", "congs", "simpset", "|",
wenzelm@6555
   277
  "and", "distinct", "inject", "induct"]
wenzelm@4873
   278
 [axm_section "typedef" "|> TypedefPackage.add_typedef" typedef_decl,
wenzelm@6523
   279
  section "record" "|> (#1 oooo RecordPackage.add_record)" record_decl,
paulson@6497
   280
  section "inductive" 	"" (inductive_decl false),
paulson@6497
   281
  section "coinductive"	"" (inductive_decl true),
paulson@6497
   282
  section "datatype" 	"" datatype_decl,
berghofe@5183
   283
  section "rep_datatype" "" rep_datatype_decl,
paulson@6497
   284
  section "primrec" 	"" primrec_decl,
wenzelm@6555
   285
  section "recdef" 	"" recdef_decl,
wenzelm@6555
   286
  section "defer_recdef" "" defer_recdef_decl];
clasohm@923
   287
clasohm@923
   288
end;