src/Pure/unify.ML
author wenzelm
Sun Mar 01 23:36:12 2009 +0100 (2009-03-01)
changeset 30190 479806475f3c
parent 29269 5c25a2012975
child 32032 a6a6e8031c14
permissions -rw-r--r--
use long names for old-style fold combinators;
berghofe@15797
     1
(*  Title:      Pure/unify.ML
wenzelm@16425
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     3
    Copyright   Cambridge University 1992
clasohm@0
     4
wenzelm@16425
     5
Higher-Order Unification.
clasohm@0
     6
wenzelm@16425
     7
Types as well as terms are unified.  The outermost functions assume
wenzelm@16425
     8
the terms to be unified already have the same type.  In resolution,
wenzelm@16425
     9
this is assured because both have type "prop".
clasohm@0
    10
*)
clasohm@0
    11
wenzelm@16425
    12
signature UNIFY =
wenzelm@16425
    13
sig
wenzelm@24178
    14
  val trace_bound_value: Config.value Config.T
wenzelm@24178
    15
  val trace_bound: int Config.T
wenzelm@24178
    16
  val search_bound_value: Config.value Config.T
wenzelm@24178
    17
  val search_bound: int Config.T
wenzelm@24178
    18
  val trace_simp_value: Config.value Config.T
wenzelm@24178
    19
  val trace_simp: bool Config.T
wenzelm@24178
    20
  val trace_types_value: Config.value Config.T
wenzelm@24178
    21
  val trace_types: bool Config.T
wenzelm@16425
    22
  val unifiers: theory * Envir.env * ((term * term) list) ->
wenzelm@16425
    23
    (Envir.env * (term * term) list) Seq.seq
wenzelm@19864
    24
  val smash_unifiers: theory -> (term * term) list -> Envir.env -> Envir.env Seq.seq
wenzelm@19864
    25
  val matchers: theory -> (term * term) list -> Envir.env Seq.seq
wenzelm@19864
    26
  val matches_list: theory -> term list -> term list -> bool
wenzelm@16425
    27
end
clasohm@0
    28
wenzelm@19864
    29
structure Unify : UNIFY =
clasohm@0
    30
struct
clasohm@0
    31
clasohm@0
    32
(*Unification options*)
clasohm@0
    33
wenzelm@24178
    34
(*tracing starts above this depth, 0 for full*)
paulson@28173
    35
val trace_bound_value = Config.declare true "unify_trace_bound" (Config.Int 50);
wenzelm@24178
    36
val trace_bound = Config.int trace_bound_value;
wenzelm@24178
    37
wenzelm@24178
    38
(*unification quits above this depth*)
paulson@28173
    39
val search_bound_value = Config.declare true "unify_search_bound" (Config.Int 60);
wenzelm@24178
    40
val search_bound = Config.int search_bound_value;
wenzelm@24178
    41
wenzelm@24178
    42
(*print dpairs before calling SIMPL*)
wenzelm@24178
    43
val trace_simp_value = Config.declare true "unify_trace_simp" (Config.Bool false);
wenzelm@24178
    44
val trace_simp = Config.bool trace_simp_value;
wenzelm@24178
    45
wenzelm@24178
    46
(*announce potential incompleteness of type unification*)
wenzelm@24178
    47
val trace_types_value = Config.declare true "unify_trace_types" (Config.Bool false);
wenzelm@24178
    48
val trace_types = Config.bool trace_types_value;
wenzelm@24178
    49
clasohm@0
    50
clasohm@0
    51
type binderlist = (string*typ) list;
clasohm@0
    52
clasohm@0
    53
type dpair = binderlist * term * term;
clasohm@0
    54
wenzelm@19864
    55
fun body_type(Envir.Envir{iTs,...}) =
clasohm@0
    56
