src/HOL/datatype.ML
author wenzelm
Mon, 03 Nov 1997 12:26:45 +0100
changeset 4092 9faf228771dc
parent 4040 20f7471eedbf
child 4107 2270829d2364
permissions -rw-r--r--
added simpset thy_data;
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/datatype.ML
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     2
   ID:          $Id$
1668
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
     3
   Author:      Max Breitling, Carsten Clasohm, Tobias Nipkow, Norbert Voelker,
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
     4
                Konrad Slind
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     5
   Copyright 1995 TU Muenchen
3792
1ecbaca6560a TODO: handle internal / external names;
wenzelm
parents: 3768
diff changeset
     6
923
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
3615
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
     9
(** Information about datatypes **)
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    10
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    11
(* Retrieve information for a specific datatype *)
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    12
fun datatype_info thy tname =
3979
dac05c9341f4 Sign.name_of;
wenzelm
parents: 3945
diff changeset
    13
  case get_thydata (Sign.name_of (sign_of thy)) "datatypes" of
3615
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    14
      None => None
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    15
    | Some (DT_DATA ds) => assoc (ds, tname);
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    16
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    17
fun match_info thy tname =
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    18
  case datatype_info thy tname of
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    19
      Some {case_const, constructors, ...} =>
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    20
        {case_const=case_const, constructors=constructors}
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    21
    | None => error ("No datatype \"" ^ tname ^ "\" defined in this theory.");
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    22
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    23
fun induct_info thy tname =
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    24
  case datatype_info thy tname of
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    25
      Some {constructors, nchotomy, ...} =>
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    26
        {constructors=constructors, nchotomy=nchotomy}
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    27
    | None => error ("No datatype \"" ^ tname ^ "\" defined in this theory.");
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    28
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    29
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    30
(* Retrieve information for all datatypes defined in a theory and its
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    31
   ancestors *)
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    32
fun extract_info thy =
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    33
  let val (congs, rewrites) =
3979
dac05c9341f4 Sign.name_of;
wenzelm
parents: 3945
diff changeset
    34
        case get_thydata (Sign.name_of (sign_of thy)) "datatypes" of
