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