src/Pure/pattern.ML
author wenzelm
Tue Sep 26 20:54:40 2017 +0200 (22 months ago)
changeset 66695 91500c024c7f
parent 64556 851ae0e7b09c
child 69575 f77cc54f6d47
permissions -rw-r--r--
tuned;
wenzelm@12784
     1
(*  Title:      Pure/pattern.ML
wenzelm@29269
     2
    Author:     Tobias Nipkow, Christine Heinzelmann, and Stefan Berghofer, TU Muenchen
clasohm@0
     3
clasohm@0
     4
Unification of Higher-Order Patterns.
clasohm@0
     5
clasohm@0
     6
See also:
clasohm@0
     7
Tobias Nipkow. Functional Unification of Higher-Order Patterns.
clasohm@0
     8
In Proceedings of the 8th IEEE Symposium Logic in Computer Science, 1993.
nipkow@678
     9
nipkow@678
    10
TODO: optimize red by special-casing it
clasohm@0
    11
*)
clasohm@0
    12
clasohm@0
    13
signature PATTERN =
wenzelm@14787
    14
sig
wenzelm@58950
    15
  exception Unif
wenzelm@58950
    16
  exception Pattern
wenzelm@52709
    17
  val unify_trace_failure_raw: Config.raw
wenzelm@52709
    18
  val unify_trace_failure: bool Config.T
wenzelm@58950
    19
  val unify_types: Context.generic -> typ * typ -> Envir.env -> Envir.env
wenzelm@58950
    20
  val unify: Context.generic -> term * term -> Envir.env -> Envir.env
wenzelm@58950
    21
  exception MATCH
wenzelm@18182
    22
  val match: theory -> term * term -> Type.tyenv * Envir.tenv -> Type.tyenv * Envir.tenv
wenzelm@18182
    23
  val first_order_match: theory -> term * term
wenzelm@18182
    24
    -> Type.tyenv * Envir.tenv -> Type.tyenv * Envir.tenv
wenzelm@14787
    25
end;
clasohm@0
    26
wenzelm@17203
    27
structure Pattern: PATTERN =
clasohm@0
    28
struct
clasohm@0
    29
clasohm@0
    30
exception Unif;
clasohm@0
    31
exception Pattern;
clasohm@0
    32
wenzelm@52709
    33
val unify_trace_failure_raw =
wenzelm@64556
    34
  Config.declare ("unify_trace_failure", \<^here>) (fn _ => Config.Bool false);
wenzelm@52709
    35
val unify_trace_failure = Config.bool unify_trace_failure_raw;
nipkow@13642
    36
wenzelm@58950
    37
fun string_of_term ctxt env binders t =
wenzelm@58950
    38
  Syntax.string_of_term ctxt (Envir.norm_term env (subst_bounds (map Free binders, t)));
nipkow@13642
    39
haftmann@18011
    40
fun bname binders i = fst (nth binders i);
nipkow@13642
    41
fun bnames binders is = space_implode " " (map (bname binders) is);
nipkow@13642
    42
wenzelm@58950
    43
fun typ_clash context (tye,T,U) =
wenzelm@58950
    44
  if Config.get_generic context unify_trace_failure then
wenzelm@58950
    45
    let
wenzelm@58950
    46
      val ctxt = Context.proof_of context;
wenzelm@58950
    47
      val t = Syntax.string_of_typ ctxt (Envir.norm_type tye T);
wenzelm@58950
    48
      val u = Syntax.string_of_typ ctxt (Envir.norm_type tye U);
wenzelm@58950
    49
    in tracing ("The following types do not unify:\n" ^ t ^ "\n" ^ u) end
wenzelm@58950
    50
  else ();
nipkow@13642
    51
wenzelm@58950
    52
fun clash context a b =
wenzelm@58950
    53
  if Config.get_generic context unify_trace_failure
wenzelm@58950
    54
  then tracing ("Clash: " ^ a ^ " =/= " ^ b) else ();
nipkow@13642
    55
nipkow@13642
    56
fun boundVar binders i =
nipkow@13642
    57
  "bound variable " ^ bname binders i ^ " (depth " ^ string_of_int i ^ ")";
nipkow@13642
    58