3615
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    35
            None => ([], [])
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    36
          | Some (DT_DATA ds) =>
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    37
              let val info = map snd ds
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    38
              in (map #case_cong info, flat (map #case_rewrites info)) end;
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    39
  in {case_congs = congs, case_rewrites = rewrites} end;
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    40
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    41
local
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    42
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    43
fun find_tname var Bi =
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    44
  let val frees = map dest_Free (term_frees Bi)
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    45
      val params = Logic.strip_params Bi;
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    46
  in case assoc (frees@params, var) of
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    47
       None => error("No such variable in subgoal: " ^ quote var)
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    48
     | Some(Type(tn,_)) => tn
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    49
     | _ => error("Cannot induct on type of " ^ quote var)
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    50
  end;
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    51
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    52
fun get_dt_info sign tn =
3979
dac05c9341f4 Sign.name_of;
wenzelm
parents: 3945
diff changeset
    53
      case get_thydata (Sign.name_of sign) "datatypes" of
3615
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    54
        None => error ("No such datatype: " ^ quote tn)
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    55
      | Some (DT_DATA ds) =>
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    56
          case assoc (ds, tn) of
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    57
            Some info => info
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    58
          | None => error ("Not a datatype: " ^ quote tn)
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    59
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    60
fun infer_tname state sign i aterm =
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    61
let val (_, _, Bi, _) = dest_state (state, i)
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    62
    val params = Logic.strip_params Bi	        (*params of subgoal i*)
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    63
    val params = rev(rename_wrt_term Bi params) (*as they are printed*)
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    64
    val (types,sorts) = types_sorts state
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    65
    fun types'(a,~1) = (case assoc(params,a) of None => types(a,~1) | sm => sm)
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    66
      | types'(ixn) = types ixn;
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    67
    val (ct,_) = read_def_cterm (sign,types',sorts) [] false
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    68
                                (aterm,TVar(("",0),[]));
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    69
in case #T(rep_cterm ct) of
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    70
     Type(tn,_) => tn
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    71
   | _ => error("Cannot induct on type of " ^ quote aterm)
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    72
end;
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    73
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    74
in
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    75
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    76
(* generic induction tactic for datatypes *)
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    77
fun induct_tac var i state = state |>
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    78
        let val (_, _, Bi, _) = dest_state (state, i)
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    79
            val sign = #sign(rep_thm state)
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    80
            val tn = find_tname var Bi
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    81
            val ind_tac = #induct_tac(get_dt_info sign tn)
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    82
        in ind_tac var i end;
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    83
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    84
(* generic exhaustion tactic for datatypes *)
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    85
fun exhaust_tac aterm i state = state |>
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    86
        let val sign = #sign(rep_thm state)
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    87
            val tn = infer_tname state sign i aterm
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    88
            val exh_tac = #exhaust_tac(get_dt_info sign tn)
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    89
        in exh_tac aterm i end;
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    90
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    91
end;
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    92
e5322197cfea Moved some functions which used to be part of thy_data.ML
berghofe
parents: 3564
diff changeset
    93
3292
8b143c196d42 Added rotation to exhaust_tac.
nipkow
parents: 3282
diff changeset
    94
(* should go into Pure *)
3538
ed9de44032e0 Removal of the tactical STATE
paulson
parents: 3534
diff changeset
    95
fun ALLNEWSUBGOALS tac tacf i st0 = st0 |>
ed9de44032e0 Removal of the tactical STATE
paulson
parents: 3534
diff changeset
    96
  (tac i THEN
ed9de44032e0 Removal of the tactical STATE
paulson
parents: 3534
diff changeset
    97
   (fn st1 => st1 |> 
ed9de44032e0 Removal of the tactical STATE
paulson
parents: 3534
diff changeset
    98
        let val d = nprems_of st1 - nprems_of st0
ed9de44032e0 Removal of the tactical STATE
paulson
parents: 3534
diff changeset
    99
        in EVERY(map tacf ((i+d) downto i)) end));
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   100
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   101
(*used for constructor parameters*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   102
datatype dt_type = dtVar of string |
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   103
  dtTyp of dt_type list * string |
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   104
  dtRek of dt_type list * string;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   105
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   106
structure Datatype =
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   107
struct
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   108
local 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   109
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   110
val mysort = sort;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   111
open ThyParse HOLogic;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   112
exception Impossible;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   113
exception RecError of string;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   114
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   115
val is_dtRek = (fn dtRek _ => true  |  _  => false);
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   116
fun opt_parens s = if s = "" then "" else enclose "(" ")" s; 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   117
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   118
(* ----------------------------------------------------------------------- *)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   119
(* Derivation of the primrec combinator application from the equations     *)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   120
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   121
(* substitute fname(ls,xk,rs) by yk(ls,rs) in t for (xk,yk) in pairs  *) 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   122
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   123
fun subst_apps (_,_) [] t = t
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   124
  | subst_apps (fname,rpos) pairs t =
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   125
    let 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   126
    fun subst (Abs(a,T,t)) = Abs(a,T,subst t)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   127
      | subst (funct $ body) = 
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   128
        let val (f,b) = strip_comb (funct$body)
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   129
        in 
3945
ae9c61d69888 adapted to qualified names;
wenzelm
parents: 3842
diff changeset
   130
          if is_Const f andalso Sign.base_name (fst(dest_Const f)) = fname 
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   131
            then 
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   132
              let val (ls,rest) = (take(rpos,b), drop(rpos,b));
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   133
                val (xk,rs) = (hd rest,tl rest)
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   134
                  handle LIST _ => raise RecError "not enough arguments \
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   135
                   \ in recursive application on rhs"
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   136
              in 
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   137
                (case assoc (pairs,xk) of 
1574
5a63ab90ee8a modified primrec so it can be used in MiniML/Type.thy
clasohm
parents: 1465
diff changeset
   138
                   None   => list_comb(f, map subst b)
5a63ab90ee8a modified primrec so it can be used in MiniML/Type.thy
clasohm
parents: 1465
diff changeset
   139
                 | Some U => list_comb(U, map subst (ls @ rs)))
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   140
              end
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   141
          else list_comb(f, map subst b)
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   142
        end
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   143
      | subst(t) = t
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   144
    in subst t end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   145
  
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   146
(* abstract rhs *)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   147
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   148
fun abst_rec (fname,rpos,tc,ls,cargs,rs,rhs) =       
2270
d7513875b2b8 Replaced map...~~ by ListPair.map
paulson
parents: 2031
diff changeset
   149
  let val rargs = (map #1 o 
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   150
                   (filter (fn (a,T) => is_dtRek T))) (cargs ~~ tc);
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   151
      val subs = map (fn (s,T) => (s,dummyT))
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   152
                   (rev(rename_wrt_term rhs rargs));
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   153
      val subst_rhs = subst_apps (fname,rpos)
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   154
                        (map Free rargs ~~ map Free subs) rhs;
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   155
  in 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   156
      list_abs_free (cargs @ subs @ ls @ rs, subst_rhs) 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   157
  end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   158
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   159
(* parsing the prim rec equations *)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   160
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   161
fun dest_eq ( Const("Trueprop",_) $ (Const ("op =",_) $ lhs $ rhs))
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   162
                 = (lhs, rhs)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   163
   | dest_eq _ = raise RecError "not a proper equation"; 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   164
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   165
fun dest_rec eq = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   166
  let val (lhs,rhs) = dest_eq eq; 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   167
    val (name,args) = strip_comb lhs; 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   168
    val (ls',rest)  = take_prefix is_Free args; 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   169
    val (middle,rs') = take_suffix is_Free rest;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   170
    val rpos = length ls';
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   171
    val (c,cargs') = strip_comb (hd middle)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   172
      handle LIST "hd" => raise RecError "constructor missing";
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   173
    val (ls,cargs,rs) = (map dest_Free ls', map dest_Free cargs'
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   174
                         , map dest_Free rs')
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   175
      handle TERM ("dest_Free",_) => 
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   176
          raise RecError "constructor has illegal argument in pattern";
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   177
  in 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   178
    if length middle > 1 then 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   179
      raise RecError "more than one non-variable in pattern"
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   180
    else if not(null(findrep (map fst (ls @ rs @ cargs)))) then 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   181
      raise RecError "repeated variable name in pattern" 
3945
ae9c61d69888 adapted to qualified names;
wenzelm
parents: 3842
diff changeset
   182
         else (Sign.base_name (fst(dest_Const name)) handle TERM _ => 
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   183
               raise RecError "function is not declared as constant in theory"
3945
ae9c61d69888 adapted to qualified names;
wenzelm
parents: 3842
diff changeset
   184
                 ,rpos,ls, Sign.base_name (fst(dest_Const c)),cargs,rs,rhs)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   185
  end; 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   186
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   187
(* check function specified for all constructors and sort function terms *)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   188
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   189
fun check_and_sort (n,its) = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   190
  if length its = n 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   191
    then map snd (mysort (fn ((i : int,_),(j,_)) => i<j) its)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   192
  else raise error "Primrec definition error:\n\
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   193
   \Please give an equation for every constructor";
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
(* translate rec equations into function arguments suitable for rec comb *)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   196
(* theory parameter needed for printing error messages                   *) 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   197
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   198
fun trans_recs _ _ [] = error("No primrec equations.")
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   199
  | trans_recs thy cs' (eq1::eqs) = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   200
    let val (name1,rpos1,ls1,_,_,_,_) = dest_rec eq1
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   201
      handle RecError s =>
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   202
        error("Primrec definition error: " ^ s ^ ":\n" 
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   203
              ^ "   " ^ Sign.string_of_term (sign_of thy) eq1);
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   204
      val tcs = map (fn (_,c,T,_,_) => (c,T)) cs';  
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   205
      val cs = map fst tcs;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   206
      fun trans_recs' _ [] = []
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   207
        | trans_recs' cis (eq::eqs) = 
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   208
          let val (name,rpos,ls,c,cargs,rs,rhs) = dest_rec eq; 
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   209
            val tc = assoc(tcs,c);
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   210
            val i = (1 + find (c,cs))  handle LIST "find" => 0; 
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   211
          in
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   212
          if name <> name1 then 
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   213
            raise RecError "function names inconsistent"
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   214
          else if rpos <> rpos1 then 
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   215
            raise RecError "position of rec. argument inconsistent"
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   216
          else if i = 0 then 
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   217
            raise RecError "illegal argument in pattern" 
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   218
          else if i mem cis then
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   219
            raise RecError "constructor already occured as pattern "
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   220
               else (i,abst_rec (name,rpos,the tc,ls,cargs,rs,rhs))
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   221
                     :: trans_recs' (i::cis) eqs 
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   222
          end
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   223
          handle RecError s =>
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   224
                error("Primrec definition error\n" ^ s ^ "\n" 
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   225
                      ^ "   " ^ Sign.string_of_term (sign_of thy) eq);
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   226
    in (  name1, ls1
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   227
        , check_and_sort (length cs, trans_recs' [] (eq1::eqs)))
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   228
    end ;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   229
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   230
in
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   231
  fun add_datatype (typevars, tname, cons_list') thy = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   232
    let
3308
da002cef7090 Added overloaded function `size' for all datatypes.
nipkow
parents: 3292
diff changeset
   233
      val dummy = require_thy thy "Arith" "datatype definitions";
2880
a0fde30aa126 Removed (Unit) in Prod.
nipkow
parents: 2270
diff changeset
   234
      
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   235
      fun typid(dtRek(_,id)) = id
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   236
        | typid(dtVar s) = implode (tl (explode s))
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   237
        | typid(dtTyp(_,id)) = id;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   238
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   239
      fun index_vnames(vn::vns,tab) =
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   240
            (case assoc(tab,vn) of
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   241
               None => if vn mem vns
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   242
                       then (vn^"1") :: index_vnames(vns,(vn,2)::tab)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   243
                       else vn :: index_vnames(vns,tab)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   244
             | Some(i) => (vn^(string_of_int i)) ::
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   245
                          index_vnames(vns,(vn,i+1)::tab))
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   246
        | index_vnames([],tab) = [];
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   247
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   248
      fun mk_var_names types = index_vnames(map typid types,[]);
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   249
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   250
      (*search for free type variables and convert recursive *)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   251
      fun analyse_types (cons, types, syn) =
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   252
        let fun analyse(t as dtVar v) =
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   253
                  if t mem typevars then t
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   254
                  else error ("Free type variable " ^ v ^ " on rhs.")
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   255
              | analyse(dtTyp(typl,s)) =
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   256
                  if tname <> s then dtTyp(analyses typl, s)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   257
                  else if typevars = typl then dtRek(typl, s)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   258
                       else error (s ^ " used in different ways")
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   259
              | analyse(dtRek _) = raise Impossible
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   260
            and analyses ts = map analyse ts;
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   261
        in (cons, Syntax.const_name cons syn, analyses types,
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   262
            mk_var_names types, syn)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   263
        end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   264
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   265
     (*test if all elements are recursive, i.e. if the type is empty*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   266
      
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   267
      fun non_empty (cs : ('a * 'b * dt_type list * 'c *'d) list) = 
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   268
        not(forall (exists is_dtRek o #3) cs) orelse
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   269
        error("Empty datatype not allowed!");
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   270
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   271
      val cons_list = map analyse_types cons_list';
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   272
      val dummy = non_empty cons_list;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   273
      val num_of_cons = length cons_list;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   274
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   275
     (* Auxiliary functions to construct argument and equation lists *)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   276
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   277
     (*generate 'var_n, ..., var_m'*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   278
      fun Args(var, delim, n, m) = 
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   279
        space_implode delim (map (fn n => var^string_of_int(n)) (n upto m));
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   280
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   281
      fun C_exp name vns = name ^ opt_parens(space_implode ") (" vns);
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   282
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   283
     (*Arg_eqs([x1,...,xn],[y1,...,yn]) = "x1 = y1 & ... & xn = yn" *)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   284
      fun arg_eqs vns vns' =
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   285
        let fun mkeq(x,x') = x ^ "=" ^ x'
2270
d7513875b2b8 Replaced map...~~ by ListPair.map
paulson
parents: 2031
diff changeset
   286
        in space_implode " & " (ListPair.map mkeq (vns,vns')) end;
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   287
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   288
     (*Pretty printers for type lists;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   289
       pp_typlist1: parentheses, pp_typlist2: brackets*)
1279
f59b4f9f2cdc All constants introduced by datatype now operate on class term explicitly.
nipkow
parents: 980
diff changeset
   290
      fun pp_typ (dtVar s) = "(" ^ s ^ "::term)"
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   291
        | pp_typ (dtTyp (typvars, id)) =
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   292
          if null typvars then id else (pp_typlist1 typvars) ^ id
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   293
        | pp_typ (dtRek (typvars, id)) = (pp_typlist1 typvars) ^ id
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   294
      and
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   295
        pp_typlist' ts = commas (map pp_typ ts)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   296
      and
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   297
        pp_typlist1 ts = if null ts then "" else parens (pp_typlist' ts);
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   298
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   299
      fun pp_typlist2 ts = if null ts then "" else brackets (pp_typlist' ts);
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   300
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   301
     (* Generate syntax translation for case rules *)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   302
      fun calc_xrules c_nr y_nr ((_, name, _, vns, _) :: cs) = 
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   303
        let val arity = length vns;
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   304
          val body  = "z" ^ string_of_int(c_nr);
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   305
          val args1 = if arity=0 then ""
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   306
                      else " " ^ Args ("y", " ", y_nr, y_nr+arity-1);
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   307
          val args2 = if arity=0 then ""
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   308
                      else "(% " ^ Args ("y", " ", y_nr, y_nr+arity-1) 
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   309
                        ^ ". ";
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   310
          val (rest1,rest2) = 
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   311
            if null cs then ("","")
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   312
            else let val (h1, h2) = calc_xrules (c_nr+1) (y_nr+arity) cs
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   313
            in (" | " ^ h1, " " ^ h2) end;
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   314
        in (name ^ args1 ^ " => " ^ body ^ rest1,
964
5f690b184f91 fixed two severe bugs in calc_xrules and case_rule
clasohm
parents: 923
diff changeset
   315
            args2 ^ body ^ (if args2 = "" then "" else ")") ^ rest2)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   316
        end
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   317
        | calc_xrules _ _ [] = raise Impossible;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   318
      
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   319
      val xrules =
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   320
        let val (first_part, scnd_part) = calc_xrules 1 1 cons_list
3534
c245c88194ff renamed |-> <-| <-> to Parse/PrintRule;
wenzelm
parents: 3308
diff changeset
   321
        in [Syntax.ParsePrintRule (("logic", "case x of " ^ first_part),
2031
03a843f0f447 Ran expandshort
paulson
parents: 1897
diff changeset
   322
                        ("logic", tname ^ "_case " ^ scnd_part ^ " x"))]
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   323
        end;
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   324
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   325
     (*type declarations for constructors*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   326
      fun const_type (id, _, typlist, _, syn) =
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   327
        (id,  
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   328
         (if null typlist then "" else pp_typlist2 typlist ^ " => ") ^
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   329
            pp_typlist1 typevars ^ tname, syn);
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   330
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   331
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   332
      fun assumpt (dtRek _ :: ts, v :: vs ,found) =
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   333
        let val h = if found then ";P(" ^ v ^ ")" else "[| P(" ^ v ^ ")"
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   334
        in h ^ (assumpt (ts, vs, true)) end
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   335
        | assumpt (t :: ts, v :: vs, found) = assumpt (ts, vs, found)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   336
      | assumpt ([], [], found) = if found then "|] ==>" else ""
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   337
        | assumpt _ = raise Impossible;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   338
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   339
      fun t_inducting ((_, name, types, vns, _) :: cs) =
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   340
        let
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   341
          val h = if null types then " P(" ^ name ^ ")"
3842
b55686a7b22c fixed dots;
wenzelm
parents: 3792
diff changeset
   342
                  else " !!" ^ (space_implode " " vns) ^ ". " ^
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   343
                    (assumpt (types, vns, false)) ^
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   344
                    "P(" ^ C_exp name vns ^ ")";
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   345
          val rest = t_inducting cs;
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   346
        in if rest = "" then h else h ^ "; " ^ rest end
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   347
        | t_inducting [] = "";
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   348
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   349
      fun t_induct cl typ_name =
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   350
        "[|" ^ t_inducting cl ^ "|] ==> P(" ^ typ_name ^ ")";
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   351
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   352
      fun gen_typlist typevar f ((_, _, ts, _, _) :: cs) =
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   353
        let val h = if (length ts) > 0
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   354
                      then pp_typlist2(f ts) ^ "=>"
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   355
                    else ""
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   356
        in h ^ typevar ^  "," ^ (gen_typlist typevar f cs) end
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   357
        | gen_typlist _ _ [] = "";
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   358
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   359
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   360
(* -------------------------------------------------------------------- *)
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   361
(* The case constant and rules                                          *)
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   362
                
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   363
      val t_case = tname ^ "_case";
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   364
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   365
      fun case_rule n (id, name, _, vns, _) =
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   366
        let val args = if vns = [] then "" else " " ^ space_implode " " vns
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   367
        in (t_case ^ "_" ^ id,
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   368
            t_case ^ " " ^ Args("f", " ", 1, num_of_cons)
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   369
            ^ " (" ^ name ^ args ^ ") = f"^string_of_int(n) ^ args)
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   370
        end
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   371
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   372
      fun case_rules n (c :: cs) = case_rule n c :: case_rules(n+1) cs
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   373
        | case_rules _ [] = [];
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   374
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   375
      val datatype_arity = length typevars;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   376
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   377
      val types = [(tname, datatype_arity, NoSyn)];
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   378
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   379
      val arities = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   380
        let val term_list = replicate datatype_arity termS;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   381
        in [(tname, term_list, termS)] 
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   382
        end;
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   383
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   384
      val datatype_name = pp_typlist1 typevars ^ tname;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   385
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   386
      val new_tvar_name = variant (map (fn dtVar s => s) typevars) "'z";
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   387
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   388
      val case_const =
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   389
        (t_case,
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   390
         "[" ^ gen_typlist new_tvar_name I cons_list 
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   391
         ^  pp_typlist1 typevars ^ tname ^ "] =>" ^ new_tvar_name^"::term",
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   392
         NoSyn);
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   393
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   394
      val rules_case = case_rules 1 cons_list;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   395
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   396
(* -------------------------------------------------------------------- *)
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   397
(* The prim-rec combinator                                              *) 
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   398
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   399
      val t_rec = tname ^ "_rec"
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   400
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   401
(* adding type variables for dtRek types to end of list of dt_types      *)   
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   402
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   403
      fun add_reks ts = 
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   404
        ts @ map (fn _ => dtVar new_tvar_name) (filter is_dtRek ts); 
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   405
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   406
(* positions of the dtRek types in a list of dt_types, starting from 1  *)
2270
d7513875b2b8 Replaced map...~~ by ListPair.map
paulson
parents: 2031
diff changeset
   407
      fun rek_vars ts vns = map #2 (filter (is_dtRek o fst) (ts ~~ vns))
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   408
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   409
      fun rec_rule n (id,name,ts,vns,_) = 
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   410
        let val args = opt_parens(space_implode ") (" vns)
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   411
          val fargs = opt_parens(Args("f", ") (", 1, num_of_cons))
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   412
          fun rarg vn = t_rec ^ fargs ^ " (" ^ vn ^ ")"
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   413
          val rargs = opt_parens(space_implode ") ("
964
5f690b184f91 fixed two severe bugs in calc_xrules and case_rule
clasohm
parents: 923
diff changeset
   414
                                 (map rarg (rek_vars ts vns)))
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   415
        in
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   416
          (t_rec ^ "_" ^ id,
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   417
           t_rec ^ fargs ^ " (" ^ name ^ args ^ ") = f"
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   418
           ^ string_of_int(n) ^ args ^ rargs)
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   419
        end
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   420
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   421
      fun rec_rules n (c::cs) = rec_rule n c :: rec_rules (n+1) cs 
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   422
        | rec_rules _ [] = [];
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   423
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   424
      val rec_const =
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   425
        (t_rec,
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   426
         "[" ^ (gen_typlist new_tvar_name add_reks cons_list) 
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   427
         ^ (pp_typlist1 typevars) ^ tname ^ "] =>" ^ new_tvar_name^"::term",
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   428
         NoSyn);
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   429
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   430
      val rules_rec = rec_rules 1 cons_list
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   431
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   432
(* -------------------------------------------------------------------- *)
3308
da002cef7090 Added overloaded function `size' for all datatypes.
nipkow
parents: 3292
diff changeset
   433
(* The size function                                                    *) 
da002cef7090 Added overloaded function `size' for all datatypes.
nipkow
parents: 3292
diff changeset
   434
da002cef7090 Added overloaded function `size' for all datatypes.
nipkow
parents: 3292
diff changeset
   435
      fun size_eqn(_,name,types,vns,_) =
da002cef7090 Added overloaded function `size' for all datatypes.
nipkow
parents: 3292
diff changeset
   436
        let fun sum((T,vn)::args) =
da002cef7090 Added overloaded function `size' for all datatypes.
nipkow
parents: 3292
diff changeset
   437
                  if is_dtRek T then "size(" ^ vn ^ ") + " ^ sum(args)
da002cef7090 Added overloaded function `size' for all datatypes.
nipkow
parents: 3292
diff changeset
   438
                  else sum args
da002cef7090 Added overloaded function `size' for all datatypes.
nipkow
parents: 3292
diff changeset
   439
              | sum [] = "1"
da002cef7090 Added overloaded function `size' for all datatypes.
nipkow
parents: 3292
diff changeset
   440
            val rhs = if exists is_dtRek types then sum(types~~vns) else "0"
da002cef7090 Added overloaded function `size' for all datatypes.
nipkow
parents: 3292
diff changeset
   441
        in ("", "size(" ^ C_exp name vns ^ ") = " ^ rhs) end;
da002cef7090 Added overloaded function `size' for all datatypes.
nipkow
parents: 3292
diff changeset
   442
da002cef7090 Added overloaded function `size' for all datatypes.
nipkow
parents: 3292
diff changeset
   443
      val size_eqns  = map size_eqn cons_list;
da002cef7090 Added overloaded function `size' for all datatypes.
nipkow
parents: 3292
diff changeset
   444
da002cef7090 Added overloaded function `size' for all datatypes.
nipkow
parents: 3292
diff changeset
   445
(* -------------------------------------------------------------------- *)
4032
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 3979
diff changeset
   446
(* The split equation                                                   *) 
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 3979
diff changeset
   447
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 3979
diff changeset
   448
      local
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 3979
diff changeset
   449
      val fs = map (fn i => "f"^(string_of_int i)) (1 upto num_of_cons);
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 3979
diff changeset
   450
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 3979
diff changeset
   451
      fun split1case ((_,name,_,vns,_),fi) =
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 3979
diff changeset
   452
        let val svns = " " ^ (space_implode " " vns);
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 3979
diff changeset
   453
            val all = if null vns then "" else "!" ^ svns ^ ". "
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 3979
diff changeset
   454
        in "("^all^tname^"0 = "^C_exp name vns^" --> P("^fi^svns^"))" end;
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 3979
diff changeset
   455
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 3979
diff changeset
   456
      val rhss  = map split1case (cons_list ~~ fs);
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 3979
diff changeset
   457
      val rhs = space_implode " & " rhss;
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 3979
diff changeset
   458
      val lhs = "P(" ^ t_case ^ " " ^ (space_implode " " fs) ^" "^ tname^"0)"
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 3979
diff changeset
   459
      in
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 3979
diff changeset
   460
      val split_eqn = lhs ^ " = (" ^ rhs ^ ")"
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 3979
diff changeset
   461
      end;
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 3979
diff changeset
   462
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 3979
diff changeset
   463
(* -------------------------------------------------------------------- *)
4b1c69d8b767 For each datatype `t' there is now a theorem `split_t_case' of the form
nipkow
parents: 3979
diff changeset
   464
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   465
      val consts = 
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   466
        map const_type cons_list
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   467
        @ (if num_of_cons < dtK then []
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   468
           else [(tname ^ "_ord", datatype_name ^ "=>nat", NoSyn)])
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   469
        @ [case_const,rec_const];
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   470
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   471
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   472
      fun Ci_ing ((id, name, _, vns, _) :: cs) =
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   473
           if null vns then Ci_ing cs
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   474
           else let val vns' = variantlist(vns,vns)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   475
                in ("inject_" ^ id,
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   476
                    "(" ^ (C_exp name vns) ^ "=" ^ (C_exp name vns')
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   477
                    ^ ") = (" ^ (arg_eqs vns vns') ^ ")") :: (Ci_ing cs)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   478
                end
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   479
        | Ci_ing [] = [];
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   480
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   481
      fun Ci_negOne (id1,name1,_,vns1,_) (id2,name2,_,vns2,_) =
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   482
            let val vns2' = variantlist(vns2,vns1)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   483
                val ax = C_exp name1 vns1 ^ "~=" ^ C_exp name2 vns2'
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   484
        in (id1 ^ "_not_" ^ id2, ax) end;
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   485
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   486
      fun Ci_neg1 [] = []
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   487
        | Ci_neg1 (c1::cs) = (map (Ci_negOne c1) cs) @ Ci_neg1 cs;
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   488
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   489
      fun suc_expr n = 
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   490
        if n=0 then "0" else "Suc(" ^ suc_expr(n-1) ^ ")";
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   491
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   492
      fun Ci_neg2() =
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   493
        let val ord_t = tname ^ "_ord";
2270
d7513875b2b8 Replaced map...~~ by ListPair.map
paulson
parents: 2031
diff changeset
   494
          val cis = ListPair.zip (cons_list, 0 upto (num_of_cons - 1))
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   495
          fun Ci_neg2equals ((id, name, _, vns, _), n) =
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   496
            let val ax = ord_t ^ "(" ^ (C_exp name vns) ^ ") = " ^ (suc_expr n)
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   497
            in (ord_t ^ "_" ^ id, ax) end
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   498
        in (ord_t ^ "_distinct", ord_t^"(x) ~= "^ord_t^"(y) ==> x ~= y") ::
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   499
          (map Ci_neg2equals cis)
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   500
        end;
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   501
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   502
      val rules_distinct = if num_of_cons < dtK then Ci_neg1 cons_list
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   503
                           else Ci_neg2();
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   504
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   505
      val rules_inject = Ci_ing cons_list;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   506
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   507
      val rule_induct = (tname ^ "_induct", t_induct cons_list tname);
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   508
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   509
      val rules = rule_induct ::
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   510
        (rules_inject @ rules_distinct @ rules_case @ rules_rec);
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   511
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   512
      fun add_primrec eqns thy =
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   513
        let val rec_comb = Const(t_rec,dummyT)
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   514
          val teqns = map (fn neq => snd(read_axm (sign_of thy) neq)) eqns
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   515
          val (fname,ls,fns) = trans_recs thy cons_list teqns
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   516
          val rhs = 
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   517
            list_abs_free
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   518
            (ls @ [(tname,dummyT)]
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   519
             ,list_comb(rec_comb
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   520
                        , fns @ map Bound (0 ::(length ls downto 1))));
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   521
          val sg = sign_of thy;
1574
5a63ab90ee8a modified primrec so it can be used in MiniML/Type.thy
clasohm
parents: 1465
diff changeset
   522
          val defpair = (fname ^ "_" ^ tname ^ "_def",
5a63ab90ee8a modified primrec so it can be used in MiniML/Type.thy
clasohm
parents: 1465
diff changeset
   523
                         Logic.mk_equals (Const(fname,dummyT), rhs))
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   524
          val defpairT as (_, _ $ Const(_,T) $ _ ) = inferT_axm sg defpair;
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   525
          val varT = Type.varifyT T;
3945
ae9c61d69888 adapted to qualified names;
wenzelm
parents: 3842
diff changeset
   526
          val ftyp = the (Sign.const_type sg (Sign.intern_const sg fname));
4040
20f7471eedbf PureThy.add_store_defs_i, PureThy.add_store_axioms;
wenzelm
parents: 4032
diff changeset
   527
        in PureThy.add_store_defs_i [defpairT] thy end;
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   528
1360
6bdee79ef125 added call of store_datatype
clasohm
parents: 1279
diff changeset
   529
    in
3768
67f4ac759100 fully qualified names: Theory.add_XXX;
wenzelm
parents: 3615
diff changeset
   530
      (thy |> Theory.add_types types
67f4ac759100 fully qualified names: Theory.add_XXX;
wenzelm
parents: 3615
diff changeset
   531
           |> Theory.add_arities arities
67f4ac759100 fully qualified names: Theory.add_XXX;
wenzelm
parents: 3615
diff changeset
   532
           |> Theory.add_consts consts
67f4ac759100 fully qualified names: Theory.add_XXX;
wenzelm
parents: 3615
diff changeset
   533
           |> Theory.add_trrules xrules
4040
20f7471eedbf PureThy.add_store_defs_i, PureThy.add_store_axioms;
wenzelm
parents: 4032
diff changeset
   534
           |> PureThy.add_store_axioms rules, add_primrec, size_eqns, split_eqn)
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   535
    end
3040
7d48671753da Introduced a generic "induct_tac" which picks up the right induction scheme
nipkow
parents: 2880
diff changeset
   536
3564
f886dbd91ee5 Now Datatype.occs_in_prems prints the necessary warning ITSELF.
paulson
parents: 3538
diff changeset
   537
(*Warn if the (induction) variable occurs Free among the premises, which
f886dbd91ee5 Now Datatype.occs_in_prems prints the necessary warning ITSELF.
paulson
parents: 3538
diff changeset
   538
  usually signals a mistake.  But calls the tactic either way!*)
f886dbd91ee5 Now Datatype.occs_in_prems prints the necessary warning ITSELF.
paulson
parents: 3538
diff changeset
   539
fun occs_in_prems tacf a = 
f886dbd91ee5 Now Datatype.occs_in_prems prints the necessary warning ITSELF.
paulson
parents: 3538
diff changeset
   540
  SUBGOAL (fn (Bi,i) =>
f886dbd91ee5 Now Datatype.occs_in_prems prints the necessary warning ITSELF.
paulson
parents: 3538
diff changeset
   541
	   (if  exists (fn Free(a',_) => a=a')
f886dbd91ee5 Now Datatype.occs_in_prems prints the necessary warning ITSELF.
paulson
parents: 3538
diff changeset
   542
	              (foldr add_term_frees (#2 (strip_context Bi), []))
f886dbd91ee5 Now Datatype.occs_in_prems prints the necessary warning ITSELF.
paulson
parents: 3538
diff changeset
   543
	     then warning "Induction variable occurs also among premises!"
f886dbd91ee5 Now Datatype.occs_in_prems prints the necessary warning ITSELF.
paulson
parents: 3538
diff changeset
   544
	     else ();
f886dbd91ee5 Now Datatype.occs_in_prems prints the necessary warning ITSELF.
paulson
parents: 3538
diff changeset
   545
	    tacf a i));
3040
7d48671753da Introduced a generic "induct_tac" which picks up the right induction scheme
nipkow
parents: 2880
diff changeset
   546
7d48671753da Introduced a generic "induct_tac" which picks up the right induction scheme
nipkow
parents: 2880
diff changeset
   547
end;
7d48671753da Introduced a generic "induct_tac" which picks up the right induction scheme
nipkow
parents: 2880
diff changeset
   548
7d48671753da Introduced a generic "induct_tac" which picks up the right induction scheme
nipkow
parents: 2880
diff changeset
   549
end;
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   550
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   551
(*
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   552
Informal description of functions used in datatype.ML for the Isabelle/HOL
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   553
implementation of prim. rec. function definitions. (N. Voelker, Feb. 1995) 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   554
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   555
* subst_apps (fname,rpos) pairs t:
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   556
   substitute the term 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   557
       fname(ls,xk,rs) 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   558
   by 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   559
      yk(ls,rs) 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   560
   in t for (xk,yk) in pairs, where rpos = length ls. 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   561
   Applied with : 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   562
     fname = function name 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   563
     rpos = position of recursive argument 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   564
     pairs = list of pairs (xk,yk), where 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   565
          xk are the rec. arguments of the constructor in the pattern,
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   566
          yk is a variable with name derived from xk 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   567
     t = rhs of equation 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   568
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   569
* abst_rec (fname,rpos,tc,ls,cargs,rs,rhs)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   570
  - filter recursive arguments from constructor arguments cargs,
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   571
  - perform substitutions on rhs, 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   572
  - derive list subs of new variable names yk for use in subst_apps, 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   573
  - abstract rhs with respect to cargs, subs, ls and rs. 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   574
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   575
* dest_eq t 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   576
  destruct a term denoting an equation into lhs and rhs. 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   577
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   578
* dest_req eq 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   579
  destruct an equation of the form 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   580
      name (vl1..vlrpos, Ci(vi1..vin), vr1..vrn) = rhs
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   581
  into 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   582
  - function name  (name) 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   583
  - position of the first non-variable parameter  (rpos)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   584
  - the list of first rpos parameters (ls = [vl1..vlrpos]) 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   585
  - the constructor (fst( dest_Const c) = Ci)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   586
  - the arguments of the constructor (cargs = [vi1..vin])
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   587
  - the rest of the variables in the pattern (rs = [vr1..vrn])
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   588
  - the right hand side of the equation (rhs).  
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   589
 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   590
* check_and_sort (n,its)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   591
  check that  n = length its holds, and sort elements of its by 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   592
  first component. 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   593
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   594
* trans_recs thy cs' (eq1::eqs)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   595
  destruct eq1 into name1, rpos1, ls1, etc.. 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   596
  get constructor list with and without type (tcs resp. cs) from cs',  
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   597
  for every equation:  
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   598
    destruct it into (name,rpos,ls,c,cargs,rs,rhs)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   599
    get typed constructor tc from c and tcs 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   600
    determine the index i of the constructor 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   601
    check function name and position of rec. argument by comparison
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   602
    with first equation 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   603
    check for repeated variable names in pattern
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   604
    derive function term f_i which is used as argument of the rec. combinator
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   605
    sort the terms f_i according to i and return them together
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   606
      with the function name and the parameter of the definition (ls). 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   607
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   608
* Application:
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   609
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   610
  The rec. combinator is applied to the function terms resulting from
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   611
  trans_rec. This results in a function which takes the recursive arg. 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   612
  as first parameter and then the arguments corresponding to ls. The
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   613
  order of parameters is corrected by setting the rhs equal to 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   614
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   615
  list_abs_free
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   616
            (ls @ [(tname,dummyT)]
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   617
             ,list_comb(rec_comb
5d7a7e439cec expanded tabs
clasohm
parents: 1455
diff changeset
   618
                        , fns @ map Bound (0 ::(length ls downto 1))));
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   619
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   620
  Note the de-Bruijn indices counting the number of lambdas between the
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   621
  variable and its binding. 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   622
*)
1668
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   623
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   624
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   625
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   626
(* ----------------------------------------------- *)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   627
(* The following has been written by Konrad Slind. *)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   628
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   629
3040
7d48671753da Introduced a generic "induct_tac" which picks up the right induction scheme
nipkow
parents: 2880
diff changeset
   630
(* type dtype_info is defined in simpdata.ML *)
1668
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   631
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   632
signature Dtype_sig =
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   633
sig
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   634
  val build_case_cong: Sign.sg -> thm list -> cterm
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   635
  val build_nchotomy: Sign.sg -> thm list -> cterm
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   636
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   637
  val prove_case_cong: thm -> thm list -> cterm -> thm
1690
e0ff33a33fa5 added changes by Konrad to prove_nchotomy
clasohm
parents: 1668
diff changeset
   638
  val prove_nchotomy: (string -> int -> tactic) -> cterm -> thm
1668
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   639
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   640
  val case_thms : Sign.sg -> thm list -> (string -> int -> tactic)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   641
                   -> {nchotomy:thm, case_cong:thm}
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   642
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   643
  val build_record : (theory * (string * string list)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   644
                      * (string -> int -> tactic))
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   645
                     -> (string * dtype_info) 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   646
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   647
end;
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   648
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   649
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   650
(*---------------------------------------------------------------------------
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   651
 * This structure is support for the Isabelle datatype package. It provides
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   652
 * entrypoints for 1) building and proving the case congruence theorem for
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   653
 * a datatype and 2) building and proving the "exhaustion" theorem for
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   654
 * a datatype (I have called this theorem "nchotomy" for no good reason).
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   655
 *
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   656
 * It also brings all these together in the function "build_record", which
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   657
 * is probably what will be used.
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   658
 *
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   659
 * Since these routines are required in order to support TFL, they have
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   660
 * been written so they will compile "stand-alone", i.e., in Isabelle-HOL
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   661
 * without any TFL code around.
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   662
 *---------------------------------------------------------------------------*)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   663
structure Dtype : Dtype_sig =
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   664
struct
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   665
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   666
exception DTYPE_ERR of {func:string, mesg:string};
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   667
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   668
(*---------------------------------------------------------------------------
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   669
 * General support routines
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   670
 *---------------------------------------------------------------------------*)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   671
fun itlist f L base_value =
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   672
   let fun it [] = base_value
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   673
         | it (a::rst) = f a (it rst)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   674
   in it L 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   675
   end;
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   676
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   677
fun end_itlist f =
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   678
let fun endit [] = raise DTYPE_ERR{func="end_itlist", mesg="list too short"}
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   679
      | endit alist = 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   680
         let val (base::ralist) = rev alist
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   681
         in itlist f (rev ralist) base  end
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   682
in endit
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   683
end;
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   684
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   685
fun unzip L = itlist (fn (x,y) => fn (l1,l2) =>((x::l1),(y::l2))) L ([],[]);
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   686
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   687
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   688
(*---------------------------------------------------------------------------
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   689
 * Miscellaneous Syntax manipulation
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   690
 *---------------------------------------------------------------------------*)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   691
val mk_var = Free;
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   692
val mk_const = Const
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   693
fun mk_comb(Rator,Rand) = Rator $ Rand;
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   694
fun mk_abs(r as (Var((s,_),ty),_))  = Abs(s,ty,abstract_over r)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   695
  | mk_abs(r as (Free(s,ty),_))     = Abs(s,ty,abstract_over r)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   696
  | mk_abs _ = raise DTYPE_ERR{func="mk_abs", mesg="1st not a variable"};
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   697
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   698
fun dest_var(Var((s,i),ty)) = (s,ty)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   699
  | dest_var(Free(s,ty))    = (s,ty)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   700
  | dest_var _ = raise DTYPE_ERR{func="dest_var", mesg="not a variable"};
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   701
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   702
fun dest_const(Const p) = p
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   703
  | dest_const _ = raise DTYPE_ERR{func="dest_const", mesg="not a constant"};
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   704
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   705
fun dest_comb(t1 $ t2) = (t1,t2)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   706
  | dest_comb _ =  raise DTYPE_ERR{func = "dest_comb", mesg = "not a comb"};
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   707
val rand = #2 o dest_comb;
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   708
val rator = #1 o dest_comb;
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   709
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   710
fun dest_abs(a as Abs(s,ty,M)) = 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   711
     let val v = Free(s, ty)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   712
      in (v, betapply (a,v)) end
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   713
  | dest_abs _ =  raise DTYPE_ERR{func="dest_abs", mesg="not an abstraction"};
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   714
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   715
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   716
val bool = Type("bool",[])
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   717
and prop = Type("prop",[]);
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   718
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   719
fun mk_eq(lhs,rhs) = 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   720
   let val ty = type_of lhs
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   721
       val c = mk_const("op =", ty --> ty --> bool)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   722
   in list_comb(c,[lhs,rhs])
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   723
   end
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   724
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   725
fun dest_eq(Const("op =",_) $ M $ N) = (M, N)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   726
  | dest_eq _ = raise DTYPE_ERR{func="dest_eq", mesg="not an equality"};
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   727
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   728
fun mk_disj(disj1,disj2) =
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   729
   let val c = Const("op |", bool --> bool --> bool)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   730
   in list_comb(c,[disj1,disj2])
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   731
   end;
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   732
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   733
fun mk_forall (r as (Bvar,_)) = 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   734
  let val ty = type_of Bvar
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   735
      val c = Const("All", (ty --> bool) --> bool)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   736
  in mk_comb(c, mk_abs r)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   737
  end;
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   738
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   739
fun mk_exists (r as (Bvar,_)) = 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   740
  let val ty = type_of Bvar 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   741
      val c = Const("Ex", (ty --> bool) --> bool)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   742
  in mk_comb(c, mk_abs r)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   743
  end;
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   744
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   745
fun mk_prop (tm as Const("Trueprop",_) $ _) = tm
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   746
  | mk_prop tm = mk_comb(Const("Trueprop", bool --> prop),tm);
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   747
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   748
fun drop_prop (Const("Trueprop",_) $ X) = X
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   749
  | drop_prop X = X;
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   750
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   751
fun mk_all (r as (Bvar,_)) = mk_comb(all (type_of Bvar), mk_abs r);
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   752
fun list_mk_all(V,t) = itlist(fn v => fn b => mk_all(v,b)) V t;
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   753
fun list_mk_exists(V,t) = itlist(fn v => fn b => mk_exists(v,b)) V t;
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   754
val list_mk_disj = end_itlist(fn d1 => fn tm => mk_disj(d1,tm))
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   755
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   756
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   757
fun dest_thm thm = 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   758
   let val {prop,hyps,...} = rep_thm thm
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   759
   in (map drop_prop hyps, drop_prop prop)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   760
   end;
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   761
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   762
val concl = #2 o dest_thm;
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   763
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   764
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   765
(*---------------------------------------------------------------------------
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   766
 * Names of all variables occurring in a term, including bound ones. These
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   767
 * are added into the second argument.
3265
8358e19d0d4c Replaced Konrad's own add_term_names by the predefined one.
nipkow
parents: 3197
diff changeset
   768
 *---------------------------------------------------------------------------
1668
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   769
fun add_term_names tm =
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   770
let fun insert (x:string) = 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   771
     let fun canfind[] = [x] 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   772
           | canfind(alist as (y::rst)) = 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   773
              if (x<y) then x::alist
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   774
              else if (x=y) then y::rst
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   775
              else y::canfind rst 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   776
     in canfind end
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   777
    fun add (Free(s,_)) V = insert s V
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   778
      | add (Var((s,_),_)) V = insert s V
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   779
      | add (Abs(s,_,body)) V = add body (insert s V)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   780
      | add (f$t) V = add t (add f V)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   781
      | add _ V = V
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   782
in add tm
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   783
end;
3265
8358e19d0d4c Replaced Konrad's own add_term_names by the predefined one.
nipkow
parents: 3197
diff changeset
   784
Why bound ones???
8358e19d0d4c Replaced Konrad's own add_term_names by the predefined one.
nipkow
parents: 3197
diff changeset
   785
*)
1668
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   786
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   787
(*---------------------------------------------------------------------------
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   788
 * We need to make everything free, so that we can put the term into a
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   789
 * goalstack, or submit it as an argument to prove_goalw_cterm.
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   790
 *---------------------------------------------------------------------------*)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   791
fun make_free_ty(Type(s,alist)) = Type(s,map make_free_ty alist)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   792
  | make_free_ty(TVar((s,i),srt)) = TFree(s,srt)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   793
  | make_free_ty x = x;
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   794
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   795
fun make_free (Var((s,_),ty)) = Free(s,make_free_ty ty)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   796
  | make_free (Abs(s,x,body)) = Abs(s,make_free_ty x, make_free body)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   797
  | make_free (f$t) = (make_free f $ make_free t)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   798
  | make_free (Const(s,ty)) = Const(s, make_free_ty ty)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   799
  | make_free (Free(s,ty)) = Free(s, make_free_ty ty)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   800
  | make_free b = b;
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   801
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   802
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   803
(*---------------------------------------------------------------------------
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   804
 * Structure of case congruence theorem looks like this:
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   805
 *
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   806
 *    (M = M') 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   807
 *    ==> (!!x1,...,xk. (M' = C1 x1..xk) ==> (f1 x1..xk = f1' x1..xk)) 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   808
 *    ==> ... 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   809
 *    ==> (!!x1,...,xj. (M' = Cn x1..xj) ==> (fn x1..xj = fn' x1..xj)) 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   810
 *    ==>
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   811
 *      (ty_case f1..fn M = ty_case f1'..fn' m')
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   812
 *
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   813
 * The input is the list of rules for the case construct for the type, i.e.,
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   814
 * that found in the "ty.cases" field of a theory where datatype "ty" is
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   815
 * defined.
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   816
 *---------------------------------------------------------------------------*)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   817
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   818
fun build_case_cong sign case_rewrites =
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   819
 let val clauses = map concl case_rewrites
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   820
     val clause1 = hd clauses
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   821
     val left = (#1 o dest_eq) clause1
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   822
     val ty = type_of ((#2 o dest_comb) left)
3265
8358e19d0d4c Replaced Konrad's own add_term_names by the predefined one.
nipkow
parents: 3197
diff changeset
   823
     val varnames = foldr add_term_names (clauses, [])
1668
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   824
     val M = variant varnames "M"
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   825
     val Mvar = Free(M, ty)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   826
     val M' = variant (M::varnames) M
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   827
     val M'var = Free(M', ty)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   828
     fun mk_clause clause =
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   829
       let val (lhs,rhs) = dest_eq clause
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   830
           val func = (#1 o strip_comb) rhs
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   831
           val (constr,xbar) = strip_comb(rand lhs)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   832
           val (Name,Ty) = dest_var func
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   833
           val func'name = variant (M::M'::varnames) (Name^"a")
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   834
           val func' = mk_var(func'name,Ty)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   835
       in (func', list_mk_all
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   836
                  (xbar, Logic.mk_implies
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   837
                         (mk_prop(mk_eq(M'var, list_comb(constr,xbar))),
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   838
                          mk_prop(mk_eq(list_comb(func, xbar),
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   839
                                        list_comb(func',xbar))))))   end
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   840
     val (funcs',clauses') = unzip (map mk_clause clauses)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   841
     val lhsM = mk_comb(rator left, Mvar)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   842
     val c = #1(strip_comb left)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   843
 in
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   844
 cterm_of sign
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   845
  (make_free
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   846
   (Logic.list_implies(mk_prop(mk_eq(Mvar, M'var))::clauses',
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   847
                       mk_prop(mk_eq(lhsM, list_comb(c,(funcs'@[M'var])))))))
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   848
 end
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   849
 handle _ => raise DTYPE_ERR{func="build_case_cong",mesg="failed"};
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   850
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   851
  
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   852
(*---------------------------------------------------------------------------
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   853
 * Proves the result of "build_case_cong". 
1897
71e51870cc9a Replaced prove_case_cong by Konrad Slinds optimized version.
berghofe
parents: 1810
diff changeset
   854
 * This one solves it a disjunct at a time, and builds the ss only once.
1668
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   855
 *---------------------------------------------------------------------------*)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   856
fun prove_case_cong nchotomy case_rewrites ctm =
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   857
 let val {sign,t,...} = rep_cterm ctm
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   858
     val (Const("==>",_) $ tm $ _) = t
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   859
     val (Const("Trueprop",_) $ (Const("op =",_) $ _ $ Ma)) = tm
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   860
     val (Free(str,_)) = Ma
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   861
     val thm = prove_goalw_cterm[] ctm
1897
71e51870cc9a Replaced prove_case_cong by Konrad Slinds optimized version.
berghofe
parents: 1810
diff changeset
   862
      (fn prems => 
71e51870cc9a Replaced prove_case_cong by Konrad Slinds optimized version.
berghofe
parents: 1810
diff changeset
   863
        let val simplify = asm_simp_tac(HOL_ss addsimps (prems@case_rewrites))
71e51870cc9a Replaced prove_case_cong by Konrad Slinds optimized version.
berghofe
parents: 1810
diff changeset
   864
        in [simp_tac (HOL_ss addsimps [hd prems]) 1,
71e51870cc9a Replaced prove_case_cong by Konrad Slinds optimized version.
berghofe
parents: 1810
diff changeset
   865
            cut_inst_tac [("x",str)] (nchotomy RS spec) 1,
71e51870cc9a Replaced prove_case_cong by Konrad Slinds optimized version.
berghofe
parents: 1810
diff changeset
   866
            REPEAT (etac disjE 1 THEN REPEAT (etac exE 1) THEN simplify 1),
71e51870cc9a Replaced prove_case_cong by Konrad Slinds optimized version.
berghofe
parents: 1810
diff changeset
   867
            REPEAT (etac exE 1) THEN simplify 1 (* Get last disjunct *)]
71e51870cc9a Replaced prove_case_cong by Konrad Slinds optimized version.
berghofe
parents: 1810
diff changeset
   868
        end) 
1668
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   869
 in standard (thm RS eq_reflection)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   870
 end
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   871
 handle _ => raise DTYPE_ERR{func="prove_case_cong",mesg="failed"};
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   872
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   873
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   874
(*---------------------------------------------------------------------------
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   875
 * Structure of exhaustion theorem looks like this:
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   876
 *
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   877
 *    !v. (EX y1..yi. v = C1 y1..yi) | ... | (EX y1..yj. v = Cn y1..yj)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   878
 *
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   879
 * As for "build_case_cong", the input is the list of rules for the case 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   880
 * construct (the case "rewrites").
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   881
 *---------------------------------------------------------------------------*)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   882
fun build_nchotomy sign case_rewrites =
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   883
 let val clauses = map concl case_rewrites
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   884
     val C_ybars = map (rand o #1 o dest_eq) clauses
3265
8358e19d0d4c Replaced Konrad's own add_term_names by the predefined one.
nipkow
parents: 3197
diff changeset
   885
     val varnames = foldr add_term_names (C_ybars, [])
1668
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   886
     val vname = variant varnames "v"
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   887
     val ty = type_of (hd C_ybars)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   888
     val v = mk_var(vname,ty)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   889
     fun mk_disj C_ybar =
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   890
       let val ybar = #2(strip_comb C_ybar)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   891
       in list_mk_exists(ybar, mk_eq(v,C_ybar))
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   892
       end
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   893
 in
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   894
 cterm_of sign
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   895
   (make_free(mk_prop (mk_forall(v, list_mk_disj (map mk_disj C_ybars)))))
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   896
 end
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   897
 handle _ => raise DTYPE_ERR{func="build_nchotomy",mesg="failed"};
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   898
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   899
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   900
(*---------------------------------------------------------------------------
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   901
 * Takes the induction tactic for the datatype, and the result from 
1690
e0ff33a33fa5 added changes by Konrad to prove_nchotomy
clasohm
parents: 1668
diff changeset
   902
 * "build_nchotomy" 
e0ff33a33fa5 added changes by Konrad to prove_nchotomy
clasohm
parents: 1668
diff changeset
   903
 *
e0ff33a33fa5 added changes by Konrad to prove_nchotomy
clasohm
parents: 1668
diff changeset
   904
 *    !v. (EX y1..yi. v = C1 y1..yi) | ... | (EX y1..yj. v = Cn y1..yj)
e0ff33a33fa5 added changes by Konrad to prove_nchotomy
clasohm
parents: 1668
diff changeset
   905
 *
e0ff33a33fa5 added changes by Konrad to prove_nchotomy
clasohm
parents: 1668
diff changeset
   906
 * and proves the theorem. The proof works along a diagonal: the nth 
e0ff33a33fa5 added changes by Konrad to prove_nchotomy
clasohm
parents: 1668
diff changeset
   907
 * disjunct in the nth subgoal is easy to solve. Thus this routine depends 
e0ff33a33fa5 added changes by Konrad to prove_nchotomy
clasohm
parents: 1668
diff changeset
   908
 * on the order of goals arising out of the application of the induction 
e0ff33a33fa5 added changes by Konrad to prove_nchotomy
clasohm
parents: 1668
diff changeset
   909
 * tactic. A more general solution would have to use injectiveness and 
e0ff33a33fa5 added changes by Konrad to prove_nchotomy
clasohm
parents: 1668
diff changeset
   910
 * distinctness rewrite rules.
1668
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   911
 *---------------------------------------------------------------------------*)
1690
e0ff33a33fa5 added changes by Konrad to prove_nchotomy
clasohm
parents: 1668
diff changeset
   912
fun prove_nchotomy induct_tac ctm =
e0ff33a33fa5 added changes by Konrad to prove_nchotomy
clasohm
parents: 1668
diff changeset
   913
 let val (Const ("Trueprop",_) $ g) = #t(rep_cterm ctm)
1668
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   914
     val (Const ("All",_) $ Abs (v,_,_)) = g
1690
e0ff33a33fa5 added changes by Konrad to prove_nchotomy
clasohm
parents: 1668
diff changeset
   915
     (* For goal i, select the correct disjunct to attack, then prove it *)
e0ff33a33fa5 added changes by Konrad to prove_nchotomy
clasohm
parents: 1668
diff changeset
   916
     fun tac i 0 = (rtac disjI1 i ORELSE all_tac) THEN
e0ff33a33fa5 added changes by Konrad to prove_nchotomy
clasohm
parents: 1668
diff changeset
   917
                   REPEAT (rtac exI i) THEN (rtac refl i)
e0ff33a33fa5 added changes by Konrad to prove_nchotomy
clasohm
parents: 1668
diff changeset
   918
       | tac i n = rtac disjI2 i THEN tac i (n-1)
1668
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   919
 in 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   920
 prove_goalw_cterm[] ctm
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   921
     (fn _ => [rtac allI 1,
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   922
               induct_tac v 1,
1690
e0ff33a33fa5 added changes by Konrad to prove_nchotomy
clasohm
parents: 1668
diff changeset
   923
               ALLGOALS (fn i => tac i (i-1))])
1668
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   924
 end
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   925
 handle _ => raise DTYPE_ERR {func="prove_nchotomy", mesg="failed"};
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   926
3282
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
   927
(*---------------------------------------------------------------------------
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
   928
 * Turn nchotomy into exhaustion:
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
   929
 *    [| !!y1..yi. v = C1 y1..yi ==> P; ...; !!y1..yj. v = Cn y1..yj ==> P |]
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
   930
 *    ==> P
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
   931
 *---------------------------------------------------------------------------*)
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
   932
fun mk_exhaust nchotomy =
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
   933
  let val tac = rtac impI 1 THEN
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
   934
                REPEAT(SOMEGOAL(eresolve_tac [disjE,exE]))
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
   935
  in standard(rule_by_tactic tac (nchotomy RS spec RS rev_mp)) end;
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
   936
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
   937
(* find name of v in exhaustion: *)
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
   938
fun exhaust_var thm =
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
   939
  let val _ $ ( _ $ Var((x,_),_) $ _ ) =
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
   940
        hd(Logic.strip_assums_hyp(hd(prems_of thm)))
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
   941
  in x end;
1668
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   942
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   943
(*---------------------------------------------------------------------------
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   944
 * Brings the preceeding functions together.
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   945
 *---------------------------------------------------------------------------*)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   946
fun case_thms sign case_rewrites induct_tac =
1690
e0ff33a33fa5 added changes by Konrad to prove_nchotomy
clasohm
parents: 1668
diff changeset
   947
  let val nchotomy = prove_nchotomy induct_tac
e0ff33a33fa5 added changes by Konrad to prove_nchotomy
clasohm
parents: 1668
diff changeset
   948
                                    (build_nchotomy sign case_rewrites)
1668
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   949
      val cong = prove_case_cong nchotomy case_rewrites
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   950
                                 (build_case_cong sign case_rewrites)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   951
  in {nchotomy=nchotomy, case_cong=cong}
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   952
  end;
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   953
1690
e0ff33a33fa5 added changes by Konrad to prove_nchotomy
clasohm
parents: 1668
diff changeset
   954
1668
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   955
(*---------------------------------------------------------------------------
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   956
 * Tests
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   957
 *
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   958
 * 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   959
     Dtype.case_thms (sign_of List.thy) List.list.cases List.list.induct_tac;
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   960
     Dtype.case_thms (sign_of Prod.thy) [split] 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   961
                     (fn s => res_inst_tac [("p",s)] PairE_lemma);
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   962
     Dtype.case_thms (sign_of Nat.thy) [nat_case_0, nat_case_Suc] nat_ind_tac;
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   963
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   964
 *
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   965
 *---------------------------------------------------------------------------*)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   966
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   967
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   968
(*---------------------------------------------------------------------------
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   969
 * Given a theory and the name (and constructors) of a datatype declared in 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   970
 * an ancestor of that theory and an induction tactic for that datatype, 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   971
 * return the information that TFL needs. This should only be called once for
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   972
 * a datatype, because "build_record" proves various facts, and thus is slow. 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   973
 * It fails on the datatype of pairs, which must be included for TFL to work. 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   974
 * The test shows how to  build the record for pairs.
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   975
 *---------------------------------------------------------------------------*)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   976
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   977
local fun mk_rw th = (th RS eq_reflection) handle _ => th
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   978
      fun get_fact thy s = (get_axiom thy s handle _ => get_thm thy s)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   979
in
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   980
fun build_record (thy,(ty,cl),itac) =
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   981
 let val sign = sign_of thy
3945
ae9c61d69888 adapted to qualified names;
wenzelm
parents: 3842
diff changeset
   982
     val intern_const = Sign.intern_const sign;
ae9c61d69888 adapted to qualified names;
wenzelm
parents: 3842
diff changeset
   983
     fun const s =
ae9c61d69888 adapted to qualified names;
wenzelm
parents: 3842
diff changeset
   984
       let val s' = intern_const s
ae9c61d69888 adapted to qualified names;
wenzelm
parents: 3842
diff changeset
   985
       in Const(s', the (Sign.const_type sign s')) end
1668
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   986
     val case_rewrites = map (fn c => get_fact thy (ty^"_case_"^c)) cl
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   987
     val {nchotomy,case_cong} = case_thms sign case_rewrites itac
3282
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
   988
     val exhaustion = mk_exhaust nchotomy
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
   989
     val exh_var = exhaust_var exhaustion;
3292
8b143c196d42 Added rotation to exhaust_tac.
nipkow
parents: 3282
diff changeset
   990
     fun exhaust_tac a =
8b143c196d42 Added rotation to exhaust_tac.
nipkow
parents: 3282
diff changeset
   991
           ALLNEWSUBGOALS (res_inst_tac [(exh_var,a)] exhaustion)
8b143c196d42 Added rotation to exhaust_tac.
nipkow
parents: 3282
diff changeset
   992
                          (rotate_tac ~1);
3564
f886dbd91ee5 Now Datatype.occs_in_prems prints the necessary warning ITSELF.
paulson
parents: 3538
diff changeset
   993
     val induct_tac = Datatype.occs_in_prems itac 
1668
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   994
 in
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   995
  (ty, {constructors = map(fn s => const s handle _ => const("op "^s)) cl,
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   996
        case_const = const (ty^"_case"),
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   997
        case_rewrites = map mk_rw case_rewrites,
3040
7d48671753da Introduced a generic "induct_tac" which picks up the right induction scheme
nipkow
parents: 2880
diff changeset
   998
        induct_tac = induct_tac,
1668
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
   999
        nchotomy = nchotomy,
3282
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
  1000
        exhaustion = exhaustion,
c31e6239d4c9 Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents: 3265
diff changeset
  1001
        exhaust_tac = exhaust_tac,
1668
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1002
        case_cong = case_cong})
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1003
 end
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1004
end;
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1005
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1006
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1007
(*---------------------------------------------------------------------------
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1008
 * Test
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1009
 *
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1010
 * 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1011
    map Dtype.build_record 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1012
          [(Nat.thy, ("nat",["0", "Suc"]), nat_ind_tac),
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1013
           (List.thy,("list",["[]", "#"]), List.list.induct_tac)]
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1014
    @
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1015
    [let val prod_case_thms = Dtype.case_thms (sign_of Prod.thy) [split] 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1016
                                 (fn s => res_inst_tac [("p",s)] PairE_lemma)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1017
         fun const s = Const(s, the(Sign.const_type (sign_of Prod.thy) s))
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1018
     in ("*", 
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1019
         {constructors = [const "Pair"],
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1020
            case_const = const "split",
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1021
         case_rewrites = [split RS eq_reflection],
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1022
             case_cong = #case_cong prod_case_thms,
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1023
              nchotomy = #nchotomy prod_case_thms}) end];
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1024
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1025
 *
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1026
 *---------------------------------------------------------------------------*)
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1027
8ead1fe65aad added Konrad's code for the datatype package
clasohm
parents: 1574
diff changeset
  1028
end;