src/Pure/pattern.ML
author lcp
Tue, 18 Jan 1994 15:57:40 +0100
changeset 230 ec8a2b6aa8a7
parent 63 b1349b598560
child 678 6151b7f3b606
permissions -rw-r--r--
Many other files modified as follows: s|Sign.cterm|cterm|g s|Sign.ctyp|ctyp|g s|Sign.rep_cterm|rep_cterm|g s|Sign.rep_ctyp|rep_ctyp|g s|Sign.pprint_cterm|pprint_cterm|g s|Sign.pprint_ctyp|pprint_ctyp|g s|Sign.string_of_cterm|string_of_cterm|g s|Sign.string_of_ctyp|string_of_ctyp|g s|Sign.term_of|term_of|g s|Sign.typ_of|typ_of|g s|Sign.read_cterm|read_cterm|g s|Sign.read_insts|read_insts|g s|Sign.cfun|cterm_fun|g
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     1
(*  Title: 	pattern
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     2
    ID:         $Id$
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     3
    Author: 	Tobias Nipkow and Christine Heinzelmann, TU Muenchen
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     4
    Copyright   1993  TU Muenchen
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     5
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     6
Unification of Higher-Order Patterns.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     8
See also:
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     9
Tobias Nipkow. Functional Unification of Higher-Order Patterns.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    10
In Proceedings of the 8th IEEE Symposium Logic in Computer Science, 1993.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    11
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    12
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    13
signature PATTERN =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    14
sig
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    15
  type type_sig
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    16
  type sg
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    17
  type env
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    18
  val eta_contract: term -> term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    19
  val match: type_sig -> term * term
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    20
        -> (indexname*typ)list * (indexname*term)list
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    21
  val eta_matches: type_sig -> term * term -> bool
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    22
  val unify: sg * env * (term * term)list -> env
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    23
  exception Unif
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    24
  exception MATCH
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    25
  exception Pattern
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    26
end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    27
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    28
functor PatternFun(structure Sign:SIGN and Envir:ENVIR): PATTERN =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    29
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    30
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    31
structure Type = Sign.Type;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    32
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    33
type type_sig = Type.type_sig
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    34
type sg = Sign.sg
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    35
type env = Envir.env
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    36
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    37
exception Unif;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    38
exception Pattern;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    39
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    40
fun occurs(F,t,env) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    41
    let fun occ(Var(G,_))   = (case Envir.lookup(env,G) of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    42
                                 Some(t) => occ t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    43
                               | None    => F=G)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    44
          | occ(t1$t2)      = occ t1 orelse occ t2
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    45
          | occ(Abs(_,_,t)) = occ t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    46
          | occ _           = false
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    47
    in occ t end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    48
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    49
(* Something's wrong *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    50
fun ill_formed s = error ("Ill-formed argument in "^s);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    51
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    52
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    53
fun mapbnd f =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    54
    let fun mpb d (Bound(i))     = if i < d then Bound(i) else Bound(f(i-d)+d)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    55
          | mpb d (Free(c,T))    = Free(c,T)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    56
          | mpb d (Const(c,T))   = Const(c,T)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    57
          | mpb d (Var(iname,T)) = Var(iname,T)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    58
          | mpb d (Abs(s,T,t))   = Abs(s,T,mpb(d+1) t)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    59
          | mpb d ((u1 $ u2))    = mpb d (u1)$ mpb d (u2)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    60
    in mpb 0 end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    61
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    62
fun idx [] j     = ~10000
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    63
  | idx(i::is) j = if i=j then length is else idx is j;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    64
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    65
val nth_type = snd o nth_elem;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    66
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    67
fun at xs i = nth_elem (i,xs);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    68
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    69
fun mkabs (binders,is,t)  =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    70
    let fun mk(i::is) = let val (x,T) = nth_elem(i,binders)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    71
                        in Abs(x,T,mk is) end 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    72
          | mk []     = t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    73
    in mk is end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    74
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    75
val incr = mapbnd (fn i => i+1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    76
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    77
(* termlist --> intlist *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    78
fun ints_of []             = []
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    79
  | ints_of (Bound i ::bs) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    80
      let val is = ints_of bs
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    81
      in if i mem is then raise Pattern else i::is end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    82
  | ints_of _              = raise Pattern;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    83
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    84
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    85
fun app (s,(i::is)) = app (s$Bound(i),is)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    86
  | app (s,[])      = s;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    87
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    88
fun red (Abs(_,_,s)) (i::is) js = red s is (i::js)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    89
  | red s            is      jn = app (mapbnd (at jn) s,is);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    90
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    91
(* split_type ([T1,....,Tn]---> T,n,[]) = ([Tn,...,T1],T) *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    92
fun split_type (T,0,Ts)                    = (Ts,T)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    93
  | split_type (Type ("fun",[T1,T2]),n,Ts) = split_type (T2,n-1,T1::Ts)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    94
  | split_type _                           = ill_formed("split_type");
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    95
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    96
fun type_of_G (T,n,is) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    97
  let val (Ts,U) = split_type(T,n,[]) in map(at Ts)is ---> U end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    98
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    99
fun mkhnf (binders,is,G,js) = mkabs (binders, is, app(G,js));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   100
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   101
fun mknewhnf(env,binders,is,F as (a,_),T,js) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   102
  let val (env',G) = Envir.genvar a (env,type_of_G(T,length is,js))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   103
  in Envir.update((F,mkhnf(binders,is,G,js)),env') end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   104
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   105
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   106
fun devar env t = case strip_comb t of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   107
        (Var(F,_),ys) =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   108
            (case Envir.lookup(env,F) of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   109
               Some(t) => devar env (red t (ints_of  ys) []) 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   110
             | None    => t)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   111
      |  _            => t;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   112
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   113
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   114
(* mk_proj_list(is) = [ |is| - k | 1 <= k <= |is| and is[k] >= 0 ] *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   115
fun mk_proj_list is =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   116
    let fun mk(i::is,j) = if i >= 0 then j :: mk(is,j-1) else mk(is,j-1)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   117
          | mk([],_)    = []
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   118
    in mk(is,length is - 1) end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   119
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   120
fun proj(s,env,binders,is) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   121
    let fun trans d i = if i<d then i else (idx is (i-d))+d;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   122
        fun pr(s,env,d,binders) = (case devar env s of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   123
              Abs(a,T,t) => let val (t',env') = pr(t,env,d+1,((a,T)::binders))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   124
                            in (Abs(a,T,t'),env') end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   125
            | t => (case strip_comb t of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   126
                (c as Const _,ts) =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   127
                         let val (ts',env') = prs(ts,env,d,binders)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   128
                         in (list_comb(c,ts'),env') end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   129
                 | (f as Free _,ts) =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   130
                         let val (ts',env') = prs(ts,env,d,binders)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   131
                         in (list_comb(f,ts'),env') end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   132
                 | (Bound(i),ts) =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   133
                         let val j = trans d i
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   134
                         in if j < 0 then raise Unif
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   135
                            else let val (ts',env') = prs(ts,env,d,binders)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   136
                                 in (list_comb(Bound j,ts'),env') end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   137
                         end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   138
                 | (Var(F as (a,_),Fty),ts) =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   139
                      let val js = ints_of ts;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   140
                          val js' = map (trans d) js;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   141
                          val ks = mk_proj_list js';
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   142
                          val ls = filter (fn i => i >= 0) js'
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   143
                          val Hty = type_of_G(Fty,length js,ks)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   144
                          val (env',H) = Envir.genvar a (env,Hty)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   145
                          val env'' =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   146
                                Envir.update((F,mkhnf(binders,js,H,ks)),env')
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   147
                      in (app(H,ls),env'') end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   148
                 | _  => raise Pattern))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   149
        and prs(s::ss,env,d,binders) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   150
              let val (s',env1) = pr(s,env,d,binders)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   151
                  val (ss',env2) = prs(ss,env1,d,binders)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   152
              in (s'::ss',env2) end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   153
          | prs([],env,_,_) = ([],env)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   154
   in if downto0(is,length binders - 1) then (s,env)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   155
      else pr(s,env,0,binders)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   156
   end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   157
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   158
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   159
(* mk_ff_list(is,js) = [ length(is) - k | 1 <= k <= |is| and is[k] = js[k] ] *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   160
fun mk_ff_list(is,js) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   161
    let fun mk([],[],_)        = [] 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   162
          | mk(i::is,j::js, k) = if i=j then k :: mk(is,js,k-1)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   163
                                        else mk(is,js,k-1)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   164
          | mk _               = ill_formed"mk_ff_list"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   165
    in mk(is,js,length is-1) end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   166
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   167
fun flexflex1(env,binders,F,Fty,is,js) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   168
  if is=js then env
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   169
  else let val ks = mk_ff_list(is,js)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   170
       in mknewhnf(env,binders,is,F,Fty,ks) end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   171
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   172
fun flexflex2(env,binders,F,Fty,is,G,Gty,js) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   173
  let fun ff(F,Fty,is,G as (a,_),Gty,js) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   174
            if js subset is
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   175
            then let val t= mkabs(binders,is,app(Var(G,Gty),map (idx is) js))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   176
                 in Envir.update((F,t),env) end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   177
            else let val ks = is inter js
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   178
                     val Hty = type_of_G(Fty,length is,map (idx is) ks)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   179
                     val (env',H) = Envir.genvar a (env,Hty)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   180
                     fun lam(is) = mkabs(binders,is,app(H,map (idx is) ks));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   181
                 in Envir.update((G,lam js), Envir.update((F,lam is),env'))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   182
                 end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   183
  in if xless(G,F) then ff(F,Fty,is,G,Gty,js) else ff(G,Gty,js,F,Fty,is) end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   184
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   185
val tsgr = ref(Type.tsig0);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   186
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   187
fun unify_types(T,U, env as Envir.Envir{asol,iTs,maxidx}) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   188
  if T=U then env
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   189
  else let val iTs' = Type.unify (!tsgr) ((U,T),iTs)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   190
       in Envir.Envir{asol=asol,maxidx=maxidx,iTs=iTs'} end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   191
       handle Type.TUNIFY => raise Unif;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   192
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   193
fun unif binders (env,(s,t)) = case (devar env s,devar env t) of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   194
      (Abs(ns,Ts,ts),Abs(nt,Tt,tt)) =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   195
         let val name = if ns = "" then nt else ns
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   196
         in unif ((name,Ts)::binders) (env,(ts,tt)) end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   197
    | (Abs(ns,Ts,ts),t) => unif ((ns,Ts)::binders) (env,(ts,(incr t)$Bound(0)))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   198
    | (t,Abs(nt,Tt,tt)) => unif ((nt,Tt)::binders) (env,((incr t)$Bound(0),tt))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   199
    | p => cases(binders,env,p)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   200
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   201
and cases(binders,env,(s,t)) = case (strip_comb s,strip_comb t) of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   202
       ((Var(F,Fty),ss),(Var(G,Gty),ts)) => 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   203
         if F = G then flexflex1(env,binders,F,Fty,ints_of ss,ints_of ts)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   204
                  else flexflex2(env,binders,F,Fty,ints_of ss,G,Gty,ints_of ts)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   205
      | ((Var(F,_),ss),_)             => flexrigid(env,binders,F,ints_of ss,t)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   206
      | (_,(Var(F,_),ts))             => flexrigid(env,binders,F,ints_of ts,s)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   207
      | ((Const c,ss),(Const d,ts))   => rigidrigid(env,binders,c,d,ss,ts)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   208
      | ((Free(f),ss),(Free(g),ts))   => rigidrigid(env,binders,f,g,ss,ts)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   209
      | ((Bound(i),ss),(Bound(j),ts)) => rigidrigidB (env,binders,i,j,ss,ts) 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   210
      | ((Abs(_),_),_)                => raise Pattern
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   211
      | (_,(Abs(_),_))                => raise Pattern
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   212
      | _                             => raise Unif
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   213
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   214
and rigidrigid (env,binders,(a,Ta),(b,Tb),ss,ts) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   215
      if a<>b then raise Unif
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   216
      else foldl (unif binders) (unify_types(Ta,Tb,env), ss~~ts)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   217
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   218
and rigidrigidB (env,binders,i,j,ss,ts) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   219
     if i <> j then raise Unif else foldl (unif binders) (env ,ss~~ts)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   220
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   221
and flexrigid (env,binders,F,is,t) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   222
      if occurs(F,t,env) then raise Unif
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   223
      else let val (u,env') = proj(t,env,binders,is)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   224
           in Envir.update((F,mkabs(binders,is,u)),env') end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   225
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   226
fun unify(sg,env,tus) = (tsgr := #tsig(Sign.rep_sg sg);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   227
                         foldl (unif []) (env,tus));
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   228
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   229
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   230
(*Perform eta-contractions upon a term*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   231
fun eta_contract (Abs(a,T,body)) = 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   232
      (case eta_contract body  of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   233
        body' as (f $ Bound i) => 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   234
	  if i=0 andalso not (0 mem loose_bnos f) then incr_boundvars ~1 f 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   235
	  else Abs(a,T,body')
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   236
      | body' => Abs(a,T,body'))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   237
  | eta_contract(f$t) = eta_contract f $ eta_contract t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   238
  | eta_contract t = t;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   239
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   240
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   241
(* Pattern matching. Raises MATCH if non-pattern *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   242
exception MATCH;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   243
(* something wron with types, esp in abstractions
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   244
fun typ_match args = Type.typ_match (!tsgr) args
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   245
                     handle Type.TYPE_MATCH => raise MATCH;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   246
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   247
fun match_bind(itms,binders,ixn,is,t) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   248
  let val js = loose_bnos t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   249
  in if null is
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   250
     then if null js then (ixn,t)::itms else raise MATCH
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   251
     else if js subset is
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   252
          then let val t' = if downto0(is,length binders - 1) then t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   253
                            else mapbnd (idx is) t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   254
               in (ixn, eta_contract(mkabs(binders,is,t'))) :: itms end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   255
          else raise MATCH
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   256
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   257
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   258
fun match_rr (iTs,(a,Ta),(b,Tb)) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   259
      if a<>b then raise MATCH else typ_match (iTs,(Ta,Tb))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   260
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   261
(* Pre: pat and obj have same type *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   262
fun mtch(binders,env as (iTs,itms),pat,obj) = case pat of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   263
      Var(ixn,_) => (case assoc(itms,ixn) of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   264
                       None => (iTs,match_bind(itms,binders,ixn,[],obj))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   265
                     | Some u => if obj aconv u then env else raise MATCH)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   266
    | Abs(ns,Ts,ts) =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   267
        (case obj of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   268
           Abs(nt,Tt,tt) => mtch((nt,Tt)::binders,env,ts,tt)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   269
         | _ => let val Tt = typ_subst_TVars iTs Ts
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   270
                in  mtch((ns,Tt)::binders,env,ts,(incr obj)$Bound(0)) end)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   271
    | _ => (case obj of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   272
              Abs(nt,Tt,tt) =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   273
                mtch((nt,Tt)::binders,env,(incr pat)$Bound(0),tt)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   274
            | _ => cases(binders,env,pat,obj))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   275
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   276
and cases(binders,env as (iTs,itms),pat,obj) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   277
  let fun structural() = case (pat,obj) of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   278
            (Const c,Const d) => (match_rr(iTs,c,d),itms)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   279
          | (Free f,Free g)   => (match_rr(iTs,f,g),itms)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   280
          | (Bound i,Bound j) => if i=j then env else raise MATCH
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   281
          | (f$t,g$u)         => mtch(binders,mtch(binders,env,t,u),f,g)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   282
          | _                 => raise MATCH
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   283
  in case strip_comb pat of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   284
       (Var(ixn,_),bs) =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   285
         (let val is = ints_of bs
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   286
          in case assoc(itms,ixn) of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   287
               None => (iTs,match_bind(itms,binders,ixn,is,obj))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   288
             | Some u => if obj aconv (red u is []) then env else raise MATCH
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   289
          end (* if ints_of fails: *) handle Pattern => structural())
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   290
     | _ => structural()
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   291
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   292
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   293
fun match tsg = (tsgr := tsg;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   294
                 fn (pat,obj) => 
63
b1349b598560 now calls new fastype_of in three places
lcp
parents: 0
diff changeset
   295
                   let val pT = fastype_of pat
b1349b598560 now calls new fastype_of in three places
lcp
parents: 0
diff changeset
   296
                       and oT = fastype_of obj
b1349b598560 now calls new fastype_of in three places
lcp
parents: 0
diff changeset
   297
                       val iTs = typ_match ([],(pT,oT))
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   298
                   in mtch([], (iTs,[]), pat, eta_contract obj)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   299
                      handle Pattern => raise MATCH
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   300
                   end)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   301
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   302
(*Predicate: does the pattern match the object?*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   303
fun matches tsig args = (match tsig args; true)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   304
                        handle MATCH => false;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   305
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   306
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   307
(*First-order matching;  term_match tsig (pattern, object)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   308
    returns a (tyvar,typ)list and (var,term)list.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   309
  The pattern and object may have variables in common.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   310
  Instantiation does not affect the object, so matching ?a with ?a+1 works.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   311
  A Const does not match a Free of the same name! 
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   312
  Does not notice eta-equality, thus f does not match %(x)f(x)  *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   313
fun match tsig (pat,obj) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   314
  let fun typ_match args = (Type.typ_match tsig args)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   315
			   handle Type.TYPE_MATCH => raise MATCH;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   316
      fun mtch (tyinsts,insts) = fn
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   317
	(Var(ixn,T), t)  =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   318
	  if null (loose_bnos t)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   319
	  then case assoc(insts,ixn) of
63
b1349b598560 now calls new fastype_of in three places
lcp
parents: 0
diff changeset
   320
		  None => (typ_match (tyinsts, (T, fastype_of t)), 
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   321
			   (ixn,t)::insts)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   322
		| Some u => if t aconv u then (tyinsts,insts) else raise MATCH
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   323
	  else raise MATCH
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   324
      | (Free (a,T), Free (b,U)) =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   325
	  if  a=b  then (typ_match (tyinsts,(T,U)), insts)  else raise MATCH
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   326
      | (Const (a,T), Const (b,U))  =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   327
	  if  a=b  then (typ_match (tyinsts,(T,U)), insts)  else raise MATCH
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   328
      | (Bound i, Bound j)  =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   329
          if  i=j  then  (tyinsts,insts)  else raise MATCH
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   330
      | (Abs(_,T,t), Abs(_,U,u))  =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   331
	  mtch (typ_match (tyinsts,(T,U)),insts) (t,u)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   332
      | (f$t, g$u) => mtch (mtch (tyinsts,insts) (f,g)) (t, u)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   333
      | _ => raise MATCH
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   334
  in mtch([],[]) (pat,obj) end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   335
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   336
(*Predicate: does the pattern match the object?*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   337
fun eta_matches tsig (pat,obj) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   338
      (match tsig (eta_contract pat,eta_contract obj); true)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   339
      handle MATCH => false;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   340
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   341
end;