let uniq = fun ?(sorted = true) list ->
  let list =
    if sorted then list else List.sort (Pervasives.compare) list
  in
    match list with
      | [] -> []
      | hd :: tl ->
          let rec uniq = fun x -> function
            | [] -> [x]
            | hd :: tl when hd = x -> uniq x tl
            | hd :: tl when hd > x -> x :: (uniq hd tl)
            | _ ->
                invalid_arg "list is not sorted"
          in
            uniq hd tl