Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mention requirement of reasonable Ord instance #9

Open
josephcsible opened this issue Oct 28, 2019 · 1 comment
Open

Mention requirement of reasonable Ord instance #9

josephcsible opened this issue Oct 28, 2019 · 1 comment

Comments

@josephcsible
Copy link

If a map's keys are Floats, Doubles, or some other type with an unreasonable Ord instance (not saying "unlawful" because Ord has no laws ☹️), then the guarantees provided by justified-containers don't hold. Example:

import Prelude hiding (lookup)
import Data.Map.Justified
import qualified Data.Map as M

test_table :: M.Map Float String
test_table = M.fromList [ (0, "foo"), (-1, "bar"), (0/0, "oops"), (-5, "baz"), (-6, "qux"), (-3, "quux") ]

main :: IO ()
main = withMap test_table $ \table -> do
  case member 0 table of
    Nothing -> putStrLn "Sorry, I couldn't prove that the key is present."
    Just key -> do
      putStrLn ("Found key: " ++ show key)
      putStrLn ("Value for key: " ++ lookup key table)
      inserting (0/0) "whoops" table $ \(newKey, upgrade, table') -> do
        putStrLn ("Value for key in updated map: " ++ lookup (upgrade key) table')
Found key: Key 0.0
Value for key: foo
Value for key in updated map: Main.hs: Data.Map.Justified has been subverted!
CallStack (from HasCallStack):
  error, called at src/Data/Map/Justified.hs:402:17 in justified-containers-0.3.0.0-QwQWf1ossWJ9BbDcxnzmi:Data.Map.Justified

There's obviously nothing justified-containers can really do about this, so I'd just like it to be mentioned in the documentation.

@Elvecent
Copy link

gdp package has Lawful constraint wrapper intended to mark "correct" instances. Of course, Double and Float aren't supposed to be Lawful Eq, and, therefore, Lawful Ord. But this is additional overhead, so...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants