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