├── .github ├── PULL_REQUEST_TEMPLATE.md └── workflows │ └── ci.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── bower.json ├── package.json ├── src └── Data │ └── Exists.purs └── test └── Test └── Main.purs /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | **Description of the change** 2 | 3 | Clearly and concisely describe the purpose of the pull request. If this PR relates to an existing issue or change proposal, please link to it. Include any other background context that would help reviewers understand the motivation for this PR. 4 | 5 | --- 6 | 7 | **Checklist:** 8 | 9 | - [ ] Added the change to the changelog's "Unreleased" section with a reference to this PR (e.g. "- Made a change (#0000)") 10 | - [ ] Linked any existing issues or proposals that this pull request should close 11 | - [ ] Updated or added relevant documentation 12 | - [ ] Added a test for the contribution (if applicable) 13 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [master] 6 | pull_request: 7 | branches: [master] 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v2 14 | 15 | - uses: purescript-contrib/setup-purescript@main 16 | with: 17 | purescript: "unstable" 18 | 19 | - uses: actions/setup-node@v2 20 | with: 21 | node-version: "14.x" 22 | 23 | - name: Install dependencies 24 | run: | 25 | npm install -g bower 26 | npm install 27 | bower install --production 28 | 29 | - name: Build source 30 | run: npm run-script build 31 | 32 | - name: Run tests 33 | run: | 34 | bower install 35 | npm run-script test --if-present 36 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.* 2 | !/.gitignore 3 | !/.github/ 4 | /bower_components/ 5 | /node_modules/ 6 | /output/ 7 | package-lock.json 8 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | Notable changes to this project are documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 4 | 5 | ## [Unreleased] 6 | 7 | Breaking changes: 8 | 9 | New features: 10 | 11 | Bugfixes: 12 | 13 | Other improvements: 14 | 15 | ## [v6.0.0](https://github.com/purescript/purescript-exists/releases/tag/v6.0.0) - 2022-04-27 16 | 17 | Breaking changes: 18 | - Update project and deps to PureScript v0.15.0 (#17 by @JordanMartinez) 19 | 20 | New features: 21 | 22 | Bugfixes: 23 | 24 | Other improvements: 25 | 26 | ## [v5.1.0](https://github.com/purescript/purescript-exists/releases/tag/v5.1.0) - 2021-06-25 27 | 28 | New features: 29 | - Generalized `Exists` to hold non-`Type`-kinded types (#14 by @rhendric) 30 | 31 | ## [v5.0.0](https://github.com/purescript/purescript-exists/releases/tag/v5.0.0) - 2021-02-26 32 | 33 | Breaking changes: 34 | - Added support for PureScript 0.14 and dropped support for all previous versions (#10) 35 | 36 | New features: 37 | - Added roles declarations to allow safe coercions (#9) 38 | 39 | Other improvements: 40 | - Migrated CI to GitHub Actions and updated installation instructions to use Spago (#11) 41 | - Added a CHANGELOG.md file and pull request template (#12, #13) 42 | 43 | ## [v4.0.0](https://github.com/purescript/purescript-exists/releases/tag/v4.0.0) - 2018-05-23 44 | 45 | Updated for PureScript 0.12 46 | 47 | ## [v3.0.0](https://github.com/purescript/purescript-exists/releases/tag/v3.0.0) - 2017-03-26 48 | 49 | - Updated for PureScript 0.11 50 | 51 | ## [v2.0.0](https://github.com/purescript/purescript-exists/releases/tag/v2.0.0) - 2016-10-03 52 | 53 | - Updated dependencies 54 | 55 | ## [v1.0.0](https://github.com/purescript/purescript-exists/releases/tag/v1.0.0) - 2016-06-01 56 | 57 | This release is intended for the PureScript 0.9.1 compiler and newer. 58 | 59 | **Note**: The v1.0.0 tag is not meant to indicate the library is “finished”, the core libraries are all being bumped to this for the 0.9 compiler release so as to use semver more correctly. 60 | 61 | ## [v1.0.0-rc.1](https://github.com/purescript/purescript-exists/releases/tag/v1.0.0-rc.1) - 2016-03-17 62 | 63 | - Release candidate for the psc 0.8+ core libraries 64 | 65 | ## [v0.2.0](https://github.com/purescript/purescript-exists/releases/tag/v0.2.0) - 2015-06-30 66 | 67 | This release works with versions 0.7.\* of the PureScript compiler. It will not work with older versions. If you are using an older version, you should require an older, compatible version of this library. 68 | 69 | ## [v0.2.0-rc.1](https://github.com/purescript/purescript-exists/releases/tag/v0.2.0-rc.1) - 2015-06-07 70 | 71 | Initial release candidate of the library intended for the 0.7 compiler. 72 | 73 | ## [v0.1.1](https://github.com/purescript/purescript-exists/releases/tag/v0.1.1) - 2015-02-24 74 | 75 | Update docs 76 | 77 | ## [v0.1.0](https://github.com/purescript/purescript-exists/releases/tag/v0.1.0) - 2014-11-05 78 | 79 | Initial versioned release. 80 | 81 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2018 PureScript 2 | 3 | Redistribution and use in source and binary forms, with or without modification, 4 | are permitted provided that the following conditions are met: 5 | 6 | 1. Redistributions of source code must retain the above copyright notice, this 7 | list of conditions and the following disclaimer. 8 | 9 | 2. Redistributions in binary form must reproduce the above copyright notice, 10 | this list of conditions and the following disclaimer in the documentation and/or 11 | other materials provided with the distribution. 12 | 13 | 3. Neither the name of the copyright holder nor the names of its contributors 14 | may be used to endorse or promote products derived from this software without 15 | specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 21 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 24 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # purescript-exists 2 | 3 | [![Latest release](http://img.shields.io/github/release/purescript/purescript-exists.svg)](https://github.com/purescript/purescript-exists/releases) 4 | [![Build status](https://github.com/purescript/purescript-exists/workflows/CI/badge.svg?branch=master)](https://github.com/purescript/purescript-exists/actions?query=workflow%3ACI+branch%3Amaster) 5 | [![Pursuit](https://pursuit.purescript.org/packages/purescript-exists/badge)](https://pursuit.purescript.org/packages/purescript-exists) 6 | 7 | The `Exists` type, for encoding existential types. 8 | 9 | ## Installation 10 | 11 | ``` 12 | spago install exists 13 | ``` 14 | 15 | ## Overview 16 | 17 | The type `Exists f` is isomorphic to the existential type `exists a. f a`. 18 | 19 | For example, consider the type `exists s. Tuple s (s -> Tuple s a)` which represents infinite streams of elements of type `a`. 20 | 21 | This type can be constructed by creating a type constructor `StreamF` as follows: 22 | 23 | ```purescript 24 | data StreamF a s = StreamF s (s -> Tuple s a) 25 | ``` 26 | 27 | We can then define the type of streams using `Exists`: 28 | 29 | ```purescript 30 | type Stream a = Exists (StreamF a) 31 | ``` 32 | 33 | The `mkExists` and `runExists` functions then enable packing and unpacking of `SteamF` into/out of `Stream`. 34 | 35 | ## Documentation 36 | 37 | Module documentation is [published on Pursuit](http://pursuit.purescript.org/packages/purescript-exists). 38 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "purescript-exists", 3 | "homepage": "https://github.com/purescript/purescript-exists", 4 | "description": "Existential types as a library", 5 | "license": "BSD-3-Clause", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/purescript/purescript-exists.git" 9 | }, 10 | "ignore": [ 11 | "**/.*", 12 | "bower_components", 13 | "node_modules", 14 | "output", 15 | "test", 16 | "bower.json", 17 | "package.json" 18 | ], 19 | "dependencies": { 20 | "purescript-unsafe-coerce": "^6.0.0" 21 | }, 22 | "devDependencies": { 23 | "purescript-console": "^6.0.0" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "scripts": { 4 | "clean": "rimraf output && rimraf .pulp-cache", 5 | "build": "pulp build -- --censor-lib --strict", 6 | "test": "pulp test" 7 | }, 8 | "devDependencies": { 9 | "pulp": "16.0.0-0", 10 | "purescript-psa": "^0.8.2", 11 | "rimraf": "^3.0.2" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/Data/Exists.purs: -------------------------------------------------------------------------------- 1 | module Data.Exists where 2 | 3 | import Unsafe.Coerce (unsafeCoerce) 4 | 5 | -- | This type constructor can be used to existentially quantify over a type. 6 | -- | 7 | -- | Specifically, the type `Exists f` is isomorphic to the existential type `exists a. f a`. 8 | -- | 9 | -- | Existential types can be encoded using universal types (`forall`) for endofunctors in more general 10 | -- | categories. The benefit of this library is that, by using the FFI, we can create an efficient 11 | -- | representation of the existential by simply hiding type information. 12 | -- | 13 | -- | For example, consider the type `exists s. Tuple s (s -> Tuple s a)` which represents infinite streams 14 | -- | of elements of type `a`. 15 | -- | 16 | -- | This type can be constructed by creating a type constructor `StreamF` as follows: 17 | -- | 18 | -- | ```purescript 19 | -- | data StreamF a s = StreamF s (s -> Tuple s a) 20 | -- | ``` 21 | -- | 22 | -- | We can then define the type of streams using `Exists`: 23 | -- | 24 | -- | ```purescript 25 | -- | type Stream a = Exists (StreamF a) 26 | -- | ``` 27 | foreign import data Exists :: forall k. (k -> Type) -> Type 28 | 29 | type role Exists representational 30 | 31 | -- | The `mkExists` function is used to introduce a value of type `Exists f`, by providing a value of 32 | -- | type `f a`, for some type `a` which will be hidden in the existentially-quantified type. 33 | -- | 34 | -- | For example, to create a value of type `Stream Number`, we might use `mkExists` as follows: 35 | -- | 36 | -- | ```purescript 37 | -- | nats :: Stream Number 38 | -- | nats = mkExists $ StreamF 0 (\n -> Tuple (n + 1) n) 39 | -- | ``` 40 | mkExists :: forall f a. f a -> Exists f 41 | mkExists = unsafeCoerce 42 | 43 | -- | The `runExists` function is used to eliminate a value of type `Exists f`. The rank 2 type ensures 44 | -- | that the existentially-quantified type does not escape its scope. Since the function is required 45 | -- | to work for _any_ type `a`, it will work for the existentially-quantified type. 46 | -- | 47 | -- | For example, we can write a function to obtain the head of a stream by using `runExists` as follows: 48 | -- | 49 | -- | ```purescript 50 | -- | head :: forall a. Stream a -> a 51 | -- | head = runExists head' 52 | -- | where 53 | -- | head' :: forall s. StreamF a s -> a 54 | -- | head' (StreamF s f) = snd (f s) 55 | -- | ``` 56 | runExists :: forall f r. (forall a. f a -> r) -> Exists f -> r 57 | runExists = unsafeCoerce 58 | -------------------------------------------------------------------------------- /test/Test/Main.purs: -------------------------------------------------------------------------------- 1 | module Test.Main where 2 | 3 | import Prelude 4 | 5 | import Effect (Effect) 6 | import Effect.Console (logShow) 7 | 8 | import Data.Exists (Exists, mkExists, runExists) 9 | 10 | data Tuple a b = Tuple a b 11 | 12 | snd :: forall a b. Tuple a b -> b 13 | snd (Tuple _ b) = b 14 | 15 | data StreamF a s = StreamF s (s -> Tuple s a) 16 | 17 | type Stream a = Exists (StreamF a) 18 | 19 | nats :: Stream Int 20 | nats = mkExists $ StreamF 0 (\n -> Tuple (n + 1) n) 21 | 22 | head :: forall a. Stream a -> a 23 | head = runExists head' 24 | where 25 | head' :: forall s. StreamF a s -> a 26 | head' (StreamF s f) = snd (f s) 27 | 28 | data Maybe a = Nothing | Just a 29 | 30 | count :: forall a. Maybe a -> Int 31 | count Nothing = 0 32 | count _ = 1 33 | 34 | data FooF f = FooF (forall a. f a -> Int) (f String) 35 | 36 | type Foo = Exists FooF 37 | 38 | foo :: Foo 39 | foo = mkExists $ FooF count (Just "test") 40 | 41 | x :: Int 42 | x = runExists runFooF foo 43 | where 44 | runFooF :: forall f. FooF f -> Int 45 | runFooF (FooF f fStr) = f fStr 46 | 47 | main :: Effect Unit 48 | main = logShow $ head nats 49 | --------------------------------------------------------------------------------