loop :: Int -> Int -> Int -> Int
loop twit l u 
   | u <= l    = l
   | otherwise = case compare (choose 0) 0 of
                   LT -> loop (u - (k + 1)) (k+1) u 
                   EQ -> k
                   GT -> loop (k - l) l     k 
  where k = (u + l) `shiftRI` 1

{-@ loop1 :: l:Nat -> u:{v:Nat | l <= v} -> Int / [u - l] @-}
loop :: Int -> Int -> Int -> Int
loop twit l u 
   | u <= l    = l
   | otherwise = case compare (choose 0) 0 of
                   LT -> loop (u - (k + 1)) (k+1) u 
                   EQ -> k
                   GT -> loop (k - l) l     k 
  where k = (u + l) `shiftRI` 1

{-@ loop1 :: l:Nat -> u:{v:Nat | l <= v} -> Int / [u - l] @-}
loop twit l u 
   | u <= l    = l
   | otherwise = case compare (choose 0) 0 of
                   LT -> loop (u - (k + 1)) (k+1) u 
                   EQ -> k
                   GT -> loop (k - l) l     k 
  where k = (u + l) `shiftRI` 1

{-@ loop1 :: l:Nat -> u:{v:Nat | l <= v} -> Int / [u - l] @-}