+exception NotCompatible;;
+
+(* intersect_attributes is successful iff there is no attribute with *)
+(* two different values in the two lists. The returned list is the *)
+(* union of the two lists. *)
+let rec intersect_attributes (attr1, attr2) =
+ match attr1, attr2 with
+ [],_ -> attr2
+ | _,[] -> attr1
+ | (key1,value1)::tl1, (key2,_)::_ when key1 < key2 ->
+ (key1,value1)::(intersect_attributes (tl1,attr2))
+ | (key1,_)::_, (key2,value2)::tl2 when key2 < key1 ->
+ (key2,value2)::(intersect_attributes (attr1,tl2))
+ | entry1::tl1, entry2::tl2 when entry1 = entry2 ->
+ entry1::(intersect_attributes (tl1,tl2))
+ | _, _ -> raise NotCompatible (* same keys, different values *)
+;;
+
+
+let rec diff_ex l1 l2 =
+ let module S = Mathql_semantics in
+ match (l1, l2) with
+ [],_ -> []
+ | l,[] -> l
+ | {S.uri = uri1}::_, {S.uri = uri2}::tl2 when uri2 < uri1 ->
+ (diff_ex l1 tl2)
+ | {S.uri = uri1 ; S.attributes = attributes1}::tl1,
+ {S.uri = uri2}::_ when uri1 < uri2 ->
+ {S.uri = uri1 ; S.attributes = attributes1 ; S.extra = ""}::(diff_ex tl1 l2)
+ | {S.uri = uri1 ; S.attributes = attributes1}::tl1,
+ {S.uri = uri2 ; S.attributes = attributes2}::tl2 ->
+ try
+ let attributes' = intersect_attributes (attributes1, attributes2) in
+ diff_ex tl1 tl2
+ with
+ NotCompatible ->
+ {S.uri = uri1 ; S.attributes = attributes1 ; S.extra = ""}::(diff_ex tl1 tl2)