TFL/thry.sml
author paulson
Thu, 15 May 1997 12:29:59 +0200
changeset 3191 14bd6e5985f1
parent 2112 3902e9af752f
child 3245 241838c01caf
permissions -rw-r--r--
TFL now integrated with HOL (more work needed)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     1
structure Thry : Thry_sig (* LThry_sig *) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     2
struct
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     3
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     4
structure USyntax  = USyntax;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     5
type Type = USyntax.Type
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     6
type Preterm = USyntax.Preterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     7
type Term = USyntax.Term
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     8
type Thm = Thm.thm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     9
type Thry = theory;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    10
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    11
open Mask;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    12
structure S = USyntax;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    13
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    14
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    15
fun THRY_ERR{func,mesg} = Utils.ERR{module = "Thry",func=func,mesg=mesg};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    16
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    17
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    18
 *    Matching 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    19
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    20
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    21
local open Utils
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    22
      infix 3 |->
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    23
      fun tybind (x,y) = TVar (x,["term"])  |-> y
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    24
      fun tmbind (x,y) = Var  (x,type_of y) |-> y
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    25
in
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    26
 fun match_term thry pat ob = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    27
    let val tsig = #tsig(Sign.rep_sg(sign_of thry))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    28
        val (ty_theta,tm_theta) = Pattern.match tsig (pat,ob)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    29
    in (map tmbind tm_theta, map tybind ty_theta)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    30
    end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    31
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    32
 fun match_type thry pat ob = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    33
    map tybind(Type.typ_match (#tsig(Sign.rep_sg(sign_of thry))) ([],(pat,ob)))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    34
end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    35
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    36
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    37
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    38
 * Typing 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    39
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    40
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    41
fun typecheck thry = cterm_of (sign_of thry);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    42
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    43
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    44
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    45
(*----------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    46
 * Making a definition. The argument "tm" looks like "f = WFREC R M". This 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    47
 * entrypoint is specialized for interactive use, since it closes the theory
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    48
 * after making the definition. This allows later interactive definitions to
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    49
 * refer to previous ones. The name for the new theory is automatically 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    50
 * generated from the name of the argument theory.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    51
 *---------------------------------------------------------------------------*)
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2112
diff changeset
    52
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2112
diff changeset
    53
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2112
diff changeset
    54
(*---------------------------------------------------------------------------
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2112
diff changeset
    55
 * TFL attempts to make definitions where the lhs is a variable. Isabelle
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2112
diff changeset
    56
 * wants it to be a constant, so here we map it to a constant. Moreover, the
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2112
diff changeset
    57
 * theory should already have the constant, so we refrain from adding the
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2112
diff changeset
    58
 * constant to the theory. We just add the axiom and return the theory.
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2112
diff changeset
    59
 *---------------------------------------------------------------------------*)
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    60
local val (imp $ tprop $ (eeq $ _ $ _ )) = #prop(rep_thm(eq_reflection))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    61
      val Const(eeq_name, ty) = eeq
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    62
      val prop = #2 (S.strip_type ty)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    63
in
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    64
fun make_definition parent s tm = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    65
   let val {lhs,rhs} = S.dest_eq tm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    66
       val {Name,Ty} = S.dest_var lhs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    67
       val lhs1 = S.mk_const{Name = Name, Ty = Ty}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    68
       val eeq1 = S.mk_const{Name = eeq_name, Ty = Ty --> Ty --> prop}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    69
       val dtm = S.list_mk_comb(eeq1,[lhs1,rhs])      (* Rename "=" to "==" *)
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2112
diff changeset
    70
       val (_, tm', _) = Sign.infer_types (sign_of parent)
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2112
diff changeset
    71
	             (K None) (K None) [] true ([dtm],propT)
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2112
diff changeset
    72
       val new_thy = add_defs_i [(s,tm')] parent
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    73
   in 
3191
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2112
diff changeset
    74
   (freezeT((get_axiom new_thy s) RS meta_eq_to_obj_eq), new_thy)
14bd6e5985f1 TFL now integrated with HOL (more work needed)
paulson
parents: 2112
diff changeset
    75
   end;
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    76
end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    77
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    78
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    79
 * Utility routine. Insert into list ordered by the key (a string). If two 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    80
 * keys are equal, the new element replaces the old. A more efficient option 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    81
 * for the future is needed. In fact, having the list of datatype facts be 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    82
 * ordered is useless, since the lookup should never fail!
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    83
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    84
fun insert (el as (x:string, _)) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    85
 let fun canfind[] = [el] 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    86
       | canfind(alist as ((y as (k,_))::rst)) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    87
           if (x<k) then el::alist
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    88
           else if (x=k) then el::rst
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    89
           else y::canfind rst 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    90
 in canfind
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    91
 end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    92
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    93
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    94
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    95
 *     A collection of facts about datatypes
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    96
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    97
val nat_record = Dtype.build_record (Nat.thy, ("nat",["0","Suc"]), nat_ind_tac)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    98
val prod_record =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    99
    let val prod_case_thms = Dtype.case_thms (sign_of Prod.thy) [split] 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   100
                                 (fn s => res_inst_tac [("p",s)] PairE_lemma)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   101
         fun const s = Const(s, the(Sign.const_type (sign_of Prod.thy) s))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   102
     in ("*", 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   103
         {constructors = [const "Pair"],
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   104
            case_const = const "split",
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   105
         case_rewrites = [split RS eq_reflection],
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   106
             case_cong = #case_cong prod_case_thms,
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   107
              nchotomy = #nchotomy prod_case_thms}) 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   108
     end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   109
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   110
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   111
 * Hacks to make interactive mode work. Referring to "datatypes" directly
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   112
 * is temporary, I hope!
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   113
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   114
val match_info = fn thy =>
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   115
    fn "*" => Utils.SOME({case_const = #case_const (#2 prod_record),
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   116
                     constructors = #constructors (#2 prod_record)})
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   117
     | "nat" => Utils.SOME({case_const = #case_const (#2 nat_record),
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   118
                       constructors = #constructors (#2 nat_record)})
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   119
     | ty => case assoc(!datatypes,ty)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   120
               of None => Utils.NONE
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   121
                | Some{case_const,constructors, ...} =>
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   122
                   Utils.SOME{case_const=case_const, constructors=constructors}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   123
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   124
val induct_info = fn thy =>
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   125
    fn "*" => Utils.SOME({nchotomy = #nchotomy (#2 prod_record),
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   126
                     constructors = #constructors (#2 prod_record)})
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   127
     | "nat" => Utils.SOME({nchotomy = #nchotomy (#2 nat_record),
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   128
                       constructors = #constructors (#2 nat_record)})
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   129
     | ty => case assoc(!datatypes,ty)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   130
               of None => Utils.NONE
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   131
                | Some{nchotomy,constructors, ...} =>
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   132
                  Utils.SOME{nchotomy=nchotomy, constructors=constructors}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   133
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   134
val extract_info = fn thy => 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   135
 let val case_congs = map (#case_cong o #2) (!datatypes)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   136
         val case_rewrites = flat(map (#case_rewrites o #2) (!datatypes))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   137
 in {case_congs = #case_cong (#2 prod_record)::
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   138
                  #case_cong (#2 nat_record)::case_congs,
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   139
     case_rewrites = #case_rewrites(#2 prod_record)@
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   140
                     #case_rewrites(#2 nat_record)@case_rewrites}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   141
 end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   142
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   143
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   144
end; (* Thry *)