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