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