TFL/usyntax.sml
author wenzelm
Mon, 17 Feb 1997 17:23:14 +0100
changeset 2652 024654b75919
parent 2112 3902e9af752f
child 3245 241838c01caf
permissions -rw-r--r--
file moved;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     1
structure USyntax : USyntax_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 Utils = Utils;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     5
open Utils;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     6
open Mask;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     7
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     8
infix 7 |->;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     9
infix 4 ##;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    10
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    11
fun ERR{func,mesg} = Utils.ERR{module = "USyntax", func = func, mesg = mesg};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    12
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    13
type Type = typ
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    14
type Term = cterm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    15
type Preterm = term
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
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    19
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    20
 *                            Types 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    21
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    22
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    23
fun mk_type{Tyop, Args} = Type(Tyop,Args);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    24
val mk_prim_vartype = TVar;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    25
fun mk_vartype s = mk_prim_vartype((s,0),["term"]);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    26
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    27
fun dest_type(Type(ty,args)) = {Tyop = ty, Args = args}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    28
  | dest_type _ = raise ERR{func = "dest_type", mesg = "Not a compound type"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    29
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    30
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    31
(* But internally, it's useful *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    32
fun dest_vtype (TVar x) = x
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    33
  | dest_vtype _ = raise ERR{func = "dest_vtype", 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    34
                             mesg = "not a flexible type variable"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    35
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    36
val is_vartype = Utils.can dest_vtype;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    37
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    38
val type_vars  = map mk_prim_vartype o typ_tvars
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    39
fun type_varsl L = Utils.mk_set (Utils.curry op=)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    40
                      (Utils.rev_itlist (Utils.curry op @ o type_vars) L []);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    41
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    42
val alpha  = mk_vartype "'a"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    43
val beta   = mk_vartype "'b"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    44
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    45
val bool = Type("bool",[]);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    46
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    47
fun match_type ty1 ty2 = raise ERR{func="match_type",mesg="not implemented"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    48
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    49
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    50
(* What nonsense *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    51
nonfix -->; 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    52
val --> = -->;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    53
infixr 3 -->;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    54
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    55
(* hol_type -> hol_type list * hol_type *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    56
fun strip_type ty =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    57
   let val {Tyop = "fun", Args = [ty1,ty2]} = dest_type ty
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    58
       val (D,r) = strip_type ty2
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    59
   in (ty1::D, r)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    60
   end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    61
   handle _ => ([],ty);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    62
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    63
(* hol_type -> hol_type list *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    64
fun strip_prod_type ty =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    65
   let val {Tyop = "*", Args = [ty1,ty2]} = dest_type ty
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    66
   in strip_prod_type ty1 @ strip_prod_type ty2
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    67
   end handle _ => [ty];
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    68
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    69
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    70
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    71
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    72
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    73
 *                              Terms 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    74
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    75
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    76
nonfix aconv;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    77
val aconv = Utils.curry (op aconv);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    78
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    79
fun free_vars tm = add_term_frees(tm,[]);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    80
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    81
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    82
(* Free variables, in order of occurrence, from left to right in the 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    83
 * syntax tree. *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    84
fun free_vars_lr tm = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    85
  let fun memb x = let fun m[] = false | m(y::rst) = (x=y)orelse m rst in m end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    86
      fun add (t, frees) = case t of
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    87
            Free   _ => if (memb t frees) then frees else t::frees
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    88
          | Abs (_,_,body) => add(body,frees)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    89
          | f$t =>  add(t, add(f, frees))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    90
          | _ => frees
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    91
  in rev(add(tm,[]))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    92
  end;
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
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    96
fun free_varsl L = Utils.mk_set aconv
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    97
                      (Utils.rev_itlist (Utils.curry op @ o free_vars) L []);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    98
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    99
val type_of =  type_of;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   100
val type_vars_in_term = map mk_prim_vartype o term_tvars;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   101
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   102
(* Can't really be very exact in Isabelle *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   103
fun all_vars tm = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   104
  let fun memb x = let fun m[] = false | m(y::rst) = (x=y)orelse m rst in m end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   105
      fun add (t, A) = case t of
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   106
            Free   _ => if (memb t A) then A else t::A
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   107
          | Abs (s,ty,body) => add(body, add(Free(s,ty),A))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   108
          | f$t =>  add(t, add(f, A))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   109
          | _ => A
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   110
  in rev(add(tm,[]))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   111
  end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   112
fun all_varsl L = Utils.mk_set aconv
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   113
                      (Utils.rev_itlist (Utils.curry op @ o all_vars) L []);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   114
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   115
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   116
(* Prelogic *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   117
val subst = subst_free o map (fn (a |-> b) => (a,b));
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   118
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   119
fun dest_tybinding (v |-> ty) = (#1(dest_vtype v),ty)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   120
fun inst theta = subst_vars (map dest_tybinding theta,[])
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   121
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   122
fun beta_conv((t1 as Abs _ ) $ t2) = betapply(t1,t2)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   123
  | beta_conv _ = raise ERR{func = "beta_conv", mesg = "Not a beta-redex"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   124
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   125
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   126
(* Construction routines *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   127
(* fun mk_var{Name,Ty} = Var((Name,0),Ty); *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   128
fun mk_var{Name,Ty} = Free(Name,Ty);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   129
val mk_prim_var = Var;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   130
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   131
val string_variant = variant;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   132
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   133
local fun var_name(Var((Name,_),_)) = Name
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   134
        | var_name(Free(s,_)) = s
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   135
        | var_name _ = raise ERR{func = "variant",
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   136
                                 mesg = "list elem. is not a variable"}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   137
in
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   138
fun variant [] v = v
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   139
  | variant vlist (Var((Name,i),ty)) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   140
       Var((string_variant (map var_name vlist) Name,i),ty)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   141
  | variant vlist (Free(Name,ty)) =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   142
       Free(string_variant (map var_name vlist) Name,ty)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   143
  | variant _ _ = raise ERR{func = "variant",
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   144
                            mesg = "2nd arg. should be a variable"}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   145
end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   146
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   147
fun mk_const{Name,Ty} = Const(Name,Ty)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   148
fun mk_comb{Rator,Rand} = Rator $ Rand;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   149
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   150
fun mk_abs{Bvar as Var((s,_),ty),Body}  = Abs(s,ty,abstract_over(Bvar,Body))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   151
  | mk_abs{Bvar as Free(s,ty),Body}  = Abs(s,ty,abstract_over(Bvar,Body))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   152
  | mk_abs _ = raise ERR{func = "mk_abs", mesg = "Bvar is not a variable"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   153
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   154
fun list_mk_comb (h,[]) = h
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   155
  | list_mk_comb (h,L) =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   156
      rev_itlist (fn t1 => fn t2 => mk_comb{Rator = t2, Rand = t1}) L h;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   157
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   158
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   159
fun mk_eq{lhs,rhs} = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   160
   let val ty = type_of lhs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   161
       val c = mk_const{Name = "op =", Ty = ty --> ty --> bool}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   162
   in list_mk_comb(c,[lhs,rhs])
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   163
   end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   164
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   165
fun mk_imp{ant,conseq} = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   166
   let val c = mk_const{Name = "op -->", Ty = bool --> bool --> bool}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   167
   in list_mk_comb(c,[ant,conseq])
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   168
   end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   169
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   170
fun mk_select (r as {Bvar,Body}) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   171
  let val ty = type_of Bvar
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   172
      val c = mk_const{Name = "Eps", Ty = (ty --> bool) --> ty}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   173
  in list_mk_comb(c,[mk_abs r])
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   174
  end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   175
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   176
fun mk_forall (r as {Bvar,Body}) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   177
  let val ty = type_of Bvar
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   178
      val c = mk_const{Name = "All", Ty = (ty --> bool) --> bool}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   179
  in list_mk_comb(c,[mk_abs r])
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   180
  end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   181
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   182
fun mk_exists (r as {Bvar,Body}) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   183
  let val ty = type_of Bvar 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   184
      val c = mk_const{Name = "Ex", Ty = (ty --> bool) --> bool}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   185
  in list_mk_comb(c,[mk_abs r])
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   186
  end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   187
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   188
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   189
fun mk_conj{conj1,conj2} =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   190
   let val c = mk_const{Name = "op &", Ty = bool --> bool --> bool}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   191
   in list_mk_comb(c,[conj1,conj2])
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   192
   end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   193
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   194
fun mk_disj{disj1,disj2} =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   195
   let val c = mk_const{Name = "op |", Ty = bool --> bool --> bool}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   196
   in list_mk_comb(c,[disj1,disj2])
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   197
   end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   198
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   199
fun prod_ty ty1 ty2 = mk_type{Tyop = "*", Args = [ty1,ty2]};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   200
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   201
local
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   202
fun mk_uncurry(xt,yt,zt) =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   203
    mk_const{Name = "split", Ty = (xt --> yt --> zt) --> prod_ty xt yt --> zt}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   204
fun dest_pair(Const("Pair",_) $ M $ N) = {fst=M, snd=N}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   205
  | dest_pair _ = raise ERR{func = "dest_pair", mesg = "not a pair"}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   206
fun is_var(Var(_)) = true | is_var (Free _) = true | is_var _ = false
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   207
in
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   208
fun mk_pabs{varstruct,body} = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   209
 let fun mpa(varstruct,body) =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   210
       if (is_var varstruct)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   211
       then mk_abs{Bvar = varstruct, Body = body}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   212
       else let val {fst,snd} = dest_pair varstruct
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   213
            in mk_comb{Rator= mk_uncurry(type_of fst,type_of snd,type_of body),
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   214
                       Rand = mpa(fst,mpa(snd,body))}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   215
            end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   216
 in mpa(varstruct,body)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   217
 end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   218
 handle _ => raise ERR{func = "mk_pabs", mesg = ""};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   219
end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   220
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   221
(* Destruction routines *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   222
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   223
datatype lambda = VAR   of {Name : string, Ty : Type}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   224
                | CONST of {Name : string, Ty : Type}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   225
                | COMB  of {Rator: Preterm, Rand : Preterm}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   226
                | LAMB  of {Bvar : Preterm, Body : Preterm};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   227
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   228
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   229
fun dest_term(Var((s,i),ty)) = VAR{Name = s, Ty = ty}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   230
  | dest_term(Free(s,ty))    = VAR{Name = s, Ty = ty}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   231
  | dest_term(Const(s,ty))   = CONST{Name = s, Ty = ty}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   232
  | dest_term(M$N)           = COMB{Rator=M,Rand=N}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   233
  | dest_term(Abs(s,ty,M))   = let  val v = mk_var{Name = s, Ty = ty}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   234
                               in LAMB{Bvar = v, Body = betapply (M,v)}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   235
                               end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   236
  | dest_term(Bound _)       = raise ERR{func = "dest_term",mesg = "Bound"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   237
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   238
fun dest_var(Var((s,i),ty)) = {Name = s, Ty = ty}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   239
  | dest_var(Free(s,ty))    = {Name = s, Ty = ty}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   240
  | dest_var _ = raise ERR{func = "dest_var", mesg = "not a variable"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   241
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   242
fun dest_const(Const(s,ty)) = {Name = s, Ty = ty}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   243
  | dest_const _ = raise ERR{func = "dest_const", mesg = "not a constant"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   244
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   245
fun dest_comb(t1 $ t2) = {Rator = t1, Rand = t2}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   246
  | dest_comb _ =  raise ERR{func = "dest_comb", mesg = "not a comb"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   247
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   248
fun dest_abs(a as Abs(s,ty,M)) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   249
     let val v = mk_var{Name = s, Ty = ty}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   250
     in {Bvar = v, Body = betapply (a,v)}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   251
     end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   252
  | dest_abs _ =  raise ERR{func = "dest_abs", mesg = "not an abstraction"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   253
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   254
fun dest_eq(Const("op =",_) $ M $ N) = {lhs=M, rhs=N}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   255
  | dest_eq _ = raise ERR{func = "dest_eq", mesg = "not an equality"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   256
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   257
fun dest_imp(Const("op -->",_) $ M $ N) = {ant=M, conseq=N}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   258
  | dest_imp _ = raise ERR{func = "dest_imp", mesg = "not an implication"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   259
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   260
fun dest_select(Const("Eps",_) $ (a as Abs _)) = dest_abs a
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   261
  | dest_select _ = raise ERR{func = "dest_select", mesg = "not a select"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   262
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   263
fun dest_forall(Const("All",_) $ (a as Abs _)) = dest_abs a
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   264
  | dest_forall _ = raise ERR{func = "dest_forall", mesg = "not a forall"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   265
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   266
fun dest_exists(Const("Ex",_) $ (a as Abs _)) = dest_abs a
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   267
  | dest_exists _ = raise ERR{func = "dest_exists", mesg="not an existential"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   268
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   269
fun dest_neg(Const("not",_) $ M) = M
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   270
  | dest_neg _ = raise ERR{func = "dest_neg", mesg = "not a negation"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   271
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   272
fun dest_conj(Const("op &",_) $ M $ N) = {conj1=M, conj2=N}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   273
  | dest_conj _ = raise ERR{func = "dest_conj", mesg = "not a conjunction"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   274
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   275
fun dest_disj(Const("op |",_) $ M $ N) = {disj1=M, disj2=N}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   276
  | dest_disj _ = raise ERR{func = "dest_disj", mesg = "not a disjunction"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   277
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   278
fun mk_pair{fst,snd} = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   279
   let val ty1 = type_of fst
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   280
       val ty2 = type_of snd
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   281
       val c = mk_const{Name = "Pair", Ty = ty1 --> ty2 --> prod_ty ty1 ty2}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   282
   in list_mk_comb(c,[fst,snd])
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   283
   end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   284
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   285
fun dest_pair(Const("Pair",_) $ M $ N) = {fst=M, snd=N}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   286
  | dest_pair _ = raise ERR{func = "dest_pair", mesg = "not a pair"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   287
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   288
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   289
local  val ucheck = Utils.assert (curry (op =) "split" o #Name o dest_const)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   290
in
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   291
fun dest_pabs tm =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   292
   let val {Bvar,Body} = dest_abs tm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   293
   in {varstruct = Bvar, body = Body}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   294
   end handle _ 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   295
   => let val {Rator,Rand} = dest_comb tm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   296
          val _ = ucheck Rator
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   297
          val {varstruct = lv,body} = dest_pabs Rand
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   298
          val {varstruct = rv,body} = dest_pabs body
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   299
      in {varstruct = mk_pair{fst = lv, snd = rv}, body = body}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   300
      end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   301
end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   302
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   303
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   304
(* Garbage - ought to be dropped *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   305
val lhs   = #lhs o dest_eq
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   306
val rhs   = #rhs o dest_eq
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   307
val rator = #Rator o dest_comb
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   308
val rand  = #Rand o dest_comb
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   309
val bvar  = #Bvar o dest_abs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   310
val body  = #Body o dest_abs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   311
  
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   312
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   313
(* Query routines *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   314
val is_var    = can dest_var
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   315
val is_const  = can dest_const
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   316
val is_comb   = can dest_comb
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   317
val is_abs    = can dest_abs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   318
val is_eq     = can dest_eq
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   319
val is_imp    = can dest_imp
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   320
val is_forall = can dest_forall
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   321
val is_exists = can dest_exists
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   322
val is_neg    = can dest_neg
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   323
val is_conj   = can dest_conj
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   324
val is_disj   = can dest_disj
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   325
val is_pair   = can dest_pair
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   326
val is_pabs   = can dest_pabs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   327
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   328
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   329
(* Construction of a Term from a list of Terms *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   330
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   331
fun list_mk_abs(L,tm) = itlist (fn v => fn M => mk_abs{Bvar=v, Body=M}) L tm;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   332
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   333
(* These others are almost never used *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   334
fun list_mk_imp(A,c) = itlist(fn a => fn tm => mk_imp{ant=a,conseq=tm}) A c;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   335
fun list_mk_exists(V,t) = itlist(fn v => fn b => mk_exists{Bvar=v, Body=b})V t;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   336
fun list_mk_forall(V,t) = itlist(fn v => fn b => mk_forall{Bvar=v, Body=b})V t;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   337
val list_mk_conj = end_itlist(fn c1 => fn tm => mk_conj{conj1=c1, conj2=tm})
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   338
val list_mk_disj = end_itlist(fn d1 => fn tm => mk_disj{disj1=d1, disj2=tm})
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   339
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   340
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   341
(* Need to reverse? *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   342
fun gen_all tm = list_mk_forall(free_vars tm, tm);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   343
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   344
(* Destructing a Term to a list of Terms *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   345
fun strip_comb tm = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   346
   let fun dest(M$N, A) = dest(M, N::A)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   347
         | dest x = x
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   348
   in dest(tm,[])
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   349
   end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   350
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   351
fun strip_abs(tm as Abs _) =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   352
       let val {Bvar,Body} = dest_abs tm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   353
           val (bvs, core) = strip_abs Body
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   354
       in (Bvar::bvs, core)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   355
       end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   356
  | strip_abs M = ([],M);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   357
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   358
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   359
fun strip_imp fm =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   360
   if (is_imp fm)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   361
   then let val {ant,conseq} = dest_imp fm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   362
	    val (was,wb) = strip_imp conseq
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   363
        in ((ant::was), wb)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   364
        end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   365
   else ([],fm);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   366
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   367
fun strip_forall fm =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   368
   if (is_forall fm)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   369
   then let val {Bvar,Body} = dest_forall fm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   370
            val (bvs,core) = strip_forall Body
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   371
        in ((Bvar::bvs), core)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   372
        end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   373
   else ([],fm);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   374
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   375
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   376
fun strip_exists fm =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   377
   if (is_exists fm)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   378
   then let val {Bvar, Body} = dest_exists fm 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   379
            val (bvs,core) = strip_exists Body
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   380
        in (Bvar::bvs, core)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   381
        end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   382
   else ([],fm);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   383
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   384
fun strip_conj w = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   385
   if (is_conj w)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   386
   then let val {conj1,conj2} = dest_conj w
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   387
        in (strip_conj conj1@strip_conj conj2)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   388
        end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   389
   else [w];
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   390
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   391
fun strip_disj w =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   392
   if (is_disj w)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   393
   then let val {disj1,disj2} = dest_disj w 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   394
        in (strip_disj disj1@strip_disj disj2)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   395
        end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   396
   else [w];
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   397
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   398
fun strip_pair tm = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   399
   if (is_pair tm) 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   400
   then let val {fst,snd} = dest_pair tm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   401
            fun dtuple t =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   402
               if (is_pair t)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   403
               then let val{fst,snd} = dest_pair t
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   404
                    in (fst :: dtuple snd)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   405
                    end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   406
               else [t]
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   407
        in fst::dtuple snd
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   408
        end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   409
   else [tm];
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   410
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   411
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   412
fun mk_preterm tm = #t(rep_cterm tm);
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   413
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   414
fun mk_prop (tm as Const("Trueprop",_) $ _) = tm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   415
  | mk_prop tm = mk_comb{Rator=mk_const{Name = "Trueprop", 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   416
                           Ty = bool --> mk_type{Tyop = "prop",Args=[]}},
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   417
                         Rand = tm};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   418
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   419
fun drop_Trueprop (Const("Trueprop",_) $ X) = X
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   420
  | drop_Trueprop X = X;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   421
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   422
(* Miscellaneous *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   423
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   424
fun mk_vstruct ty V =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   425
  let fun follow_prod_type ty vs =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   426
      let val {Tyop = "*", Args = [ty1,ty2]} = dest_type ty
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   427
          val (ltm,vs1) = follow_prod_type ty1 vs
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   428
          val (rtm,vs2) = follow_prod_type ty2 vs1
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   429
      in (mk_pair{fst=ltm, snd=rtm}, vs2)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   430
      end handle _ => (hd vs, tl vs)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   431
 in fst(follow_prod_type ty V)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   432
 end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   433
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   434
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   435
(* Search a term for a sub-term satisfying the predicate p. *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   436
fun find_term p =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   437
   let fun find tm =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   438
      if (p tm)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   439
      then tm 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   440
      else if (is_abs tm)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   441
           then find (#Body(dest_abs tm))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   442
           else let val {Rator,Rand} = dest_comb tm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   443
                in find Rator handle _ => find Rand
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   444
                end handle _ => raise ERR{func = "find_term",mesg = ""}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   445
   in find
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   446
   end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   447
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   448
(*******************************************************************
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   449
 * find_terms: (term -> bool) -> term -> term list
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   450
 * 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   451
 *  Find all subterms in a term that satisfy a given predicate p.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   452
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   453
 *******************************************************************)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   454
fun find_terms p =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   455
   let fun accum tl tm =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   456
      let val tl' = if (p tm) then (tm::tl) else tl 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   457
      in if (is_abs tm)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   458
         then accum tl' (#Body(dest_abs tm))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   459
         else let val {Rator,Rand} = dest_comb tm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   460
              in accum (accum tl' Rator) Rand
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   461
              end handle _ => tl'
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   462
      end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   463
   in accum []
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   464
   end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   465
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   466
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   467
val Term_to_string = string_of_cterm;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   468
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   469
fun dest_relation tm =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   470
   if (type_of tm = bool)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   471
   then let val (Const("op :",_) $ (Const("Pair",_)$y$x) $ R) = tm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   472
        in (R,y,x)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   473
        end handle _ => raise ERR{func="dest_relation",
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   474
                                  mesg="unexpected term structure"}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   475
   else raise ERR{func="dest_relation",mesg="not a boolean term"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   476
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   477
fun is_WFR tm = (#Name(dest_const(rator tm)) = "wf") handle _ => false;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   478
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   479
fun ARB ty = mk_select{Bvar=mk_var{Name="v",Ty=ty},
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   480
                       Body=mk_const{Name="True",Ty=bool}};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   481
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   482
end; (* Syntax *)