let inter s t =
let rec loop ans s t =
match (s,t) with
| (_, []) -> ans
| ([], _) -> ans
| ((u::s as ul), (v::t as vl)) ->
if u.Range.lo > v.Range.hi then
loop ans ul t
else if u.Range.hi < v.Range.lo then
loop ans s vl
else
match Range.intersect u v with
| None -> invalid_arg "impossible to get here"
| Some w ->
match Pervasives.compare u.Range.hi v.Range.hi with
| -1 -> loop (w::ans) s vl
| 0 -> loop (w::ans) s t
| 1 -> loop (w::ans) ul t
| _ -> invalid_arg "impossible to get here"
in
to_canonical (loop [] s t)