src/HOL/thy_syntax.ML
author berghofe
Thu Jul 30 22:58:05 1998 +0200 (1998-07-30)
changeset 5221 7e9f9ab61798
parent 5218 1a49756a2e68
child 5222 3e40c6bffb87
permissions -rw-r--r--
Fixed primrec.
     1 (*  Title:      HOL/thy_syntax.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel and Lawrence C Paulson and Carsten Clasohm
     4 
     5 Additional theory file sections for HOL.
     6 *)
     7 
     8 local
     9 
    10 open ThyParse;
    11 
    12 
    13 (** typedef **)
    14 
    15 fun mk_typedef_decl (((((opt_name, vs), t), mx), rhs), wt) =
    16   let
    17     val name' = if_none opt_name t;
    18     val name = strip_quotes name';
    19   in
    20     (cat_lines [name', mk_triple (t, mk_list vs, mx), rhs, wt],
    21       [name ^ "_def", "Rep_" ^ name, "Rep_" ^ name ^ "_inverse",
    22         "Abs_" ^ name ^ "_inverse"])
    23   end;
    24 
    25 val typedef_decl =
    26   optional ("(" $$-- name --$$ ")" >> Some) None --
    27   type_args -- name -- opt_infix --$$ "=" -- string -- opt_witness
    28   >> mk_typedef_decl;
    29 
    30 
    31 
    32 (** record **)
    33 
    34 val record_decl =
    35   (type_args -- name >> (mk_pair o apfst mk_list)) --$$ "="
    36     -- optional (typ --$$ "+" >> (parens o cat "Some")) "None"
    37     -- repeat1 ((name --$$ "::" -- typ) >> mk_pair)
    38   >> (fn ((x, y), zs) => cat_lines [x, y, mk_big_list zs]);
    39 
    40 
    41 (** (co)inductive **)
    42 
    43 fun inductive_decl coind =
    44   let
    45     fun mk_intr_name (s, _) =   (*the "op" cancels any infix status*)
    46       if Syntax.is_identifier s then "op " ^ s else "_";
    47     fun mk_params (((recs, ipairs), monos), con_defs) =
    48       let val big_rec_name = space_implode "_" (map (scan_to_id o trim) recs)
    49           and srec_tms = mk_list recs
    50           and sintrs   = mk_big_list (map snd ipairs)
    51       in
    52         ";\n\n\
    53         \local\n\
    54         \val (thy, {defs, mono, unfold, intrs, elims, mk_cases, induct, ...}) =\n\
    55         \  InductivePackage.add_inductive true " ^
    56         (if coind then "true " else "false ") ^ srec_tms ^ " " ^
    57          sintrs ^ " " ^ monos ^ " " ^ con_defs ^ " thy;\n\
    58         \in\n\
    59         \structure " ^ big_rec_name ^ " =\n\
    60         \struct\n\
    61         \  val defs = defs;\n\
    62         \  val mono = mono;\n\
    63         \  val unfold = unfold;\n\
    64         \  val intrs = intrs;\n\
    65         \  val elims = elims;\n\
    66         \  val elim = hd elims;\n\
    67         \  val " ^ (if coind then "co" else "") ^ "induct = induct;\n\
    68         \  val mk_cases = mk_cases;\n\
    69         \  val " ^ mk_list (map mk_intr_name ipairs) ^ " = intrs;\n\
    70         \end;\n\
    71         \val thy = thy;\nend;\n\
    72         \val thy = thy\n"
    73       end
    74     val ipairs = "intrs" $$-- repeat1 (ident -- !! string)
    75     fun optstring s = optional (s $$-- string >> trim) "[]"
    76   in
    77     repeat1 name -- ipairs -- optstring "monos" -- optstring "con_defs"
    78       >> mk_params
    79   end;
    80 
    81 
    82 (** datatype **)
    83 
    84 local
    85   (*** generate string for calling add_datatype ***)
    86   (*** and bindig theorems to ML identifiers    ***)
    87 
    88   fun mk_bind_thms_string names =
    89    (case find_first (not o Syntax.is_identifier) names of
    90       Some id => (warning (id ^ " is not a valid identifier"); "")
    91     | None =>
    92         let
    93           fun mk_dt_struct (s, (id, i)) =
    94             s ^ "structure " ^ id ^ " =\n\
    95             \struct\n\
    96             \  val distinct = nth_elem (" ^ i ^ ", distinct);\n\
    97             \  val inject = nth_elem (" ^ i ^ ", inject);\n\
    98             \  val exhaust = nth_elem (" ^ i ^ ", exhaustion);\n\
    99             \  val cases = nth_elem (" ^ i ^ ", case_thms);\n\
   100             \  val (split, split_asm) = nth_elem (" ^ i ^ ", split_thms);\n" ^
   101               (if length names = 1 then
   102                  "  val induct = induction;\n\
   103                  \  val recs = rec_thms;\n\
   104                  \  val simps = simps;\n\
   105                  \  val size = size;\n"
   106                else "") ^
   107             "end;\n";
   108 
   109           val structs = foldl mk_dt_struct
   110             ("", (names ~~ (map string_of_int (0 upto length names - 1))));
   111 
   112         in
   113           (if length names > 1 then
   114              "structure " ^ (space_implode "_" names) ^ " =\n\
   115              \struct\n\
   116              \val induct = induction;\n\
   117              \val recs = rec_thms;\n\
   118              \val simps = simps;\n\
   119              \val size = size;\n"
   120            else "") ^ structs ^
   121           (if length names > 1 then "end;\n" else "")
   122         end);
   123 
   124   fun mk_dt_string dts =
   125     let
   126       val names = map (fn ((((alt_name, _), name), _), _) =>
   127         strip_quotes (if_none alt_name name)) dts;
   128 
   129       val add_datatype_args = brackets (commas (map quote names)) ^ " " ^
   130         brackets (commas (map (fn ((((_, vs), name), mx), cs) =>
   131           parens (brackets (commas vs) ^ ", " ^ name ^ ", " ^ mx ^ ", " ^
   132             brackets (commas cs))) dts));
   133 
   134     in
   135       ";\nlocal\n\
   136       \val (thy, {distinct, inject, exhaustion, rec_thms,\n\
   137       \  case_thms, split_thms, induction, size, simps}) =\n\
   138       \  DatatypePackage.add_datatype " ^ add_datatype_args ^ " thy;\n\
   139       \in\n" ^ mk_bind_thms_string names ^
   140       "val thy = thy;\nend;\nval thy = thy\n"
   141     end;
   142 
   143   fun mk_rep_dt_string (((names, distinct), inject), induct) =
   144     ";\nlocal\n\
   145     \val (thy, {distinct, inject, exhaustion, rec_thms,\n\
   146     \  case_thms, split_thms, induction, size, simps}) =\n\
   147     \  DatatypePackage.add_rep_datatype " ^
   148     (case names of
   149         Some names => "(Some [" ^ commas (map quote names) ^ "]) (" ^
   150           distinct ^ ") (" ^ inject ^ ") (" ^ induct ^ ") thy;\nin\n" ^
   151             mk_bind_thms_string names
   152       | None => "None (" ^ distinct ^ ") (" ^ inject ^ ") (" ^ induct ^
   153           ") thy;\nin\n") ^
   154     "val thy = thy;\nend;\nval thy = thy\n";
   155 
   156   (*** parsers ***)
   157 
   158   val simple_typ = ident || (type_var >> strip_quotes);
   159 
   160   fun complex_typ toks =
   161     let val typ = simple_typ || "(" $$-- complex_typ --$$ ")";
   162         val typ2 = complex_typ || "(" $$-- complex_typ --$$ ")";
   163     in
   164      (typ ^^ (repeat ident >> (cat "" o space_implode " ")) ||
   165       "(" $$-- !! (list1 typ2 >> (parens o commas)) --$$ ")" ^^ !!
   166         (repeat1 ident >> (cat "" o space_implode " "))) toks
   167     end;
   168 
   169   val opt_typs = repeat ((string >> strip_quotes) ||
   170     simple_typ || ("(" $$-- complex_typ --$$ ")"));
   171   val constructor = name -- opt_typs -- opt_mixfix >> (fn ((n, Ts), mx) =>
   172     parens (n ^ ", " ^ mx ^ ", " ^ brackets (commas (map quote Ts))));
   173   val opt_name = optional ("(" $$-- name --$$ ")" >> Some) None
   174 
   175 in
   176   val datatype_decl =
   177     enum1 "and" (opt_name -- type_args -- name -- opt_infix --$$ "=" --
   178       enum1 "|" constructor) >> mk_dt_string;
   179   val rep_datatype_decl =
   180     ((optional ((repeat1 (name >> strip_quotes)) >> Some) None) --
   181       ("distinct" $$-- (name >> strip_quotes)) -- ("inject" $$--
   182         (name >> strip_quotes)) -- ("induct" $$-- (name >> strip_quotes))) >>
   183           mk_rep_dt_string;
   184 end;
   185 
   186 
   187 (** primrec **)
   188 
   189 fun mk_primrec_decl (alt_name, (names, eqns)) =
   190   ";\nval (thy, " ^ (if null names then "_" else brackets (commas names)) ^
   191   ") = PrimrecPackage.add_primrec " ^ alt_name ^ " " ^
   192     brackets (commas_quote names) ^ " " ^ brackets (commas eqns) ^ " thy;\n\
   193   \val thy = thy\n";
   194 
   195 (* either names and axioms or just axioms *)
   196 val primrec_decl = (optional ("(" $$-- name --$$ ")" >> (cat "Some")) "None" --
   197   ((repeat1 (ident -- string) >> ListPair.unzip) ||
   198    (repeat1 string >> (pair [])))) >> mk_primrec_decl;
   199 
   200 
   201 (** rec: interface to Slind's TFL **)
   202 
   203 
   204 (*fname: name of function being defined; rel: well-founded relation*)
   205 fun mk_rec_decl ((((fname, rel), congs), ss), axms) =
   206   let val fid = trim fname
   207       val intrnl_name = fid ^ "_Intrnl"
   208   in
   209 	 (";\n\n\
   210           \val _ = writeln \"Recursive function " ^ fid ^ "\"\n\
   211           \val (thy, pats_" ^ intrnl_name ^ ") = Tfl.define thy " ^ 
   212 	                 quote fid ^ " " ^ 
   213 	                 rel ^ "\n" ^ mk_big_list axms ^ ";\n\
   214           \val thy = thy"
   215          ,
   216           "structure " ^ fid ^ " =\n\
   217           \  struct\n\
   218           \  val _ = writeln \"Proofs for recursive function " ^ fid ^ "\"\n\
   219           \  val {rules, induct, tcs} = \n\
   220           \    \t Tfl.simplify_defn (" ^ ss ^ ", " ^ congs ^ ")\n\
   221           \    \t\t  (thy, (" ^ quote fid ^ ", pats_" ^ intrnl_name ^ "))\n\
   222           \  end;\n\
   223           \val pats_" ^ intrnl_name ^ " = ();\n")
   224   end;
   225 
   226 val rec_decl = (name -- string -- 
   227 		optional ("congs" $$-- string >> trim) "[]" -- 
   228 		optional ("simpset" $$-- string >> trim) "simpset()" -- 
   229 		repeat1 string >> mk_rec_decl) ;
   230 
   231 
   232 
   233 (** augment thy syntax **)
   234 
   235 in
   236 
   237 val _ = ThySyn.add_syntax
   238  ["intrs", "monos", "con_defs", "congs", "simpset", "|",
   239   "and", "distinct", "inject", "induct"]
   240  [axm_section "typedef" "|> TypedefPackage.add_typedef" typedef_decl,
   241   section "record" "|> RecordPackage.add_record" record_decl,
   242   section "inductive" "" (inductive_decl false),
   243   section "coinductive" "" (inductive_decl true),
   244   section "datatype" "" datatype_decl,
   245   section "rep_datatype" "" rep_datatype_decl,
   246   section "primrec" "" primrec_decl,
   247   ("recdef", rec_decl)];
   248 
   249 end;