src/HOL/thy_syntax.ML
author wenzelm
Thu Oct 23 12:49:16 1997 +0200 (1997-10-23)
changeset 3980 21ef91734970
parent 3945 ae9c61d69888
child 4001 b6a3c2665c45
permissions -rw-r--r--
added record section;
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
TODO:
clasohm@923
     8
  move datatype / primrec stuff to pre_datatype.ML (?)
clasohm@923
     9
*)
clasohm@923
    10
clasohm@923
    11
(*the kind of distinctiveness axioms depends on number of constructors*)
nipkow@2930
    12
val dtK = 7;  (* FIXME rename?, move? *)
clasohm@923
    13
wenzelm@3622
    14
wenzelm@3622
    15
local
clasohm@923
    16
clasohm@923
    17
open ThyParse;
clasohm@923
    18
clasohm@923
    19
clasohm@1475
    20
(** typedef **)
clasohm@923
    21
clasohm@1475
    22
fun mk_typedef_decl (((((opt_name, vs), t), mx), rhs), wt) =
clasohm@923
    23
  let
clasohm@923
    24
    val name' = if_none opt_name t;
clasohm@923
    25
    val name = strip_quotes name';
clasohm@923
    26
  in
clasohm@923
    27
    (cat_lines [name', mk_triple (t, mk_list vs, mx), rhs, wt],
clasohm@923
    28
      [name ^ "_def", "Rep_" ^ name, "Rep_" ^ name ^ "_inverse",
clasohm@923
    29
        "Abs_" ^ name ^ "_inverse"])
clasohm@923
    30
  end;
clasohm@923
    31
clasohm@1475
    32
val typedef_decl =
clasohm@923
    33
  optional ("(" $$-- name --$$ ")" >> Some) None --
clasohm@923
    34
  type_args -- name -- opt_infix --$$ "=" -- string -- opt_witness
clasohm@1475
    35
  >> mk_typedef_decl;
clasohm@923
    36
clasohm@923
    37
clasohm@923
    38
wenzelm@3980
    39
(** record **)
wenzelm@3980
    40
wenzelm@3980
    41
val record_decl =
wenzelm@3980
    42
  name --$$ "=" -- optional (name --$$ "+" >> (parens o cat "Some")) "None" --
wenzelm@3980
    43
  repeat1 ((name --$$ "::" -- typ) >> mk_pair)
wenzelm@3980
    44
  >> (fn ((x, y), zs) => space_implode " " [x, y, mk_list zs]);
wenzelm@3980
    45
wenzelm@3980
    46
clasohm@923
    47
(** (co)inductive **)
clasohm@923
    48
clasohm@923
    49
(*co is either "" or "Co"*)
clasohm@923
    50
fun inductive_decl co =
clasohm@923
    51
  let
clasohm@923
    52
    fun mk_intr_name (s, _) =   (*the "op" cancels any infix status*)
clasohm@923
    53
      if Syntax.is_identifier s then "op " ^ s else "_";
clasohm@923
    54
    fun mk_params (((recs, ipairs), monos), con_defs) =
clasohm@923
    55
      let val big_rec_name = space_implode "_" (map (scan_to_id o trim) recs)
clasohm@923
    56
          and srec_tms = mk_list recs
clasohm@923
    57
          and sintrs   = mk_big_list (map snd ipairs)
paulson@3194
    58
          val intrnl_name = big_rec_name ^ "_Intrnl"
clasohm@923
    59
      in
clasohm@923
    60
         (";\n\n\
paulson@3194
    61
          \structure " ^ intrnl_name ^ " =\n\
clasohm@923
    62
          \  struct\n\
clasohm@923
    63
          \  val _ = writeln \"" ^ co ^
clasohm@923
    64
                     "Inductive definition " ^ big_rec_name ^ "\"\n\
paulson@1430
    65
          \  val rec_tms\t= map (readtm (sign_of thy) Ind_Syntax.termTVar) "
clasohm@923
    66
                           ^ srec_tms ^ "\n\
clasohm@923
    67
          \  and intr_tms\t= map (readtm (sign_of thy) propT)\n"
clasohm@923
    68
                           ^ sintrs ^ "\n\
paulson@1430
    69
          \  end;\n\n\
clasohm@923
    70
          \val thy = thy |> " ^ co ^ "Ind.add_fp_def_i \n    (" ^
paulson@3194
    71
             intrnl_name ^ ".rec_tms, " ^
paulson@3194
    72
             intrnl_name ^ ".intr_tms)"
clasohm@923
    73
         ,
clasohm@923
    74
          "structure " ^ big_rec_name ^ " =\n\
paulson@1430
    75
          \ let\n\
paulson@1430
    76
          \  val _ = writeln \"Proofs for " ^ co ^ 
paulson@1430
    77
                     "Inductive definition " ^ big_rec_name ^ "\"\n\
clasohm@923
    78
          \  structure Result = " ^ co ^ "Ind_section_Fun\n\
paulson@3194
    79
          \\t  (open " ^ intrnl_name ^ "\n\
paulson@1430
    80
          \\t   val thy\t\t= thy\n\
paulson@1430
    81
          \\t   val monos\t\t= " ^ monos ^ "\n\
paulson@1430
    82
          \\t   val con_defs\t\t= " ^ con_defs ^ ");\n\n\
clasohm@1465
    83
          \ in\n\
paulson@1430
    84
          \  struct\n\
clasohm@923
    85
          \  val " ^ mk_list (map mk_intr_name ipairs) ^ " = Result.intrs;\n\
clasohm@923
    86
          \  open Result\n\
paulson@1430
    87
          \  end\n\
paulson@1430
    88
          \ end;\n\n\
paulson@3194
    89
          \structure " ^ intrnl_name ^ " = struct end;\n\n"
clasohm@923
    90
         )
clasohm@923
    91
      end
clasohm@923
    92
    val ipairs = "intrs" $$-- repeat1 (ident -- !! string)
paulson@3403
    93
    fun optstring s = optional (s $$-- string >> trim) "[]"
clasohm@923
    94
  in
paulson@1788
    95
    repeat1 name -- ipairs -- optstring "monos" -- optstring "con_defs"
clasohm@923
    96
      >> mk_params
clasohm@923
    97
  end;
clasohm@923
    98
clasohm@923
    99
clasohm@923
   100
clasohm@923
   101
(** datatype **)
clasohm@923
   102
clasohm@923
   103
local
clasohm@923
   104
  (* FIXME err -> add_datatype *)
clasohm@923
   105
  fun mk_cons cs =
clasohm@923
   106
    (case duplicates (map (fst o fst) cs) of
clasohm@923
   107
      [] => map (fn ((s, ts), syn) => mk_triple (s, mk_list ts, syn)) cs
clasohm@923
   108
    | dups => error ("Duplicate constructors: " ^ commas_quote dups));
clasohm@923
   109
clasohm@923
   110
  (*generate names of distinctiveness axioms*)
clasohm@923
   111
  fun mk_distinct_rules cs tname =
clasohm@923
   112
    let
clasohm@923
   113
      val uqcs = map (fn ((s, _), _) => strip_quotes s) cs;
clasohm@923
   114
      (*combine all constructor names with all others w/o duplicates*)
clasohm@923
   115
      fun neg_one c = map (fn c2 => quote (c ^ "_not_" ^ c2));
clasohm@923
   116
      fun neg1 [] = []
clasohm@923
   117
        | neg1 (c1 :: cs) = neg_one c1 cs @ neg1 cs;
clasohm@923
   118
    in
clasohm@923
   119
      if length uqcs < dtK then neg1 uqcs
clasohm@923
   120
      else quote (tname ^ "_ord_distinct") ::
clasohm@923
   121
        map (fn c => quote (tname ^ "_ord_" ^ c)) uqcs
clasohm@923
   122
    end;
clasohm@923
   123
clasohm@923
   124
  fun mk_rules tname cons pre = " map (get_axiom thy) " ^
paulson@3194
   125
    mk_list (map (fn ((s,_), _) => quote (tname ^ pre ^ strip_quotes s)) cons);
clasohm@923
   126
clasohm@1668
   127
  (*generate string for calling add_datatype and build_record*)
clasohm@923
   128
  fun mk_params ((ts, tname), cons) =
nipkow@3308
   129
   ("val (thy," ^ tname ^ "_add_primrec," ^ tname ^ "_size_eqns) = Datatype.add_datatype\n"
clasohm@923
   130
    ^ mk_triple (mk_list ts, quote tname, mk_list (mk_cons cons)) ^ " thy\n\
nipkow@3308
   131
    \val thy = ("^tname^"_add_primrec "^tname^"_size_eqns thy)"
paulson@2922
   132
    ,
wenzelm@3980
   133
    "val _ = deny (" ^ quote tname ^ " mem (Sign.stamp_names_of (sign_of thy)))\n\
nipkow@3665
   134
    \   (\"Datatype \\\""^tname^"\\\" would clash with the theory of the same name!\");\n\
nipkow@3665
   135
    \structure " ^ tname ^ " =\n\
clasohm@923
   136
    \struct\n\
clasohm@923
   137
    \ val inject = map (get_axiom thy) " ^
clasohm@923
   138
        mk_list (map (fn ((s, _), _) => quote ("inject_" ^ strip_quotes s))
clasohm@923
   139
          (filter_out (null o snd o fst) cons)) ^ ";\n\
clasohm@923
   140
    \ val distinct = " ^
clasohm@923
   141
        (if length cons < dtK then "let val distinct' = " else "") ^
clasohm@923
   142
        "map (get_axiom thy) " ^ mk_list (mk_distinct_rules cons tname) ^
clasohm@923
   143
        (if length cons < dtK then
clasohm@923
   144
          "  in distinct' @ (map (fn t => sym COMP (t RS contrapos))\
clasohm@923
   145
          \ distinct') end"
clasohm@923
   146
         else "") ^ ";\n\
clasohm@923
   147
    \ val induct = get_axiom thy \"" ^ tname ^ "_induct\";\n\
clasohm@923
   148
    \ val cases =" ^ mk_rules tname cons "_case_" ^ ";\n\
clasohm@923
   149
    \ val recs =" ^ mk_rules tname cons "_rec_" ^ ";\n\
clasohm@923
   150
    \ val simps = inject @ distinct @ cases @ recs;\n\
clasohm@923
   151
    \ fun induct_tac a = res_inst_tac [(" ^ quote tname ^ ", a)] induct;\n\
clasohm@1264
   152
    \end;\n\
clasohm@1668
   153
    \val dummy = datatypes := Dtype.build_record (thy, " ^
wenzelm@3945
   154
      mk_pair ("Sign.intern_tycon (sign_of thy) " ^ quote tname,
wenzelm@3945
   155
        mk_list (map (fst o fst) cons)) ^
clasohm@1668
   156
      ", " ^ tname ^ ".induct_tac) :: (!datatypes);\n\
nipkow@2930
   157
    \val dummy = Addsimps(" ^ tname ^ ".cases @ " ^ tname ^ ".recs);\n\
nipkow@2930
   158
    \val dummy = AddIffs " ^ tname ^ ".inject;\n\
nipkow@2930
   159
    \val dummy = " ^
nipkow@2930
   160
      (if length cons < dtK then "AddIffs " else "Addsimps ") ^
nipkow@3308
   161
      tname ^ ".distinct;\n\
nipkow@3308
   162
    \val dummy = Addsimps(map (fn (_,eqn) =>\n\
nipkow@3308
   163
    \ prove_goalw thy [get_def thy " ^ quote("size_"^tname) ^
nipkow@3308
   164
                     "] eqn (fn _ => [Simp_tac 1]))\n" ^
nipkow@3308
   165
    tname^"_size_eqns)\n"
nipkow@3308
   166
   );
clasohm@923
   167
clasohm@923
   168
  (*parsers*)
clasohm@923
   169
  val tvars = type_args >> map (cat "dtVar");
clasohm@1316
   170
clasohm@1316
   171
  val simple_typ = ident >> (cat "dtTyp" o curry mk_pair "[]" o quote) ||
clasohm@1316
   172
    type_var >> cat "dtVar";
clasohm@1316
   173
clasohm@1251
   174
  fun complex_typ toks =
clasohm@1316
   175
    let val typ = simple_typ || "(" $$-- complex_typ --$$ ")";
clasohm@1316
   176
        val typ2 = complex_typ || "(" $$-- complex_typ --$$ ")";
clasohm@1316
   177
    in
clasohm@1316
   178
     (typ -- repeat (ident>>quote) >>
clasohm@1316
   179
        (foldl (fn (x,y) => "dtTyp " ^ mk_pair (brackets x, y))) ||
clasohm@1316
   180
      "(" $$-- !! (list1 typ2) --$$ ")" -- !! (repeat1 (ident>>quote)) >>
clasohm@1316
   181
       (fn (fst, ids) => foldl (fn (x,y) => "dtTyp " ^
clasohm@1316
   182
                         mk_pair (brackets x, y)) (commas fst, ids))) toks
clasohm@1316
   183
    end;
clasohm@1316
   184
clasohm@977
   185
  val opt_typs = repeat (simple_typ || ("(" $$-- complex_typ --$$ ")"));
clasohm@923
   186
  val constructor = name -- opt_typs -- opt_mixfix;
clasohm@923
   187
in
clasohm@923
   188
  val datatype_decl =
clasohm@923
   189
    tvars -- ident --$$ "=" -- enum1 "|" constructor >> mk_params;
clasohm@923
   190
end;
clasohm@923
   191
clasohm@923
   192
clasohm@923
   193
clasohm@923
   194
(** primrec **)
clasohm@923
   195
paulson@2922
   196
(*recursion equations have user-supplied names*)
berghofe@1845
   197
fun mk_primrec_decl_1 ((fname, tname), axms) =
clasohm@923
   198
  let
clasohm@1574
   199
    (*Isolate type name from the structure's identifier it may be stored in*)
clasohm@1574
   200
    val tname' = implode (snd (take_suffix (not_equal ".") (explode tname)));
clasohm@1574
   201
clasohm@923
   202
    fun mk_prove (name, eqn) =
clasohm@1264
   203
      "val " ^ name ^ " = store_thm (" ^ quote name
clasohm@1574
   204
      ^ ", prove_goalw thy [get_def thy "
clasohm@1574
   205
      ^ (quote (strip_quotes fname ^ "_" ^ tname')) ^ "] " ^ eqn ^ "\n\
clasohm@1264
   206
      \  (fn _ => [Simp_tac 1]));";
clasohm@1264
   207
clasohm@923
   208
    val axs = mk_list (map (fn (n, a) => mk_pair (quote n, a)) axms);
paulson@2922
   209
  in ("|> " ^ tname ^ "_add_primrec " ^ axs
paulson@2922
   210
      , 
paulson@2922
   211
      cat_lines (map mk_prove axms)
clasohm@1264
   212
      ^ "\nval dummy = Addsimps " ^ mk_list (map fst axms) ^ ";")
clasohm@1264
   213
  end;
clasohm@923
   214
paulson@2922
   215
(*recursion equations have no names*)
berghofe@1845
   216
fun mk_primrec_decl_2 ((fname, tname), axms) =
berghofe@1845
   217
  let
berghofe@1845
   218
    (*Isolate type name from the structure's identifier it may be stored in*)
berghofe@1845
   219
    val tname' = implode (snd (take_suffix (not_equal ".") (explode tname)));
berghofe@1845
   220
berghofe@1845
   221
    fun mk_prove eqn =
berghofe@1845
   222
      "prove_goalw thy [get_def thy "
berghofe@1845
   223
      ^ (quote (strip_quotes fname ^ "_" ^ tname')) ^ "] " ^ eqn ^ " \
berghofe@1845
   224
      \(fn _ => [Simp_tac 1])";
berghofe@1845
   225
berghofe@1845
   226
    val axs = mk_list (map (fn a => mk_pair ("\"\"", a)) axms);
paulson@2922
   227
  in ("|> " ^ tname ^ "_add_primrec " ^ axs
paulson@2922
   228
      ,
berghofe@1845
   229
      "val dummy = Addsimps " ^
berghofe@1845
   230
      brackets(space_implode ",\n" (map mk_prove axms)) ^ ";")
berghofe@1845
   231
  end;
berghofe@1845
   232
paulson@2922
   233
(*function name, argument type and either (name,axiom) pairs or just axioms*)
clasohm@923
   234
val primrec_decl =
berghofe@1845
   235
  (name -- long_id -- repeat1 (ident -- string) >> mk_primrec_decl_1) ||
berghofe@1845
   236
  (name -- long_id -- repeat1 string >> mk_primrec_decl_2) ;
clasohm@923
   237
clasohm@923
   238
clasohm@923
   239
paulson@2922
   240
paulson@2922
   241
(** rec: interface to Slind's TFL **)
paulson@2922
   242
paulson@2922
   243
paulson@3194
   244
(*fname: name of function being defined; rel: well-founded relation*)
paulson@3456
   245
fun mk_rec_decl ((((fname, rel), congs), ss), axms) =
paulson@2922
   246
  let val fid = trim fname
paulson@3194
   247
      val intrnl_name = fid ^ "_Intrnl"
paulson@2922
   248
  in
paulson@2922
   249
	 (";\n\n\
paulson@3194
   250
          \val _ = writeln \"Recursive function " ^ fid ^ "\"\n\
paulson@3194
   251
          \val (thy, pats_" ^ intrnl_name ^ ") = Tfl.define thy " ^ 
paulson@3345
   252
	                 quote fid ^ " " ^ 
paulson@3194
   253
	                 rel ^ "\n" ^ mk_big_list axms ^ ";\n\
paulson@2922
   254
          \val thy = thy"
paulson@2922
   255
         ,
paulson@3194
   256
          "structure " ^ fid ^ " =\n\
paulson@3194
   257
          \  struct\n\
paulson@3194
   258
          \  val _ = writeln \"Proofs for recursive function " ^ fid ^ "\"\n\
paulson@3194
   259
          \  val {rules, induct, tcs} = \n\
paulson@3456
   260
          \    \t Tfl.simplify_defn (" ^ ss ^ ", " ^ congs ^ ")\n\
paulson@3456
   261
          \    \t\t  (thy, (" ^ quote fid ^ ", pats_" ^ intrnl_name ^ "))\n\
paulson@3194
   262
          \  end;\n\
paulson@3194
   263
          \val pats_" ^ intrnl_name ^ " = ();\n")
paulson@2922
   264
  end;
paulson@2922
   265
paulson@3403
   266
val rec_decl = (name -- string -- 
paulson@3456
   267
		optional ("congs" $$-- string >> trim) "[]" -- 
paulson@3403
   268
		optional ("simpset" $$-- string >> trim) "!simpset" -- 
paulson@3403
   269
		repeat1 string >> mk_rec_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@3622
   278
 ["intrs", "monos", "con_defs", "congs", "simpset", "|"]
clasohm@1475
   279
 [axm_section "typedef" "|> Typedef.add_typedef" typedef_decl,
wenzelm@3980
   280
  (section "record" "|> Record.add_record" record_decl),
clasohm@923
   281
  ("inductive", inductive_decl ""),
clasohm@923
   282
  ("coinductive", inductive_decl "Co"),
clasohm@923
   283
  ("datatype", datatype_decl),
paulson@2922
   284
  ("primrec", primrec_decl),
paulson@2922
   285
  ("recdef", rec_decl)];
clasohm@923
   286
clasohm@923
   287
end;