src/HOL/thy_syntax.ML
author wenzelm
Mon Nov 03 21:13:24 1997 +0100 (1997-11-03)
changeset 4106 01fa6e7e7196
parent 4091 771b1f6422a8
child 4184 23a09f2fd687
permissions -rw-r--r--
tuned;
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
clasohm@923
     8
(*the kind of distinctiveness axioms depends on number of constructors*)
nipkow@2930
     9
val dtK = 7;  (* FIXME rename?, move? *)
clasohm@923
    10
wenzelm@3622
    11
wenzelm@3622
    12
local
clasohm@923
    13
clasohm@923
    14
open ThyParse;
clasohm@923
    15
clasohm@923
    16
clasohm@1475
    17
(** typedef **)
clasohm@923
    18
clasohm@1475
    19
fun mk_typedef_decl (((((opt_name, vs), t), mx), rhs), wt) =
clasohm@923
    20
  let
clasohm@923
    21
    val name' = if_none opt_name t;
clasohm@923
    22
    val name = strip_quotes name';
clasohm@923
    23
  in
clasohm@923
    24
    (cat_lines [name', mk_triple (t, mk_list vs, mx), rhs, wt],
clasohm@923
    25
      [name ^ "_def", "Rep_" ^ name, "Rep_" ^ name ^ "_inverse",
clasohm@923
    26
        "Abs_" ^ name ^ "_inverse"])
clasohm@923
    27
  end;
clasohm@923
    28
clasohm@1475
    29
val typedef_decl =
clasohm@923
    30
  optional ("(" $$-- name --$$ ")" >> Some) None --
clasohm@923
    31
  type_args -- name -- opt_infix --$$ "=" -- string -- opt_witness
clasohm@1475
    32
  >> mk_typedef_decl;
clasohm@923
    33
clasohm@923
    34
clasohm@923
    35
wenzelm@3980
    36
(** record **)
wenzelm@3980
    37
wenzelm@3980
    38
val record_decl =
wenzelm@3980
    39
  name --$$ "=" -- optional (name --$$ "+" >> (parens o cat "Some")) "None" --
wenzelm@3980
    40
  repeat1 ((name --$$ "::" -- typ) >> mk_pair)
wenzelm@4001
    41
  >> (fn ((x, y), zs) => cat_lines [x, y, mk_big_list zs]);
wenzelm@3980
    42
wenzelm@3980
    43
clasohm@923
    44
(** (co)inductive **)
clasohm@923
    45
clasohm@923
    46
(*co is either "" or "Co"*)
clasohm@923
    47
fun inductive_decl co =
clasohm@923
    48
  let
clasohm@923
    49
    fun mk_intr_name (s, _) =   (*the "op" cancels any infix status*)
clasohm@923
    50
      if Syntax.is_identifier s then "op " ^ s else "_";
clasohm@923
    51
    fun mk_params (((recs, ipairs), monos), con_defs) =
clasohm@923
    52
      let val big_rec_name = space_implode "_" (map (scan_to_id o trim) recs)
clasohm@923
    53
          and srec_tms = mk_list recs
clasohm@923
    54
          and sintrs   = mk_big_list (map snd ipairs)
paulson@3194
    55
          val intrnl_name = big_rec_name ^ "_Intrnl"
clasohm@923
    56
      in
clasohm@923
    57
         (";\n\n\
paulson@3194
    58
          \structure " ^ intrnl_name ^ " =\n\
clasohm@923
    59
          \  struct\n\
clasohm@923
    60
          \  val _ = writeln \"" ^ co ^
clasohm@923
    61
                     "Inductive definition " ^ big_rec_name ^ "\"\n\
paulson@1430
    62
          \  val rec_tms\t= map (readtm (sign_of thy) Ind_Syntax.termTVar) "
clasohm@923
    63
                           ^ srec_tms ^ "\n\
clasohm@923
    64
          \  and intr_tms\t= map (readtm (sign_of thy) propT)\n"
clasohm@923
    65
                           ^ sintrs ^ "\n\
paulson@1430
    66
          \  end;\n\n\
clasohm@923
    67
          \val thy = thy |> " ^ co ^ "Ind.add_fp_def_i \n    (" ^
paulson@3194
    68
             intrnl_name ^ ".rec_tms, " ^
paulson@3194
    69
             intrnl_name ^ ".intr_tms)"
clasohm@923
    70
         ,
clasohm@923
    71
          "structure " ^ big_rec_name ^ " =\n\
paulson@1430
    72
          \ let\n\
paulson@1430
    73
          \  val _ = writeln \"Proofs for " ^ co ^ 
paulson@1430
    74
                     "Inductive definition " ^ big_rec_name ^ "\"\n\
clasohm@923
    75
          \  structure Result = " ^ co ^ "Ind_section_Fun\n\
paulson@3194
    76
          \\t  (open " ^ intrnl_name ^ "\n\
paulson@1430
    77
          \\t   val thy\t\t= thy\n\
paulson@1430
    78
          \\t   val monos\t\t= " ^ monos ^ "\n\
paulson@1430
    79
          \\t   val con_defs\t\t= " ^ con_defs ^ ");\n\n\
clasohm@1465
    80
          \ in\n\
paulson@1430
    81
          \  struct\n\
clasohm@923
    82
          \  val " ^ mk_list (map mk_intr_name ipairs) ^ " = Result.intrs;\n\
clasohm@923
    83
          \  open Result\n\
paulson@1430
    84
          \  end\n\
paulson@1430
    85
          \ end;\n\n\
paulson@3194
    86
          \structure " ^ intrnl_name ^ " = struct end;\n\n"
clasohm@923
    87
         )
clasohm@923
    88
      end
clasohm@923
    89
    val ipairs = "intrs" $$-- repeat1 (ident -- !! string)
paulson@3403
    90
    fun optstring s = optional (s $$-- string >> trim) "[]"
clasohm@923
    91
  in
paulson@1788
    92
    repeat1 name -- ipairs -- optstring "monos" -- optstring "con_defs"
clasohm@923
    93
      >> mk_params
clasohm@923
    94
  end;
clasohm@923
    95
clasohm@923
    96
clasohm@923
    97
clasohm@923
    98
(** datatype **)
clasohm@923
    99
clasohm@923
   100
local
clasohm@923
   101
  (* FIXME err -> add_datatype *)
clasohm@923
   102
  fun mk_cons cs =
clasohm@923
   103
    (case duplicates (map (fst o fst) cs) of
clasohm@923
   104
      [] => map (fn ((s, ts), syn) => mk_triple (s, mk_list ts, syn)) cs
clasohm@923
   105
    | dups => error ("Duplicate constructors: " ^ commas_quote dups));
clasohm@923
   106
clasohm@923
   107
  (*generate names of distinctiveness axioms*)
clasohm@923
   108
  fun mk_distinct_rules cs tname =
clasohm@923
   109
    let
clasohm@923
   110
      val uqcs = map (fn ((s, _), _) => strip_quotes s) cs;
clasohm@923
   111
      (*combine all constructor names with all others w/o duplicates*)
clasohm@923
   112
      fun neg_one c = map (fn c2 => quote (c ^ "_not_" ^ c2));
clasohm@923
   113
      fun neg1 [] = []
clasohm@923
   114
        | neg1 (c1 :: cs) = neg_one c1 cs @ neg1 cs;
clasohm@923
   115
    in
clasohm@923
   116
      if length uqcs < dtK then neg1 uqcs
clasohm@923
   117
      else quote (tname ^ "_ord_distinct") ::
clasohm@923
   118
        map (fn c => quote (tname ^ "_ord_" ^ c)) uqcs
clasohm@923
   119
    end;
clasohm@923
   120
clasohm@923
   121
  fun mk_rules tname cons pre = " map (get_axiom thy) " ^
paulson@3194
   122
    mk_list (map (fn ((s,_), _) => quote (tname ^ pre ^ strip_quotes s)) cons);
clasohm@923
   123
clasohm@1668
   124
  (*generate string for calling add_datatype and build_record*)
clasohm@923
   125
  fun mk_params ((ts, tname), cons) =
wenzelm@4106
   126
    "val (thy,"^tname^"_add_primrec,size_"^tname^"_eqns,split_"^tname^"_eqn) =\n\
nipkow@4032
   127
    \    Datatype.add_datatype\n"
clasohm@923
   128
    ^ mk_triple (mk_list ts, quote tname, mk_list (mk_cons cons)) ^ " thy\n\
wenzelm@4106
   129
    \val thy = ("^tname^"_add_primrec size_"^tname^"_eqns thy)\n\
wenzelm@4106
   130
    \val _ = deny (" ^ quote tname ^ " mem (Sign.stamp_names_of (sign_of thy)))\n\
nipkow@3665
   131
    \   (\"Datatype \\\""^tname^"\\\" would clash with the theory of the same name!\");\n\
nipkow@3665
   132
    \structure " ^ tname ^ " =\n\
clasohm@923
   133
    \struct\n\
clasohm@923
   134
    \ val inject = map (get_axiom thy) " ^
clasohm@923
   135
        mk_list (map (fn ((s, _), _) => quote ("inject_" ^ strip_quotes s))
clasohm@923
   136
          (filter_out (null o snd o fst) cons)) ^ ";\n\
clasohm@923
   137
    \ val distinct = " ^
clasohm@923
   138
        (if length cons < dtK then "let val distinct' = " else "") ^
clasohm@923
   139
        "map (get_axiom thy) " ^ mk_list (mk_distinct_rules cons tname) ^
clasohm@923
   140
        (if length cons < dtK then
clasohm@923
   141
          "  in distinct' @ (map (fn t => sym COMP (t RS contrapos))\
clasohm@923
   142
          \ distinct') end"
clasohm@923
   143
         else "") ^ ";\n\
clasohm@923
   144
    \ val induct = get_axiom thy \"" ^ tname ^ "_induct\";\n\
clasohm@923
   145
    \ val cases =" ^ mk_rules tname cons "_case_" ^ ";\n\
clasohm@923
   146
    \ val recs =" ^ mk_rules tname cons "_rec_" ^ ";\n\
clasohm@923
   147
    \ val simps = inject @ distinct @ cases @ recs;\n\
clasohm@923
   148
    \ fun induct_tac a = res_inst_tac [(" ^ quote tname ^ ", a)] induct;\n\
clasohm@1264
   149
    \end;\n\
wenzelm@4106
   150
    \val thy = thy |> Dtype.add_record " ^
wenzelm@4106
   151
      mk_triple
wenzelm@4106
   152
        ("Sign.intern_tycon (sign_of thy) " ^ quote tname,
wenzelm@4106
   153
          mk_list (map (fst o fst) cons),
wenzelm@4106
   154
          tname ^ ".induct_tac") ^ ";\n\
wenzelm@4106
   155
    \val dummy = context thy;\n\
nipkow@2930
   156
    \val dummy = Addsimps(" ^ tname ^ ".cases @ " ^ tname ^ ".recs);\n\
nipkow@2930
   157
    \val dummy = AddIffs " ^ tname ^ ".inject;\n\
nipkow@2930
   158
    \val dummy = " ^
nipkow@2930
   159
      (if length cons < dtK then "AddIffs " else "Addsimps ") ^
nipkow@3308
   160
      tname ^ ".distinct;\n\
nipkow@3308
   161
    \val dummy = Addsimps(map (fn (_,eqn) =>\n\
nipkow@3308
   162
    \ prove_goalw thy [get_def thy " ^ quote("size_"^tname) ^
nipkow@4032
   163
                     "] eqn (fn _ => [Simp_tac 1])) size_"^tname^"_eqns);\n\
nipkow@4032
   164
    \val split_"^tname^"_case = prove_goal thy split_"^tname^"_eqn\n\
wenzelm@4106
   165
    \  (fn _ => [#exhaust_tac (datatype_info thy (Sign.intern_tycon (sign_of thy) "
wenzelm@4106
   166
      ^ quote tname ^ ")) \""^tname^"0\" 1,\n\
wenzelm@4106
   167
    \            ALLGOALS Asm_simp_tac]);\n\
wenzelm@4106
   168
    \val thy = thy\n";
wenzelm@4106
   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) "[]" -- 
wenzelm@4091
   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"),
wenzelm@4106
   294
  (section "datatype" "" datatype_decl),
paulson@2922
   295
  ("primrec", primrec_decl),
paulson@2922
   296
  ("recdef", rec_decl)];
clasohm@923
   297
clasohm@923
   298
end;