wenzelm@58950
    59
fun clashBB context binders i j =
wenzelm@58950
    60
  if Config.get_generic context unify_trace_failure
wenzelm@58950
    61
  then clash context (boundVar binders i) (boundVar binders j) else ();
nipkow@13642
    62
wenzelm@58950
    63
fun clashB context binders i s =
wenzelm@58950
    64
  if Config.get_generic context unify_trace_failure
wenzelm@58950
    65
  then clash context (boundVar binders i) s else ();
nipkow@13642
    66
wenzelm@58950
    67
fun proj_fail context (env,binders,F,_,is,t) =
wenzelm@58950
    68
  if Config.get_generic context unify_trace_failure then
wenzelm@58950
    69
    let
wenzelm@58950
    70
      val ctxt = Context.proof_of context
wenzelm@58950
    71
      val f = Term.string_of_vname F
wenzelm@58950
    72
      val xs = bnames binders is
wenzelm@58950
    73
      val u = string_of_term ctxt env binders t
wenzelm@58950
    74
      val ys = bnames binders (subtract (op =) is (loose_bnos t))
wenzelm@58950
    75
    in
wenzelm@58950
    76
      tracing ("Cannot unify variable " ^ f ^
wenzelm@58950
    77
        " (depending on bound variables " ^ xs ^ ")\nwith term " ^ u ^
wenzelm@58950
    78
        "\nTerm contains additional bound variable(s) " ^ ys)
wenzelm@58950
    79
    end
wenzelm@58950
    80
  else ();
nipkow@13642
    81
wenzelm@58950
    82
fun ocheck_fail context (F,t,binders,env) =
wenzelm@58950
    83
  if Config.get_generic context unify_trace_failure then
wenzelm@58950
    84
    let
wenzelm@58950
    85
      val ctxt = Context.proof_of context
wenzelm@58950
    86
      val f = Term.string_of_vname F
wenzelm@58950
    87
      val u = string_of_term ctxt env binders t
wenzelm@58950
    88
    in tracing ("Variable " ^ f ^ " occurs in term\n" ^ u ^ "\nCannot unify!\n") end
wenzelm@58950
    89
  else ();
nipkow@13642
    90
wenzelm@12784
    91
fun occurs(F,t,env) =
wenzelm@51700
    92
    let fun occ(Var (G, T))   = (case Envir.lookup env (G, T) of
skalberg@15531
    93
                                 SOME(t) => occ t
skalberg@15531
    94
                               | NONE    => F=G)
clasohm@0
    95
          | occ(t1$t2)      = occ t1 orelse occ t2
clasohm@0
    96
          | occ(Abs(_,_,t)) = occ t
clasohm@0
    97
          | occ _           = false
clasohm@0
    98
    in occ t end;
clasohm@0
    99
clasohm@0
   100
clasohm@0
   101
fun mapbnd f =
clasohm@0
   102
    let fun mpb d (Bound(i))     = if i < d then Bound(i) else Bound(f(i-d)+d)
clasohm@0
   103
          | mpb d (Abs(s,T,t))   = Abs(s,T,mpb(d+1) t)
nipkow@678
   104
          | mpb d ((u1 $ u2))    = (mpb d u1)$(mpb d u2)
nipkow@678
   105
          | mpb _ atom           = atom
clasohm@0
   106
    in mpb 0 end;
clasohm@0
   107
berghofe@14861
   108
fun idx [] j     = raise Unif
wenzelm@16668
   109
  | idx(i::is) j = if (i:int) =j then length is else idx is j;
clasohm@0
   110
clasohm@0
   111
fun mkabs (binders,is,t)  =
haftmann@18011
   112
    let fun mk(i::is) = let val (x,T) = nth binders i
wenzelm@12784
   113
                        in Abs(x,T,mk is) end
clasohm@0
   114
          | mk []     = t
clasohm@0
   115
    in mk is end;
clasohm@0
   116
clasohm@0
   117
val incr = mapbnd (fn i => i+1);
clasohm@0
   118
clasohm@0
   119
fun ints_of []             = []
wenzelm@12784
   120
  | ints_of (Bound i ::bs) =
clasohm@0
   121
      let val is = ints_of bs