let fun bT(Type("fun",[_,T])) = bT T
haftmann@26328
    57
      | bT(T as TVar ixnS) = (case Type.lookup iTs ixnS of
wenzelm@19864
    58
    NONE => T | SOME(T') => bT T')
clasohm@0
    59
      | bT T = T
clasohm@0
    60
in bT end;
clasohm@0
    61
wenzelm@19864
    62
fun binder_types(Envir.Envir{iTs,...}) =
clasohm@0
    63
let fun bTs(Type("fun",[T,U])) = T :: bTs U
haftmann@26328
    64
      | bTs(T as TVar ixnS) = (case Type.lookup iTs ixnS of
wenzelm@19864
    65
    NONE => [] | SOME(T') => bTs T')
clasohm@0
    66
      | bTs _ = []
clasohm@0
    67
in bTs end;
clasohm@0
    68
clasohm@0
    69
fun strip_type env T = (binder_types env T, body_type env T);
clasohm@0
    70
berghofe@12231
    71
fun fastype env (Ts, t) = Envir.fastype env (map snd Ts) t;
clasohm@0
    72
clasohm@0
    73
clasohm@0
    74
(*Eta normal form*)
clasohm@0
    75
fun eta_norm(env as Envir.Envir{iTs,...}) =
clasohm@0
    76
  let fun etif (Type("fun",[T,U]), t) =
wenzelm@19864
    77
      Abs("", T, etif(U, incr_boundvars 1 t $ Bound 0))
wenzelm@19864
    78
  | etif (TVar ixnS, t) =
haftmann@26328
    79
      (case Type.lookup iTs ixnS of
wenzelm@19864
    80
      NONE => t | SOME(T) => etif(T,t))
wenzelm@19864
    81
  | etif (_,t) = t;
clasohm@0
    82
      fun eta_nm (rbinder, Abs(a,T,body)) =
wenzelm@19864
    83
      Abs(a, T, eta_nm ((a,T)::rbinder, body))
wenzelm@19864
    84
  | eta_nm (rbinder, t) = etif(fastype env (rbinder,t), t)
clasohm@0
    85
  in eta_nm end;
clasohm@0
    86
clasohm@0
    87
clasohm@0
    88
(*OCCURS CHECK
wenzelm@19864
    89
  Does the uvar occur in the term t?
clasohm@0
    90
  two forms of search, for whether there is a rigid path to the current term.
clasohm@0
    91
  "seen" is list of variables passed thru, is a memo variable for sharing.
berghofe@15797
    92
  This version searches for nonrigid occurrence, returns true if found.
berghofe@15797
    93
  Since terms may contain variables with same name and different types,
berghofe@15797
    94
  the occurs check must ignore the types of variables. This avoids
berghofe@15797
    95
  that ?x::?'a is unified with f(?x::T), which may lead to a cyclic
berghofe@15797
    96
  substitution when ?'a is instantiated with T later. *)
clasohm@0
    97
fun occurs_terms (seen: (indexname list) ref,
wenzelm@19864
    98
      env: Envir.env, v: indexname, ts: term list): bool =
clasohm@0
    99
  let fun occurs [] = false
wenzelm@19864
   100
  | occurs (t::ts) =  occur t  orelse  occurs ts
clasohm@0
   101
      and occur (Const _)  = false
wenzelm@19864
   102
  | occur (Bound _)  = false
wenzelm@19864
   103
  | occur (Free _)  = false
wenzelm@19864
   104
  | occur (Var (w, T))  =
wenzelm@20083
   105
      if member (op =) (!seen) w then false
wenzelm@29269
   106
      else if Term.eq_ix (v, w) then true
wenzelm@19864
   107
        (*no need to lookup: v has no assignment*)
wenzelm@19864
   108
      else (seen := w:: !seen;
wenzelm@19864
   109
            case Envir.lookup (env, (w, T)) of
wenzelm@19864
   110
          NONE    => false
wenzelm@19864
   111
        | SOME t => occur t)
wenzelm@19864
   112
  | occur (Abs(_,_,body)) = occur body
wenzelm@19864
   113
  | occur (f$t) = occur t  orelse   occur f
clasohm@0
   114
  in  occurs ts  end;
clasohm@0
   115
clasohm@0
   116
clasohm@0
   117
clasohm@0
   118
(* f(a1,...,an)  ---->   (f,  [a1,...,an])  using the assignments*)
clasohm@0
   119
fun head_of_in (env,t) : term = case t of
clasohm@0
   120
    f$_ => head_of_in(env,f)
wenzelm@19864
   121
  | Var vT => (case Envir.lookup (env, vT) of
wenzelm@19864
   122
      SOME u => head_of_in(env,u)  |  NONE   => t)
clasohm@0
   123
  | _ => t;
clasohm@0
   124
clasohm@0
   125
clasohm@0
   126
datatype occ = NoOcc | Nonrigid | Rigid;
clasohm@0
   127
clasohm@0
   128
(* Rigid occur check
clasohm@0
   129
Returns Rigid    if it finds a rigid occurrence of the variable,
clasohm@0
   130
        Nonrigid if it finds a nonrigid path to the variable.
clasohm@0
   131
        NoOcc    otherwise.
clasohm@0
   132
  Continues searching for a rigid occurrence even if it finds a nonrigid one.
clasohm@0
   133
clasohm@0
   134
Condition for detecting non-unifable terms: [ section 5.3 of Huet (1975) ]
clasohm@0
   135
   a rigid path to the variable, appearing with no arguments.
clasohm@0
   136
Here completeness is sacrificed in order to reduce danger of divergence:
clasohm@0
   137
   reject ALL rigid paths to the variable.
wenzelm@19864
   138
Could check for rigid paths to bound variables that are out of scope.
clasohm@0
   139
Not necessary because the assignment test looks at variable's ENTIRE rbinder.
clasohm@0
   140
clasohm@0
   141
Treatment of head(arg1,...,argn):
clasohm@0
   142
If head is a variable then no rigid path, switch to nonrigid search
wenzelm@19864
   143
for arg1,...,argn.
wenzelm@19864
   144
If head is an abstraction then possibly no rigid path (head could be a
clasohm@0
   145
   constant function) so again use nonrigid search.  Happens only if
wenzelm@19864
   146
   term is not in normal form.
clasohm@0
   147
clasohm@0
   148
Warning: finds a rigid occurrence of ?f in ?f(t).
clasohm@0
   149
  Should NOT be called in this case: there is a flex-flex unifier
clasohm@0
   150
*)
wenzelm@19864
   151
fun rigid_occurs_term (seen: (indexname list)ref, env, v: indexname, t) =
wenzelm@19864
   152
  let fun nonrigid t = if occurs_terms(seen,env,v,[t]) then Nonrigid
wenzelm@19864
   153
           else NoOcc
clasohm@0
   154
      fun occurs [] = NoOcc
wenzelm@19864
   155
  | occurs (t::ts) =
clasohm@0
   156
            (case occur t of
clasohm@0
   157
               Rigid => Rigid
clasohm@0
   158
             | oc =>  (case occurs ts of NoOcc => oc  |  oc2 => oc2))
clasohm@0
   159
      and occomb (f$t) =
clasohm@0
   160
            (case occur t of
clasohm@0
   161
               Rigid => Rigid
clasohm@0
   162
             | oc =>  (case occomb f of NoOcc => oc  |  oc2 => oc2))
clasohm@0
   163
        | occomb t = occur t
clasohm@0
   164
      and occur (Const _)  = NoOcc
wenzelm@19864
   165
  | occur (Bound _)  = NoOcc
wenzelm@19864
   166
  | occur (Free _)  = NoOcc
wenzelm@19864
   167
  | occur (Var (w, T))  =
wenzelm@20083
   168
      if member (op =) (!seen) w then NoOcc
wenzelm@29269
   169
      else if Term.eq_ix (v, w) then Rigid
wenzelm@19864
   170
      else (seen := w:: !seen;
wenzelm@19864
   171
            case Envir.lookup (env, (w, T)) of
wenzelm@19864
   172
          NONE    => NoOcc
wenzelm@19864
   173
        | SOME t => occur t)
wenzelm@19864
   174
  | occur (Abs(_,_,body)) = occur body
wenzelm@19864
   175
  | occur (t as f$_) =  (*switch to nonrigid search?*)
wenzelm@19864
   176
     (case head_of_in (env,f) of
wenzelm@19864
   177
        Var (w,_) => (*w is not assigned*)
wenzelm@29269
   178
    if Term.eq_ix (v, w) then Rigid
wenzelm@19864
   179
    else  nonrigid t
wenzelm@19864
   180
      | Abs(_,_,body) => nonrigid t (*not in normal form*)
wenzelm@19864
   181
      | _ => occomb t)
clasohm@0
   182
  in  occur t  end;
clasohm@0
   183
clasohm@0
   184
wenzelm@19864
   185
exception CANTUNIFY;  (*Signals non-unifiability.  Does not signal errors!*)
wenzelm@19864
   186
exception ASSIGN; (*Raised if not an assignment*)
clasohm@0
   187
clasohm@0
   188
wenzelm@16664
   189
fun unify_types thy (T,U, env as Envir.Envir{asol,iTs,maxidx}) =
nipkow@1435
   190
  if T=U then env
wenzelm@16934
   191
  else let val (iTs',maxidx') = Sign.typ_unify thy (U, T) (iTs, maxidx)
nipkow@1435
   192
       in Envir.Envir{asol=asol,maxidx=maxidx',iTs=iTs'} end
paulson@1505
   193
       handle Type.TUNIFY => raise CANTUNIFY;
clasohm@0
   194
wenzelm@16664
   195
fun test_unify_types thy (args as (T,U,_)) =
wenzelm@26939
   196
let val str_of = Syntax.string_of_typ_global thy;
wenzelm@16664
   197
    fun warn() = tracing ("Potential loss of completeness: " ^ str_of U ^ " = " ^ str_of T);
wenzelm@16664
   198
    val env' = unify_types thy args
clasohm@0
   199
in if is_TVar(T) orelse is_TVar(U) then warn() else ();
clasohm@0
   200
   env'
clasohm@0
   201
end;
clasohm@0
   202
clasohm@0
   203
(*Is the term eta-convertible to a single variable with the given rbinder?
clasohm@0
   204
  Examples: ?a   ?f(B.0)   ?g(B.1,B.0)
clasohm@0
   205
  Result is var a for use in SIMPL. *)
clasohm@0
   206
fun get_eta_var ([], _, Var vT)  =  vT
clasohm@0
   207
  | get_eta_var (_::rbinder, n, f $ Bound i) =
wenzelm@19864
   208
  if  n=i  then  get_eta_var (rbinder, n+1, f)
wenzelm@19864
   209
     else  raise ASSIGN
clasohm@0
   210
  | get_eta_var _ = raise ASSIGN;
clasohm@0
   211
clasohm@0
   212
clasohm@0
   213
(*Solve v=u by assignment -- "fixedpoint" to Huet -- if v not in u.
clasohm@0
   214
  If v occurs rigidly then nonunifiable.
clasohm@0
   215
  If v occurs nonrigidly then must use full algorithm. *)
wenzelm@16664
   216
fun assignment thy (env, rbinder, t, u) =
berghofe@15797
   217
    let val vT as (v,T) = get_eta_var (rbinder, 0, t)
berghofe@15797
   218
    in  case rigid_occurs_term (ref [], env, v, u) of
wenzelm@19864
   219
        NoOcc => let val env = unify_types thy (body_type env T,
wenzelm@19864
   220
             fastype env (rbinder,u),env)
wenzelm@19864
   221
    in Envir.update ((vT, Logic.rlist_abs (rbinder, u)), env) end
wenzelm@19864
   222
      | Nonrigid =>  raise ASSIGN
wenzelm@19864
   223
      | Rigid =>  raise CANTUNIFY
clasohm@0
   224
    end;
clasohm@0
   225
clasohm@0
   226
clasohm@0
   227
(*Extends an rbinder with a new disagreement pair, if both are abstractions.
clasohm@0
   228
  Tries to unify types of the bound variables!
clasohm@0
   229
  Checks that binders have same length, since terms should be eta-normal;
clasohm@0
   230
    if not, raises TERM, probably indicating type mismatch.
wenzelm@19864
   231
  Uses variable a (unless the null string) to preserve user's naming.*)
wenzelm@16664
   232
fun new_dpair thy (rbinder, Abs(a,T,body1), Abs(b,U,body2), env) =
wenzelm@19864
   233
  let val env' = unify_types thy (T,U,env)
wenzelm@19864
   234
      val c = if a="" then b else a
wenzelm@19864
   235
  in new_dpair thy ((c,T) :: rbinder, body1, body2, env') end
wenzelm@16664
   236
    | new_dpair _ (_, Abs _, _, _) = raise TERM ("new_dpair", [])
wenzelm@16664
   237
    | new_dpair _ (_, _, Abs _, _) = raise TERM ("new_dpair", [])
wenzelm@16664
   238
    | new_dpair _ (rbinder, t1, t2, env) = ((rbinder, t1, t2), env);
clasohm@0
   239
clasohm@0
   240
wenzelm@16664
   241
fun head_norm_dpair thy (env, (rbinder,t,u)) : dpair * Envir.env =
wenzelm@16664
   242
     new_dpair thy (rbinder,
wenzelm@19864
   243
    eta_norm env (rbinder, Envir.head_norm env t),
wenzelm@19864
   244
      eta_norm env (rbinder, Envir.head_norm env u), env);
clasohm@0
   245
clasohm@0
   246
clasohm@0
   247
clasohm@0
   248
(*flexflex: the flex-flex pairs,  flexrigid: the flex-rigid pairs
clasohm@0
   249
  Does not perform assignments for flex-flex pairs:
lcp@646
   250
    may create nonrigid paths, which prevent other assignments.
lcp@646
   251
  Does not even identify Vars in dpairs such as ?a =?= ?b; an attempt to
lcp@646
   252
    do so caused numerous problems with no compensating advantage.
lcp@646
   253
*)
wenzelm@16664
   254
fun SIMPL0 thy (dp0, (env,flexflex,flexrigid))
wenzelm@19864
   255
  : Envir.env * dpair list * dpair list =
wenzelm@16664
   256
    let val (dp as (rbinder,t,u), env) = head_norm_dpair thy (env,dp0);
wenzelm@19864
   257
      fun SIMRANDS(f$t, g$u, env) =
wenzelm@19864
   258
      SIMPL0 thy ((rbinder,t,u), SIMRANDS(f,g,env))
wenzelm@19864
   259
        | SIMRANDS (t as _$_, _, _) =
wenzelm@19864
   260
    raise TERM ("SIMPL: operands mismatch", [t,u])
wenzelm@19864
   261
        | SIMRANDS (t, u as _$_, _) =
wenzelm@19864
   262
    raise TERM ("SIMPL: operands mismatch", [t,u])
wenzelm@19864
   263
        | SIMRANDS(_,_,env) = (env,flexflex,flexrigid);
clasohm@0
   264
    in case (head_of t, head_of u) of
clasohm@0
   265
       (Var(_,T), Var(_,U)) =>
wenzelm@19864
   266
      let val T' = body_type env T and U' = body_type env U;
wenzelm@19864
   267
    val env = unify_types thy (T',U',env)
wenzelm@19864
   268
      in (env, dp::flexflex, flexrigid) end
clasohm@0
   269
     | (Var _, _) =>
wenzelm@19864
   270
      ((assignment thy (env,rbinder,t,u), flexflex, flexrigid)
wenzelm@19864
   271
       handle ASSIGN => (env, flexflex, dp::flexrigid))
clasohm@0
   272
     | (_, Var _) =>
wenzelm@19864
   273
      ((assignment thy (env,rbinder,u,t), flexflex, flexrigid)
wenzelm@19864
   274
       handle ASSIGN => (env, flexflex, (rbinder,u,t)::flexrigid))
clasohm@0
   275
     | (Const(a,T), Const(b,U)) =>
wenzelm@19864
   276
      if a=b then SIMRANDS(t,u, unify_types thy (T,U,env))
wenzelm@19864
   277
      else raise CANTUNIFY
clasohm@0
   278
     | (Bound i,    Bound j)    =>
wenzelm@19864
   279
      if i=j  then SIMRANDS(t,u,env) else raise CANTUNIFY
clasohm@0
   280
     | (Free(a,T),  Free(b,U))  =>
wenzelm@19864
   281
      if a=b then SIMRANDS(t,u, unify_types thy (T,U,env))
wenzelm@19864
   282
      else raise CANTUNIFY
clasohm@0
   283
     | _ => raise CANTUNIFY
clasohm@0
   284
    end;
clasohm@0
   285
clasohm@0
   286
clasohm@0
   287
(* changed(env,t) checks whether the head of t is a variable assigned in env*)
clasohm@0
   288
fun changed (env, f$_) = changed (env,f)
berghofe@15797
   289
  | changed (env, Var v) =
skalberg@15531
   290
      (case Envir.lookup(env,v) of NONE=>false  |  _ => true)
clasohm@0
   291
  | changed _ = false;
clasohm@0
   292
clasohm@0
   293
clasohm@0
   294
(*Recursion needed if any of the 'head variables' have been updated
clasohm@0
   295
  Clever would be to re-do just the affected dpairs*)
wenzelm@16664
   296
fun SIMPL thy (env,dpairs) : Envir.env * dpair list * dpair list =
clasohm@0
   297
    let val all as (env',flexflex,flexrigid) =
wenzelm@23178
   298
      List.foldr (SIMPL0 thy) (env,[],[]) dpairs;
wenzelm@19864
   299
  val dps = flexrigid@flexflex
clasohm@0
   300
    in if exists (fn ((_,t,u)) => changed(env',t) orelse changed(env',u)) dps
wenzelm@16664
   301
       then SIMPL thy (env',dps) else all
clasohm@0
   302
    end;
clasohm@0
   303
clasohm@0
   304
wenzelm@19864
   305
(*Makes the terms E1,...,Em,    where Ts = [T...Tm].
clasohm@0
   306
  Each Ei is   ?Gi(B.(n-1),...,B.0), and has type Ti
clasohm@0
   307
  The B.j are bound vars of binder.
wenzelm@19864
   308
  The terms are not made in eta-normal-form, SIMPL does that later.
clasohm@0
   309
  If done here, eta-expansion must be recursive in the arguments! *)
clasohm@0
   310
fun make_args name (binder: typ list, env, []) = (env, [])   (*frequent case*)
clasohm@0
   311
  | make_args name (binder: typ list, env, Ts) : Envir.env * term list =
clasohm@0
   312
       let fun funtype T = binder--->T;
wenzelm@19864
   313
     val (env', vars) = Envir.genvars name (env, map funtype Ts)
wenzelm@18945
   314
       in  (env',  map (fn var=> Logic.combound(var, 0, length binder)) vars)  end;
clasohm@0
   315
clasohm@0
   316
clasohm@0
   317
(*Abstraction over a list of types, like list_abs*)
clasohm@0
   318
fun types_abs ([],u) = u
clasohm@0
   319
  | types_abs (T::Ts, u) = Abs("", T, types_abs(Ts,u));
clasohm@0
   320
clasohm@0
   321
(*Abstraction over the binder of a type*)
clasohm@0
   322
fun type_abs (env,T,t) = types_abs(binder_types env T, t);
clasohm@0
   323
clasohm@0
   324
clasohm@0
   325
(*MATCH taking "big steps".
clasohm@0
   326
  Copies u into the Var v, using projection on targs or imitation.
clasohm@0
   327
  A projection is allowed unless SIMPL raises an exception.
clasohm@0
   328
  Allocates new variables in projection on a higher-order argument,
clasohm@0
   329
    or if u is a variable (flex-flex dpair).
clasohm@0
   330
  Returns long sequence of every way of copying u, for backtracking
clasohm@0
   331
  For example, projection in ?b'(?a) may be wrong if other dpairs constrain ?a.
wenzelm@19864
   332
  The order for trying projections is crucial in ?b'(?a)
clasohm@0
   333
  NB "vname" is only used in the call to make_args!!   *)
wenzelm@19864
   334
fun matchcopy thy vname = let fun mc(rbinder, targs, u, ed as (env,dpairs))
wenzelm@19864
   335
  : (term * (Envir.env * dpair list))Seq.seq =
wenzelm@24178
   336
let
wenzelm@24178
   337
  val trace_tps = Config.get_thy thy trace_types;
wenzelm@24178
   338
  (*Produce copies of uarg and cons them in front of uargs*)
wenzelm@24178
   339
  fun copycons uarg (uargs, (env, dpairs)) =
wenzelm@19864
   340
  Seq.map(fn (uarg', ed') => (uarg'::uargs, ed'))
wenzelm@19864
   341
      (mc (rbinder, targs,eta_norm env (rbinder, Envir.head_norm env uarg),
wenzelm@19864
   342
     (env, dpairs)));
wenzelm@19864
   343
  (*Produce sequence of all possible ways of copying the arg list*)
wenzelm@19473
   344
    fun copyargs [] = Seq.cons ([],ed) Seq.empty
wenzelm@17344
   345
      | copyargs (uarg::uargs) = Seq.maps (copycons uarg) (copyargs uargs);
clasohm@0
   346
    val (uhead,uargs) = strip_comb u;
clasohm@0
   347
    val base = body_type env (fastype env (rbinder,uhead));
clasohm@0
   348
    fun joinargs (uargs',ed') = (list_comb(uhead,uargs'), ed');
clasohm@0
   349
    (*attempt projection on argument with given typ*)
clasohm@0
   350
    val Ts = map (curry (fastype env) rbinder) targs;
wenzelm@19864
   351
    fun projenv (head, (Us,bary), targ, tail) =
wenzelm@24178
   352
  let val env = if trace_tps then test_unify_types thy (base,bary,env)
wenzelm@19864
   353
          else unify_types thy (base,bary,env)
wenzelm@19864
   354
  in Seq.make (fn () =>
wenzelm@19864
   355
      let val (env',args) = make_args vname (Ts,env,Us);
wenzelm@19864
   356
    (*higher-order projection: plug in targs for bound vars*)
wenzelm@19864
   357
    fun plugin arg = list_comb(head_of arg, targs);
wenzelm@19864
   358
    val dp = (rbinder, list_comb(targ, map plugin args), u);
wenzelm@19864
   359
    val (env2,frigid,fflex) = SIMPL thy (env', dp::dpairs)
wenzelm@19864
   360
        (*may raise exception CANTUNIFY*)
wenzelm@19864
   361
      in  SOME ((list_comb(head,args), (env2, frigid@fflex)),
wenzelm@19864
   362
      tail)
wenzelm@19864
   363
      end  handle CANTUNIFY => Seq.pull tail)
wenzelm@19864
   364
  end handle CANTUNIFY => tail;
clasohm@0
   365
    (*make a list of projections*)
clasohm@0
   366
    fun make_projs (T::Ts, targ::targs) =
wenzelm@19864
   367
        (Bound(length Ts), T, targ) :: make_projs (Ts,targs)
clasohm@0
   368
      | make_projs ([],[]) = []
clasohm@0
   369
      | make_projs _ = raise TERM ("make_projs", u::targs);
clasohm@0
   370
    (*try projections and imitation*)
clasohm@0
   371
    fun matchfun ((bvar,T,targ)::projs) =
wenzelm@19864
   372
         (projenv(bvar, strip_type env T, targ, matchfun projs))
clasohm@0
   373
      | matchfun [] = (*imitation last of all*)
wenzelm@19864
   374
        (case uhead of
wenzelm@19864
   375
     Const _ => Seq.map joinargs (copyargs uargs)
wenzelm@19864
   376
         | Free _  => Seq.map joinargs (copyargs uargs)
wenzelm@19864
   377
         | _ => Seq.empty)  (*if Var, would be a loop!*)
clasohm@0
   378
in case uhead of
wenzelm@19864
   379
  Abs(a, T, body) =>
wenzelm@19864
   380
      Seq.map(fn (body', ed') => (Abs (a,T,body'), ed'))
wenzelm@19864
   381
    (mc ((a,T)::rbinder,
wenzelm@19864
   382
      (map (incr_boundvars 1) targs) @ [Bound 0], body, ed))
wenzelm@19864
   383
      | Var (w,uary) =>
wenzelm@19864
   384
      (*a flex-flex dpair: make variable for t*)
wenzelm@19864
   385
      let val (env', newhd) = Envir.genvar (#1 w) (env, Ts---> base)
wenzelm@19864
   386
    val tabs = Logic.combound(newhd, 0, length Ts)
wenzelm@19864
   387
    val tsub = list_comb(newhd,targs)
wenzelm@19864
   388
      in  Seq.single (tabs, (env', (rbinder,tsub,u):: dpairs))
wenzelm@19864
   389
      end
clasohm@0
   390
      | _ =>  matchfun(rev(make_projs(Ts, targs)))
clasohm@0
   391
end
clasohm@0
   392
in mc end;
clasohm@0
   393
clasohm@0
   394
clasohm@0
   395
(*Call matchcopy to produce assignments to the variable in the dpair*)
wenzelm@16664
   396
fun MATCH thy (env, (rbinder,t,u), dpairs)
wenzelm@19864
   397
  : (Envir.env * dpair list)Seq.seq =
berghofe@15797
   398
  let val (Var (vT as (v, T)), targs) = strip_comb t;
clasohm@0
   399
      val Ts = binder_types env T;
clasohm@0
   400
      fun new_dset (u', (env',dpairs')) =
wenzelm@19864
   401
    (*if v was updated to s, must unify s with u' *)
wenzelm@19864
   402
    case Envir.lookup (env', vT) of
wenzelm@19864
   403
        NONE => (Envir.update ((vT, types_abs(Ts, u')), env'),  dpairs')
wenzelm@19864
   404
      | SOME s => (env', ([], s, types_abs(Ts, u'))::dpairs')
wenzelm@4270
   405
  in Seq.map new_dset
wenzelm@16664
   406
         (matchcopy thy (#1 v) (rbinder, targs, u, (env,dpairs)))
clasohm@0
   407
  end;
clasohm@0
   408
clasohm@0
   409
clasohm@0
   410
clasohm@0
   411
(**** Flex-flex processing ****)
clasohm@0
   412
wenzelm@19864
   413
(*At end of unification, do flex-flex assignments like ?a -> ?f(?b)
clasohm@0
   414
  Attempts to update t with u, raising ASSIGN if impossible*)
wenzelm@19864
   415
fun ff_assign thy (env, rbinder, t, u) : Envir.env =
berghofe@15797
   416
let val vT as (v,T) = get_eta_var(rbinder,0,t)
berghofe@15797
   417
in if occurs_terms (ref [], env, v, [u]) then raise ASSIGN
wenzelm@16664
   418
   else let val env = unify_types thy (body_type env T,
wenzelm@19864
   419
          fastype env (rbinder,u),
wenzelm@19864
   420
          env)
wenzelm@19864
   421
  in Envir.vupdate ((vT, Logic.rlist_abs (rbinder, u)), env) end
clasohm@0
   422
end;
clasohm@0
   423
clasohm@0
   424
clasohm@0
   425
(*Flex argument: a term, its type, and the index that refers to it.*)
clasohm@0
   426
type flarg = {t: term,  T: typ,  j: int};
clasohm@0
   427
clasohm@0
   428
clasohm@0
   429
(*Form the arguments into records for deletion/sorting.*)
clasohm@0
   430
fun flexargs ([],[],[]) = [] : flarg list
clasohm@0
   431
  | flexargs (j::js, t::ts, T::Ts) = {j=j, t=t, T=T} :: flexargs(js,ts,Ts)
clasohm@0
   432
  | flexargs _ = error"flexargs";
clasohm@0
   433
clasohm@0
   434
clasohm@0
   435
(*If an argument contains a banned Bound, then it should be deleted.
lcp@651
   436
  But if the only path is flexible, this is difficult; the code gives up!
lcp@651
   437
  In  %x y.?a(x) =?= %x y.?b(?c(y)) should we instantiate ?b or ?c *)
lcp@651
   438
exception CHANGE_FAIL;   (*flexible occurrence of banned variable*)
clasohm@0
   439
clasohm@0
   440
lcp@651
   441
(*Check whether the 'banned' bound var indices occur rigidly in t*)
wenzelm@19864
   442
fun rigid_bound (lev, banned) t =
wenzelm@19864
   443
  let val (head,args) = strip_comb t
wenzelm@19864
   444
  in
lcp@651
   445
      case head of
wenzelm@20664
   446
    Bound i => member (op =) banned (i-lev)  orelse
wenzelm@19864
   447
               exists (rigid_bound (lev, banned)) args
wenzelm@19864
   448
  | Var _ => false  (*no rigid occurrences here!*)
wenzelm@19864
   449
  | Abs (_,_,u) =>
wenzelm@19864
   450
         rigid_bound(lev+1, banned) u  orelse
wenzelm@19864
   451
         exists (rigid_bound (lev, banned)) args
wenzelm@19864
   452
  | _ => exists (rigid_bound (lev, banned)) args
clasohm@0
   453
  end;
clasohm@0
   454
lcp@651
   455
(*Squash down indices at level >=lev to delete the banned from a term.*)
lcp@651
   456
fun change_bnos banned =
wenzelm@19864
   457
  let fun change lev (Bound i) =
wenzelm@19864
   458
      if i<lev then Bound i
wenzelm@20664
   459
      else  if member (op =) banned (i-lev)
wenzelm@19864
   460
      then raise CHANGE_FAIL (**flexible occurrence: give up**)
wenzelm@19864
   461
      else  Bound (i - length (List.filter (fn j => j < i-lev) banned))
wenzelm@19864
   462
  | change lev (Abs (a,T,t)) = Abs (a, T, change(lev+1) t)
wenzelm@19864
   463
  | change lev (t$u) = change lev t $ change lev u
wenzelm@19864
   464
  | change lev t = t
lcp@651
   465
  in  change 0  end;
clasohm@0
   466
clasohm@0
   467
(*Change indices, delete the argument if it contains a banned Bound*)
lcp@651
   468
fun change_arg banned ({j,t,T}, args) : flarg list =
wenzelm@19864
   469
    if rigid_bound (0, banned) t  then  args  (*delete argument!*)
lcp@651
   470
    else  {j=j, t= change_bnos banned t, T=T} :: args;
clasohm@0
   471
clasohm@0
   472
clasohm@0
   473
(*Sort the arguments to create assignments if possible:
clasohm@0
   474
  create eta-terms like ?g(B.1,B.0) *)
clasohm@0
   475
fun arg_less ({t= Bound i1,...}, {t= Bound i2,...}) = (i2<i1)
clasohm@0
   476
  | arg_less (_:flarg, _:flarg) = false;
clasohm@0
   477
clasohm@0
   478
(*Test whether the new term would be eta-equivalent to a variable --
clasohm@0
   479
  if so then there is no point in creating a new variable*)
clasohm@0
   480
fun decreasing n ([]: flarg list) = (n=0)
clasohm@0
   481
  | decreasing n ({j,...}::args) = j=n-1 andalso decreasing (n-1) args;
clasohm@0
   482
clasohm@0
   483
(*Delete banned indices in the term, simplifying it.
clasohm@0
   484
  Force an assignment, if possible, by sorting the arguments.
clasohm@0
   485
  Update its head; squash indices in arguments. *)
clasohm@0
   486
fun clean_term banned (env,t) =
clasohm@0
   487
    let val (Var(v,T), ts) = strip_comb t
wenzelm@19864
   488
  val (Ts,U) = strip_type env T
wenzelm@19864
   489
  and js = length ts - 1  downto 0
wenzelm@19864
   490
  val args = sort (make_ord arg_less)
wenzelm@23178
   491
    (List.foldr (change_arg banned) [] (flexargs (js,ts,Ts)))
wenzelm@19864
   492
  val ts' = map (#t) args
clasohm@0
   493
    in
clasohm@0
   494
    if decreasing (length Ts) args then (env, (list_comb(Var(v,T), ts')))
clasohm@0
   495
    else let val (env',v') = Envir.genvar (#1v) (env, map (#T) args ---> U)
wenzelm@19864
   496
       val body = list_comb(v', map (Bound o #j) args)
wenzelm@19864
   497
       val env2 = Envir.vupdate ((((v, T), types_abs(Ts, body)),   env'))
wenzelm@19864
   498
       (*the vupdate affects ts' if they contain v*)
wenzelm@19864
   499
   in
wenzelm@19864
   500
       (env2, Envir.norm_term env2 (list_comb(v',ts')))
clasohm@0
   501
         end
clasohm@0
   502
    end;
clasohm@0
   503
clasohm@0
   504
clasohm@0
   505
(*Add tpair if not trivial or already there.
clasohm@0
   506
  Should check for swapped pairs??*)
clasohm@0
   507
fun add_tpair (rbinder, (t0,u0), tpairs) : (term*term) list =
wenzelm@19864
   508
  if t0 aconv u0 then tpairs
clasohm@0
   509
  else
wenzelm@18945
   510
  let val t = Logic.rlist_abs(rbinder, t0)  and  u = Logic.rlist_abs(rbinder, u0);
clasohm@0
   511
      fun same(t',u') = (t aconv t') andalso (u aconv u')
clasohm@0
   512
  in  if exists same tpairs  then tpairs  else (t,u)::tpairs  end;
clasohm@0
   513
clasohm@0
   514
clasohm@0
   515
(*Simplify both terms and check for assignments.
clasohm@0
   516
  Bound vars in the binder are "banned" unless used in both t AND u *)
wenzelm@19864
   517
fun clean_ffpair thy ((rbinder, t, u), (env,tpairs)) =
clasohm@0
   518
  let val loot = loose_bnos t  and  loou = loose_bnos u
wenzelm@19864
   519
      fun add_index (((a,T), j), (bnos, newbinder)) =
wenzelm@20664
   520
            if  member (op =) loot j  andalso  member (op =) loou j
wenzelm@19864
   521
            then  (bnos, (a,T)::newbinder)  (*needed by both: keep*)
wenzelm@19864
   522
            else  (j::bnos, newbinder);   (*remove*)
clasohm@0
   523
      val indices = 0 upto (length rbinder - 1);
wenzelm@23178
   524
      val (banned,rbin') = List.foldr add_index ([],[]) (rbinder~~indices);
clasohm@0
   525
      val (env', t') = clean_term banned (env, t);
clasohm@0
   526
      val (env'',u') = clean_term banned (env',u)
wenzelm@16664
   527
  in  (ff_assign thy (env'', rbin', t', u'), tpairs)
wenzelm@16664
   528
      handle ASSIGN => (ff_assign thy (env'', rbin', u', t'), tpairs)
clasohm@0
   529
      handle ASSIGN => (env'', add_tpair(rbin', (t',u'), tpairs))
clasohm@0
   530
  end
clasohm@0
   531
  handle CHANGE_FAIL => (env, add_tpair(rbinder, (t,u), tpairs));
clasohm@0
   532
clasohm@0
   533
clasohm@0
   534
(*IF the flex-flex dpair is an assignment THEN do it  ELSE  put in tpairs
clasohm@0
   535
  eliminates trivial tpairs like t=t, as well as repeated ones
wenzelm@19864
   536
  trivial tpairs can easily escape SIMPL:  ?A=t, ?A=?B, ?B=t gives t=t
clasohm@0
   537
  Resulting tpairs MAY NOT be in normal form:  assignments may occur here.*)
wenzelm@19864
   538
fun add_ffpair thy ((rbinder,t0,u0), (env,tpairs))
clasohm@0
   539
      : Envir.env * (term*term)list =
clasohm@0
   540
  let val t = Envir.norm_term env t0  and  u = Envir.norm_term env u0
clasohm@0
   541
  in  case  (head_of t, head_of u) of
clasohm@0
   542
      (Var(v,T), Var(w,U)) =>  (*Check for identical variables...*)
wenzelm@29269
   543
  if Term.eq_ix (v, w) then     (*...occur check would falsely return true!*)
wenzelm@19864
   544
      if T=U then (env, add_tpair (rbinder, (t,u), tpairs))
wenzelm@19864
   545
      else raise TERM ("add_ffpair: Var name confusion", [t,u])
wenzelm@29269
   546
  else if TermOrd.indexname_ord (v, w) = LESS then (*prefer to update the LARGER variable*)
wenzelm@19864
   547
       clean_ffpair thy ((rbinder, u, t), (env,tpairs))
wenzelm@16664
   548
        else clean_ffpair thy ((rbinder, t, u), (env,tpairs))
clasohm@0
   549
    | _ => raise TERM ("add_ffpair: Vars expected", [t,u])
clasohm@0
   550
  end;
clasohm@0
   551
clasohm@0
   552
clasohm@0
   553
(*Print a tracing message + list of dpairs.
clasohm@0
   554
  In t==u print u first because it may be rigid or flexible --
clasohm@0
   555
    t is always flexible.*)
wenzelm@16664
   556
fun print_dpairs thy msg (env,dpairs) =
clasohm@0
   557
  let fun pdp (rbinder,t,u) =
wenzelm@26939
   558
        let fun termT t = Syntax.pretty_term_global thy
wenzelm@18945
   559
                              (Envir.norm_term env (Logic.rlist_abs(rbinder,t)))
clasohm@0
   560
            val bsymbs = [termT u, Pretty.str" =?=", Pretty.brk 1,
clasohm@0
   561
                          termT t];
wenzelm@12262
   562
        in tracing(Pretty.string_of(Pretty.blk(0,bsymbs))) end;
skalberg@15570
   563
  in  tracing msg;  List.app pdp dpairs  end;
clasohm@0
   564
clasohm@0
   565
clasohm@0
   566
(*Unify the dpairs in the environment.
wenzelm@19864
   567
  Returns flex-flex disagreement pairs NOT IN normal form.
clasohm@0
   568
  SIMPL may raise exception CANTUNIFY. *)
wenzelm@19864
   569
fun hounifiers (thy,env, tus : (term*term)list)
wenzelm@4270
   570
  : (Envir.env * (term*term)list)Seq.seq =
wenzelm@24178
   571
  let
wenzelm@24178
   572
    val trace_bnd = Config.get_thy thy trace_bound;
wenzelm@24178
   573
    val search_bnd = Config.get_thy thy search_bound;
wenzelm@24178
   574
    val trace_smp = Config.get_thy thy trace_simp;
wenzelm@24178
   575
    fun add_unify tdepth ((env,dpairs), reseq) =
wenzelm@19864
   576
    Seq.make (fn()=>
wenzelm@19864
   577
    let val (env',flexflex,flexrigid) =
wenzelm@24178
   578
         (if tdepth> trace_bnd andalso trace_smp
wenzelm@19864
   579
    then print_dpairs thy "Enter SIMPL" (env,dpairs)  else ();
wenzelm@19864
   580
    SIMPL thy (env,dpairs))
wenzelm@19864
   581
    in case flexrigid of
wenzelm@23178
   582
        [] => SOME (List.foldr (add_ffpair thy) (env',[]) flexflex, reseq)
wenzelm@19864
   583
      | dp::frigid' =>
wenzelm@24178
   584
    if tdepth > search_bnd then
wenzelm@19864
   585
        (warning "Unification bound exceeded"; Seq.pull reseq)
wenzelm@19864
   586
    else
wenzelm@24178
   587
    (if tdepth > trace_bnd then
wenzelm@19864
   588
        print_dpairs thy "Enter MATCH" (env',flexrigid@flexflex)
wenzelm@19864
   589
     else ();
wenzelm@19864
   590
     Seq.pull (Seq.it_right (add_unify (tdepth+1))
wenzelm@19864
   591
         (MATCH thy (env',dp, frigid'@flexflex), reseq)))
wenzelm@19864
   592
    end
wenzelm@19864
   593
    handle CANTUNIFY =>
wenzelm@24178
   594
      (if tdepth > trace_bnd then tracing"Failure node" else ();
wenzelm@19864
   595
       Seq.pull reseq));
clasohm@0
   596
     val dps = map (fn(t,u)=> ([],t,u)) tus
wenzelm@16425
   597
  in add_unify 1 ((env, dps), Seq.empty) end;
clasohm@0
   598
wenzelm@18184
   599
fun unifiers (params as (thy, env, tus)) =
wenzelm@19473
   600
  Seq.cons (fold (Pattern.unify thy) tus env, []) Seq.empty
wenzelm@16425
   601
    handle Pattern.Unif => Seq.empty
wenzelm@16425
   602
         | Pattern.Pattern => hounifiers params;
clasohm@0
   603
clasohm@0
   604
clasohm@0
   605
(*For smash_flexflex1*)
clasohm@0
   606
fun var_head_of (env,t) : indexname * typ =
clasohm@0
   607
  case head_of (strip_abs_body (Envir.norm_term env t)) of
clasohm@0
   608
      Var(v,T) => (v,T)
clasohm@0
   609
    | _ => raise CANTUNIFY;  (*not flexible, cannot use trivial substitution*)
clasohm@0
   610
clasohm@0
   611
clasohm@0
   612
(*Eliminate a flex-flex pair by the trivial substitution, see Huet (1975)
clasohm@0
   613
  Unifies ?f(t1...rm) with ?g(u1...un) by ?f -> %x1...xm.?a, ?g -> %x1...xn.?a
wenzelm@19864
   614
  Unfortunately, unifies ?f(t,u) with ?g(t,u) by ?f, ?g -> %(x,y)?a,
wenzelm@19864
   615
  though just ?g->?f is a more general unifier.
clasohm@0
   616
  Unlike Huet (1975), does not smash together all variables of same type --
clasohm@0
   617
    requires more work yet gives a less general unifier (fewer variables).
clasohm@0
   618
  Handles ?f(t1...rm) with ?f(u1...um) to avoid multiple updates. *)
clasohm@0
   619
fun smash_flexflex1 ((t,u), env) : Envir.env =
berghofe@15797
   620
  let val vT as (v,T) = var_head_of (env,t)
berghofe@15797
   621
      and wU as (w,U) = var_head_of (env,u);
clasohm@0
   622
      val (env', var) = Envir.genvar (#1v) (env, body_type env T)
berghofe@15797
   623
      val env'' = Envir.vupdate ((wU, type_abs (env', U, var)), env')
berghofe@15797
   624
  in  if vT = wU then env''  (*the other update would be identical*)
berghofe@15797
   625
      else Envir.vupdate ((vT, type_abs (env', T, var)), env'')
clasohm@0
   626
  end;
clasohm@0
   627
clasohm@0
   628
clasohm@0
   629
(*Smash all flex-flexpairs.  Should allow selection of pairs by a predicate?*)
clasohm@0
   630
fun smash_flexflex (env,tpairs) : Envir.env =
wenzelm@23178
   631
  List.foldr smash_flexflex1 env tpairs;
clasohm@0
   632
clasohm@0
   633
(*Returns unifiers with no remaining disagreement pairs*)
wenzelm@19864
   634
fun smash_unifiers thy tus env =
wenzelm@16425
   635
    Seq.map smash_flexflex (unifiers(thy,env,tus));
clasohm@0
   636
wenzelm@19864
   637
wenzelm@19864
   638
(*Pattern matching*)
wenzelm@20020
   639
fun first_order_matchers thy pairs (Envir.Envir {asol = tenv, iTs = tyenv, maxidx}) =
wenzelm@20020
   640
  let val (tyenv', tenv') = fold (Pattern.first_order_match thy) pairs (tyenv, tenv)
wenzelm@19864
   641
  in Seq.single (Envir.Envir {asol = tenv', iTs = tyenv', maxidx = maxidx}) end
wenzelm@19864
   642
  handle Pattern.MATCH => Seq.empty;
wenzelm@19864
   643
wenzelm@19864
   644
(*General matching -- keeps variables disjoint*)
wenzelm@19864
   645
fun matchers _ [] = Seq.single (Envir.empty ~1)
wenzelm@19864
   646
  | matchers thy pairs =
wenzelm@19864
   647
      let
wenzelm@19864
   648
        val maxidx = fold (Term.maxidx_term o #2) pairs ~1;
wenzelm@19864
   649
        val offset = maxidx + 1;
wenzelm@19864
   650
        val pairs' = map (apfst (Logic.incr_indexes ([], offset))) pairs;
wenzelm@19864
   651
        val maxidx' = fold (fn (t, u) => Term.maxidx_term t #> Term.maxidx_term u) pairs' ~1;
wenzelm@19864
   652
wenzelm@19864
   653
        val pat_tvars = fold (Term.add_tvars o #1) pairs' [];
wenzelm@19864
   654
        val pat_vars = fold (Term.add_vars o #1) pairs' [];
wenzelm@19864
   655
wenzelm@19864
   656
        val decr_indexesT =
wenzelm@19864
   657
          Term.map_atyps (fn T as TVar ((x, i), S) =>
wenzelm@19864
   658
            if i > maxidx then TVar ((x, i - offset), S) else T | T => T);
wenzelm@19864
   659
        val decr_indexes =
wenzelm@20548
   660
          Term.map_types decr_indexesT #>
wenzelm@19864
   661
          Term.map_aterms (fn t as Var ((x, i), T) =>
wenzelm@19864
   662
            if i > maxidx then Var ((x, i - offset), T) else t | t => t);
wenzelm@19864
   663
wenzelm@19864
   664
        fun norm_tvar (Envir.Envir {iTs = tyenv, ...}) ((x, i), S) =
wenzelm@19864
   665
          ((x, i - offset), (S, decr_indexesT (Envir.norm_type tyenv (TVar ((x, i), S)))));
wenzelm@19864
   666
        fun norm_var (env as Envir.Envir {iTs = tyenv, ...}) ((x, i), T) =
wenzelm@19864
   667
          let
wenzelm@19864
   668
            val T' = Envir.norm_type tyenv T;
wenzelm@19864
   669
            val t' = Envir.norm_term env (Var ((x, i), T'));
wenzelm@19864
   670
          in ((x, i - offset), (decr_indexesT T', decr_indexes t')) end;
wenzelm@19864
   671
wenzelm@19864
   672
        fun result env =
wenzelm@19876
   673
          if Envir.above env maxidx then   (* FIXME proper handling of generated vars!? *)
wenzelm@19864
   674
            SOME (Envir.Envir {maxidx = maxidx,
wenzelm@19866
   675
              iTs = Vartab.make (map (norm_tvar env) pat_tvars),
wenzelm@19866
   676
              asol = Vartab.make (map (norm_var env) pat_vars)})
wenzelm@19866
   677
          else NONE;
wenzelm@19864
   678
wenzelm@19864
   679
        val empty = Envir.empty maxidx';
wenzelm@19864
   680
      in
wenzelm@19876
   681
        Seq.append
wenzelm@19920
   682
          (Seq.map_filter result (smash_unifiers thy pairs' empty))
wenzelm@20020
   683
          (first_order_matchers thy pairs empty)
wenzelm@19864
   684
      end;
wenzelm@19864
   685
wenzelm@19864
   686
fun matches_list thy ps os =
wenzelm@19864
   687
  length ps = length os andalso is_some (Seq.pull (matchers thy (ps ~~ os)));
wenzelm@19864
   688
clasohm@0
   689
end;