src/HOL/thy_syntax.ML
author paulson
Mon Oct 07 10:28:44 1996 +0200 (1996-10-07)
changeset 2056 93c093620c28
parent 1845 afa622bc829d
child 2922 580647a879cf
permissions -rw-r--r--
Removed commands made redundant by new one-point rules
     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 TODO:
     8   move datatype / primrec stuff to pre_datatype.ML (?)
     9 *)
    10 
    11 (*the kind of distinctiveness axioms depends on number of constructors*)
    12 val dtK = 5;  (* FIXME rename?, move? *)
    13 
    14 structure ThySynData: THY_SYN_DATA =
    15 struct
    16 
    17 open ThyParse;
    18 
    19 
    20 (** typedef **)
    21 
    22 fun mk_typedef_decl (((((opt_name, vs), t), mx), rhs), wt) =
    23   let
    24     val name' = if_none opt_name t;
    25     val name = strip_quotes name';
    26   in
    27     (cat_lines [name', mk_triple (t, mk_list vs, mx), rhs, wt],
    28       [name ^ "_def", "Rep_" ^ name, "Rep_" ^ name ^ "_inverse",
    29         "Abs_" ^ name ^ "_inverse"])
    30   end;
    31 
    32 val typedef_decl =
    33   optional ("(" $$-- name --$$ ")" >> Some) None --
    34   type_args -- name -- opt_infix --$$ "=" -- string -- opt_witness
    35   >> mk_typedef_decl;
    36 
    37 
    38 
    39 (** (co)inductive **)
    40 
    41 (*co is either "" or "Co"*)
    42 fun inductive_decl co =
    43   let
    44     fun mk_intr_name (s, _) =   (*the "op" cancels any infix status*)
    45       if Syntax.is_identifier s then "op " ^ s else "_";
    46     fun mk_params (((recs, ipairs), monos), con_defs) =
    47       let val big_rec_name = space_implode "_" (map (scan_to_id o trim) recs)
    48           and srec_tms = mk_list recs
    49           and sintrs   = mk_big_list (map snd ipairs)
    50           val stri_name = big_rec_name ^ "_Intrnl"
    51       in
    52          (";\n\n\
    53           \structure " ^ stri_name ^ " =\n\
    54           \  struct\n\
    55           \  val _ = writeln \"" ^ co ^
    56                      "Inductive definition " ^ big_rec_name ^ "\"\n\
    57           \  val rec_tms\t= map (readtm (sign_of thy) Ind_Syntax.termTVar) "
    58                            ^ srec_tms ^ "\n\
    59           \  and intr_tms\t= map (readtm (sign_of thy) propT)\n"
    60                            ^ sintrs ^ "\n\
    61           \  end;\n\n\
    62           \val thy = thy |> " ^ co ^ "Ind.add_fp_def_i \n    (" ^
    63              stri_name ^ ".rec_tms, " ^
    64              stri_name ^ ".intr_tms)"
    65          ,
    66           "structure " ^ big_rec_name ^ " =\n\
    67           \ let\n\
    68           \  val _ = writeln \"Proofs for " ^ co ^ 
    69                      "Inductive definition " ^ big_rec_name ^ "\"\n\
    70           \  structure Result = " ^ co ^ "Ind_section_Fun\n\
    71           \\t  (open " ^ stri_name ^ "\n\
    72           \\t   val thy\t\t= thy\n\
    73           \\t   val monos\t\t= " ^ monos ^ "\n\
    74           \\t   val con_defs\t\t= " ^ con_defs ^ ");\n\n\
    75           \ in\n\
    76           \  struct\n\
    77           \  val " ^ mk_list (map mk_intr_name ipairs) ^ " = Result.intrs;\n\
    78           \  open Result\n\
    79           \  end\n\
    80           \ end;\n\n\
    81           \structure " ^ stri_name ^ " = struct end;\n\n"
    82          )
    83       end
    84     val ipairs = "intrs" $$-- repeat1 (ident -- !! string)
    85     fun optstring s = optional (s $$-- string) "\"[]\"" >> trim
    86   in
    87     repeat1 name -- ipairs -- optstring "monos" -- optstring "con_defs"
    88       >> mk_params
    89   end;
    90 
    91 
    92 
    93 (** datatype **)
    94 
    95 local
    96   (* FIXME err -> add_datatype *)
    97   fun mk_cons cs =
    98     (case duplicates (map (fst o fst) cs) of
    99       [] => map (fn ((s, ts), syn) => mk_triple (s, mk_list ts, syn)) cs
   100     | dups => error ("Duplicate constructors: " ^ commas_quote dups));
   101 
   102   (*generate names of distinctiveness axioms*)
   103   fun mk_distinct_rules cs tname =
   104     let
   105       val uqcs = map (fn ((s, _), _) => strip_quotes s) cs;
   106       (*combine all constructor names with all others w/o duplicates*)
   107       fun neg_one c = map (fn c2 => quote (c ^ "_not_" ^ c2));
   108       fun neg1 [] = []
   109         | neg1 (c1 :: cs) = neg_one c1 cs @ neg1 cs;
   110     in
   111       if length uqcs < dtK then neg1 uqcs
   112       else quote (tname ^ "_ord_distinct") ::
   113         map (fn c => quote (tname ^ "_ord_" ^ c)) uqcs
   114     end;
   115 
   116   fun mk_rules tname cons pre = " map (get_axiom thy) " ^
   117     mk_list (map (fn ((s, _), _) => quote (tname ^ pre ^ strip_quotes s)) cons);
   118 
   119   (*generate string for calling add_datatype and build_record*)
   120   fun mk_params ((ts, tname), cons) =
   121    ("val (thy, " ^ tname ^ "_add_primrec) = Datatype.add_datatype\n"
   122     ^ mk_triple (mk_list ts, quote tname, mk_list (mk_cons cons)) ^ " thy\n\
   123     \val thy = thy",
   124     "structure " ^ tname ^ " =\n\
   125     \struct\n\
   126     \ val inject = map (get_axiom thy) " ^
   127         mk_list (map (fn ((s, _), _) => quote ("inject_" ^ strip_quotes s))
   128           (filter_out (null o snd o fst) cons)) ^ ";\n\
   129     \ val distinct = " ^
   130         (if length cons < dtK then "let val distinct' = " else "") ^
   131         "map (get_axiom thy) " ^ mk_list (mk_distinct_rules cons tname) ^
   132         (if length cons < dtK then
   133           "  in distinct' @ (map (fn t => sym COMP (t RS contrapos))\
   134           \ distinct') end"
   135          else "") ^ ";\n\
   136     \ val induct = get_axiom thy \"" ^ tname ^ "_induct\";\n\
   137     \ val cases =" ^ mk_rules tname cons "_case_" ^ ";\n\
   138     \ val recs =" ^ mk_rules tname cons "_rec_" ^ ";\n\
   139     \ val simps = inject @ distinct @ cases @ recs;\n\
   140     \ fun induct_tac a = res_inst_tac [(" ^ quote tname ^ ", a)] induct;\n\
   141     \end;\n\
   142     \val dummy = datatypes := Dtype.build_record (thy, " ^
   143       mk_pair (quote tname, mk_list (map (fst o fst) cons)) ^
   144       ", " ^ tname ^ ".induct_tac) :: (!datatypes);\n\
   145     \val dummy = Addsimps " ^ tname ^ ".simps;\n");
   146 
   147   (*parsers*)
   148   val tvars = type_args >> map (cat "dtVar");
   149 
   150   val simple_typ = ident >> (cat "dtTyp" o curry mk_pair "[]" o quote) ||
   151     type_var >> cat "dtVar";
   152 
   153   fun complex_typ toks =
   154     let val typ = simple_typ || "(" $$-- complex_typ --$$ ")";
   155         val typ2 = complex_typ || "(" $$-- complex_typ --$$ ")";
   156     in
   157      (typ -- repeat (ident>>quote) >>
   158         (foldl (fn (x,y) => "dtTyp " ^ mk_pair (brackets x, y))) ||
   159       "(" $$-- !! (list1 typ2) --$$ ")" -- !! (repeat1 (ident>>quote)) >>
   160        (fn (fst, ids) => foldl (fn (x,y) => "dtTyp " ^
   161                          mk_pair (brackets x, y)) (commas fst, ids))) toks
   162     end;
   163 
   164   val opt_typs = repeat (simple_typ || ("(" $$-- complex_typ --$$ ")"));
   165   val constructor = name -- opt_typs -- opt_mixfix;
   166 in
   167   val datatype_decl =
   168     tvars -- ident --$$ "=" -- enum1 "|" constructor >> mk_params;
   169 end;
   170 
   171 
   172 
   173 (** primrec **)
   174 
   175 fun mk_primrec_decl_1 ((fname, tname), axms) =
   176   let
   177     (*Isolate type name from the structure's identifier it may be stored in*)
   178     val tname' = implode (snd (take_suffix (not_equal ".") (explode tname)));
   179 
   180     fun mk_prove (name, eqn) =
   181       "val " ^ name ^ " = store_thm (" ^ quote name
   182       ^ ", prove_goalw thy [get_def thy "
   183       ^ (quote (strip_quotes fname ^ "_" ^ tname')) ^ "] " ^ eqn ^ "\n\
   184       \  (fn _ => [Simp_tac 1]));";
   185 
   186     val axs = mk_list (map (fn (n, a) => mk_pair (quote n, a)) axms);
   187   in ("|> " ^ tname ^ "_add_primrec " ^ axs, cat_lines (map mk_prove axms)
   188       ^ "\nval dummy = Addsimps " ^ mk_list (map fst axms) ^ ";")
   189   end;
   190 
   191 fun mk_primrec_decl_2 ((fname, tname), axms) =
   192   let
   193     (*Isolate type name from the structure's identifier it may be stored in*)
   194     val tname' = implode (snd (take_suffix (not_equal ".") (explode tname)));
   195 
   196     fun mk_prove eqn =
   197       "prove_goalw thy [get_def thy "
   198       ^ (quote (strip_quotes fname ^ "_" ^ tname')) ^ "] " ^ eqn ^ " \
   199       \(fn _ => [Simp_tac 1])";
   200 
   201     val axs = mk_list (map (fn a => mk_pair ("\"\"", a)) axms);
   202   in ("|> " ^ tname ^ "_add_primrec " ^ axs,
   203       "val dummy = Addsimps " ^
   204       brackets(space_implode ",\n" (map mk_prove axms)) ^ ";")
   205   end;
   206 
   207 val primrec_decl =
   208   (name -- long_id -- repeat1 (ident -- string) >> mk_primrec_decl_1) ||
   209   (name -- long_id -- repeat1 string >> mk_primrec_decl_2) ;
   210 
   211 
   212 
   213 (** sections **)
   214 
   215 val user_keywords = ["intrs", "monos", "con_defs", "|"];
   216 
   217 val user_sections =
   218  [axm_section "typedef" "|> Typedef.add_typedef" typedef_decl,
   219   ("inductive", inductive_decl ""),
   220   ("coinductive", inductive_decl "Co"),
   221   ("datatype", datatype_decl),
   222   ("primrec", primrec_decl)];
   223 
   224 
   225 end;
   226 
   227 
   228 structure ThySyn = ThySynFun(ThySynData);
   229 init_thy_reader ();
   230