wenzelm@20672
   122
      in if member (op =) is i then raise Pattern else i::is end
clasohm@0
   123
  | ints_of _              = raise Pattern;
clasohm@0
   124
berghofe@12232
   125
fun ints_of' env ts = ints_of (map (Envir.head_norm env) ts);
berghofe@12232
   126
clasohm@0
   127
clasohm@0
   128
fun app (s,(i::is)) = app (s$Bound(i),is)
clasohm@0
   129
  | app (s,[])      = s;
clasohm@0
   130
clasohm@0
   131
fun red (Abs(_,_,s)) (i::is) js = red s is (i::js)
nipkow@678
   132
  | red t            []      [] = t
haftmann@18011
   133
  | red t            is      jn = app (mapbnd (nth jn) t,is);
nipkow@678
   134
clasohm@0
   135
clasohm@0
   136
(* split_type ([T1,....,Tn]---> T,n,[]) = ([Tn,...,T1],T) *)
clasohm@0
   137
fun split_type (T,0,Ts)                    = (Ts,T)
clasohm@0
   138
  | split_type (Type ("fun",[T1,T2]),n,Ts) = split_type (T2,n-1,T1::Ts)
wenzelm@52133
   139
  | split_type _                           = raise Fail "split_type";
clasohm@0
   140
wenzelm@32032
   141
fun type_of_G env (T, n, is) =
wenzelm@32032
   142
  let
wenzelm@32032
   143
    val tyenv = Envir.type_env env;
wenzelm@32032
   144
    val (Ts, U) = split_type (Envir.norm_type tyenv T, n, []);
haftmann@18011
   145
  in map (nth Ts) is ---> U end;
clasohm@0
   146
wenzelm@52130
   147
fun mk_hnf (binders,is,G,js) = mkabs (binders, is, app(G,js));
clasohm@0
   148
wenzelm@52130
   149
