Skip to content

Use leibniz equality #9

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

Closed
wants to merge 3 commits into from
Closed
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
26 changes: 22 additions & 4 deletions src/Type/Equality.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Type.Equality
( class TypeEquals
, to
, from
, leibniz
, leibnizOp
) where

-- | This type class asserts that types `a` and `b`
Expand All @@ -14,10 +16,26 @@ module Type.Equality
-- | Note: any instance will necessarily overlap with
-- | `refl` below, so instances of this class should
-- | not be defined in libraries.
class TypeEquals :: forall k. k -> k -> Constraint
class TypeEquals a b | a -> b, b -> a where
to :: a -> b
from :: b -> a
leibniz :: forall p. p a -> p b
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about proof or typeEquals?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like proof for this, yeah


instance refl :: TypeEquals a a where
to a = a
from a = a
leibniz a = a

newtype Op :: forall k. k -> k -> Type
newtype Op a b = Op (forall p. p b -> p a)

leibnizOp :: forall p a b. TypeEquals a b => p b -> p a
leibnizOp = case leibniz (Op (\pa -> pa) :: Op a a) of
(Op f :: Op a b) -> f

newtype To a = To a

to :: forall a b. TypeEquals a b => a -> b
to a = case leibniz (To a) of
To b -> b
Comment on lines +36 to +37
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The To newtype is perhaps better for readability but we could define to as leibniz \a -> a.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I missed that that was possible.


from :: forall a b. TypeEquals a b => b -> a
from b = case leibnizOp (To b) of
To a -> a
Comment on lines +40 to +41
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is leibnizOp really necessary? Given Data.Op.Op we could define from as case leibniz (Op \b -> b) of Op f -> f.

If we don’t want any dependency newtype Op a b = Op (b -> a) is also perhaps a bit simpler than newtype Op a b = Op (forall p. p b -> p a) and could rename it to Symm or something more semantic for this use case (the reversal of a leibniz equality).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having the symmetric/opposite form of Leibniz in this place is good, because sometimes you’ll need both directions (just for easier access) and you don’t want to have to add another constraint. It’s just one newtype that only needs to be used internally.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would prefer not to export leibnizOp at first; if people want to flip it around they can use the leibniz library, right? If we’re saying this is only the basic stuff and people should use the leibniz library for more involved usage, then to me that seems consistent with not exporting leibnizOp.

7 changes: 5 additions & 2 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Prelude
import Effect (Effect)
import Effect.Console (log)
import Data.Newtype (class Newtype, unwrap)
import Type.Equality (class TypeEquals, to, from)
import Type.Equality (class TypeEquals, leibniz, to, from)

newtype RecordNewtype = RecordNewtype
{ message :: String }
Expand All @@ -15,5 +15,8 @@ instance newtypeRecordNewtype ::
wrap = RecordNewtype <<< to
unwrap (RecordNewtype rec) = from rec

test2 :: forall ty row. TypeEquals row ( message :: String ) => Newtype ty (Record row) => ty -> String
test2 = unwrap >>> leibniz >>> _.message

main :: Effect Unit
main = log (unwrap (RecordNewtype { message: "Done" })).message
main = log (test2 (RecordNewtype { message: "Done" }))