Skip to content

Generalize Exists to hold non-Type-kinded types #14

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 2 commits into from
Jun 25, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Breaking changes:

New features:
- Added roles declarations to allow safe coercions (#9)
- Generalized `Exists` to hold non-`Type`-kinded types (#14)

Bugfixes:

Expand Down
4 changes: 2 additions & 2 deletions src/Data/Exists.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Data.Exists where

import Unsafe.Coerce (unsafeCoerce)

-- | This type constructor can be used to existentially quantify over a type of kind `Type`.
-- | This type constructor can be used to existentially quantify over a type.
-- |
-- | Specifically, the type `Exists f` is isomorphic to the existential type `exists a. f a`.
-- |
Expand All @@ -24,7 +24,7 @@ import Unsafe.Coerce (unsafeCoerce)
-- | ```purescript
-- | type Stream a = Exists (StreamF a)
-- | ```
foreign import data Exists :: (Type -> Type) -> Type
foreign import data Exists :: forall k. (k -> Type) -> Type

type role Exists representational

Expand Down
19 changes: 19 additions & 0 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,24 @@ head = runExists head'
head' :: forall s. StreamF a s -> a
head' (StreamF s f) = snd (f s)

data Maybe a = Nothing | Just a

count :: forall a. Maybe a -> Int
count Nothing = 0
count _ = 1

data FooF f = FooF (forall a. f a -> Int) (f String)

type Foo = Exists FooF

foo :: Foo
foo = mkExists $ FooF count (Just "test")

x :: Int
x = runExists runFooF foo
where
runFooF :: forall f. FooF f -> Int
runFooF (FooF f fStr) = f fStr

main :: Effect Unit
main = logShow $ head nats