let compare_terms handle_error =
  let rec compare_terms_rec ter1 ter2 =
    match (ter1,ter2) with
      (_,Base(Var(n2)))
    | (_,Base(Const(n2))) ->
        handle_error
          (fun _ ->
            try compare_types ter1 (Globals.type_table#get_type n2)
            with
               Not_found -> raise (Invalid_build "4")
            | Invalid_argument _ -> raise (Invalid_build "2"))
          ()
    | (Inv(a1),Inv(a2)) ->
        compare_terms_rec a1 a2
    | (Crypt(a1,b1),Crypt(a2,b2))
    | (Scrypt(a1,b1),Scrypt(a2,b2))
    | (Cons(a1,b1),Cons(a2,b2))
    | (Pair(a1,b1),Pair(a2,b2)) ->
        compare_terms_rec a1 a2;
        compare_terms_rec b1 b2        
    | (Function(a1,l1),Function(a2,l2)) ->
        compare_terms_rec a1 a2;
        List.iter2 compare_terms_rec l1 l2
(*    | (Lst(l1),Lst(l2))*)
    | (Set(l1),Set(l2)) ->
        List.iter2 compare_terms_rec l1 l2
    | _ -> handle_error (fun _ -> raise (Invalid_build "1")) ()
  in
  compare_terms_rec