src/HOL/thy_syntax.ML
author nipkow
Tue, 05 Jan 1999 17:27:59 +0100
changeset 6059 aa00e235ea27
parent 6035 c041fc54ab4c
child 6103 36f272ea9413
permissions -rw-r--r--
In Main: moved Bin to the left to preserve the solver in its simpset.
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
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     5
Additional theory file sections for HOL.
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
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    17
    val name' = if_none opt_name t;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    18
    val name = strip_quotes name';
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 =
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    26
  optional ("(" $$-- name --$$ ")" >> Some) None --
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)) --$$ "="
4226
38c91213f26b fixed record parser;
wenzelm
parents: 4225
diff changeset
    36
    -- optional (typ --$$ "+" >> (parens o cat "Some")) "None"
4225
3d9e551bc5a6 improved record syntax;
wenzelm
parents: 4204
diff changeset
    37
    -- repeat1 ((name --$$ "::" -- typ) >> mk_pair)
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
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    41
(** (co)inductive **)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    42
5097
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    43
fun inductive_decl coind =
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    44
  let
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    45
    fun mk_intr_name (s, _) =   (*the "op" cancels any infix status*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    46
      if Syntax.is_identifier s then "op " ^ s else "_";
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    47
    fun mk_params (((recs, ipairs), monos), con_defs) =
6035
c041fc54ab4c replaced obsolete "trim" by "strip_quotes"
paulson
parents: 5716
diff changeset
    48
      let val big_rec_name = space_implode "_" (map (scan_to_id o strip_quotes) recs)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    49
          and srec_tms = mk_list recs
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    50
          and sintrs   = mk_big_list (map snd ipairs)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    51
      in
5097
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    52
        ";\n\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    53
        \local\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    54
        \val (thy, {defs, mono, unfold, intrs, elims, mk_cases, induct, ...}) =\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    55
        \  InductivePackage.add_inductive true " ^
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    56
        (if coind then "true " else "false ") ^ srec_tms ^ " " ^
5716
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
    57
         sintrs ^ " " ^ mk_list monos ^ " " ^ mk_list con_defs ^ " thy;\n\
5097
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    58
        \in\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    59
        \structure " ^ big_rec_name ^ " =\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    60
        \struct\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    61
        \  val defs = defs;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    62
        \  val mono = mono;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    63
        \  val unfold = unfold;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    64
        \  val intrs = intrs;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    65
        \  val elims = elims;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    66
        \  val elim = hd elims;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    67
        \  val " ^ (if coind then "co" else "") ^ "induct = induct;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    68
        \  val mk_cases = mk_cases;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    69
        \  val " ^ mk_list (map mk_intr_name ipairs) ^ " = intrs;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    70
        \end;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    71
        \val thy = thy;\nend;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    72
        \val thy = thy\n"
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    73
      end
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    74
    val ipairs = "intrs" $$-- repeat1 (ident -- !! string)
5716
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
    75
    fun optlist s = optional (s $$-- list1 name) []
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    76
  in
5716
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
    77
    repeat1 name -- ipairs -- optlist "monos" -- optlist "con_defs"
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    78
      >> mk_params
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    79
  end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    80
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    81
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    82
(** datatype **)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    83
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    84
local
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
    85
  (*** generate string for calling add_datatype ***)
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
    86
  (*** and bindig theorems to ML identifiers    ***)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    87
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
    88
  fun mk_bind_thms_string names =
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
    89
   (case find_first (not o Syntax.is_identifier) names of
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
    90
      Some id => (warning (id ^ " is not a valid identifier"); "")
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
    91
    | None =>
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
    92
        let
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
    93
          fun mk_dt_struct (s, (id, i)) =
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
    94
            s ^ "structure " ^ id ^ " =\n\
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
    95
            \struct\n\
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
    96
            \  val distinct = nth_elem (" ^ i ^ ", distinct);\n\
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
    97
            \  val inject = nth_elem (" ^ i ^ ", inject);\n\
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
    98
            \  val exhaust = nth_elem (" ^ i ^ ", exhaustion);\n\
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
    99
            \  val cases = nth_elem (" ^ i ^ ", case_thms);\n\
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   100
            \  val (split, split_asm) = nth_elem (" ^ i ^ ", split_thms);\n" ^
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   101
              (if length names = 1 then
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   102
                 "  val induct = induction;\n\
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   103
                 \  val recs = rec_thms;\n\
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   104
                 \  val simps = simps;\n\
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   105
                 \  val size = size;\n"
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   106
               else "") ^
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   107
            "end;\n";
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   108
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   109
          val structs = foldl mk_dt_struct
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   110
            ("", (names ~~ (map string_of_int (0 upto length names - 1))));
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   111
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   112
        in
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   113
          (if length names > 1 then
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   114
             "structure " ^ (space_implode "_" names) ^ " =\n\
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   115
             \struct\n\
5658
082debccf486 Changed structure of name spaces for datatypes.
berghofe
parents: 5222
diff changeset
   116
             \  val induct = induction;\n\
082debccf486 Changed structure of name spaces for datatypes.
berghofe
parents: 5222
diff changeset
   117
             \  val recs = rec_thms;\n\
082debccf486 Changed structure of name spaces for datatypes.
berghofe
parents: 5222
diff changeset
   118
             \  val simps = simps;\n\
082debccf486 Changed structure of name spaces for datatypes.
berghofe
parents: 5222
diff changeset
   119
             \  val size = size;\nend;\n"
082debccf486 Changed structure of name spaces for datatypes.
berghofe
parents: 5222
diff changeset
   120
           else "") ^ structs
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   121
        end);
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   122
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   123
  fun mk_dt_string dts =
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   124
    let
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   125
      val names = map (fn ((((alt_name, _), name), _), _) =>
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   126
        strip_quotes (if_none alt_name name)) dts;
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   127
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   128
      val add_datatype_args = brackets (commas (map quote names)) ^ " " ^
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   129
        brackets (commas (map (fn ((((_, vs), name), mx), cs) =>
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   130
          parens (brackets (commas vs) ^ ", " ^ name ^ ", " ^ mx ^ ", " ^
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   131
            brackets (commas cs))) dts));
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   132
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   133
    in
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   134
      ";\nlocal\n\
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   135
      \val (thy, {distinct, inject, exhaustion, rec_thms,\n\
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   136
      \  case_thms, split_thms, induction, size, simps}) =\n\
5658
082debccf486 Changed structure of name spaces for datatypes.
berghofe
parents: 5222
diff changeset
   137
      \  DatatypePackage.add_datatype false " ^ add_datatype_args ^ " thy;\n\
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   138
      \in\n" ^ mk_bind_thms_string names ^
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   139
      "val thy = thy;\nend;\nval thy = thy\n"
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   140
    end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   141
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   142
  fun mk_rep_dt_string (((names, distinct), inject), induct) =
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   143
    ";\nlocal\n\
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   144
    \val (thy, {distinct, inject, exhaustion, rec_thms,\n\
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   145
    \  case_thms, split_thms, induction, size, simps}) =\n\
5716
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   146
    \  DatatypePackage.rep_datatype " ^
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   147
    (case names of
5716
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   148
        Some names => "(Some [" ^ commas_quote names ^ "]) " ^
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   149
          mk_list (map mk_list distinct) ^ " " ^ mk_list (map mk_list inject) ^
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   150
            " " ^ induct ^ " thy;\nin\n" ^ mk_bind_thms_string names
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   151
      | None => "None " ^ mk_list (map mk_list distinct) ^ " " ^
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   152
          mk_list (map mk_list inject) ^ " " ^ induct ^ " thy;\nin\n") ^
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   153
    "val thy = thy;\nend;\nval thy = thy\n";
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   154
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   155
  (*** parsers ***)
4106
wenzelm
parents: 4091
diff changeset
   156
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   157
  val simple_typ = ident || (type_var >> strip_quotes);
1316
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   158
1251
81fc4d8e3eda added nested types on right hand side of datatype definitions
clasohm
parents: 977
diff changeset
   159
  fun complex_typ toks =
1316
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   160
    let val typ = simple_typ || "(" $$-- complex_typ --$$ ")";
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   161
        val typ2 = complex_typ || "(" $$-- complex_typ --$$ ")";
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   162
    in
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   163
     (typ ^^ (repeat ident >> (cat "" o space_implode " ")) ||
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   164
      "(" $$-- !! (list1 typ2 >> (parens o commas)) --$$ ")" ^^ !!
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   165
        (repeat1 ident >> (cat "" o space_implode " "))) toks
1316
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   166
    end;
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   167
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   168
  val opt_typs = repeat ((string >> strip_quotes) ||
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   169
    simple_typ || ("(" $$-- complex_typ --$$ ")"));
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   170
  val constructor = name -- opt_typs -- opt_mixfix >> (fn ((n, Ts), mx) =>
5716
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   171
    parens (n ^ ", " ^ brackets (commas_quote Ts) ^ ", " ^ mx));
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   172
  val opt_name = optional ("(" $$-- name --$$ ")" >> Some) None
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   173
5716
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   174
  fun optlistlist s = optional (s $$-- enum "and" (list name)) [[]]
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   175
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   176
in
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   177
  val datatype_decl =
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   178
    enum1 "and" (opt_name -- type_args -- name -- opt_infix --$$ "=" --
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   179
      enum1 "|" constructor) >> mk_dt_string;
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   180
  val rep_datatype_decl =
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   181
    ((optional ((repeat1 (name >> strip_quotes)) >> Some) None) --
5716
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   182
      optlistlist "distinct" -- optlistlist "inject" --
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   183
        ("induct" $$-- name)) >> mk_rep_dt_string;
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   184
end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   185
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   186
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   187
(** primrec **)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   188
5716
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   189
fun mk_primrec_decl (alt_name, eqns) =
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   190
  let
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   191
    val names = map (fn (s, _) => if s = "" then "_" else s) eqns
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   192
  in
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   193
    ";\nval (thy, " ^ mk_list names ^
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   194
    ") = PrimrecPackage.add_primrec " ^ alt_name ^ " " ^
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   195
      mk_list (map mk_pair (map (apfst quote) eqns)) ^ " " ^ " thy;\n\
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   196
    \val thy = thy\n"
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   197
  end;
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   198
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   199
(* either names and axioms or just axioms *)
5716
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   200
val primrec_decl = (optional ("(" $$-- name --$$ ")") "\"\"" --
a3d2a6b6693e Changed syntax of rep_datatype and inductive: Theorems
berghofe
parents: 5658
diff changeset
   201
  (repeat1 ((ident -- string) || (string >> pair "")))) >> mk_primrec_decl;
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   202
2922
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   203
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   204
(** rec: interface to Slind's TFL **)
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   205
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   206
3194
36bfceef1800 TFL theory section
paulson
parents: 2930
diff changeset
   207
(*fname: name of function being defined; rel: well-founded relation*)
3456
fdb1768ebd3e New "congs" keyword for recdef theory section
paulson
parents: 3403
diff changeset
   208
fun mk_rec_decl ((((fname, rel), congs), ss), axms) =
6035
c041fc54ab4c replaced obsolete "trim" by "strip_quotes"
paulson
parents: 5716
diff changeset
   209
  let val fid = strip_quotes fname
3194
36bfceef1800 TFL theory section
paulson
parents: 2930
diff changeset
   210
      val intrnl_name = fid ^ "_Intrnl"
2922
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   211
  in
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   212
	 (";\n\n\
3194
36bfceef1800 TFL theory section
paulson
parents: 2930
diff changeset
   213
          \val _ = writeln \"Recursive function " ^ fid ^ "\"\n\
36bfceef1800 TFL theory section
paulson
parents: 2930
diff changeset
   214
          \val (thy, pats_" ^ intrnl_name ^ ") = Tfl.define thy " ^ 
3345
4d888ddd6284 Now recdef checks the name of the function being defined.
paulson
parents: 3308
diff changeset
   215
	                 quote fid ^ " " ^ 
3194
36bfceef1800 TFL theory section
paulson
parents: 2930
diff changeset
   216
	                 rel ^ "\n" ^ mk_big_list axms ^ ";\n\
2922
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   217
          \val thy = thy"
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   218
         ,
3194
36bfceef1800 TFL theory section
paulson
parents: 2930
diff changeset
   219
          "structure " ^ fid ^ " =\n\
36bfceef1800 TFL theory section
paulson
parents: 2930
diff changeset
   220
          \  struct\n\
36bfceef1800 TFL theory section
paulson
parents: 2930
diff changeset
   221
          \  val _ = writeln \"Proofs for recursive function " ^ fid ^ "\"\n\
36bfceef1800 TFL theory section
paulson
parents: 2930
diff changeset
   222
          \  val {rules, induct, tcs} = \n\
3456
fdb1768ebd3e New "congs" keyword for recdef theory section
paulson
parents: 3403
diff changeset
   223
          \    \t Tfl.simplify_defn (" ^ ss ^ ", " ^ congs ^ ")\n\
fdb1768ebd3e New "congs" keyword for recdef theory section
paulson
parents: 3403
diff changeset
   224
          \    \t\t  (thy, (" ^ quote fid ^ ", pats_" ^ intrnl_name ^ "))\n\
3194
36bfceef1800 TFL theory section
paulson
parents: 2930
diff changeset
   225
          \  end;\n\
36bfceef1800 TFL theory section
paulson
parents: 2930
diff changeset
   226
          \val pats_" ^ intrnl_name ^ " = ();\n")
2922
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   227
  end;
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   228
6035
c041fc54ab4c replaced obsolete "trim" by "strip_quotes"
paulson
parents: 5716
diff changeset
   229
val rec_decl = 
c041fc54ab4c replaced obsolete "trim" by "strip_quotes"
paulson
parents: 5716
diff changeset
   230
    (name -- string -- 
c041fc54ab4c replaced obsolete "trim" by "strip_quotes"
paulson
parents: 5716
diff changeset
   231
     optional ("congs" $$-- string >> strip_quotes) "[]" -- 
c041fc54ab4c replaced obsolete "trim" by "strip_quotes"
paulson
parents: 5716
diff changeset
   232
     optional ("simpset" $$-- string >> strip_quotes) "simpset()" -- 
c041fc54ab4c replaced obsolete "trim" by "strip_quotes"
paulson
parents: 5716
diff changeset
   233
     repeat1 string >> mk_rec_decl) ;
2922
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   234
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   235
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   236
3622
85898be702b2 use ThySyn.add_syntax;
wenzelm
parents: 3617
diff changeset
   237
(** augment thy syntax **)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   238
3622
85898be702b2 use ThySyn.add_syntax;
wenzelm
parents: 3617
diff changeset
   239
in
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   240
3622
85898be702b2 use ThySyn.add_syntax;
wenzelm
parents: 3617
diff changeset
   241
val _ = ThySyn.add_syntax
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   242
 ["intrs", "monos", "con_defs", "congs", "simpset", "|",
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   243
  "and", "distinct", "inject", "induct"]
4873
b5999638979e TypedefPackage.add_typedef;
wenzelm
parents: 4536
diff changeset
   244
 [axm_section "typedef" "|> TypedefPackage.add_typedef" typedef_decl,
5097
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
   245
  section "record" "|> RecordPackage.add_record" record_decl,
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
   246
  section "inductive" "" (inductive_decl false),
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
   247
  section "coinductive" "" (inductive_decl true),
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
   248
  section "datatype" "" datatype_decl,
5183
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   249
  section "rep_datatype" "" rep_datatype_decl,
89f162de39cf Adapted to new datatype package.
berghofe
parents: 5097
diff changeset
   250
  section "primrec" "" primrec_decl,
2922
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   251
  ("recdef", rec_decl)];
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   252
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   253
end;