Skip to content

Add Bounded instance for records #208

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
Sep 17, 2020
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
47 changes: 46 additions & 1 deletion src/Data/Bounded.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ module Data.Bounded
, bottom
, top
, module Data.Ord
, class BoundedRecord, bottomRecord, topRecord
) where

import Data.Ord (class Ord, Ordering(..), compare, (<), (<=), (>), (>=))
import Data.Ord (class Ord, class OrdRecord, Ordering(..), compare, (<), (<=), (>), (>=))
import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol)
import Data.Unit (Unit, unit)
import Prim.Row as Row
import Prim.RowList as RL
import Record.Unsafe (unsafeSet)
import Type.Data.Row (RProxy(..))
import Type.Data.RowList (RLProxy(..))

-- | The `Bounded` type class represents totally ordered types that have an
-- | upper and lower boundary.
Expand Down Expand Up @@ -54,3 +61,41 @@ foreign import bottomNumber :: Number
instance boundedNumber :: Bounded Number where
top = topNumber
bottom = bottomNumber

class OrdRecord rowlist row <= BoundedRecord rowlist row subrow | rowlist -> subrow where
topRecord :: RLProxy rowlist -> RProxy row -> Record subrow
bottomRecord :: RLProxy rowlist -> RProxy row -> Record subrow

instance boundedRecordNil :: BoundedRecord RL.Nil row () where
topRecord _ _ = {}
bottomRecord _ _ = {}

instance boundedRecordCons
:: ( IsSymbol key
, Bounded focus
, Row.Cons key focus rowTail row
, Row.Cons key focus subrowTail subrow
, BoundedRecord rowlistTail row subrowTail
)
=> BoundedRecord (RL.Cons key focus rowlistTail) row subrow where
topRecord _ rowProxy
= insert top tail
where
key = reflectSymbol (SProxy :: SProxy key)
insert = unsafeSet key :: focus -> Record subrowTail -> Record subrow
tail = topRecord (RLProxy :: RLProxy rowlistTail) rowProxy

bottomRecord _ rowProxy
= insert bottom tail
where
key = reflectSymbol (SProxy :: SProxy key)
insert = unsafeSet key :: focus -> Record subrowTail -> Record subrow
tail = bottomRecord (RLProxy :: RLProxy rowlistTail) rowProxy

instance boundedRecord
:: ( RL.RowToList row list
, BoundedRecord list row row
)
=> Bounded (Record row) where
top = topRecord (RLProxy :: RLProxy list) (RProxy :: RProxy row)
bottom = bottomRecord (RLProxy :: RLProxy list) (RProxy :: RProxy row)
7 changes: 6 additions & 1 deletion test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,9 @@ testRecordInstances = do
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

assert "Record bottom" $
(bottom :: { a :: Boolean }).a
== bottom
assert "Record top" $
(top :: { a :: Boolean }).a
== top