src/HOL/thy_syntax.ML
author berghofe
Tue, 30 Jun 1998 20:42:47 +0200
changeset 5097 6c4a7ad6ebc7
parent 4873 b5999638979e
child 5183 89f162de39cf
permissions -rw-r--r--
Adapted to new inductive definition package.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     1
(*  Title:      HOL/thy_syntax.ML
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     2
    ID:         $Id$
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     3
    Author:     Markus Wenzel and Lawrence C Paulson and Carsten Clasohm
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     4
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     5
Additional theory file sections for HOL.
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     6
*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     7
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     8
(*the kind of distinctiveness axioms depends on number of constructors*)
2930
602cdeabb89b Turned Addsimps into AddIffs for datatype laws.
nipkow
parents: 2922
diff changeset
     9
val dtK = 7;  (* FIXME rename?, move? *)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    10
3622
85898be702b2 use ThySyn.add_syntax;
wenzelm
parents: 3617
diff changeset
    11
85898be702b2 use ThySyn.add_syntax;
wenzelm
parents: 3617
diff changeset
    12
local
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    13
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    14
open ThyParse;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    15
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    16
1475
7f5a4cd08209 expanded tabs; renamed subtype to typedef;
clasohm
parents: 1465
diff changeset
    17
(** typedef **)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    18
1475
7f5a4cd08209 expanded tabs; renamed subtype to typedef;
clasohm
parents: 1465
diff changeset
    19
fun mk_typedef_decl (((((opt_name, vs), t), mx), rhs), wt) =
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    20
  let
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    21
    val name' = if_none opt_name t;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    22
    val name = strip_quotes name';
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    23
  in
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    24
    (cat_lines [name', mk_triple (t, mk_list vs, mx), rhs, wt],
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    25
      [name ^ "_def", "Rep_" ^ name, "Rep_" ^ name ^ "_inverse",
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    26
        "Abs_" ^ name ^ "_inverse"])
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    27
  end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    28
1475
7f5a4cd08209 expanded tabs; renamed subtype to typedef;
clasohm
parents: 1465
diff changeset
    29
val typedef_decl =
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    30
  optional ("(" $$-- name --$$ ")" >> Some) None --
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    31
  type_args -- name -- opt_infix --$$ "=" -- string -- opt_witness
1475
7f5a4cd08209 expanded tabs; renamed subtype to typedef;
clasohm
parents: 1465
diff changeset
    32
  >> mk_typedef_decl;
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    33
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    34
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    35
3980
21ef91734970 added record section;
wenzelm
parents: 3945
diff changeset
    36
(** record **)
21ef91734970 added record section;
wenzelm
parents: 3945
diff changeset
    37
21ef91734970 added record section;
wenzelm
parents: 3945
diff changeset
    38
val record_decl =
4225
3d9e551bc5a6 improved record syntax;
wenzelm
parents: 4204
diff changeset
    39
  (type_args -- name >> (mk_pair o apfst mk_list)) --$$ "="
4226
38c91213f26b fixed record parser;
wenzelm
parents: 4225
diff changeset
    40
    -- optional (typ --$$ "+" >> (parens o cat "Some")) "None"
4225
3d9e551bc5a6 improved record syntax;
wenzelm
parents: 4204
diff changeset
    41
    -- repeat1 ((name --$$ "::" -- typ) >> mk_pair)
4001
b6a3c2665c45 record: tuned output;
wenzelm
parents: 3980
diff changeset
    42
  >> (fn ((x, y), zs) => cat_lines [x, y, mk_big_list zs]);
3980
21ef91734970 added record section;
wenzelm
parents: 3945
diff changeset
    43
21ef91734970 added record section;
wenzelm
parents: 3945
diff changeset
    44
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    45
(** (co)inductive **)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    46
5097
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    47
fun inductive_decl coind =
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    48
  let
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    49
    fun mk_intr_name (s, _) =   (*the "op" cancels any infix status*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    50
      if Syntax.is_identifier s then "op " ^ s else "_";
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    51
    fun mk_params (((recs, ipairs), monos), con_defs) =
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    52
      let val big_rec_name = space_implode "_" (map (scan_to_id o trim) recs)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    53
          and srec_tms = mk_list recs
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    54
          and sintrs   = mk_big_list (map snd ipairs)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    55
      in
5097
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    56
        ";\n\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    57
        \local\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    58
        \val (thy, {defs, mono, unfold, intrs, elims, mk_cases, induct, ...}) =\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    59
        \  InductivePackage.add_inductive true " ^
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    60
        (if coind then "true " else "false ") ^ srec_tms ^ " " ^
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    61
         sintrs ^ " " ^ monos ^ " " ^ con_defs ^ " thy;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    62
        \in\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    63
        \structure " ^ big_rec_name ^ " =\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    64
        \struct\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    65
        \  val defs = defs;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    66
        \  val mono = mono;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    67
        \  val unfold = unfold;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    68
        \  val intrs = intrs;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    69
        \  val elims = elims;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    70
        \  val elim = hd elims;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    71
        \  val " ^ (if coind then "co" else "") ^ "induct = induct;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    72
        \  val mk_cases = mk_cases;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    73
        \  val " ^ mk_list (map mk_intr_name ipairs) ^ " = intrs;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    74
        \end;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    75
        \val thy = thy;\nend;\n\
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
    76
        \val thy = thy\n"
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    77
      end
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    78
    val ipairs = "intrs" $$-- repeat1 (ident -- !! string)
3403
6cc663f6d62e A slight simplification of optstring
paulson
parents: 3345
diff changeset
    79
    fun optstring s = optional (s $$-- string >> trim) "[]"
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    80
  in
1788
ca62fab4ce92 Quotes now optional around inductive set
paulson
parents: 1668
diff changeset
    81
    repeat1 name -- ipairs -- optstring "monos" -- optstring "con_defs"
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    82
      >> mk_params
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    83
  end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    84
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    85
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    86
(** datatype **)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    87
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    88
local
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    89
  (* FIXME err -> add_datatype *)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    90
  fun mk_cons cs =
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    91
    (case duplicates (map (fst o fst) cs) of
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    92
      [] => map (fn ((s, ts), syn) => mk_triple (s, mk_list ts, syn)) cs
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    93
    | dups => error ("Duplicate constructors: " ^ commas_quote dups));
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    94
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    95
  (*generate names of distinctiveness axioms*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    96
  fun mk_distinct_rules cs tname =
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    97
    let
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    98
      val uqcs = map (fn ((s, _), _) => strip_quotes s) cs;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    99
      (*combine all constructor names with all others w/o duplicates*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   100
      fun neg_one c = map (fn c2 => quote (c ^ "_not_" ^ c2));
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   101
      fun neg1 [] = []
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   102
        | neg1 (c1 :: cs) = neg_one c1 cs @ neg1 cs;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   103
    in
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   104
      if length uqcs < dtK then neg1 uqcs
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   105
      else quote (tname ^ "_ord_distinct") ::
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   106
        map (fn c => quote (tname ^ "_ord_" ^ c)) uqcs
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   107
    end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   108
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   109
  fun mk_rules tname cons pre = " map (get_axiom thy) " ^
3194
36bfceef1800 TFL theory section
paulson
parents: 2930
diff changeset
   110
    mk_list (map (fn ((s,_), _) => quote (tname ^ pre ^ strip_quotes s)) cons);
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   111
1668
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   112
  (*generate string for calling add_datatype and build_record*)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   113
  fun mk_params ((ts, tname), cons) =
4184
23a09f2fd687 Each datatype t now proves a theorem split_t_case_prem
nipkow
parents: 4106
diff changeset
   114
    "val (thy,"^tname^"_add_primrec,size_"^tname^"_eqns,split_"^tname^"_eqns) =\n\
4032
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 4001
diff changeset
   115
    \    Datatype.add_datatype\n"
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   116
    ^ mk_triple (mk_list ts, quote tname, mk_list (mk_cons cons)) ^ " thy\n\
4106
wenzelm
parents: 4091
diff changeset
   117
    \val thy = ("^tname^"_add_primrec size_"^tname^"_eqns thy)\n\
wenzelm
parents: 4091
diff changeset
   118
    \val _ = deny (" ^ quote tname ^ " mem (Sign.stamp_names_of (sign_of thy)))\n\
3665
3b44fac767f6 Added Larry's test for preventing a datatype shadowing a theory.
nipkow
parents: 3622
diff changeset
   119
    \   (\"Datatype \\\""^tname^"\\\" would clash with the theory of the same name!\");\n\
3b44fac767f6 Added Larry's test for preventing a datatype shadowing a theory.
nipkow
parents: 3622
diff changeset
   120
    \structure " ^ tname ^ " =\n\
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   121
    \struct\n\
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   122
    \ val inject = map (get_axiom thy) " ^
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   123
        mk_list (map (fn ((s, _), _) => quote ("inject_" ^ strip_quotes s))
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   124
          (filter_out (null o snd o fst) cons)) ^ ";\n\
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   125
    \ val distinct = " ^
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   126
        (if length cons < dtK then "let val distinct' = " else "") ^
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   127
        "map (get_axiom thy) " ^ mk_list (mk_distinct_rules cons tname) ^
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   128
        (if length cons < dtK then
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   129
          "  in distinct' @ (map (fn t => sym COMP (t RS contrapos))\
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   130
          \ distinct') end"
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   131
         else "") ^ ";\n\
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   132
    \ val induct = get_axiom thy \"" ^ tname ^ "_induct\";\n\
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   133
    \ val cases =" ^ mk_rules tname cons "_case_" ^ ";\n\
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   134
    \ val recs =" ^ mk_rules tname cons "_rec_" ^ ";\n\
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   135
    \ val simps = inject @ distinct @ cases @ recs;\n\
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   136
    \ fun induct_tac a = res_inst_tac [(" ^ quote tname ^ ", a)] induct;\n\
1264
3eb91524b938 added local simpsets; removed IOA from 'make test'
clasohm
parents: 1251
diff changeset
   137
    \end;\n\
4106
wenzelm
parents: 4091
diff changeset
   138
    \val thy = thy |> Dtype.add_record " ^
wenzelm
parents: 4091
diff changeset
   139
      mk_triple
wenzelm
parents: 4091
diff changeset
   140
        ("Sign.intern_tycon (sign_of thy) " ^ quote tname,
wenzelm
parents: 4091
diff changeset
   141
          mk_list (map (fst o fst) cons),
wenzelm
parents: 4091
diff changeset
   142
          tname ^ ".induct_tac") ^ ";\n\
wenzelm
parents: 4091
diff changeset
   143
    \val dummy = context thy;\n\
2930
602cdeabb89b Turned Addsimps into AddIffs for datatype laws.
nipkow
parents: 2922
diff changeset
   144
    \val dummy = Addsimps(" ^ tname ^ ".cases @ " ^ tname ^ ".recs);\n\
602cdeabb89b Turned Addsimps into AddIffs for datatype laws.
nipkow
parents: 2922
diff changeset
   145
    \val dummy = AddIffs " ^ tname ^ ".inject;\n\
602cdeabb89b Turned Addsimps into AddIffs for datatype laws.
nipkow
parents: 2922
diff changeset
   146
    \val dummy = " ^
602cdeabb89b Turned Addsimps into AddIffs for datatype laws.
nipkow
parents: 2922
diff changeset
   147
      (if length cons < dtK then "AddIffs " else "Addsimps ") ^
3308
da002cef7090 Added overloaded function `size' for all datatypes.
nipkow
parents: 3194
diff changeset
   148
      tname ^ ".distinct;\n\
da002cef7090 Added overloaded function `size' for all datatypes.
nipkow
parents: 3194
diff changeset
   149
    \val dummy = Addsimps(map (fn (_,eqn) =>\n\
da002cef7090 Added overloaded function `size' for all datatypes.
nipkow
parents: 3194
diff changeset
   150
    \ prove_goalw thy [get_def thy " ^ quote("size_"^tname) ^
4032
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 4001
diff changeset
   151
                     "] eqn (fn _ => [Simp_tac 1])) size_"^tname^"_eqns);\n\
4184
23a09f2fd687 Each datatype t now proves a theorem split_t_case_prem
nipkow
parents: 4106
diff changeset
   152
    \val split_"^tname^"_case = prove_goal thy (#1(split_"^tname^"_eqns))\n\
23a09f2fd687 Each datatype t now proves a theorem split_t_case_prem
nipkow
parents: 4106
diff changeset
   153
    \  (fn _ => [#exhaust_tac (datatype_info thy (Sign.intern_tycon (sign_of thy) "
23a09f2fd687 Each datatype t now proves a theorem split_t_case_prem
nipkow
parents: 4106
diff changeset
   154
      ^ quote tname ^ ")) \""^tname^"0\" 1,\n\
23a09f2fd687 Each datatype t now proves a theorem split_t_case_prem
nipkow
parents: 4106
diff changeset
   155
    \            ALLGOALS Asm_simp_tac]);\n\
4204
7b15e7eee982 renamed split_T_case_prem to split_T_case_asm
oheimb
parents: 4184
diff changeset
   156
    \val split_"^tname^"_case_asm = prove_goal thy (#2(split_"^tname^"_eqns))\n\
4106
wenzelm
parents: 4091
diff changeset
   157
    \  (fn _ => [#exhaust_tac (datatype_info thy (Sign.intern_tycon (sign_of thy) "
wenzelm
parents: 4091
diff changeset
   158
      ^ quote tname ^ ")) \""^tname^"0\" 1,\n\
4536
74f7c556fd90 added split_paired_Ex to the implicit simpset
oheimb
parents: 4226
diff changeset
   159
    \            ALLGOALS (asm_simp_tac (simpset() delsimps [split_paired_Ex]))]);\n\
4106
wenzelm
parents: 4091
diff changeset
   160
    \val thy = thy\n";
wenzelm
parents: 4091
diff changeset
   161
4032
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 4001
diff changeset
   162
(*
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 4001
diff changeset
   163
The #exhaust_tac(snd(hd(!datatypes))) in the proof of split_"^tname^"_case
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 4001
diff changeset
   164
is a hack. Ideally I would just write exhaust_tac, but the latter extracts the
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 4001
diff changeset
   165
specific exhaustion tactic from the theory associated with the proof
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 4001
diff changeset
   166
state. However, the exhaustion tactic for the current datatype has only just
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 4001
diff changeset
   167
been added to !datatypes (a few lines above) but is not yet associated with
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 4001
diff changeset
   168
the theory. Hope this can be simplified in the future.
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 4001
diff changeset
   169
*)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   170
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   171
  (*parsers*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   172
  val tvars = type_args >> map (cat "dtVar");
1316
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   173
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   174
  val simple_typ = ident >> (cat "dtTyp" o curry mk_pair "[]" o quote) ||
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   175
    type_var >> cat "dtVar";
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   176
1251
81fc4d8e3eda added nested types on right hand side of datatype definitions
clasohm
parents: 977
diff changeset
   177
  fun complex_typ toks =
1316
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   178
    let val typ = simple_typ || "(" $$-- complex_typ --$$ ")";
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   179
        val typ2 = complex_typ || "(" $$-- complex_typ --$$ ")";
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   180
    in
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   181
     (typ -- repeat (ident>>quote) >>
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   182
        (foldl (fn (x,y) => "dtTyp " ^ mk_pair (brackets x, y))) ||
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   183
      "(" $$-- !! (list1 typ2) --$$ ")" -- !! (repeat1 (ident>>quote)) >>
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   184
       (fn (fst, ids) => foldl (fn (x,y) => "dtTyp " ^
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   185
                         mk_pair (brackets x, y)) (commas fst, ids))) toks
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   186
    end;
ce35d42d2190 extended complex_typ
clasohm
parents: 1264
diff changeset
   187
977
5d57287e5e1e changed syntax of datatype declarations (curried types for constructor
clasohm
parents: 923
diff changeset
   188
  val opt_typs = repeat (simple_typ || ("(" $$-- complex_typ --$$ ")"));
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   189
  val constructor = name -- opt_typs -- opt_mixfix;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   190
in
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   191
  val datatype_decl =
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   192
    tvars -- ident --$$ "=" -- enum1 "|" constructor >> mk_params;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   193
end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   194
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   195
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   196
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   197
(** primrec **)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   198
2922
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   199
(*recursion equations have user-supplied names*)
1845
afa622bc829d Simplified syntax of primrec definitions.
berghofe
parents: 1788
diff changeset
   200
fun mk_primrec_decl_1 ((fname, tname), axms) =
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   201
  let
1574
5a63ab90ee8a modified primrec so it can be used in MiniML/Type.thy
clasohm
parents: 1475
diff changeset
   202
    (*Isolate type name from the structure's identifier it may be stored in*)
5a63ab90ee8a modified primrec so it can be used in MiniML/Type.thy
clasohm
parents: 1475
diff changeset
   203
    val tname' = implode (snd (take_suffix (not_equal ".") (explode tname)));
5a63ab90ee8a modified primrec so it can be used in MiniML/Type.thy
clasohm
parents: 1475
diff changeset
   204
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   205
    fun mk_prove (name, eqn) =
1264
3eb91524b938 added local simpsets; removed IOA from 'make test'
clasohm
parents: 1251
diff changeset
   206
      "val " ^ name ^ " = store_thm (" ^ quote name
1574
5a63ab90ee8a modified primrec so it can be used in MiniML/Type.thy
clasohm
parents: 1475
diff changeset
   207
      ^ ", prove_goalw thy [get_def thy "
5a63ab90ee8a modified primrec so it can be used in MiniML/Type.thy
clasohm
parents: 1475
diff changeset
   208
      ^ (quote (strip_quotes fname ^ "_" ^ tname')) ^ "] " ^ eqn ^ "\n\
1264
3eb91524b938 added local simpsets; removed IOA from 'make test'
clasohm
parents: 1251
diff changeset
   209
      \  (fn _ => [Simp_tac 1]));";
3eb91524b938 added local simpsets; removed IOA from 'make test'
clasohm
parents: 1251
diff changeset
   210
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   211
    val axs = mk_list (map (fn (n, a) => mk_pair (quote n, a)) axms);
2922
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   212
  in ("|> " ^ tname ^ "_add_primrec " ^ axs
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   213
      , 
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   214
      cat_lines (map mk_prove axms)
1264
3eb91524b938 added local simpsets; removed IOA from 'make test'
clasohm
parents: 1251
diff changeset
   215
      ^ "\nval dummy = Addsimps " ^ mk_list (map fst axms) ^ ";")
3eb91524b938 added local simpsets; removed IOA from 'make test'
clasohm
parents: 1251
diff changeset
   216
  end;
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   217
2922
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   218
(*recursion equations have no names*)
1845
afa622bc829d Simplified syntax of primrec definitions.
berghofe
parents: 1788
diff changeset
   219
fun mk_primrec_decl_2 ((fname, tname), axms) =
afa622bc829d Simplified syntax of primrec definitions.
berghofe
parents: 1788
diff changeset
   220
  let
afa622bc829d Simplified syntax of primrec definitions.
berghofe
parents: 1788
diff changeset
   221
    (*Isolate type name from the structure's identifier it may be stored in*)
afa622bc829d Simplified syntax of primrec definitions.
berghofe
parents: 1788
diff changeset
   222
    val tname' = implode (snd (take_suffix (not_equal ".") (explode tname)));
afa622bc829d Simplified syntax of primrec definitions.
berghofe
parents: 1788
diff changeset
   223
afa622bc829d Simplified syntax of primrec definitions.
berghofe
parents: 1788
diff changeset
   224
    fun mk_prove eqn =
afa622bc829d Simplified syntax of primrec definitions.
berghofe
parents: 1788
diff changeset
   225
      "prove_goalw thy [get_def thy "
afa622bc829d Simplified syntax of primrec definitions.
berghofe
parents: 1788
diff changeset
   226
      ^ (quote (strip_quotes fname ^ "_" ^ tname')) ^ "] " ^ eqn ^ " \
afa622bc829d Simplified syntax of primrec definitions.
berghofe
parents: 1788
diff changeset
   227
      \(fn _ => [Simp_tac 1])";
afa622bc829d Simplified syntax of primrec definitions.
berghofe
parents: 1788
diff changeset
   228
afa622bc829d Simplified syntax of primrec definitions.
berghofe
parents: 1788
diff changeset
   229
    val axs = mk_list (map (fn a => mk_pair ("\"\"", a)) axms);
2922
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   230
  in ("|> " ^ tname ^ "_add_primrec " ^ axs
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   231
      ,
1845
afa622bc829d Simplified syntax of primrec definitions.
berghofe
parents: 1788
diff changeset
   232
      "val dummy = Addsimps " ^
afa622bc829d Simplified syntax of primrec definitions.
berghofe
parents: 1788
diff changeset
   233
      brackets(space_implode ",\n" (map mk_prove axms)) ^ ";")
afa622bc829d Simplified syntax of primrec definitions.
berghofe
parents: 1788
diff changeset
   234
  end;
afa622bc829d Simplified syntax of primrec definitions.
berghofe
parents: 1788
diff changeset
   235
2922
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   236
(*function name, argument type and either (name,axiom) pairs or just axioms*)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   237
val primrec_decl =
1845
afa622bc829d Simplified syntax of primrec definitions.
berghofe
parents: 1788
diff changeset
   238
  (name -- long_id -- repeat1 (ident -- string) >> mk_primrec_decl_1) ||
afa622bc829d Simplified syntax of primrec definitions.
berghofe
parents: 1788
diff changeset
   239
  (name -- long_id -- repeat1 string >> mk_primrec_decl_2) ;
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   240
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   241
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   242
2922
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   243
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   244
(** rec: interface to Slind's TFL **)
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   245
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   246
3194
36bfceef1800 TFL theory section
paulson
parents: 2930
diff changeset
   247
(*fname: name of function being defined; rel: well-founded relation*)
3456
fdb1768ebd3e New "congs" keyword for recdef theory section
paulson
parents: 3403
diff changeset
   248
fun mk_rec_decl ((((fname, rel), congs), ss), axms) =
2922
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   249
  let val fid = trim fname
3194
36bfceef1800 TFL theory section
paulson
parents: 2930
diff changeset
   250
      val intrnl_name = fid ^ "_Intrnl"
2922
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   251
  in
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   252
	 (";\n\n\
3194
36bfceef1800 TFL theory section
paulson
parents: 2930
diff changeset
   253
          \val _ = writeln \"Recursive function " ^ fid ^ "\"\n\
36bfceef1800 TFL theory section
paulson
parents: 2930
diff changeset
   254
          \val (thy, pats_" ^ intrnl_name ^ ") = Tfl.define thy " ^ 
3345
4d888ddd6284 Now recdef checks the name of the function being defined.
paulson
parents: 3308
diff changeset
   255
	                 quote fid ^ " " ^ 
3194
36bfceef1800 TFL theory section
paulson
parents: 2930
diff changeset
   256
	                 rel ^ "\n" ^ mk_big_list axms ^ ";\n\
2922
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   257
          \val thy = thy"
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   258
         ,
3194
36bfceef1800 TFL theory section
paulson
parents: 2930
diff changeset
   259
          "structure " ^ fid ^ " =\n\
36bfceef1800 TFL theory section
paulson
parents: 2930
diff changeset
   260
          \  struct\n\
36bfceef1800 TFL theory section
paulson
parents: 2930
diff changeset
   261
          \  val _ = writeln \"Proofs for recursive function " ^ fid ^ "\"\n\
36bfceef1800 TFL theory section
paulson
parents: 2930
diff changeset
   262
          \  val {rules, induct, tcs} = \n\
3456
fdb1768ebd3e New "congs" keyword for recdef theory section
paulson
parents: 3403
diff changeset
   263
          \    \t Tfl.simplify_defn (" ^ ss ^ ", " ^ congs ^ ")\n\
fdb1768ebd3e New "congs" keyword for recdef theory section
paulson
parents: 3403
diff changeset
   264
          \    \t\t  (thy, (" ^ quote fid ^ ", pats_" ^ intrnl_name ^ "))\n\
3194
36bfceef1800 TFL theory section
paulson
parents: 2930
diff changeset
   265
          \  end;\n\
36bfceef1800 TFL theory section
paulson
parents: 2930
diff changeset
   266
          \val pats_" ^ intrnl_name ^ " = ();\n")
2922
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   267
  end;
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   268
3403
6cc663f6d62e A slight simplification of optstring
paulson
parents: 3345
diff changeset
   269
val rec_decl = (name -- string -- 
3456
fdb1768ebd3e New "congs" keyword for recdef theory section
paulson
parents: 3403
diff changeset
   270
		optional ("congs" $$-- string >> trim) "[]" -- 
4091
771b1f6422a8 isatool fixclasimp;
wenzelm
parents: 4032
diff changeset
   271
		optional ("simpset" $$-- string >> trim) "simpset()" -- 
3403
6cc663f6d62e A slight simplification of optstring
paulson
parents: 3345
diff changeset
   272
		repeat1 string >> mk_rec_decl) ;
2922
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   273
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   274
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   275
3622
85898be702b2 use ThySyn.add_syntax;
wenzelm
parents: 3617
diff changeset
   276
(** augment thy syntax **)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   277
3622
85898be702b2 use ThySyn.add_syntax;
wenzelm
parents: 3617
diff changeset
   278
in
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   279
3622
85898be702b2 use ThySyn.add_syntax;
wenzelm
parents: 3617
diff changeset
   280
val _ = ThySyn.add_syntax
85898be702b2 use ThySyn.add_syntax;
wenzelm
parents: 3617
diff changeset
   281
 ["intrs", "monos", "con_defs", "congs", "simpset", "|"]
4873
b5999638979e TypedefPackage.add_typedef;
wenzelm
parents: 4536
diff changeset
   282
 [axm_section "typedef" "|> TypedefPackage.add_typedef" typedef_decl,
5097
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
   283
  section "record" "|> RecordPackage.add_record" record_decl,
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
   284
  section "inductive" "" (inductive_decl false),
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
   285
  section "coinductive" "" (inductive_decl true),
6c4a7ad6ebc7 Adapted to new inductive definition package.
berghofe
parents: 4873
diff changeset
   286
  section "datatype" "" datatype_decl,
2922
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   287
  ("primrec", primrec_decl),
580647a879cf Using Blast_tac
paulson
parents: 1845
diff changeset
   288
  ("recdef", rec_decl)];
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   289
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   290
end;