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