Skip to content

Add/finish Ord instance for Records #180

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

Merged
merged 1 commit into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 36 additions & 34 deletions src/Data/Ord.purs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ module Data.Ord
, abs
, signum
, module Data.Ordering
, class OrdRecord, compareRecord
) where

import Data.Eq (class Eq, class Eq1)
import Data.Eq (class Eq, class Eq1, class EqRecord, (/=))
import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol)
import Data.Ord.Unsafe (unsafeCompare)
import Data.Ordering (Ordering(..))
import Data.Ring (class Ring, zero, one, negate)
import Data.Unit (Unit)
import Data.Void (Void)
import Prim.Row as Row
import Prim.RowList as RL
import Record.Unsafe (unsafeGet)
import Type.Data.RowList (RLProxy(..))

-- | The `Ord` type class represents types which support comparisons with a
-- | _total order_.
Expand Down Expand Up @@ -169,36 +175,32 @@ class Eq1 f <= Ord1 f where
instance ord1Array :: Ord1 Array where
compare1 = compare

-- Ordering for records is currently unimplemented as there are outstanding
-- questions around whether this implementation be useful. This is because it
-- prioritises the keys alphabetically, and this behaviour isn't overridable.
-- For now, we leave this unavailable, but the implementation is as follows:

-- class EqRecord rowlist row focus <= OrdRecord rowlist row focus | rowlist -> focus where
-- compareImpl :: RLProxy rowlist -> Record row -> Record row -> Ordering
--
-- instance ordRecordNil :: OrdRecord RL.Nil row focus where
-- compareImpl _ _ _ = EQ
--
-- instance ordRecordCons
-- :: ( OrdRecord rowlistTail row subfocus
-- , Row.Cons key focus rowTail row
-- , IsSymbol key
-- , Ord focus
-- )
-- => OrdRecord (RL.Cons key focus rowlistTail) row focus where
-- compareImpl _ ra rb
-- = if left /= EQ
-- then left
-- else compareImpl (RLProxy :: RLProxy rowlistTail) ra rb
-- where
-- key = reflectSymbol (SProxy :: SProxy key)
-- unsafeGet' = unsafeGet :: String -> Record row -> focus
-- left = unsafeGet' key ra `compare` unsafeGet' key rb
--
-- instance ordRecord
-- :: ( RL.RowToList row list
-- , OrdRecord list row focus
-- )
-- => Ord (Record row) where
-- compare = compareImpl (RLProxy :: RLProxy list)
class EqRecord rowlist row <= OrdRecord rowlist row where
compareRecord :: RLProxy rowlist -> Record row -> Record row -> Ordering

instance ordRecordNil :: OrdRecord RL.Nil row where
compareRecord _ _ _ = EQ

instance ordRecordCons
:: ( OrdRecord rowlistTail row
, Row.Cons key focus rowTail row
, IsSymbol key
, Ord focus
)
=> OrdRecord (RL.Cons key focus rowlistTail) row where
compareRecord _ ra rb
= if left /= EQ
then left
else compareRecord (RLProxy :: RLProxy rowlistTail) ra rb
where
key = reflectSymbol (SProxy :: SProxy key)
unsafeGet' = unsafeGet :: String -> Record row -> focus
left = unsafeGet' key ra `compare` unsafeGet' key rb

instance ordRecord
:: ( RL.RowToList row list
, OrdRecord list row
)
=> Ord (Record row) where
compare = compareRecord (RLProxy :: RLProxy list)

6 changes: 6 additions & 0 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,9 @@ testRecordInstances = do
{ a: true, b: false, c: true, d: false }
{ a: true, b: true, c: false, d: false }
== { a: true, b: true, c: false, d: true }
testOrd { a: 0, b: "hello" } { a: 42, b: "hello" } LT
testOrd { a: 42, b: "hello" } { a: 0, b: "hello" } GT
testOrd { a: 42, b: "hello" } { a: 42, b: "hello" } EQ
testOrd { a: 42, b: "hell" } { a: 42, b: "hello" } LT
testOrd { a: 42, b: "hello" } { a: 42, b: "hell" } GT