src/Pure/pattern.ML
author wenzelm
Sat, 19 Oct 2019 11:29:39 +0200
changeset 70906 b9567a9f44a0
parent 70443 a21a96eda033
permissions -rw-r--r--
proper export of bootstrap proofs (amending a6304b4664b6);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12784
bab3b002cbbb added rewrite_term;
wenzelm
parents: 12781
diff changeset
     1
(*  Title:      Pure/pattern.ML
29269
5c25a2012975 moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents: 28348
diff changeset
     2
    Author:     Tobias Nipkow, Christine Heinzelmann, and Stefan Berghofer, TU Muenchen
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     3
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     4
Unification of Higher-Order Patterns.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     5
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     6
See also:
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
Tobias Nipkow. Functional Unification of Higher-Order Patterns.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     8
In Proceedings of the 8th IEEE Symposium Logic in Computer Science, 1993.
678
6151b7f3b606 Modified pattern.ML to perform proper matching of Higher-Order Patterns.
nipkow
parents: 63
diff changeset
     9
6151b7f3b606 Modified pattern.ML to perform proper matching of Higher-Order Patterns.
nipkow
parents: 63
diff changeset
    10
TODO: optimize red by special-casing it
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    11
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    12
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    13
signature PATTERN =
14787
724ce6e574e3 adapted tsig interface;
wenzelm
parents: 14643
diff changeset
    14
sig
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    15
  exception Unif
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    16
  exception Pattern
52709
0e4bacf21e77 turned pattern unify flag into configuration option (global only);
wenzelm
parents: 52220
diff changeset
    17
  val unify_trace_failure: bool Config.T
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    18
  val unify_types: Context.generic -> typ * typ -> Envir.env -> Envir.env
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    19
  val unify: Context.generic -> term * term -> Envir.env -> Envir.env
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    20
  exception MATCH
18182
786d83044780 tuned interfaces to support incremental match/unify (cf. versions in type.ML);
wenzelm
parents: 18011
diff changeset
    21
  val match: theory -> term * term -> Type.tyenv * Envir.tenv -> Type.tyenv * Envir.tenv
786d83044780 tuned interfaces to support incremental match/unify (cf. versions in type.ML);
wenzelm
parents: 18011
diff changeset
    22
  val first_order_match: theory -> term * term
786d83044780 tuned interfaces to support incremental match/unify (cf. versions in type.ML);
wenzelm
parents: 18011
diff changeset
    23
    -> Type.tyenv * Envir.tenv -> Type.tyenv * Envir.tenv
70443
a21a96eda033 clarified modules;
wenzelm
parents: 69575
diff changeset
    24
  val pattern: term -> bool
14787
724ce6e574e3 adapted tsig interface;
wenzelm
parents: 14643
diff changeset
    25
end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    26
17203
29b2563f5c11 refer to theory instead of low-level tsig;
wenzelm
parents: 16986
diff changeset
    27
structure Pattern: PATTERN =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    28
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    29
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    30
exception Unif;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    31
exception Pattern;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    32
69575
f77cc54f6d47 clarified signature: more types;
wenzelm
parents: 64556
diff changeset
    33
val unify_trace_failure = Config.declare_bool ("unify_trace_failure", \<^here>) (K false);
13642
a3d97348ceb6 added failure trace information to pattern unification
nipkow
parents: 13195
diff changeset
    34
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    35
fun string_of_term ctxt env binders t =
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    36
  Syntax.string_of_term ctxt (Envir.norm_term env (subst_bounds (map Free binders, t)));
13642
a3d97348ceb6 added failure trace information to pattern unification
nipkow
parents: 13195
diff changeset
    37
18011
685d95c793ff cleaned up nth, nth_update, nth_map and nth_string functions
haftmann
parents: 17756
diff changeset
    38
fun bname binders i = fst (nth binders i);
13642
a3d97348ceb6 added failure trace information to pattern unification
nipkow
parents: 13195
diff changeset
    39
fun bnames binders is = space_implode " " (map (bname binders) is);
a3d97348ceb6 added failure trace information to pattern unification
nipkow
parents: 13195
diff changeset
    40
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    41
fun typ_clash context (tye,T,U) =
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    42
  if Config.get_generic context unify_trace_failure then
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    43
    let
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    44
      val ctxt = Context.proof_of context;
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    45
      val t = Syntax.string_of_typ ctxt (Envir.norm_type tye T);
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    46
      val u = Syntax.string_of_typ ctxt (Envir.norm_type tye U);
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    47
    in tracing ("The following types do not unify:\n" ^ t ^ "\n" ^ u) end
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    48
  else ();
13642
a3d97348ceb6 added failure trace information to pattern unification
nipkow
parents: 13195
diff changeset
    49
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    50
fun clash context a b =
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    51
  if Config.get_generic context unify_trace_failure
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    52
  then tracing ("Clash: " ^ a ^ " =/= " ^ b) else ();
13642
a3d97348ceb6 added failure trace information to pattern unification
nipkow
parents: 13195
diff changeset
    53
a3d97348ceb6 added failure trace information to pattern unification
nipkow
parents: 13195
diff changeset
    54
fun boundVar binders i =
a3d97348ceb6 added failure trace information to pattern unification
nipkow
parents: 13195
diff changeset
    55
  "bound variable " ^ bname binders i ^ " (depth " ^ string_of_int i ^ ")";
a3d97348ceb6 added failure trace information to pattern unification
nipkow
parents: 13195
diff changeset
    56
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    57
fun clashBB context binders i j =
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    58
  if Config.get_generic context unify_trace_failure
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    59
  then clash context (boundVar binders i) (boundVar binders j) else ();
13642
a3d97348ceb6 added failure trace information to pattern unification
nipkow
parents: 13195
diff changeset
    60
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    61
fun clashB context binders i s =
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    62
  if Config.get_generic context unify_trace_failure
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    63
  then clash context (boundVar binders i) s else ();
13642
a3d97348ceb6 added failure trace information to pattern unification
nipkow
parents: 13195
diff changeset
    64
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    65
fun proj_fail context (env,binders,F,_,is,t) =
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    66
  if Config.get_generic context unify_trace_failure then
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    67
    let
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    68
      val ctxt = Context.proof_of context
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    69
      val f = Term.string_of_vname F
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    70
      val xs = bnames binders is
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    71
      val u = string_of_term ctxt env binders t
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    72
      val ys = bnames binders (subtract (op =) is (loose_bnos t))
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    73
    in
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    74
      tracing ("Cannot unify variable " ^ f ^
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    75
        " (depending on bound variables " ^ xs ^ ")\nwith term " ^ u ^
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    76
        "\nTerm contains additional bound variable(s) " ^ ys)
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    77
    end
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    78
  else ();
13642
a3d97348ceb6 added failure trace information to pattern unification
nipkow
parents: 13195
diff changeset
    79
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    80
fun ocheck_fail context (F,t,binders,env) =
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    81
  if Config.get_generic context unify_trace_failure then
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    82
    let
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    83
      val ctxt = Context.proof_of context
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    84
      val f = Term.string_of_vname F
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    85
      val u = string_of_term ctxt env binders t
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    86
    in tracing ("Variable " ^ f ^ " occurs in term\n" ^ u ^ "\nCannot unify!\n") end
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
    87
  else ();
13642
a3d97348ceb6 added failure trace information to pattern unification
nipkow
parents: 13195
diff changeset
    88
12784
bab3b002cbbb added rewrite_term;
wenzelm
parents: 12781
diff changeset
    89
fun occurs(F,t,env) =
51700
c8f2bad67dbb tuned signature;
wenzelm
parents: 46857
diff changeset
    90
    let fun occ(Var (G, T))   = (case Envir.lookup env (G, T) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
    91
                                 SOME(t) => occ t
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
    92
                               | NONE    => F=G)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    93
          | occ(t1$t2)      = occ t1 orelse occ t2
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    94
          | occ(Abs(_,_,t)) = occ t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    95
          | occ _           = false
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    96
    in occ t end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    97
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    98
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    99
fun mapbnd f =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   100
    let fun mpb d (Bound(i))     = if i < d then Bound(i) else Bound(f(i-d)+d)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   101
          | mpb d (Abs(s,T,t))   = Abs(s,T,mpb(d+1) t)
678
6151b7f3b606 Modified pattern.ML to perform proper matching of Higher-Order Patterns.
nipkow
parents: 63
diff changeset
   102
          | mpb d ((u1 $ u2))    = (mpb d u1)$(mpb d u2)
6151b7f3b606 Modified pattern.ML to perform proper matching of Higher-Order Patterns.
nipkow
parents: 63
diff changeset
   103
          | mpb _ atom           = atom
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   104
    in mpb 0 end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   105
14861
ca5cae7fb65a Removed ~10000 hack in function idx that can lead to inconsistencies
berghofe
parents: 14787
diff changeset
   106
fun idx [] j     = raise Unif
16668
fdb4992cf1d2 avoid polyeq;
wenzelm
parents: 16651
diff changeset
   107
  | idx(i::is) j = if (i:int) =j then length is else idx is j;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   108
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   109
fun mkabs (binders,is,t)  =
18011
685d95c793ff cleaned up nth, nth_update, nth_map and nth_string functions
haftmann
parents: 17756
diff changeset
   110
    let fun mk(i::is) = let val (x,T) = nth binders i
12784
bab3b002cbbb added rewrite_term;
wenzelm
parents: 12781
diff changeset
   111
                        in Abs(x,T,mk is) end
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   112
          | mk []     = t
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   113
    in mk is end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   114
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   115
val incr = mapbnd (fn i => i+1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   116
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   117
fun ints_of []             = []
12784
bab3b002cbbb added rewrite_term;
wenzelm
parents: 12781
diff changeset
   118
  | ints_of (Bound i ::bs) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   119
      let val is = ints_of bs
20672
b96394d8c702 member (op =);
wenzelm
parents: 20098
diff changeset
   120
      in if member (op =) is i then raise Pattern else i::is end
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   121
  | ints_of _              = raise Pattern;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   122
12232
ff75ed08b3fb Replaced devar by Envir.head_norm
berghofe
parents: 8406
diff changeset
   123
fun ints_of' env ts = ints_of (map (Envir.head_norm env) ts);
ff75ed08b3fb Replaced devar by Envir.head_norm
berghofe
parents: 8406
diff changeset
   124
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   125
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   126
fun app (s,(i::is)) = app (s$Bound(i),is)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   127
  | app (s,[])      = s;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   128
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   129
fun red (Abs(_,_,s)) (i::is) js = red s is (i::js)
678
6151b7f3b606 Modified pattern.ML to perform proper matching of Higher-Order Patterns.
nipkow
parents: 63
diff changeset
   130
  | red t            []      [] = t
18011
685d95c793ff cleaned up nth, nth_update, nth_map and nth_string functions
haftmann
parents: 17756
diff changeset
   131
  | red t            is      jn = app (mapbnd (nth jn) t,is);
678
6151b7f3b606 Modified pattern.ML to perform proper matching of Higher-Order Patterns.
nipkow
parents: 63
diff changeset
   132
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   133
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   134
(* split_type ([T1,....,Tn]---> T,n,[]) = ([Tn,...,T1],T) *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   135
fun split_type (T,0,Ts)                    = (Ts,T)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   136
  | split_type (Type ("fun",[T1,T2]),n,Ts) = split_type (T2,n-1,T1::Ts)
52133
f8cd46077224 proper internal error, not user error;
wenzelm
parents: 52131
diff changeset
   137
  | split_type _                           = raise Fail "split_type";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   138
32032
a6a6e8031c14 tuned/modernized Envir operations;
wenzelm
parents: 30565
diff changeset
   139
fun type_of_G env (T, n, is) =
a6a6e8031c14 tuned/modernized Envir operations;
wenzelm
parents: 30565
diff changeset
   140
  let
a6a6e8031c14 tuned/modernized Envir operations;
wenzelm
parents: 30565
diff changeset
   141
    val tyenv = Envir.type_env env;
a6a6e8031c14 tuned/modernized Envir operations;
wenzelm
parents: 30565
diff changeset
   142
    val (Ts, U) = split_type (Envir.norm_type tyenv T, n, []);
18011
685d95c793ff cleaned up nth, nth_update, nth_map and nth_string functions
haftmann
parents: 17756
diff changeset
   143
  in map (nth Ts) is ---> U end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   144
52130
wenzelm
parents: 52129
diff changeset
   145
fun mk_hnf (binders,is,G,js) = mkabs (binders, is, app(G,js));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   146
52130
wenzelm
parents: 52129
diff changeset
   147
fun mk_new_hnf(env,binders,is,F as (a,_),T,js) =
13891
ae9a2a433388 type_of_G now applies type substitution before decomposing type.
berghofe
parents: 13645
diff changeset
   148
  let val (env',G) = Envir.genvar a (env,type_of_G env (T,length is,js))
52130
wenzelm
parents: 52129
diff changeset
   149
  in Envir.update ((F, T), mk_hnf (binders, is, G, js)) env' end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   150
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   151
23222
daca4731942f added downto0 (from library.ML);
wenzelm
parents: 22678
diff changeset
   152
(*predicate: downto0 (is, n) <=> is = [n, n - 1, ..., 0]*)
daca4731942f added downto0 (from library.ML);
wenzelm
parents: 22678
diff changeset
   153
fun downto0 (i :: is, n) = i = n andalso downto0 (is, n - 1)
daca4731942f added downto0 (from library.ML);
wenzelm
parents: 22678
diff changeset
   154
  | downto0 ([], n) = n = ~1;
daca4731942f added downto0 (from library.ML);
wenzelm
parents: 22678
diff changeset
   155
daca4731942f added downto0 (from library.ML);
wenzelm
parents: 22678
diff changeset
   156
(*mk_proj_list(is) = [ |is| - k | 1 <= k <= |is| and is[k] >= 0 ]*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   157
fun mk_proj_list is =
19502
wenzelm
parents: 19482
diff changeset
   158
    let fun mk(i::is,j) = if is_some i then j :: mk(is,j-1) else mk(is,j-1)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   159
          | mk([],_)    = []
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   160
    in mk(is,length is - 1) end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   161
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   162
fun proj(s,env,binders,is) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   163
    let fun trans d i = if i<d then i else (idx is (i-d))+d;
12232
ff75ed08b3fb Replaced devar by Envir.head_norm
berghofe
parents: 8406
diff changeset
   164
        fun pr(s,env,d,binders) = (case Envir.head_norm env s of
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   165
              Abs(a,T,t) => let val (t',env') = pr(t,env,d+1,((a,T)::binders))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   166
                            in (Abs(a,T,t'),env') end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   167
            | t => (case strip_comb t of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   168
                (c as Const _,ts) =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   169
                         let val (ts',env') = prs(ts,env,d,binders)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   170
                         in (list_comb(c,ts'),env') end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   171
                 | (f as Free _,ts) =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   172
                         let val (ts',env') = prs(ts,env,d,binders)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   173
                         in (list_comb(f,ts'),env') end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   174
                 | (Bound(i),ts) =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   175
                         let val j = trans d i
14861
ca5cae7fb65a Removed ~10000 hack in function idx that can lead to inconsistencies
berghofe
parents: 14787
diff changeset
   176
                             val (ts',env') = prs(ts,env,d,binders)
ca5cae7fb65a Removed ~10000 hack in function idx that can lead to inconsistencies
berghofe
parents: 14787
diff changeset
   177
                         in (list_comb(Bound j,ts'),env') end
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   178
                 | (Var(F as (a,_),Fty),ts) =>
12232
ff75ed08b3fb Replaced devar by Envir.head_norm
berghofe
parents: 8406
diff changeset
   179
                      let val js = ints_of' env ts;
14861
ca5cae7fb65a Removed ~10000 hack in function idx that can lead to inconsistencies
berghofe
parents: 14787
diff changeset
   180
                          val js' = map (try (trans d)) js;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   181
                          val ks = mk_proj_list js';
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19300
diff changeset
   182
                          val ls = map_filter I js'
13891
ae9a2a433388 type_of_G now applies type substitution before decomposing type.
berghofe
parents: 13645
diff changeset
   183
                          val Hty = type_of_G env (Fty,length js,ks)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   184
                          val (env',H) = Envir.genvar a (env,Hty)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   185
                          val env'' =
52130
wenzelm
parents: 52129
diff changeset
   186
                            Envir.update ((F, Fty), mk_hnf (binders, js, H, ks)) env'
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   187
                      in (app(H,ls),env'') end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   188
                 | _  => raise Pattern))
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   189
        and prs(s::ss,env,d,binders) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   190
              let val (s',env1) = pr(s,env,d,binders)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   191
                  val (ss',env2) = prs(ss,env1,d,binders)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   192
              in (s'::ss',env2) end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   193
          | prs([],env,_,_) = ([],env)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   194
   in if downto0(is,length binders - 1) then (s,env)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   195
      else pr(s,env,0,binders)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   196
   end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   197
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   198
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   199
(* mk_ff_list(is,js) = [ length(is) - k | 1 <= k <= |is| and is[k] = js[k] ] *)
12784
bab3b002cbbb added rewrite_term;
wenzelm
parents: 12781
diff changeset
   200
fun mk_ff_list(is,js) =
bab3b002cbbb added rewrite_term;
wenzelm
parents: 12781
diff changeset
   201
    let fun mk([],[],_)        = []
16668
fdb4992cf1d2 avoid polyeq;
wenzelm
parents: 16651
diff changeset
   202
          | mk(i::is,j::js, k) = if (i:int) = j then k :: mk(is,js,k-1)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   203
                                        else mk(is,js,k-1)
52133
f8cd46077224 proper internal error, not user error;
wenzelm
parents: 52131
diff changeset
   204
          | mk _               = raise Fail "mk_ff_list"
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   205
    in mk(is,js,length is-1) end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   206
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   207
fun flexflex1(env,binders,F,Fty,is,js) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   208
  if is=js then env
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   209
  else let val ks = mk_ff_list(is,js)
52130
wenzelm
parents: 52129
diff changeset
   210
       in mk_new_hnf(env,binders,is,F,Fty,ks) end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   211
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   212
fun flexflex2(env,binders,F,Fty,is,G,Gty,js) =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   213
  let fun ff(F,Fty,is,G as (a,_),Gty,js) =
33038
8f9594c31de4 dropped redundant gen_ prefix
haftmann
parents: 33037
diff changeset
   214
            if subset (op =) (js, is)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   215
            then let val t= mkabs(binders,is,app(Var(G,Gty),map (idx is) js))
51700
c8f2bad67dbb tuned signature;
wenzelm
parents: 46857
diff changeset
   216
                 in Envir.update ((F, Fty), t) env end
33049
c38f02fdf35d curried inter as canonical list operation (beware of argument order)
haftmann
parents: 33038
diff changeset
   217
            else let val ks = inter (op =) js is
13891
ae9a2a433388 type_of_G now applies type substitution before decomposing type.
berghofe
parents: 13645
diff changeset
   218
                     val Hty = type_of_G env (Fty,length is,map (idx is) ks)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   219
                     val (env',H) = Envir.genvar a (env,Hty)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   220
                     fun lam(is) = mkabs(binders,is,app(H,map (idx is) ks));
51700
c8f2bad67dbb tuned signature;
wenzelm
parents: 46857
diff changeset
   221
                 in Envir.update ((G, Gty), lam js) (Envir.update ((F, Fty), lam is) env')
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   222
                 end;
35408
b48ab741683b modernized structure Term_Ord;
wenzelm
parents: 35210
diff changeset
   223
  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
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   224
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   225
fun unify_types context (T, U) (env as Envir.Envir {maxidx, tenv, tyenv}) =
32032
a6a6e8031c14 tuned/modernized Envir operations;
wenzelm
parents: 30565
diff changeset
   226
  if T = U then env
a6a6e8031c14 tuned/modernized Envir operations;
wenzelm
parents: 30565
diff changeset
   227
  else
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   228
    let
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   229
      val thy = Context.theory_of context
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   230
      val (tyenv', maxidx') = Sign.typ_unify thy (U, T) (tyenv, maxidx)
32032
a6a6e8031c14 tuned/modernized Envir operations;
wenzelm
parents: 30565
diff changeset
   231
    in Envir.Envir {maxidx = maxidx', tenv = tenv, tyenv = tyenv'} end
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   232
    handle Type.TUNIFY => (typ_clash context (tyenv, T, U); raise Unif);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   233
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   234
fun unif context binders (s,t) env = case (Envir.head_norm env s, Envir.head_norm env t) of
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   235
      (Abs(ns,Ts,ts),Abs(nt,Tt,tt)) =>
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   236
         let val name = if ns = "" then nt else ns
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   237
         in unif context ((name,Ts)::binders) (ts,tt) (unify_types context (Ts, Tt) env) end
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   238
    | (Abs(ns,Ts,ts),t) => unif context ((ns,Ts)::binders) (ts,(incr t)$Bound(0)) env
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   239
    | (t,Abs(nt,Tt,tt)) => unif context ((nt,Tt)::binders) ((incr t)$Bound(0),tt) env
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   240
    | p => cases context (binders,env,p)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   241
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   242
and cases context (binders,env,(s,t)) = case (strip_comb s,strip_comb t) of
12784
bab3b002cbbb added rewrite_term;
wenzelm
parents: 12781
diff changeset
   243
       ((Var(F,Fty),ss),(Var(G,Gty),ts)) =>
52220
c4264f71dc3b backout 3b9c31867707 -- too risky to "amend" modules from 25 years ago that don't handle Vars with different types;
wenzelm
parents: 52179
diff changeset
   244
         if F = G then flexflex1(env,binders,F,Fty,ints_of' env ss,ints_of' env ts)
c4264f71dc3b backout 3b9c31867707 -- too risky to "amend" modules from 25 years ago that don't handle Vars with different types;
wenzelm
parents: 52179
diff changeset
   245
                  else flexflex2(env,binders,F,Fty,ints_of' env ss,G,Gty,ints_of' env ts)
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   246
      | ((Var(F,Fty),ss),_)           => flexrigid context (env,binders,F,Fty,ints_of' env ss,t)
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   247
      | (_,(Var(F,Fty),ts))           => flexrigid context (env,binders,F,Fty,ints_of' env ts,s)
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   248
      | ((Const c,ss),(Const d,ts))   => rigidrigid context (env,binders,c,d,ss,ts)
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   249
      | ((Free(f),ss),(Free(g),ts))   => rigidrigid context (env,binders,f,g,ss,ts)
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   250
      | ((Bound(i),ss),(Bound(j),ts)) => rigidrigidB context (env,binders,i,j,ss,ts)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   251
      | ((Abs(_),_),_)                => raise Pattern
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   252
      | (_,(Abs(_),_))                => raise Pattern
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   253
      | ((Const(c,_),_),(Free(f,_),_)) => (clash context c f; raise Unif)
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   254
      | ((Const(c,_),_),(Bound i,_))   => (clashB context binders i c; raise Unif)
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   255
      | ((Free(f,_),_),(Const(c,_),_)) => (clash context f c; raise Unif)
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   256
      | ((Free(f,_),_),(Bound i,_))    => (clashB context binders i f; raise Unif)
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   257
      | ((Bound i,_),(Const(c,_),_))   => (clashB context binders i c; raise Unif)
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   258
      | ((Bound i,_),(Free(f,_),_))    => (clashB context binders i f; raise Unif)
13642
a3d97348ceb6 added failure trace information to pattern unification
nipkow
parents: 13195
diff changeset
   259
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   260
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   261
and rigidrigid context (env,binders,(a,Ta),(b,Tb),ss,ts) =
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   262
      if a<>b then (clash context a b; raise Unif)
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   263
      else env |> unify_types context (Ta,Tb) |> fold (unif context binders) (ss~~ts)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   264
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   265
and rigidrigidB context (env,binders,i,j,ss,ts) =
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   266
     if i <> j then (clashBB context binders i j; raise Unif)
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   267
     else fold (unif context binders) (ss~~ts) env
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   268
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   269
and flexrigid context (params as (env,binders,F,Fty,is,t)) =
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   270
      if occurs(F,t,env) then (ocheck_fail context (F,t,binders,env); raise Unif)
13642
a3d97348ceb6 added failure trace information to pattern unification
nipkow
parents: 13195
diff changeset
   271
      else (let val (u,env') = proj(t,env,binders,is)
51700
c8f2bad67dbb tuned signature;
wenzelm
parents: 46857
diff changeset
   272
            in Envir.update ((F, Fty), mkabs (binders, is, u)) env' end
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   273
            handle Unif => (proj_fail context params; raise Unif));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   274
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58859
diff changeset
   275
fun unify context = unif context [];
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   276
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   277
678
6151b7f3b606 Modified pattern.ML to perform proper matching of Higher-Order Patterns.
nipkow
parents: 63
diff changeset
   278
4820
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   279
(*** Matching ***)
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   280
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   281
exception MATCH;
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   282
18182
786d83044780 tuned interfaces to support incremental match/unify (cf. versions in type.ML);
wenzelm
parents: 18011
diff changeset
   283
fun typ_match thy TU tyenv = Sign.typ_match thy TU tyenv
16939
87fc64d2409f Sign.typ_unify;
wenzelm
parents: 16668
diff changeset
   284
  handle Type.TYPE_MATCH => raise MATCH;
4820
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   285
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   286
(*First-order matching;
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   287
  The pattern and object may have variables in common.
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   288
  Instantiation does not affect the object, so matching ?a with ?a+1 works.
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   289
  Object is eta-contracted on the fly (by eta-expanding the pattern).
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   290
  Precondition: the pattern is already eta-contracted!
63718
600cf5c8f2ba replaced the confusing int parameter by bool
nipkow
parents: 59026
diff changeset
   291
  Types are matched on the fly.
600cf5c8f2ba replaced the confusing int parameter by bool
nipkow
parents: 59026
diff changeset
   292
  The parameter inAbs is an optimization to avoid calling is_open;
600cf5c8f2ba replaced the confusing int parameter by bool
nipkow
parents: 59026
diff changeset
   293
  it has the funny consequence that outside abstractions
600cf5c8f2ba replaced the confusing int parameter by bool
nipkow
parents: 59026
diff changeset
   294
  ?x matches terms containing loose Bounds.
600cf5c8f2ba replaced the confusing int parameter by bool
nipkow
parents: 59026
diff changeset
   295
*)
18182
786d83044780 tuned interfaces to support incremental match/unify (cf. versions in type.ML);
wenzelm
parents: 18011
diff changeset
   296
fun first_order_match thy =
4820
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   297
  let
63718
600cf5c8f2ba replaced the confusing int parameter by bool
nipkow
parents: 59026
diff changeset
   298
    fun mtch inAbs (instsp as (tyinsts,insts)) = fn
4820
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   299
        (Var(ixn,T), t)  =>
63718
600cf5c8f2ba replaced the confusing int parameter by bool
nipkow
parents: 59026
diff changeset
   300
          if inAbs andalso Term.is_open t then raise MATCH
51700
c8f2bad67dbb tuned signature;
wenzelm
parents: 46857
diff changeset
   301
          else (case Envir.lookup1 insts (ixn, T) of
18182
786d83044780 tuned interfaces to support incremental match/unify (cf. versions in type.ML);
wenzelm
parents: 18011
diff changeset
   302
                  NONE => (typ_match thy (T, fastype_of t) tyinsts,
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   303
                           Vartab.update_new (ixn, (T, t)) insts)
52131
366fa32ee2a3 tuned signature;
wenzelm
parents: 52130
diff changeset
   304
                | SOME u => if Envir.aeconv (t, u) then instsp else raise MATCH)
4820
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   305
      | (Free (a,T), Free (b,U)) =>
18182
786d83044780 tuned interfaces to support incremental match/unify (cf. versions in type.ML);
wenzelm
parents: 18011
diff changeset
   306
          if a=b then (typ_match thy (T,U) tyinsts, insts) else raise MATCH
4820
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   307
      | (Const (a,T), Const (b,U))  =>
18182
786d83044780 tuned interfaces to support incremental match/unify (cf. versions in type.ML);
wenzelm
parents: 18011
diff changeset
   308
          if a=b then (typ_match thy (T,U) tyinsts, insts) else raise MATCH
4820
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   309
      | (Bound i, Bound j)  =>  if  i=j  then  instsp  else raise MATCH
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   310
      | (Abs(_,T,t), Abs(_,U,u))  =>
63718
600cf5c8f2ba replaced the confusing int parameter by bool
nipkow
parents: 59026
diff changeset
   311
          mtch true (typ_match thy (T,U) tyinsts, insts) (t,u)
600cf5c8f2ba replaced the confusing int parameter by bool
nipkow
parents: 59026
diff changeset
   312
      | (f$t, g$u) => mtch inAbs (mtch inAbs instsp (f,g)) (t, u)
600cf5c8f2ba replaced the confusing int parameter by bool
nipkow
parents: 59026
diff changeset
   313
      | (t, Abs(_,U,u))  =>  mtch true instsp ((incr t)$(Bound 0), u)
4820
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   314
      | _ => raise MATCH
63718
600cf5c8f2ba replaced the confusing int parameter by bool
nipkow
parents: 59026
diff changeset
   315
  in fn tu => fn env => mtch false env tu end;
4820
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   316
8406
a217b0cd304d Type.unify and Type.typ_match now use Vartab instead of association lists.
berghofe
parents: 6539
diff changeset
   317
4820
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   318
(* Matching of higher-order patterns *)
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   319
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   320
fun match_bind(itms,binders,ixn,T,is,t) =
4820
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   321
  let val js = loose_bnos t
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   322
  in if null is
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   323
     then if null js then Vartab.update_new (ixn, (T, t)) itms else raise MATCH
33038
8f9594c31de4 dropped redundant gen_ prefix
haftmann
parents: 33037
diff changeset
   324
     else if subset (op =) (js, is)
4820
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   325
          then let val t' = if downto0(is,length binders - 1) then t
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   326
                            else mapbnd (idx is) t
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   327
               in Vartab.update_new (ixn, (T, mkabs (binders, is, t'))) itms end
4820
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   328
          else raise MATCH
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   329
  end;
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   330
18182
786d83044780 tuned interfaces to support incremental match/unify (cf. versions in type.ML);
wenzelm
parents: 18011
diff changeset
   331
fun match thy (po as (pat,obj)) envir =
678
6151b7f3b606 Modified pattern.ML to perform proper matching of Higher-Order Patterns.
nipkow
parents: 63
diff changeset
   332
let
4820
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   333
  (* Pre: pat and obj have same type *)
18182
786d83044780 tuned interfaces to support incremental match/unify (cf. versions in type.ML);
wenzelm
parents: 18011
diff changeset
   334
  fun mtch binders (pat,obj) (env as (iTs,itms)) =
4820
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   335
    case pat of
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   336
      Abs(ns,Ts,ts) =>
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   337
        (case obj of
18182
786d83044780 tuned interfaces to support incremental match/unify (cf. versions in type.ML);
wenzelm
parents: 18011
diff changeset
   338
           Abs(nt,Tt,tt) => mtch ((nt,Tt)::binders) (ts,tt) env
32035
8e77b6a250d5 tuned/modernized Envir.subst_XXX;
wenzelm
parents: 32032
diff changeset
   339
         | _ => let val Tt = Envir.subst_type iTs Ts
18182
786d83044780 tuned interfaces to support incremental match/unify (cf. versions in type.ML);
wenzelm
parents: 18011
diff changeset
   340
                in mtch((ns,Tt)::binders) (ts,(incr obj)$Bound(0)) env end)
4820
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   341
    | _ => (case obj of
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   342
              Abs(nt,Tt,tt) =>
18182
786d83044780 tuned interfaces to support incremental match/unify (cf. versions in type.ML);
wenzelm
parents: 18011
diff changeset
   343
                mtch((nt,Tt)::binders) ((incr pat)$Bound(0),tt) env
4820
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   344
            | _ => cases(binders,env,pat,obj))
678
6151b7f3b606 Modified pattern.ML to perform proper matching of Higher-Order Patterns.
nipkow
parents: 63
diff changeset
   345
4820
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   346
  and cases(binders,env as (iTs,itms),pat,obj) =
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   347
    let val (ph,pargs) = strip_comb pat
18182
786d83044780 tuned interfaces to support incremental match/unify (cf. versions in type.ML);
wenzelm
parents: 18011
diff changeset
   348
        fun rigrig1(iTs,oargs) = fold (mtch binders) (pargs~~oargs) (iTs,itms)
41067
c78a2d402736 eliminated some hard tabulators (deprecated);
wenzelm
parents: 40722
diff changeset
   349
          handle ListPair.UnequalLengths => raise MATCH
16668
fdb4992cf1d2 avoid polyeq;
wenzelm
parents: 16651
diff changeset
   350
        fun rigrig2((a:string,Ta),(b,Tb),oargs) =
fdb4992cf1d2 avoid polyeq;
wenzelm
parents: 16651
diff changeset
   351
              if a <> b then raise MATCH
18182
786d83044780 tuned interfaces to support incremental match/unify (cf. versions in type.ML);
wenzelm
parents: 18011
diff changeset
   352
              else rigrig1(typ_match thy (Ta,Tb) iTs, oargs)
4820
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   353
    in case ph of
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   354
         Var(ixn,T) =>
4820
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   355
           let val is = ints_of pargs
51700
c8f2bad67dbb tuned signature;
wenzelm
parents: 46857
diff changeset
   356
           in case Envir.lookup1 itms (ixn, T) of
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15574
diff changeset
   357
                NONE => (iTs,match_bind(itms,binders,ixn,T,is,obj))
52131
366fa32ee2a3 tuned signature;
wenzelm
parents: 52130
diff changeset
   358
              | SOME u => if Envir.aeconv (obj, red u is []) then env
4820
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   359
                          else raise MATCH
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   360
           end
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   361
       | _ =>
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   362
           let val (oh,oargs) = strip_comb obj
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   363
           in case (ph,oh) of
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   364
                (Const c,Const d) => rigrig2(c,d,oargs)
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   365
              | (Free f,Free g)   => rigrig2(f,g,oargs)
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   366
              | (Bound i,Bound j) => if i<>j then raise MATCH
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   367
                                     else rigrig1(iTs,oargs)
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   368
              | (Abs _, _)        => raise Pattern
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   369
              | (_, Abs _)        => raise Pattern
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   370
              | _                 => raise MATCH
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   371
           end
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   372
    end;
678
6151b7f3b606 Modified pattern.ML to perform proper matching of Higher-Order Patterns.
nipkow
parents: 63
diff changeset
   373
4820
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   374
  val pT = fastype_of pat
8f6dbbd8d497 Tried to speed up the rewriter by eta-contracting all patterns beforehand and
nipkow
parents: 4667
diff changeset
   375
  and oT = fastype_of obj
18182
786d83044780 tuned interfaces to support incremental match/unify (cf. versions in type.ML);
wenzelm
parents: 18011
diff changeset
   376
  val envir' = apfst (typ_match thy (pT, oT)) envir;
786d83044780 tuned interfaces to support incremental match/unify (cf. versions in type.ML);
wenzelm
parents: 18011
diff changeset
   377
in mtch [] po envir' handle Pattern => first_order_match thy po envir' end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   378
70443
a21a96eda033 clarified modules;
wenzelm
parents: 69575
diff changeset
   379
a21a96eda033 clarified modules;
wenzelm
parents: 69575
diff changeset
   380
fun pattern (Abs (_, _, t)) = pattern t
a21a96eda033 clarified modules;
wenzelm
parents: 69575
diff changeset
   381
  | pattern t =
a21a96eda033 clarified modules;
wenzelm
parents: 69575
diff changeset
   382
      let val (head, args) = strip_comb t in
a21a96eda033 clarified modules;
wenzelm
parents: 69575
diff changeset
   383
        if is_Var head then
a21a96eda033 clarified modules;
wenzelm
parents: 69575
diff changeset
   384
          forall is_Bound args andalso not (has_duplicates (op aconv) args)
a21a96eda033 clarified modules;
wenzelm
parents: 69575
diff changeset
   385
        else forall pattern args
a21a96eda033 clarified modules;
wenzelm
parents: 69575
diff changeset
   386
      end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   387
end;