src/HOL/thy_syntax.ML
author clasohm
Thu Nov 02 12:45:58 1995 +0100 (1995-11-02)
changeset 1316 ce35d42d2190
parent 1264 3eb91524b938
child 1430 439e1476a7f8
permissions -rw-r--r--
extended complex_typ
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*)
clasohm@923
    12
val dtK = 5;  (* FIXME rename?, move? *)
clasohm@923
    13
clasohm@923
    14
structure ThySynData: THY_SYN_DATA =
clasohm@923
    15
struct
clasohm@923
    16
clasohm@923
    17
open ThyParse;
clasohm@923
    18
clasohm@923
    19
clasohm@923
    20
(** subtype **)
clasohm@923
    21
clasohm@923
    22
fun mk_subtype_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@923
    32
val subtype_decl =
clasohm@923
    33
  optional ("(" $$-- name --$$ ")" >> Some) None --
clasohm@923
    34
  type_args -- name -- opt_infix --$$ "=" -- string -- opt_witness
clasohm@923
    35
  >> mk_subtype_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)
clasohm@923
    50
          val stri_name = big_rec_name ^ "_Intrnl"
clasohm@923
    51
      in
clasohm@923
    52
         (";\n\n\
clasohm@923
    53
          \structure " ^ stri_name ^ " =\n\
clasohm@923
    54
          \ let open Ind_Syntax in\n\
clasohm@923
    55
          \  struct\n\
clasohm@923
    56
          \  val _ = writeln \"" ^ co ^
clasohm@923
    57
                     "Inductive definition " ^ big_rec_name ^ "\"\n\
clasohm@923
    58
          \  val rec_tms\t= map (readtm (sign_of thy) termTVar) "
clasohm@923
    59
                           ^ srec_tms ^ "\n\
clasohm@923
    60
          \  and intr_tms\t= map (readtm (sign_of thy) propT)\n"
clasohm@923
    61
                           ^ sintrs ^ "\n\
clasohm@923
    62
          \  end\n\
clasohm@923
    63
          \ end;\n\n\
clasohm@923
    64
          \val thy = thy |> " ^ co ^ "Ind.add_fp_def_i \n    (" ^
clasohm@923
    65
             stri_name ^ ".rec_tms, " ^
clasohm@923
    66
             stri_name ^ ".intr_tms)"
clasohm@923
    67
         ,
clasohm@923
    68
          "structure " ^ big_rec_name ^ " =\n\
clasohm@923
    69
          \  struct\n\
clasohm@923
    70
          \  structure Result = " ^ co ^ "Ind_section_Fun\n\
clasohm@923
    71
          \  (open " ^ stri_name ^ "\n\
clasohm@923
    72
          \   val thy\t\t= thy\n\
clasohm@923
    73
          \   val monos\t\t= " ^ monos ^ "\n\
clasohm@923
    74
          \   val con_defs\t\t= " ^ con_defs ^ ");\n\n\
clasohm@923
    75
          \  val " ^ mk_list (map mk_intr_name ipairs) ^ " = Result.intrs;\n\
clasohm@923
    76
          \  open Result\n\
clasohm@923
    77
          \  end\n"
clasohm@923
    78
         )
clasohm@923
    79
      end
clasohm@923
    80
    val ipairs = "intrs" $$-- repeat1 (ident -- !! string)
clasohm@923
    81
    fun optstring s = optional (s $$-- string) "\"[]\"" >> trim
clasohm@923
    82
  in
clasohm@923
    83
    repeat1 string -- ipairs -- optstring "monos" -- optstring "con_defs"
clasohm@923
    84
      >> mk_params
clasohm@923
    85
  end;
clasohm@923
    86
clasohm@923
    87
clasohm@923
    88
clasohm@923
    89
(** datatype **)
clasohm@923
    90
clasohm@923
    91
local
clasohm@923
    92
  (* FIXME err -> add_datatype *)
clasohm@923
    93
  fun mk_cons cs =
clasohm@923
    94
    (case duplicates (map (fst o fst) cs) of
clasohm@923
    95
      [] => map (fn ((s, ts), syn) => mk_triple (s, mk_list ts, syn)) cs
clasohm@923
    96
    | dups => error ("Duplicate constructors: " ^ commas_quote dups));
clasohm@923
    97
clasohm@923
    98
  (*generate names of distinctiveness axioms*)
clasohm@923
    99
  fun mk_distinct_rules cs tname =
clasohm@923
   100
    let
clasohm@923
   101
      val uqcs = map (fn ((s, _), _) => strip_quotes s) cs;
clasohm@923
   102
      (*combine all constructor names with all others w/o duplicates*)
clasohm@923
   103
      fun neg_one c = map (fn c2 => quote (c ^ "_not_" ^ c2));
clasohm@923
   104
      fun neg1 [] = []
clasohm@923
   105
        | neg1 (c1 :: cs) = neg_one c1 cs @ neg1 cs;
clasohm@923
   106
    in
clasohm@923
   107
      if length uqcs < dtK then neg1 uqcs
clasohm@923
   108
      else quote (tname ^ "_ord_distinct") ::
clasohm@923
   109
        map (fn c => quote (tname ^ "_ord_" ^ c)) uqcs
clasohm@923
   110
    end;
clasohm@923
   111
clasohm@923
   112
  fun mk_rules tname cons pre = " map (get_axiom thy) " ^
clasohm@923
   113
    mk_list (map (fn ((s, _), _) => quote (tname ^ pre ^ strip_quotes s)) cons);
clasohm@923
   114
clasohm@923
   115
  (*generate string for calling add_datatype*)
clasohm@923
   116
  fun mk_params ((ts, tname), cons) =
clasohm@923
   117
   ("val (thy, " ^ tname ^ "_add_primrec) = Datatype.add_datatype\n"
clasohm@923
   118
    ^ mk_triple (mk_list ts, quote tname, mk_list (mk_cons cons)) ^ " thy\n\
clasohm@923
   119
    \val thy = thy",
clasohm@923
   120
    "structure " ^ tname ^ " =\n\
clasohm@923
   121
    \struct\n\
clasohm@923
   122
    \ val inject = map (get_axiom thy) " ^
clasohm@923
   123
        mk_list (map (fn ((s, _), _) => quote ("inject_" ^ strip_quotes s))
clasohm@923
   124
          (filter_out (null o snd o fst) cons)) ^ ";\n\
clasohm@923
   125
    \ val distinct = " ^
clasohm@923
   126
        (if length cons < dtK then "let val distinct' = " else "") ^
clasohm@923
   127
        "map (get_axiom thy) " ^ mk_list (mk_distinct_rules cons tname) ^
clasohm@923
   128
        (if length cons < dtK then
clasohm@923
   129
          "  in distinct' @ (map (fn t => sym COMP (t RS contrapos))\
clasohm@923
   130
          \ distinct') end"
clasohm@923
   131
         else "") ^ ";\n\
clasohm@923
   132
    \ val induct = get_axiom thy \"" ^ tname ^ "_induct\";\n\
clasohm@923
   133
    \ val cases =" ^ mk_rules tname cons "_case_" ^ ";\n\
clasohm@923
   134
    \ val recs =" ^ mk_rules tname cons "_rec_" ^ ";\n\
clasohm@923
   135
    \ val simps = inject @ distinct @ cases @ recs;\n\
clasohm@923
   136
    \ fun induct_tac a = res_inst_tac [(" ^ quote tname ^ ", a)] induct;\n\
clasohm@1264
   137
    \end;\n\
clasohm@1264
   138
    \val dummy = Addsimps " ^ tname ^ ".simps;\n");
clasohm@923
   139
clasohm@923
   140
  (*parsers*)
clasohm@923
   141
  val tvars = type_args >> map (cat "dtVar");
clasohm@1316
   142
clasohm@1316
   143
  val simple_typ = ident >> (cat "dtTyp" o curry mk_pair "[]" o quote) ||
clasohm@1316
   144
    type_var >> cat "dtVar";
clasohm@1316
   145
clasohm@1251
   146
  fun complex_typ toks =
clasohm@1316
   147
    let val typ = simple_typ || "(" $$-- complex_typ --$$ ")";
clasohm@1316
   148
        val typ2 = complex_typ || "(" $$-- complex_typ --$$ ")";
clasohm@1316
   149
    in
clasohm@1316
   150
     (typ -- repeat (ident>>quote) >>
clasohm@1316
   151
        (foldl (fn (x,y) => "dtTyp " ^ mk_pair (brackets x, y))) ||
clasohm@1316
   152
      "(" $$-- !! (list1 typ2) --$$ ")" -- !! (repeat1 (ident>>quote)) >>
clasohm@1316
   153
       (fn (fst, ids) => foldl (fn (x,y) => "dtTyp " ^
clasohm@1316
   154
                         mk_pair (brackets x, y)) (commas fst, ids))) toks
clasohm@1316
   155
    end;
clasohm@1316
   156
clasohm@977
   157
  val opt_typs = repeat (simple_typ || ("(" $$-- complex_typ --$$ ")"));
clasohm@923
   158
  val constructor = name -- opt_typs -- opt_mixfix;
clasohm@923
   159
in
clasohm@923
   160
  val datatype_decl =
clasohm@923
   161
    tvars -- ident --$$ "=" -- enum1 "|" constructor >> mk_params;
clasohm@923
   162
end;
clasohm@923
   163
clasohm@923
   164
clasohm@923
   165
clasohm@923
   166
(** primrec **)
clasohm@923
   167
clasohm@923
   168
fun mk_primrec_decl ((fname, tname), axms) =
clasohm@923
   169
  let
clasohm@923
   170
    fun mk_prove (name, eqn) =
clasohm@1264
   171
      "val " ^ name ^ " = store_thm (" ^ quote name
clasohm@1264
   172
      ^ ", prove_goalw thy [get_def thy " ^ fname ^ "] " ^ eqn ^ "\n\
clasohm@1264
   173
      \  (fn _ => [Simp_tac 1]));";
clasohm@1264
   174
clasohm@923
   175
    val axs = mk_list (map (fn (n, a) => mk_pair (quote n, a)) axms);
clasohm@1264
   176
  in ("|> " ^ tname ^ "_add_primrec " ^ axs, cat_lines (map mk_prove axms)
clasohm@1264
   177
      ^ "\nval dummy = Addsimps " ^ mk_list (map fst axms) ^ ";")
clasohm@1264
   178
  end;
clasohm@923
   179
clasohm@923
   180
val primrec_decl =
clasohm@923
   181
  name -- long_id -- repeat1 (ident -- string) >> mk_primrec_decl;
clasohm@923
   182
clasohm@923
   183
clasohm@923
   184
clasohm@923
   185
(** sections **)
clasohm@923
   186
clasohm@923
   187
val user_keywords = ["intrs", "monos", "con_defs", "|"];
clasohm@923
   188
clasohm@923
   189
val user_sections =
clasohm@923
   190
 [axm_section "subtype" "|> Subtype.add_subtype" subtype_decl,
clasohm@923
   191
  ("inductive", inductive_decl ""),
clasohm@923
   192
  ("coinductive", inductive_decl "Co"),
clasohm@923
   193
  ("datatype", datatype_decl),
clasohm@923
   194
  ("primrec", primrec_decl)];
clasohm@923
   195
clasohm@923
   196
clasohm@923
   197
end;
clasohm@923
   198
clasohm@923
   199
clasohm@923
   200
structure ThySyn = ThySynFun(ThySynData);
clasohm@923
   201
init_thy_reader ();
clasohm@923
   202