let comp (s : t_subst) (v1 : term) (v2 : term) : int = 
  let rec f = function
    | (t1::l1,t2::l2) -> 
        ( match cmp (t1,t2) with 
            | 0 -> f (l1,l2) 
            | x -> x )
    | ([],[])              ->  0
    | ([],_)              -> -1
    | (_,[])              ->  1
  and cmp_exp (a,u) (b,v) = 
    match cmp (a,b) with
      | 0 -> if u=v then 0 else if u=Pos then -1 else 1
      | x -> x
  and g = function
    | (a1::l1, a2::l2) ->
            ( match cmp_exp a1 a2 with
                | 0 -> g (l1,l2)
                | x -> x                )
    | ([],[]) -> 0
    | ([], _) -> -1
    | (_, []) -> 1
  and cmp = function
    | (Var n1, Var n2) -> 
        if n1=n2 then 
          0 
        else 
          (match (valeur n1 s, valeur n2 s) with
             | (Var m1, Var m2) -> m1 - m2  
             | (Var(_),_) -> -1  
             | (_,Var(_)) ->  1  
             | (t1,t2) -> cmp (t1,t2) )
    | (Var n, t) -> 
        ( match valeur n s with  
            | Var(_) -> -1  
            | tt -> cmp (tt,t) )
    | (t, Var n) -> 
        ( match valeur n s with  
            | Var(_) ->  1  
            | tt -> cmp (t,tt) )
    | (PInv a, PInv b) -> cmp (a,b)
    | (Xor(l1), Xor(l2))     ->  f (l1,l2)  
    | (Xor(_), _)            -> -1  
    | (_,Xor(_))             ->  1    
    | (PInv _, _) -> -1
    | (_, PInv _) ->  1
    | (Atm n1, Atm n2) -> n1 - n2          
    | (Atm(_), _) -> -1    
    | (_, Atm(_)) ->  1
    | (PCrypt(m1,k1),PCrypt(m2,k2))     -> 
        ( match cmp (m1,m2) with 
            | 0 -> cmp (k1,k2) 
            | x -> x )
    | (PCrypt(_,_), _) -> -1               
    | (_, PCrypt(_,_)) ->  1
    | (SCrypt(m1,k1),SCrypt(m2,k2))     -> 
        ( match cmp (m1,m2) with 
            | 0 -> cmp (k1,k2) 
            | x -> x )
    | (SCrypt(_,_), _)       -> -1               
    | (_, SCrypt(_,_))       ->  1
    | (Uplet(l1), Uplet(l2)) ->  f (l1,l2)  
    | (Uplet(_), _)          -> -1
    | (_, Uplet(_))          ->  1
    | (Exp(t1,l1),Exp(t2,l2))           ->
            ( match cmp (t1,t2) with
                | 0 -> g (l1,l2)
                | x -> x                        )
  in 
    cmp (v1,v2)