fun mk_new_hnf(env,binders,is,F as (a,_),T,js) =
berghofe@13891
   150
  let val (env',G) = Envir.genvar a (env,type_of_G env (T,length is,js))
wenzelm@52130
   151
  in Envir.update ((F, T), mk_hnf (binders, is, G, js)) env' end;
clasohm@0
   152
clasohm@0
   153
wenzelm@23222
   154
(*predicate: downto0 (is, n) <=> is = [n, n - 1, ..., 0]*)
wenzelm@23222
   155
fun downto0 (i :: is, n) = i = n andalso downto0 (is, n - 1)
wenzelm@23222
   156
  | downto0 ([], n) = n = ~1;
wenzelm@23222
   157
wenzelm@23222
   158
(*mk_proj_list(is) = [ |is| - k | 1 <= k <= |is| and is[k] >= 0 ]*)
clasohm@0
   159
fun mk_proj_list is =
wenzelm@19502
   160
    let fun mk(i::is,j) = if is_some i then j :: mk(is,j-1) else mk(is,j-1)
clasohm@0
   161
          | mk([],_)    = []
clasohm@0
   162
    in mk(is,length is - 1) end;
clasohm@0
   163
clasohm@0
   164
fun proj(s,env,binders,is) =
clasohm@0
   165
    let fun trans d i = if i<d then i else (idx is (i-d))+d;
berghofe@12232
   166
        fun pr(s,env,d,binders) = (case Envir.head_norm env s of
clasohm@0
   167
              Abs(a,T,t) => let val (t',env') = pr(t,env,d+1,((a,T)::binders))
clasohm@0
   168
                            in (Abs(a,T,t'),env') end
clasohm@0
   169
            | t => (case strip_comb t of
clasohm@0
   170
                (c as Const _,ts) =>
clasohm@0
   171
                         let val (ts',env') = prs(ts,env,d,binders)
clasohm@0
   172
                         in (list_comb(c,ts'),env') end
clasohm@0
   173
                 | (f as Free _,ts) =>
clasohm@0
   174
                         let val (ts',env') = prs(ts,env,d,binders)
clasohm@0
   175
                         in (list_comb(f,ts'),env') end
clasohm@0
   176
                 | (Bound(i),ts) =>
clasohm@0
   177
                         let val j = trans d i
berghofe@14861
   178
                             val (ts',env') = prs(ts,env,d,binders)
berghofe@14861
   179
                         in (list_comb(Bound j,ts'),env') end
clasohm@0
   180
                 | (Var(F as (a,_),Fty),ts) =>
berghofe@12232
   181
                      let val js = ints_of' env ts;
berghofe@14861
   182
                          val js' = map (try (trans d)) js;
clasohm@0
   183
                          val ks = mk_proj_list js';
wenzelm@19482
   184
                          val ls = map_filter I js'
berghofe@13891
   185
                          val Hty = type_of_G env (Fty,length js,ks)
clasohm@0
   186
                          val (env',H) = Envir.genvar a (env,Hty)
clasohm@0
   187
                          val env'' =
wenzelm@52130
   188
                            Envir.update ((F, Fty), mk_hnf (binders, js, H, ks)) env'
clasohm@0
   189
                      in (app(H,ls),env'') end
clasohm@0
   190
                 | _  => raise Pattern))
clasohm@0
   191
        and prs(s::ss,env,d,binders) =
clasohm@0
   192
              let val (s',env1) = pr(s,env,d,binders)
clasohm@0
   193
                  val (ss',env2) = prs(ss,env1,d,binders)
clasohm@0
   194
              in (s'::ss',env2) end
clasohm@0
   195
          | prs([],env,_,_) = ([],env)
clasohm@0
   196
   in if downto0(is,length binders - 1) then (s,env)
clasohm@0
   197
      else pr(s,env,0,binders)
clasohm@0
   198
   end;
clasohm@0
   199
clasohm@0
   200
clasohm@0
   201
(* mk_ff_list(is,js) = [ length(is) - k | 1 <= k <= |is| and is[k] = js[k] ] *)
wenzelm@12784
   202
fun mk_ff_list(is,js) =
wenzelm@12784
   203
    let fun mk([],[],_)        = []
wenzelm@16668
   204
          | mk(i::is,j::js, k) = if (i:int) = j then k :: mk(is,js,k-1)
clasohm@0
   205
                                        else mk(is,js,k-1)
wenzelm@52133
   206
          | mk _               = raise Fail "mk_ff_list"
clasohm@0
   207
    in mk(is,js,length is-1) end;
clasohm@0
   208
clasohm@0
   209
fun flexflex1(env,binders,F,Fty,is,js) =
clasohm@0
   210
  if is=js then env
clasohm@0
   211
  else let val ks = mk_ff_list(is,js)
wenzelm@52130
   212
       in mk_new_hnf(env,binders,is,F,Fty,ks) end;
clasohm@0
   213
clasohm@0
   214
fun flexflex2(env,binders,F,Fty,is,G,Gty,js) =
clasohm@0
   215
  let fun ff(F,Fty,is,G as (a,_),Gty,js) =
haftmann@33038
   216
            if subset (op =) (js, is)
clasohm@0
   217
            then let val t= mkabs(binders,is,app(Var(G,Gty),map (idx is) js))
wenzelm@51700
   218
                 in Envir.update ((F, Fty), t) env end
haftmann@33049
   219
            else let val ks = inter (op =) js is
berghofe@13891
   220
                     val Hty = type_of_G env (Fty,length is,map (idx is) ks)
clasohm@0
   221
                     val (env',H) = Envir.genvar a (env,Hty)
clasohm@0
   222
                     fun lam(is) = mkabs(binders,is,app(H,map (idx is) ks));
wenzelm@51700
   223
                 in Envir.update ((G, Gty), lam js) (Envir.update ((F, Fty), lam is) env')
clasohm@0
   224
                 end;
wenzelm@35408
   225
  in if Term_Ord.indexname_ord (G,F) = LESS then ff(F,Fty,is,G,Gty,js) else ff(G,Gty,js,F,Fty,is) end
clasohm@0
   226
wenzelm@58950
   227
fun unify_types context (T, U) (env as Envir.Envir {maxidx, tenv, tyenv}) =
wenzelm@32032
   228
  if T = U then env
wenzelm@32032
   229
  else
wenzelm@58950
   230
    let
wenzelm@58950
   231
      val thy = Context.theory_of context
wenzelm@58950
   232
      val (tyenv', maxidx') = Sign.typ_unify thy (U, T) (tyenv, maxidx)
wenzelm@32032
   233
    in Envir.Envir {maxidx = maxidx', tenv = tenv, tyenv = tyenv'} end
wenzelm@58950
   234
    handle Type.TUNIFY => (typ_clash context (tyenv, T, U); raise Unif);
clasohm@0
   235
wenzelm@58950
   236
fun unif context binders (s,t) env = case (Envir.head_norm env s, Envir.head_norm env t) of
clasohm@0
   237
      (Abs(ns,Ts,ts),Abs(nt,Tt,tt)) =>
clasohm@0
   238
         let val name = if ns = "" then nt else ns
wenzelm@58950
   239
         in unif context ((name,Ts)::binders) (ts,tt) (unify_types context (Ts, Tt) env) end
wenzelm@58950
   240
    | (Abs(ns,Ts,ts),t) => unif context ((ns,Ts)::binders) (ts,(incr t)$Bound(0)) env
wenzelm@58950
   241
    | (t,Abs(nt,Tt,tt)) => unif context ((nt,Tt)::binders) ((incr t)$Bound(0),tt) env
wenzelm@58950
   242
    | p => cases context (binders,env,p)
clasohm@0
   243
wenzelm@58950
   244
and cases context (binders,env,(s,t)) = case (strip_comb s,strip_comb t) of
wenzelm@12784
   245
       ((Var(F,Fty),ss),(Var(G,Gty),ts)) =>
wenzelm@52220
   246
         if F = G then flexflex1(env,binders,F,Fty,ints_of' env ss,ints_of' env ts)
wenzelm@52220
   247
                  else flexflex2(env,binders,F,Fty,ints_of' env ss,G,Gty,ints_of' env ts)
wenzelm@58950
   248
      | ((Var(F,Fty),ss),_)           => flexrigid context (env,binders,F,Fty,ints_of' env ss,t)
wenzelm@58950
   249
      | (_,(Var(F,Fty),ts))           => flexrigid context (env,binders,F,Fty,ints_of' env ts,s)
wenzelm@58950
   250
      | ((Const c,ss),(Const d,ts))   => rigidrigid context (env,binders,c,d,ss,ts)
wenzelm@58950
   251
      | ((Free(f),ss),(Free(g),ts))   => rigidrigid context (env,binders,f,g,ss,ts)
wenzelm@58950
   252
      | ((Bound(i),ss),(Bound(j),ts)) => rigidrigidB context (env,binders,i,j,ss,ts)
clasohm@0
   253
      | ((Abs(_),_),_)                => raise Pattern
clasohm@0
   254
      | (_,(Abs(_),_))                => raise Pattern
wenzelm@58950
   255
      | ((Const(c,_),_),(Free(f,_),_)) => (clash context c f; raise Unif)
wenzelm@58950
   256
      | ((Const(c,_),_),(Bound i,_))   => (clashB context binders i c; raise Unif)
wenzelm@58950
   257
      | ((Free(f,_),_),(Const(c,_),_)) => (clash context f c; raise Unif)
wenzelm@58950
   258
      | ((Free(f,_),_),(Bound i,_))    => (clashB context binders i f; raise Unif)
wenzelm@58950
   259
      | ((Bound i,_),(Const(c,_),_))   => (clashB context binders i c; raise Unif)
wenzelm@58950
   260
      | ((Bound i,_),(Free(f,_),_))    => (clashB context binders i f; raise Unif)
nipkow@13642
   261
clasohm@0
   262
wenzelm@58950
   263
and rigidrigid context (env,binders,(a,Ta),(b,Tb),ss,ts) =
wenzelm@58950
   264
      if a<>b then (clash context a b; raise Unif)
wenzelm@58950
   265
      else env |> unify_types context (Ta,Tb) |> fold (unif context binders) (ss~~ts)
clasohm@0
   266
wenzelm@58950
   267
and rigidrigidB context (env,binders,i,j,ss,ts) =
wenzelm@58950
   268
     if i <> j then (clashBB context binders i j; raise Unif)
wenzelm@58950
   269
     else fold (unif context binders) (ss~~ts) env
clasohm@0
   270
wenzelm@58950
   271
and flexrigid context (params as (env,binders,F,Fty,is,t)) =
wenzelm@58950
   272
      if occurs(F,t,env) then (ocheck_fail context (F,t,binders,env); raise Unif)
nipkow@13642
   273
      else (let val (u,env') = proj(t,env,binders,is)
wenzelm@51700
   274
            in Envir.update ((F, Fty), mkabs (binders, is, u)) env' end
wenzelm@58950
   275
            handle Unif => (proj_fail context params; raise Unif));
clasohm@0
   276
wenzelm@58950
   277
fun unify context = unif context [];
clasohm@0
   278
clasohm@0
   279
nipkow@678
   280
nipkow@4820
   281
(*** Matching ***)
nipkow@4820
   282
nipkow@4820
   283
exception MATCH;
nipkow@4820
   284
wenzelm@18182
   285
fun typ_match thy TU tyenv = Sign.typ_match thy TU tyenv
wenzelm@16939
   286
  handle Type.TYPE_MATCH => raise MATCH;
nipkow@4820
   287
nipkow@4820
   288
(*First-order matching;
nipkow@4820
   289
  The pattern and object may have variables in common.
nipkow@4820
   290
  Instantiation does not affect the object, so matching ?a with ?a+1 works.
nipkow@4820
   291
  Object is eta-contracted on the fly (by eta-expanding the pattern).
nipkow@4820
   292
  Precondition: the pattern is already eta-contracted!
nipkow@63718
   293
  Types are matched on the fly.
nipkow@63718
   294
  The parameter inAbs is an optimization to avoid calling is_open;
nipkow@63718
   295
  it has the funny consequence that outside abstractions
nipkow@63718
   296
  ?x matches terms containing loose Bounds.
nipkow@63718
   297
*)
wenzelm@18182
   298
fun first_order_match thy =
nipkow@4820
   299
  let
nipkow@63718
   300
    fun mtch inAbs (instsp as (tyinsts,insts)) = fn
nipkow@4820
   301
        (Var(ixn,T), t)  =>
nipkow@63718
   302
          if inAbs andalso Term.is_open t then raise MATCH
wenzelm@51700
   303
          else (case Envir.lookup1 insts (ixn, T) of
wenzelm@18182
   304
                  NONE => (typ_match thy (T, fastype_of t) tyinsts,
wenzelm@17412
   305
                           Vartab.update_new (ixn, (T, t)) insts)
wenzelm@52131
   306
                | SOME u => if Envir.aeconv (t, u) then instsp else raise MATCH)
nipkow@4820
   307
      | (Free (a,T), Free (b,U)) =>
wenzelm@18182
   308
          if a=b then (typ_match thy (T,U) tyinsts, insts) else raise MATCH
nipkow@4820
   309
      | (Const (a,T), Const (b,U))  =>
wenzelm@18182
   310
          if a=b then (typ_match thy (T,U) tyinsts, insts) else raise MATCH
nipkow@4820
   311
      | (Bound i, Bound j)  =>  if  i=j  then  instsp  else raise MATCH
nipkow@4820
   312
      | (Abs(_,T,t), Abs(_,U,u))  =>
nipkow@63718
   313
          mtch true (typ_match thy (T,U) tyinsts, insts) (t,u)
nipkow@63718
   314
      | (f$t, g$u) => mtch inAbs (mtch inAbs instsp (f,g)) (t, u)
nipkow@63718
   315
      | (t, Abs(_,U,u))  =>  mtch true instsp ((incr t)$(Bound 0), u)
nipkow@4820
   316
      | _ => raise MATCH
nipkow@63718
   317
  in fn tu => fn env => mtch false env tu end;
nipkow@4820
   318
berghofe@8406
   319
nipkow@4820
   320
(* Matching of higher-order patterns *)
nipkow@4820
   321
berghofe@15797
   322
fun match_bind(itms,binders,ixn,T,is,t) =
nipkow@4820
   323
  let val js = loose_bnos t
nipkow@4820
   324
  in if null is
wenzelm@17412
   325
     then if null js then Vartab.update_new (ixn, (T, t)) itms else raise MATCH
haftmann@33038
   326
     else if subset (op =) (js, is)
nipkow@4820
   327
          then let val t' = if downto0(is,length binders - 1) then t
nipkow@4820
   328
                            else mapbnd (idx is) t
wenzelm@17412
   329
               in Vartab.update_new (ixn, (T, mkabs (binders, is, t'))) itms end
nipkow@4820
   330
          else raise MATCH
nipkow@4820
   331
  end;
nipkow@4820
   332
wenzelm@18182
   333
fun match thy (po as (pat,obj)) envir =
nipkow@678
   334
let
nipkow@4820
   335
  (* Pre: pat and obj have same type *)
wenzelm@18182
   336
  fun mtch binders (pat,obj) (env as (iTs,itms)) =
nipkow@4820
   337
    case pat of
nipkow@4820
   338
      Abs(ns,Ts,ts) =>
nipkow@4820
   339
        (case obj of
wenzelm@18182
   340
           Abs(nt,Tt,tt) => mtch ((nt,Tt)::binders) (ts,tt) env
wenzelm@32035
   341
         | _ => let val Tt = Envir.subst_type iTs Ts
wenzelm@18182
   342
                in mtch((ns,Tt)::binders) (ts,(incr obj)$Bound(0)) env end)
nipkow@4820
   343
    | _ => (case obj of
nipkow@4820
   344
              Abs(nt,Tt,tt) =>
wenzelm@18182
   345
                mtch((nt,Tt)::binders) ((incr pat)$Bound(0),tt) env
nipkow@4820
   346
            | _ => cases(binders,env,pat,obj))
nipkow@678
   347
nipkow@4820
   348
  and cases(binders,env as (iTs,itms),pat,obj) =
nipkow@4820
   349
    let val (ph,pargs) = strip_comb pat
wenzelm@18182
   350
        fun rigrig1(iTs,oargs) = fold (mtch binders) (pargs~~oargs) (iTs,itms)
wenzelm@41067
   351
          handle ListPair.UnequalLengths => raise MATCH
wenzelm@16668
   352
        fun rigrig2((a:string,Ta),(b,Tb),oargs) =
wenzelm@16668
   353
              if a <> b then raise MATCH
wenzelm@18182
   354
              else rigrig1(typ_match thy (Ta,Tb) iTs, oargs)
nipkow@4820
   355
    in case ph of
berghofe@15797
   356
         Var(ixn,T) =>
nipkow@4820
   357
           let val is = ints_of pargs
wenzelm@51700
   358
           in case Envir.lookup1 itms (ixn, T) of
berghofe@15797
   359
                NONE => (iTs,match_bind(itms,binders,ixn,T,is,obj))
wenzelm@52131
   360
              | SOME u => if Envir.aeconv (obj, red u is []) then env
nipkow@4820
   361
                          else raise MATCH
nipkow@4820
   362
           end
nipkow@4820
   363
       | _ =>
nipkow@4820
   364
           let val (oh,oargs) = strip_comb obj
nipkow@4820
   365
           in case (ph,oh) of
nipkow@4820
   366
                (Const c,Const d) => rigrig2(c,d,oargs)
nipkow@4820
   367
              | (Free f,Free g)   => rigrig2(f,g,oargs)
nipkow@4820
   368
              | (Bound i,Bound j) => if i<>j then raise MATCH
nipkow@4820
   369
                                     else rigrig1(iTs,oargs)
nipkow@4820
   370
              | (Abs _, _)        => raise Pattern
nipkow@4820
   371
              | (_, Abs _)        => raise Pattern
nipkow@4820
   372
              | _                 => raise MATCH
nipkow@4820
   373
           end
nipkow@4820
   374
    end;
nipkow@678
   375
nipkow@4820
   376
  val pT = fastype_of pat
nipkow@4820
   377
  and oT = fastype_of obj
wenzelm@18182
   378
  val envir' = apfst (typ_match thy (pT, oT)) envir;
wenzelm@18182
   379
in mtch [] po envir' handle Pattern => first_order_match thy po envir' end;
clasohm@0
   380
clasohm@0
   381
end;
nipkow@13642
   382