src/HOL/Tools/TFL/thry.ML
author wenzelm
Mon, 18 Aug 2014 12:17:31 +0200
changeset 57978 8f4a332500e4
parent 41589 bbd861837ebc
child 58111 82db9ad610b9
permissions -rw-r--r--
Added tag Isabelle2014-RC4 for changeset 113b43b84412
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/Tools/TFL/thry.ML
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     2
    Author:     Konrad Slind, Cambridge University Computer Laboratory
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     3
*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     4
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     5
signature THRY =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     6
sig
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     7
  val match_term: theory -> term -> term -> (term * term) list * (typ * typ) list
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     8
  val match_type: theory -> typ -> typ -> (typ * typ) list
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     9
  val typecheck: theory -> term -> cterm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    10
  (*datatype facts of various flavours*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    11
  val match_info: theory -> string -> {constructors: term list, case_const: term} option
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    12
  val induct_info: theory -> string -> {constructors: term list, nchotomy: thm} option
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    13
  val extract_info: theory -> {case_congs: thm list, case_rewrites: thm list}
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    14
end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    15
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    16
structure Thry: THRY =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    17
struct
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    18
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    19
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    20
fun THRY_ERR func mesg = Utils.ERR {module = "Thry", func = func, mesg = mesg};
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    21
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    22
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    23
(*---------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    24
 *    Matching
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    25
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    26
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    27
local
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    28
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    29
fun tybind (ixn, (S, T)) = (TVar (ixn, S), T);
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    30
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    31
in
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    32
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    33
fun match_term thry pat ob =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    34
  let
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    35
    val (ty_theta, tm_theta) = Pattern.match thry (pat,ob) (Vartab.empty, Vartab.empty);
32035
8e77b6a250d5 tuned/modernized Envir.subst_XXX;
wenzelm
parents: 31784
diff changeset
    36
    fun tmbind (ixn, (T, t)) = (Var (ixn, Envir.subst_type ty_theta T), t)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    37
  in (map tmbind (Vartab.dest tm_theta), map tybind (Vartab.dest ty_theta))
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    38
  end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    39
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    40
fun match_type thry pat ob =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    41
  map tybind (Vartab.dest (Sign.typ_match thry (pat, ob) Vartab.empty));
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    42
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    43
end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    44
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    45
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    46
(*---------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    47
 * Typing
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    48
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    49
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    50
fun typecheck thry t =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    51
  Thm.cterm_of thry t
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    52
    handle TYPE (msg, _, _) => raise THRY_ERR "typecheck" msg
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    53
      | TERM (msg, _) => raise THRY_ERR "typecheck" msg;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    54
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    55
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    56
(*---------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    57
 * Get information about datatypes
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    58
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    59
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    60
fun match_info thy dtco =
31784
bd3486c57ba3 tuned interfaces of datatype module
haftmann
parents: 31723
diff changeset
    61
  case (Datatype.get_info thy dtco,
bd3486c57ba3 tuned interfaces of datatype module
haftmann
parents: 31723
diff changeset
    62
         Datatype.get_constrs thy dtco) of
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    63
      (SOME { case_name, ... }, SOME constructors) =>
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    64
        SOME {case_const = Const (case_name, Sign.the_const_type thy case_name), constructors = map Const constructors}
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    65
    | _ => NONE;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    66
31784
bd3486c57ba3 tuned interfaces of datatype module
haftmann
parents: 31723
diff changeset
    67
fun induct_info thy dtco = case Datatype.get_info thy dtco of
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    68
        NONE => NONE
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    69
      | SOME {nchotomy, ...} =>
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    70
          SOME {nchotomy = nchotomy,
31784
bd3486c57ba3 tuned interfaces of datatype module
haftmann
parents: 31723
diff changeset
    71
                constructors = (map Const o the o Datatype.get_constrs thy) dtco};
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    72
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    73
fun extract_info thy =
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32035
diff changeset
    74
 let val infos = map snd (Symtab.dest (Datatype.get_all thy))
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    75
 in {case_congs = map (mk_meta_eq o #case_cong) infos,
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32035
diff changeset
    76
     case_rewrites = maps (map mk_meta_eq o #case_rewrites) infos}
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    77
 end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    78
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    79
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    80
end;