src/HOL/thy_syntax.ML
author nipkow
Thu Oct 30 09:45:03 1997 +0100 (1997-10-30 ago)
changeset 4032 4b1c69d8b767
parent 4001 b6a3c2665c45
child 4091 771b1f6422a8
permissions -rw-r--r--
For each datatype `t' there is now a theorem `split_t_case' of the form

P(t_case f1 ... fn x) = ((!y1 ... ym1. x = C1 y1 ... ym1 --> P(f1 y1 ... ym1))&
...
(!y1 ... ymn. x = Cn y1 ... ymn --> P(f1 y1 ... ymn)))


The simplifier now reduces !x. (..x.. & x = t & ..x..) --> P x
to (..t.. & ..t..) --> P t
(and similarly for t=x).
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@4001
    44
  >> (fn ((x, y), zs) => cat_lines [x, y, mk_big_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@4032
   129
   ("val (thy,"^tname^"_add_primrec,size_"^tname^"_eqns,split_"^tname^"_eqn) =\n\
nipkow@4032
   130
    \    Datatype.add_datatype\n"
clasohm@923
   131
    ^ mk_triple (mk_list ts, quote tname, mk_list (mk_cons cons)) ^ " thy\n\
nipkow@4032
   132
    \val thy = ("^tname^"_add_primrec size_"^tname^"_eqns thy)"
paulson@2922
   133
    ,
wenzelm@3980
   134
    "val _ = deny (" ^ quote tname ^ " mem (Sign.stamp_names_of (sign_of thy)))\n\
nipkow@3665
   135
    \   (\"Datatype \\\""^tname^"\\\" would clash with the theory of the same name!\");\n\
nipkow@3665
   136
    \structure " ^ tname ^ " =\n\
clasohm@923
   137
    \struct\n\
clasohm@923
   138
    \ val inject = map (get_axiom thy) " ^
clasohm@923
   139
        mk_list (map (fn ((s, _), _) => quote ("inject_" ^ strip_quotes s))
clasohm@923
   140
          (filter_out (null o snd o fst) cons)) ^ ";\n\
clasohm@923
   141
    \ val distinct = " ^
clasohm@923
   142
        (if length cons < dtK then "let val distinct' = " else "") ^
clasohm@923
   143
        "map (get_axiom thy) " ^ mk_list (mk_distinct_rules cons tname) ^
clasohm@923
   144
        (if length cons < dtK then
clasohm@923
   145
          "  in distinct' @ (map (fn t => sym COMP (t RS contrapos))\
clasohm@923
   146
          \ distinct') end"
clasohm@923
   147
         else "") ^ ";\n\
clasohm@923
   148
    \ val induct = get_axiom thy \"" ^ tname ^ "_induct\";\n\
clasohm@923
   149
    \ val cases =" ^ mk_rules tname cons "_case_" ^ ";\n\
clasohm@923
   150
    \ val recs =" ^ mk_rules tname cons "_rec_" ^ ";\n\
clasohm@923
   151
    \ val simps = inject @ distinct @ cases @ recs;\n\
clasohm@923
   152
    \ fun induct_tac a = res_inst_tac [(" ^ quote tname ^ ", a)] induct;\n\
clasohm@1264
   153
    \end;\n\
clasohm@1668
   154
    \val dummy = datatypes := Dtype.build_record (thy, " ^
wenzelm@3945
   155
      mk_pair ("Sign.intern_tycon (sign_of thy) " ^ quote tname,
wenzelm@3945
   156
        mk_list (map (fst o fst) cons)) ^
clasohm@1668
   157
      ", " ^ tname ^ ".induct_tac) :: (!datatypes);\n\
nipkow@2930
   158
    \val dummy = Addsimps(" ^ tname ^ ".cases @ " ^ tname ^ ".recs);\n\
nipkow@2930
   159
    \val dummy = AddIffs " ^ tname ^ ".inject;\n\
nipkow@2930
   160
    \val dummy = " ^
nipkow@2930
   161
      (if length cons < dtK then "AddIffs " else "Addsimps ") ^
nipkow@3308
   162
      tname ^ ".distinct;\n\
nipkow@3308
   163
    \val dummy = Addsimps(map (fn (_,eqn) =>\n\
nipkow@3308
   164
    \ prove_goalw thy [get_def thy " ^ quote("size_"^tname) ^
nipkow@4032
   165
                     "] eqn (fn _ => [Simp_tac 1])) size_"^tname^"_eqns);\n\
nipkow@4032
   166
    \val split_"^tname^"_case = prove_goal thy split_"^tname^"_eqn\n\
nipkow@4032
   167
    \  (fn _ => [#exhaust_tac(snd(hd(!datatypes))) \""^tname^"0\" 1,\n\
nipkow@4032
   168
    \            ALLGOALS Asm_simp_tac])"
nipkow@3308
   169
   );
nipkow@4032
   170
(*
nipkow@4032
   171
The #exhaust_tac(snd(hd(!datatypes))) in the proof of split_"^tname^"_case
nipkow@4032
   172
is a hack. Ideally I would just write exhaust_tac, but the latter extracts the
nipkow@4032
   173
specific exhaustion tactic from the theory associated with the proof
nipkow@4032
   174
state. However, the exhaustion tactic for the current datatype has only just
nipkow@4032
   175
been added to !datatypes (a few lines above) but is not yet associated with
nipkow@4032
   176
the theory. Hope this can be simplified in the future.
nipkow@4032
   177
*)
clasohm@923
   178
clasohm@923
   179
  (*parsers*)
clasohm@923
   180
  val tvars = type_args >> map (cat "dtVar");
clasohm@1316
   181
clasohm@1316
   182
  val simple_typ = ident >> (cat "dtTyp" o curry mk_pair "[]" o quote) ||
clasohm@1316
   183
    type_var >> cat "dtVar";
clasohm@1316
   184
clasohm@1251
   185
  fun complex_typ toks =
clasohm@1316
   186
    let val typ = simple_typ || "(" $$-- complex_typ --$$ ")";
clasohm@1316
   187
        val typ2 = complex_typ || "(" $$-- complex_typ --$$ ")";
clasohm@1316
   188
    in
clasohm@1316
   189
     (typ -- repeat (ident>>quote) >>
clasohm@1316
   190
        (foldl (fn (x,y) => "dtTyp " ^ mk_pair (brackets x, y))) ||
clasohm@1316
   191
      "(" $$-- !! (list1 typ2) --$$ ")" -- !! (repeat1 (ident>>quote)) >>
clasohm@1316
   192
       (fn (fst, ids) => foldl (fn (x,y) => "dtTyp " ^
clasohm@1316
   193
                         mk_pair (brackets x, y)) (commas fst, ids))) toks
clasohm@1316
   194
    end;
clasohm@1316
   195
clasohm@977
   196
  val opt_typs = repeat (simple_typ || ("(" $$-- complex_typ --$$ ")"));
clasohm@923
   197
  val constructor = name -- opt_typs -- opt_mixfix;
clasohm@923
   198
in
clasohm@923
   199
  val datatype_decl =
clasohm@923
   200
    tvars -- ident --$$ "=" -- enum1 "|" constructor >> mk_params;
clasohm@923
   201
end;
clasohm@923
   202
clasohm@923
   203
clasohm@923
   204
clasohm@923
   205
(** primrec **)
clasohm@923
   206
paulson@2922
   207
(*recursion equations have user-supplied names*)
berghofe@1845
   208
fun mk_primrec_decl_1 ((fname, tname), axms) =
clasohm@923
   209
  let
clasohm@1574
   210
    (*Isolate type name from the structure's identifier it may be stored in*)
clasohm@1574
   211
    val tname' = implode (snd (take_suffix (not_equal ".") (explode tname)));
clasohm@1574
   212
clasohm@923
   213
    fun mk_prove (name, eqn) =
clasohm@1264
   214
      "val " ^ name ^ " = store_thm (" ^ quote name
clasohm@1574
   215
      ^ ", prove_goalw thy [get_def thy "
clasohm@1574
   216
      ^ (quote (strip_quotes fname ^ "_" ^ tname')) ^ "] " ^ eqn ^ "\n\
clasohm@1264
   217
      \  (fn _ => [Simp_tac 1]));";
clasohm@1264
   218
clasohm@923
   219
    val axs = mk_list (map (fn (n, a) => mk_pair (quote n, a)) axms);
paulson@2922
   220
  in ("|> " ^ tname ^ "_add_primrec " ^ axs
paulson@2922
   221
      , 
paulson@2922
   222
      cat_lines (map mk_prove axms)
clasohm@1264
   223
      ^ "\nval dummy = Addsimps " ^ mk_list (map fst axms) ^ ";")
clasohm@1264
   224
  end;
clasohm@923
   225
paulson@2922
   226
(*recursion equations have no names*)
berghofe@1845
   227
fun mk_primrec_decl_2 ((fname, tname), axms) =
berghofe@1845
   228
  let
berghofe@1845
   229
    (*Isolate type name from the structure's identifier it may be stored in*)
berghofe@1845
   230
    val tname' = implode (snd (take_suffix (not_equal ".") (explode tname)));
berghofe@1845
   231
berghofe@1845
   232
    fun mk_prove eqn =
berghofe@1845
   233
      "prove_goalw thy [get_def thy "
berghofe@1845
   234
      ^ (quote (strip_quotes fname ^ "_" ^ tname')) ^ "] " ^ eqn ^ " \
berghofe@1845
   235
      \(fn _ => [Simp_tac 1])";
berghofe@1845
   236
berghofe@1845
   237
    val axs = mk_list (map (fn a => mk_pair ("\"\"", a)) axms);
paulson@2922
   238
  in ("|> " ^ tname ^ "_add_primrec " ^ axs
paulson@2922
   239
      ,
berghofe@1845
   240
      "val dummy = Addsimps " ^
berghofe@1845
   241
      brackets(space_implode ",\n" (map mk_prove axms)) ^ ";")
berghofe@1845
   242
  end;
berghofe@1845
   243
paulson@2922
   244
(*function name, argument type and either (name,axiom) pairs or just axioms*)
clasohm@923
   245
val primrec_decl =
berghofe@1845
   246
  (name -- long_id -- repeat1 (ident -- string) >> mk_primrec_decl_1) ||
berghofe@1845
   247
  (name -- long_id -- repeat1 string >> mk_primrec_decl_2) ;
clasohm@923
   248
clasohm@923
   249
clasohm@923
   250
paulson@2922
   251
paulson@2922
   252
(** rec: interface to Slind's TFL **)
paulson@2922
   253
paulson@2922
   254
paulson@3194
   255
(*fname: name of function being defined; rel: well-founded relation*)
paulson@3456
   256
fun mk_rec_decl ((((fname, rel), congs), ss), axms) =
paulson@2922
   257
  let val fid = trim fname
paulson@3194
   258
      val intrnl_name = fid ^ "_Intrnl"
paulson@2922
   259
  in
paulson@2922
   260
	 (";\n\n\
paulson@3194
   261
          \val _ = writeln \"Recursive function " ^ fid ^ "\"\n\
paulson@3194
   262
          \val (thy, pats_" ^ intrnl_name ^ ") = Tfl.define thy " ^ 
paulson@3345
   263
	                 quote fid ^ " " ^ 
paulson@3194
   264
	                 rel ^ "\n" ^ mk_big_list axms ^ ";\n\
paulson@2922
   265
          \val thy = thy"
paulson@2922
   266
         ,
paulson@3194
   267
          "structure " ^ fid ^ " =\n\
paulson@3194
   268
          \  struct\n\
paulson@3194
   269
          \  val _ = writeln \"Proofs for recursive function " ^ fid ^ "\"\n\
paulson@3194
   270
          \  val {rules, induct, tcs} = \n\
paulson@3456
   271
          \    \t Tfl.simplify_defn (" ^ ss ^ ", " ^ congs ^ ")\n\
paulson@3456
   272
          \    \t\t  (thy, (" ^ quote fid ^ ", pats_" ^ intrnl_name ^ "))\n\
paulson@3194
   273
          \  end;\n\
paulson@3194
   274
          \val pats_" ^ intrnl_name ^ " = ();\n")
paulson@2922
   275
  end;
paulson@2922
   276
paulson@3403
   277
val rec_decl = (name -- string -- 
paulson@3456
   278
		optional ("congs" $$-- string >> trim) "[]" -- 
paulson@3403
   279
		optional ("simpset" $$-- string >> trim) "!simpset" -- 
paulson@3403
   280
		repeat1 string >> mk_rec_decl) ;
paulson@2922
   281
paulson@2922
   282
paulson@2922
   283
wenzelm@3622
   284
(** augment thy syntax **)
clasohm@923
   285
wenzelm@3622
   286
in
clasohm@923
   287
wenzelm@3622
   288
val _ = ThySyn.add_syntax
wenzelm@3622
   289
 ["intrs", "monos", "con_defs", "congs", "simpset", "|"]
clasohm@1475
   290
 [axm_section "typedef" "|> Typedef.add_typedef" typedef_decl,
wenzelm@3980
   291
  (section "record" "|> Record.add_record" record_decl),
clasohm@923
   292
  ("inductive", inductive_decl ""),
clasohm@923
   293
  ("coinductive", inductive_decl "Co"),
clasohm@923
   294
  ("datatype", datatype_decl),
paulson@2922
   295
  ("primrec", primrec_decl),
paulson@2922
   296
  ("recdef", rec_decl)];
clasohm@923
   297
clasohm@923
   298
end;