TFL/thry.ML
author paulson
Wed Jul 25 13:13:01 2001 +0200 (2001-07-25)
changeset 11451 8abfb4f7bd02
parent 10769 70b9b0cfe05f
child 12340 24d31d47af1a
permissions -rw-r--r--
partial restructuring to reduce dependence on Axiom of Choice
wenzelm@10769
     1
(*  Title:      TFL/thry.ML
wenzelm@10769
     2
    ID:         $Id$
wenzelm@10769
     3
    Author:     Konrad Slind, Cambridge University Computer Laboratory
wenzelm@10769
     4
    Copyright   1997  University of Cambridge
wenzelm@10769
     5
*)
wenzelm@10769
     6
wenzelm@10769
     7
signature THRY =
wenzelm@10769
     8
sig
wenzelm@10769
     9
  val match_term: theory -> term -> term -> (term * term) list * (typ * typ) list
wenzelm@10769
    10
  val match_type: theory -> typ -> typ -> (typ * typ) list
wenzelm@10769
    11
  val typecheck: theory -> term -> cterm
wenzelm@10769
    12
  (*datatype facts of various flavours*)
wenzelm@10769
    13
  val match_info: theory -> string -> {constructors: term list, case_const: term} option
wenzelm@10769
    14
  val induct_info: theory -> string -> {constructors: term list, nchotomy: thm} option
wenzelm@10769
    15
  val extract_info: theory -> {case_congs: thm list, case_rewrites: thm list}
wenzelm@10769
    16
end;
wenzelm@10769
    17
wenzelm@10769
    18
structure Thry: THRY =
wenzelm@10769
    19
struct
wenzelm@10769
    20
wenzelm@10769
    21
wenzelm@10769
    22
fun THRY_ERR func mesg = Utils.ERR {module = "Thry", func = func, mesg = mesg};
wenzelm@10769
    23
wenzelm@10769
    24
wenzelm@10769
    25
(*---------------------------------------------------------------------------
wenzelm@10769
    26
 *    Matching
wenzelm@10769
    27
 *---------------------------------------------------------------------------*)
wenzelm@10769
    28
wenzelm@10769
    29
local fun tybind (x,y) = (TVar (x, HOLogic.termS) , y)
wenzelm@10769
    30
      fun tmbind (x,y) = (Var  (x, Term.type_of y), y)
wenzelm@10769
    31
in
wenzelm@10769
    32
 fun match_term thry pat ob =
wenzelm@10769
    33
    let val tsig = Sign.tsig_of (Theory.sign_of thry)
wenzelm@10769
    34
        val (ty_theta,tm_theta) = Pattern.match tsig (pat,ob)
wenzelm@10769
    35
    in (map tmbind tm_theta, map tybind ty_theta)
wenzelm@10769
    36
    end
wenzelm@10769
    37
wenzelm@10769
    38
 fun match_type thry pat ob = map tybind (Vartab.dest
wenzelm@10769
    39
   (Type.typ_match (Sign.tsig_of (Theory.sign_of thry)) (Vartab.empty, (pat,ob))))
wenzelm@10769
    40
end;
wenzelm@10769
    41
wenzelm@10769
    42
wenzelm@10769
    43
(*---------------------------------------------------------------------------
wenzelm@10769
    44
 * Typing
wenzelm@10769
    45
 *---------------------------------------------------------------------------*)
wenzelm@10769
    46
wenzelm@10769
    47
fun typecheck thry t =
wenzelm@10769
    48
  Thm.cterm_of (Theory.sign_of thry) t
wenzelm@10769
    49
    handle TYPE (msg, _, _) => raise THRY_ERR "typecheck" msg
wenzelm@10769
    50
      | TERM (msg, _) => raise THRY_ERR "typecheck" msg;
wenzelm@10769
    51
wenzelm@10769
    52
wenzelm@10769
    53
(*---------------------------------------------------------------------------
wenzelm@10769
    54
 * Get information about datatypes
wenzelm@10769
    55
 *---------------------------------------------------------------------------*)
wenzelm@10769
    56
wenzelm@10769
    57
fun get_info thy ty = Symtab.lookup (DatatypePackage.get_datatypes thy, ty);
wenzelm@10769
    58
wenzelm@10769
    59
fun match_info thy tname =
wenzelm@10769
    60
  case (DatatypePackage.case_const_of thy tname, DatatypePackage.constrs_of thy tname) of
wenzelm@10769
    61
      (Some case_const, Some constructors) =>
wenzelm@10769
    62
        Some {case_const = case_const, constructors = constructors}
wenzelm@10769
    63
    | _ => None;
wenzelm@10769
    64
wenzelm@10769
    65
fun induct_info thy tname = case get_info thy tname of
wenzelm@10769
    66
        None => None
wenzelm@10769
    67
      | Some {nchotomy, ...} =>
wenzelm@10769
    68
          Some {nchotomy = nchotomy,
wenzelm@10769
    69
                constructors = the (DatatypePackage.constrs_of thy tname)};
wenzelm@10769
    70
wenzelm@10769
    71
fun extract_info thy =
wenzelm@10769
    72
 let val infos = map snd (Symtab.dest (DatatypePackage.get_datatypes thy))
wenzelm@10769
    73
 in {case_congs = map (mk_meta_eq o #case_cong) infos,
wenzelm@10769
    74
     case_rewrites = flat (map (map mk_meta_eq o #case_rewrites) infos)}
wenzelm@10769
    75
 end;
wenzelm@10769
    76
wenzelm@10769
    77
wenzelm@10769
    78
end;