doclos :: Env -> Int -> Either Int CFormula
doclos = f 0
 where
  f ns [] i = Left (ns + i)
  f ns (Lift n : xs) i = f (ns + n) xs i
  f ns (Sub s : _) 0 = Right (lift ns s)
  f ns (Skip : _) 0 = Left ns
  f ns (Skip : xs) i = f (ns + 1) xs (i - 1)
  f ns (Sub _ : xs) i = f ns xs (i - 1)
doclos :: Env -> Int -> Either Int CFormula
doclos = f 0
 where
  f ns [] i = Left (ns + i)
  f ns (Lift n : xs) i = f (ns + n) xs i
  f ns (Sub s : _) 0 = Right (lift ns s)
  f ns (Skip : _) 0 = Left ns
  f ns (Skip : xs) i = f (ns + 1) xs (i - 1)
  f ns (Sub _ : xs) i = f ns xs (i - 1)
doclos = f 0
 where
  f ns [] i = Left (ns + i)
  f ns (Lift n : xs) i = f (ns + n) xs i
  f ns (Sub s : _) 0 = Right (lift ns s)
  f ns (Skip : _) 0 = Left ns
  f ns (Skip : xs) i = f (ns + 1) xs (i - 1)
  f ns (Sub _ : xs) i = f ns xs (i - 1)