├── .gitignore ├── packages.dhall ├── .tidyrc.json ├── test └── Test │ ├── Main.purs │ └── Example │ ├── Expr.purs │ └── List.purs ├── .editorconfig ├── .github ├── ISSUE_TEMPLATE │ ├── config.yml │ ├── bug-report.md │ └── change-request.md ├── PULL_REQUEST_TEMPLATE.md └── workflows │ └── ci.yml ├── spago.dhall ├── CONTRIBUTING.md ├── bower.json ├── docs └── README.md ├── src ├── Matryoshka │ ├── Util.purs │ ├── Algebra.purs │ ├── Coalgebra.purs │ ├── Transform.purs │ ├── Class │ │ ├── Recursive.purs │ │ └── Corecursive.purs │ ├── Pattern │ │ └── CoEnvT.purs │ ├── Refold.purs │ ├── DistributiveLaw.purs │ ├── Unfold.purs │ └── Fold.purs └── Matryoshka.purs ├── CHANGELOG.md ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | .* 2 | !.gitignore 3 | !.github 4 | !.editorconfig 5 | !.tidyrc.json 6 | 7 | output 8 | generated-docs 9 | bower_components 10 | -------------------------------------------------------------------------------- /packages.dhall: -------------------------------------------------------------------------------- 1 | let upstream = 2 | https://raw.githubusercontent.com/purescript/package-sets/prepare-0.15/src/packages.dhall 3 | 4 | in upstream 5 | -------------------------------------------------------------------------------- /.tidyrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "importSort": "source", 3 | "importWrap": "source", 4 | "indent": 2, 5 | "operatorsFile": null, 6 | "ribbon": 1, 7 | "typeArrowPlacement": "first", 8 | "unicode": "never", 9 | "width": null 10 | } 11 | -------------------------------------------------------------------------------- /test/Test/Main.purs: -------------------------------------------------------------------------------- 1 | module Test.Main where 2 | 3 | import Prelude 4 | import Effect (Effect) 5 | import Test.Example.Expr (exprExample) 6 | import Test.Example.List (listExample) 7 | 8 | main :: Effect Unit 9 | main = do 10 | exprExample 11 | listExample 12 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # https://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | contact_links: 3 | - name: PureScript Discourse 4 | url: https://discourse.purescript.org/ 5 | about: Ask and answer questions on the PureScript discussion forum. 6 | - name: PureScript Discord 7 | url: https://purescript.org/chat 8 | about: Ask and answer questions on the PureScript chat. 9 | -------------------------------------------------------------------------------- /spago.dhall: -------------------------------------------------------------------------------- 1 | { name = "matryoshka" 2 | , dependencies = 3 | [ "bifunctors" 4 | , "console" 5 | , "control" 6 | , "distributive" 7 | , "effect" 8 | , "either" 9 | , "fixed-points" 10 | , "foldable-traversable" 11 | , "free" 12 | , "identity" 13 | , "lists" 14 | , "newtype" 15 | , "prelude" 16 | , "profunctor" 17 | , "transformers" 18 | , "tuples" 19 | ] 20 | , packages = ./packages.dhall 21 | , sources = [ "src/**/*.purs", "test/**/*.purs" ] 22 | } 23 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to Matryoshka 2 | 3 | Thanks for your interest in contributing to `matryoshka`! We welcome new contributions regardless of your level of experience or familiarity with PureScript. 4 | 5 | Every library in the Contributors organization shares a simple handbook that helps new contributors get started. With that in mind, please [read the short contributing guide on purescript-contrib/governance](https://github.com/purescript-contrib/governance/blob/main/contributing.md) before contributing to this library. 6 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug-report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Report an issue 4 | title: "" 5 | labels: bug 6 | assignees: "" 7 | --- 8 | 9 | **Describe the bug** 10 | A clear and concise description of the bug. 11 | 12 | **To Reproduce** 13 | A minimal code example (preferably a runnable example on [Try PureScript](https://try.purescript.org)!) or steps to reproduce the issue. 14 | 15 | **Expected behavior** 16 | A clear and concise description of what you expected to happen. 17 | 18 | **Additional context** 19 | Add any other context about the problem here. 20 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/change-request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Change request 3 | about: Propose an improvement to this library 4 | title: "" 5 | labels: "" 6 | assignees: "" 7 | --- 8 | 9 | **Is your change request related to a problem? Please describe.** 10 | A clear and concise description of the problem. 11 | 12 | Examples: 13 | 14 | - It's frustrating to have to [...] 15 | - I was looking for a function to [...] 16 | 17 | **Describe the solution you'd like** 18 | A clear and concise description of what a good solution to you looks like, including any solutions you've already considered. 19 | 20 | **Additional context** 21 | Add any other context about the change request here. 22 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | **Description of the change** 2 | 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. 3 | 4 | --- 5 | 6 | **Checklist:** 7 | 8 | - [ ] Added the change to the changelog's "Unreleased" section with a link to this PR and your username 9 | - [ ] Linked any existing issues or proposals that this pull request should close 10 | - [ ] Updated or added relevant documentation in the README and/or documentation directory 11 | - [ ] Added a test for the contribution (if applicable) 12 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "purescript-matryoshka", 3 | "homepage": "https://github.com/purescript-contrib/purescript-matryoshka", 4 | "license": "Apache-2.0", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/purescript-contrib/purescript-matryoshka.git" 8 | }, 9 | "ignore": [ 10 | "**/.*", 11 | "bower_components", 12 | "node_modules", 13 | "output", 14 | "bower.json", 15 | "package.json" 16 | ], 17 | "dependencies": { 18 | "purescript-fixed-points": "^7.0.0", 19 | "purescript-free": "^7.0.0", 20 | "purescript-prelude": "^6.0.0", 21 | "purescript-profunctor": "^6.0.0", 22 | "purescript-transformers": "^6.0.0" 23 | }, 24 | "devDependencies": { 25 | "purescript-console": "^6.0.0", 26 | "purescript-effect": "^4.0.0" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | # Matryoshka Documentation 2 | 3 | This directory contains documentation for `matryoshka`. If you are interested in contributing new documentation, please read the [contributor guidelines](../CONTRIBUTING.md) and [What Nobody Tells You About Documentation](https://documentation.divio.com) for help getting started. 4 | 5 | Real documentation coming soon, for now refer to the [original Matryoshka docs](https://github.com/precog/matryoshka/blob/main/README.md) for an explanation of the general concepts! 6 | A port of the example used in these docs is available [here](https://github.com/purescript-contrib/purescript-matryoshka/blob/main/test/Test/Example/Expr.purs). 7 | A simple example showing the use of `cata`, `ana` and `hylo` is [here](https://github.com/purescript-contrib/purescript-matryoshka/blob/main/test/Test/Example/List.purs). 8 | -------------------------------------------------------------------------------- /test/Test/Example/Expr.purs: -------------------------------------------------------------------------------- 1 | module Test.Example.Expr where 2 | 3 | import Effect (Effect) 4 | import Effect.Console (log, logShow) 5 | import Data.Functor.Mu (Mu) 6 | import Matryoshka (class Corecursive, class Recursive, Algebra, cata, embed) 7 | import Prelude hiding (mul) 8 | 9 | data ExprF a = Num Int | Mul a a 10 | 11 | derive instance functorExprF :: Functor ExprF 12 | 13 | eval :: Algebra ExprF Int 14 | eval (Num i) = i 15 | eval (Mul i j) = i * j 16 | 17 | evalExpr :: forall t. Recursive t ExprF => t -> Int 18 | evalExpr = cata eval 19 | 20 | num :: forall t. Corecursive t ExprF => Int -> t 21 | num i = embed (Num i) 22 | 23 | mul :: forall t. Corecursive t ExprF => t -> t -> t 24 | mul i j = embed (Mul i j) 25 | 26 | someExpr :: forall t. Corecursive t ExprF => t 27 | someExpr = mul (num 2) (mul (num 3) (num 4)) 28 | 29 | type Expr = Mu ExprF 30 | 31 | exprExample :: Effect Unit 32 | exprExample = do 33 | log "expr example" 34 | logShow $ evalExpr (someExpr :: Expr) 35 | -------------------------------------------------------------------------------- /src/Matryoshka/Util.purs: -------------------------------------------------------------------------------- 1 | {- 2 | Copyright 2016 SlamData, Inc. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | -} 16 | 17 | module Matryoshka.Util where 18 | 19 | import Prelude 20 | 21 | import Matryoshka.Class.Corecursive (class Corecursive, embed) 22 | import Matryoshka.Class.Recursive (class Recursive, project) 23 | 24 | mapR :: forall t f u g. Recursive t f => Corecursive u g => (f t -> g u) -> t -> u 25 | mapR f = embed <<< f <<< project 26 | 27 | traverseR 28 | :: forall t f u g m 29 | . Recursive t f 30 | => Corecursive u g 31 | => Functor m 32 | => (f t -> m (g u)) 33 | -> t 34 | -> m u 35 | traverseR f = map embed <<< f <<< project 36 | -------------------------------------------------------------------------------- /src/Matryoshka/Algebra.purs: -------------------------------------------------------------------------------- 1 | {- 2 | Copyright 2016 SlamData, Inc. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | -} 16 | 17 | module Matryoshka.Algebra where 18 | 19 | type GAlgebra :: (Type -> Type) -> (Type -> Type) -> Type -> Type 20 | type GAlgebra w f a = f (w a) -> a 21 | 22 | type GAlgebraM :: (Type -> Type) -> (Type -> Type) -> (Type -> Type) -> Type -> Type 23 | type GAlgebraM w m f a = f (w a) -> m a 24 | 25 | type Algebra f a = f a -> a 26 | 27 | type AlgebraM :: (Type -> Type) -> (Type -> Type) -> Type -> Type 28 | type AlgebraM m f a = f a -> m a 29 | 30 | type ElgotAlgebra :: (Type -> Type) -> (Type -> Type) -> Type -> Type 31 | type ElgotAlgebra w f a = w (f a) -> a 32 | -------------------------------------------------------------------------------- /src/Matryoshka/Coalgebra.purs: -------------------------------------------------------------------------------- 1 | {- 2 | Copyright 2016 SlamData, Inc. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | -} 16 | 17 | module Matryoshka.Coalgebra where 18 | 19 | type GCoalgebra :: (Type -> Type) -> (Type -> Type) -> Type -> Type 20 | type GCoalgebra n f a = a -> f (n a) 21 | 22 | type GCoalgebraM :: (Type -> Type) -> (Type -> Type) -> (Type -> Type) -> Type -> Type 23 | type GCoalgebraM n m f a = a -> m (f (n a)) 24 | 25 | type Coalgebra f a = a -> f a 26 | 27 | type CoalgebraM :: (Type -> Type) -> (Type -> Type) -> Type -> Type 28 | type CoalgebraM m f a = a -> m (f a) 29 | 30 | type ElgotCoalgebra :: (Type -> Type) -> (Type -> Type) -> Type -> Type 31 | type ElgotCoalgebra e f a = a -> e (f a) 32 | -------------------------------------------------------------------------------- /src/Matryoshka/Transform.purs: -------------------------------------------------------------------------------- 1 | {- 2 | Copyright 2016 SlamData, Inc. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | -} 16 | 17 | module Matryoshka.Transform where 18 | 19 | type Transform :: Type -> (Type -> Type) -> (Type -> Type) -> Type 20 | type Transform t f g = f t -> g t 21 | 22 | type TransformM :: (Type -> Type) -> Type -> (Type -> Type) -> (Type -> Type) -> Type 23 | type TransformM m t f g = f t -> m (g t) 24 | 25 | type AlgebraicGTransform :: (Type -> Type) -> Type -> (Type -> Type) -> (Type -> Type) -> Type 26 | type AlgebraicGTransform w t f g = f (w t) -> g t 27 | 28 | type CoalgebraicGTransform :: (Type -> Type) -> Type -> (Type -> Type) -> (Type -> Type) -> Type 29 | type CoalgebraicGTransform n t f g = f t -> g (n t) 30 | -------------------------------------------------------------------------------- /test/Test/Example/List.purs: -------------------------------------------------------------------------------- 1 | module Test.Example.List where 2 | 3 | import Prelude 4 | 5 | import Effect (Effect) 6 | import Effect.Console (log, logShow) 7 | 8 | import Data.Functor.Mu (Mu) 9 | import Data.TacitString (TacitString) 10 | import Matryoshka (class Corecursive, Algebra, Coalgebra, ana, cata, embed, hylo) 11 | 12 | data ListF a t = Nil | Cons a t 13 | 14 | derive instance functorListF :: Functor (ListF a) 15 | 16 | instance showListF :: Show a => Show (ListF a TacitString) where 17 | show Nil = "Nil" 18 | show (Cons h t) = show h <> " : " <> show t 19 | 20 | nil :: forall a t. Corecursive t (ListF a) => t 21 | nil = embed Nil 22 | 23 | cons :: forall a t. Corecursive t (ListF a) => a -> t -> t 24 | cons h t = embed (Cons h t) 25 | 26 | prod :: Algebra (ListF Int) Int 27 | prod Nil = 1 28 | prod (Cons h t) = h * t 29 | 30 | count :: Coalgebra (ListF Int) Int 31 | count n 32 | | n <= 0 = Nil 33 | | otherwise = Cons n (n - 1) 34 | 35 | fac :: Int -> Int 36 | fac = hylo prod count 37 | 38 | type List a = Mu (ListF a) 39 | 40 | listExample :: Effect Unit 41 | listExample = do 42 | log "list example" 43 | let someList = cons 1 (cons 2 (cons 3 (cons 4 nil))) 44 | logShow $ cata prod (someList :: List Int) 45 | logShow (ana count 4 :: List Int) 46 | logShow (fac 6) 47 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | branches: [main] 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v2 15 | 16 | - name: Set up a PureScript toolchain 17 | uses: purescript-contrib/setup-purescript@main 18 | with: 19 | purescript: "unstable" 20 | purs-tidy: "latest" 21 | 22 | - name: Cache PureScript dependencies 23 | uses: actions/cache@v2 24 | with: 25 | key: ${{ runner.os }}-spago-${{ hashFiles('**/*.dhall') }} 26 | path: | 27 | .spago 28 | output 29 | 30 | - name: Install dependencies 31 | run: spago install 32 | 33 | - name: Build source 34 | run: spago build --no-install --purs-args '--censor-lib --strict' 35 | 36 | - name: Run tests 37 | run: spago test --no-install 38 | 39 | - name: Check formatting 40 | run: purs-tidy check src test 41 | 42 | - name: Verify Bower & Pulp 43 | run: | 44 | npm install bower pulp@16.0.0-0 45 | npx bower install 46 | npx pulp build -- --censor-lib --strict 47 | if [ -d "test" ]; then 48 | npx pulp test 49 | fi 50 | -------------------------------------------------------------------------------- /src/Matryoshka/Class/Recursive.purs: -------------------------------------------------------------------------------- 1 | {- 2 | Copyright 2016 SlamData, Inc. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | -} 16 | 17 | module Matryoshka.Class.Recursive where 18 | 19 | import Prelude 20 | 21 | import Control.Comonad.Cofree (Cofree, head, tail) 22 | import Control.Comonad.Env.Trans (EnvT(..)) 23 | import Control.Monad.Free (Free, resume) 24 | 25 | import Data.Either (Either(..), either) 26 | import Data.Functor.Mu (Mu, unroll) 27 | import Data.Functor.Nu (Nu, observe) 28 | import Data.Tuple (Tuple(..)) 29 | 30 | import Matryoshka.Pattern.CoEnvT (CoEnvT(..)) 31 | 32 | class Functor f <= Recursive t f | t -> f where 33 | project :: t -> f t 34 | 35 | instance recursiveMu :: Functor f => Recursive (Mu f) f where 36 | project = unroll 37 | 38 | instance recursiveNu :: Functor f => Recursive (Nu f) f where 39 | project = observe 40 | 41 | instance recursiveFree :: Functor f => Recursive (Free f a) (CoEnvT a f) where 42 | project = CoEnvT <<< either Right Left <<< resume 43 | 44 | instance recursiveCofree :: Functor f => Recursive (Cofree f a) (EnvT a f) where 45 | project cf = EnvT $ Tuple (head cf) (tail cf) 46 | -------------------------------------------------------------------------------- /src/Matryoshka/Pattern/CoEnvT.purs: -------------------------------------------------------------------------------- 1 | {- 2 | Copyright 2016 SlamData, Inc. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | -} 16 | 17 | -- | The pattern functor for `Free`. This is not `Reader`, it's the dual of 18 | -- | `EnvT` in the sense that it uses a coproduct (`Either`) rather than a 19 | -- | product (`Tuple`). 20 | module Matryoshka.Pattern.CoEnvT where 21 | 22 | import Prelude 23 | 24 | import Data.Either (Either) 25 | import Data.Newtype (class Newtype) 26 | import Data.Bifunctor (lmap) 27 | 28 | newtype CoEnvT :: Type -> (Type -> Type) -> Type -> Type 29 | newtype CoEnvT e m a = CoEnvT (Either e (m a)) 30 | 31 | runEnvT :: forall e m a. CoEnvT e m a -> Either e (m a) 32 | runEnvT (CoEnvT x) = x 33 | 34 | withEnvT :: forall e1 e2 m a. (e1 -> e2) -> CoEnvT e1 m a -> CoEnvT e2 m a 35 | withEnvT f (CoEnvT e) = CoEnvT (lmap f e) 36 | 37 | mapEnvT :: forall e m1 m2 a b. (m1 a -> m2 b) -> CoEnvT e m1 a -> CoEnvT e m2 b 38 | mapEnvT f (CoEnvT e) = CoEnvT (map f e) 39 | 40 | derive instance newtypeEnvT :: Newtype (CoEnvT e m a) _ 41 | 42 | instance functorEnvT :: Functor m => Functor (CoEnvT e m) where 43 | map f (CoEnvT e) = CoEnvT (map (map f) e) 44 | -------------------------------------------------------------------------------- /src/Matryoshka/Class/Corecursive.purs: -------------------------------------------------------------------------------- 1 | {- 2 | Copyright 2016 SlamData, Inc. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | -} 16 | 17 | module Matryoshka.Class.Corecursive where 18 | 19 | import Prelude 20 | 21 | import Control.Comonad.Cofree (Cofree, mkCofree) 22 | import Control.Comonad.Env (EnvT(..)) 23 | import Control.Monad.Free (Free, liftF) 24 | 25 | import Data.Either (either) 26 | import Data.Functor.Mu (Mu, roll) 27 | import Data.Functor.Nu (Nu, unfold, observe) 28 | import Data.Tuple (Tuple(..)) 29 | 30 | import Matryoshka.Pattern.CoEnvT (CoEnvT(..)) 31 | 32 | class Functor f <= Corecursive t f | t -> f where 33 | embed :: f t -> t 34 | 35 | instance corecursiveMu :: Functor f => Corecursive (Mu f) f where 36 | embed = roll 37 | 38 | instance corecursiveNu :: Functor f => Corecursive (Nu f) f where 39 | embed = flip unfold (map observe) 40 | 41 | instance corecursiveFree :: Functor f => Corecursive (Free f a) (CoEnvT a f) where 42 | embed (CoEnvT e) = either pure (join <<< liftF) e 43 | 44 | instance corecursiveCofree :: Functor f => Corecursive (Cofree f a) (EnvT a f) where 45 | embed et = mkCofree (ask et) (lower et) 46 | where 47 | ask (EnvT (Tuple e _)) = e 48 | lower (EnvT (Tuple _ x)) = x 49 | -------------------------------------------------------------------------------- /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 | ## [v1.0.0](https://github.com/purescript-contrib/purescript-matryoshka/releases/tag/v1.0.0) - 2022-04-27 16 | 17 | Breaking changes: 18 | - Update project and deps to PureScript v0.15.0 (#26 by @JordanMartinez) 19 | 20 | New features: 21 | 22 | Bugfixes: 23 | 24 | Other improvements: 25 | - Added `purs-tidy` formatter (#25 by @thomashoneyman) 26 | - Updated dependencies to clear build errors related to unlisted dependencies (#24 by @flounders) 27 | 28 | ## [v0.5.0](https://github.com/purescript-contrib/purescript-matryoshka/releases/tag/v0.5.0) - 2021-02-26 29 | 30 | Breaking changes: 31 | - Added support for PureScript 0.14 and dropped support for all previous versions (#20) 32 | 33 | New features: 34 | 35 | Bugfixes: 36 | 37 | Other improvements: 38 | - Changed default branch to `main` from `master` 39 | - Updated to comply with Contributors library guidelines by adding new issue and pull request templates, updating documentation, and migrating to Spago for local development and CI (#15) 40 | 41 | ## [v0.4.0](https://github.com/purescript-contrib/purescript-matryoshka/releases/tag/v0.4.0) - 2018-06-28 42 | 43 | - Updated for PureScript 0.12 (@xgrommx) 44 | 45 | ## [v0.3.0](https://github.com/purescript-contrib/purescript-matryoshka/releases/tag/v0.3.0) - 2017-04-11 46 | 47 | - Updated for PureScript 0.11 48 | 49 | ## [v0.2.0](https://github.com/purescript-contrib/purescript-matryoshka/releases/tag/v0.2.0) - 2017-03-06 50 | 51 | - Updated `purescript-fixed-points` dependency 52 | 53 | ## [v0.1.1](https://github.com/purescript-contrib/purescript-matryoshka/releases/tag/v0.1.1) - 2017-01-03 54 | 55 | - Relaxed `Comonad` constraint to `Functor` for `Corecursive Cofree` instance 56 | 57 | ## [v0.1.0](https://github.com/purescript-contrib/purescript-matryoshka/releases/tag/v0.1.0) - 2016-12-30 58 | 59 | - Initial release 60 | -------------------------------------------------------------------------------- /src/Matryoshka.purs: -------------------------------------------------------------------------------- 1 | {- 2 | Copyright 2016 SlamData, Inc. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | -} 16 | 17 | module Matryoshka 18 | ( module Matryoshka.Algebra 19 | , module Matryoshka.Class.Corecursive 20 | , module Matryoshka.Class.Recursive 21 | , module Matryoshka.Coalgebra 22 | , module Matryoshka.DistributiveLaw 23 | , module Matryoshka.Fold 24 | , module Matryoshka.Refold 25 | , module Matryoshka.Transform 26 | , module Matryoshka.Unfold 27 | , module Matryoshka.Util 28 | ) where 29 | 30 | import Matryoshka.Algebra (Algebra, AlgebraM, ElgotAlgebra, GAlgebra, GAlgebraM) 31 | import Matryoshka.Class.Corecursive (class Corecursive, embed) 32 | import Matryoshka.Class.Recursive (class Recursive, project) 33 | import Matryoshka.Coalgebra (Coalgebra, CoalgebraM, ElgotCoalgebra, GCoalgebra, GCoalgebraM) 34 | import Matryoshka.DistributiveLaw (DistributiveLaw, distAna, distApo, distApplicative, distCata, distDistributive, distFutu, distGApo, distGApoT, distGFutu, distGHisto, distHisto, distPara, distParaT, distZygo, distZygoT) 35 | import Matryoshka.Fold (annotateTopDown, annotateTopDownM, cata, cataM, children, elgotCata, elgotHisto, elgotPara, elgotZygo, gElgotZygo, gcata, gcataM, ghisto, gpara, gprepro, gzygo, histo, isLeaf, lambek, mutu, para, paraM, prepro, topDownCata, topDownCataM, transCata, transCataM, transCataT, transCataTM, transPara, transParaT, transPrepro, universe, zygo) 36 | import Matryoshka.Refold (chrono, codyna, codynaM, convertTo, dyna, ghylo, ghyloM, hylo, hyloM, transHylo) 37 | import Matryoshka.Transform (AlgebraicGTransform, CoalgebraicGTransform, Transform, TransformM) 38 | import Matryoshka.Unfold (ana, anaM, apo, apoM, colambek, elgotAna, elgotApo, elgotFutu, futu, futuM, gana, ganaM, gapo, gpostpro, postpro, transAna, transAnaM, transAnaT, transAnaTM, transApo, transApoT, transPostpro) 39 | import Matryoshka.Util (mapR, traverseR) 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Matryoshka 2 | 3 | [![CI](https://github.com/purescript-contrib/purescript-matryoshka/workflows/CI/badge.svg?branch=main)](https://github.com/purescript-contrib/purescript-matryoshka/actions?query=workflow%3ACI+branch%3Amain) 4 | [![Release](https://img.shields.io/github/release/purescript-contrib/purescript-matryoshka.svg)](https://github.com/purescript-contrib/purescript-matryoshka/releases) 5 | [![Pursuit](https://pursuit.purescript.org/packages/purescript-matryoshka/badge)](https://pursuit.purescript.org/packages/purescript-matryoshka) 6 | [![Maintainer: garyb](https://img.shields.io/badge/maintainer-garyb-teal.svg)](https://github.com/garyb) 7 | 8 | Generalized folds, unfolds, and traversals for fixed point data structures in PureScript. (A port of [Matryoshka](https://github.com/precog/matryoshka)). 9 | 10 | ## Installation 11 | 12 | Install `matryoshka` with [Spago](https://github.com/purescript/spago): 13 | 14 | ```sh 15 | spago install matryoshka 16 | ``` 17 | 18 | ## Quick start 19 | 20 | The quick start hasn't been written yet (contributions are welcome!). The quick start covers a common, minimal use case for the library, whereas longer examples and tutorials are kept in the [docs directory](./docs). 21 | 22 | ## Documentation 23 | 24 | `matryoshka` documentation is stored in a few places: 25 | 26 | 1. Module documentation is [published on Pursuit](https://pursuit.purescript.org/packages/purescript-matryoshka). 27 | 2. Written documentation is kept in the [docs directory](./docs). 28 | 3. Usage examples can be found in [the test suite](./test). 29 | 30 | If you get stuck, there are several ways to get help: 31 | 32 | - [Open an issue](https://github.com/purescript-contrib/purescript-matryoshka/issues) if you have encountered a bug or problem. 33 | - Ask general questions on the [PureScript Discourse](https://discourse.purescript.org) forum or the [PureScript Discord](https://purescript.org/chat) chat. 34 | 35 | ## Contributing 36 | 37 | You can contribute to `matryoshka` in several ways: 38 | 39 | 1. If you encounter a problem or have a question, please [open an issue](https://github.com/purescript-contrib/purescript-matryoshka/issues). We'll do our best to work with you to resolve or answer it. 40 | 41 | 2. If you would like to contribute code, tests, or documentation, please [read the contributor guide](./CONTRIBUTING.md). It's a short, helpful introduction to contributing to this library, including development instructions. 42 | 43 | 3. If you have written a library, tutorial, guide, or other resource based on this package, please share it on the [PureScript Discourse](https://discourse.purescript.org)! Writing libraries and learning resources are a great way to help this library succeed. 44 | -------------------------------------------------------------------------------- /src/Matryoshka/Refold.purs: -------------------------------------------------------------------------------- 1 | {- 2 | Copyright 2016 SlamData, Inc. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | -} 16 | 17 | module Matryoshka.Refold where 18 | 19 | import Prelude 20 | 21 | import Control.Comonad (class Comonad, duplicate, extract) 22 | import Control.Comonad.Cofree (Cofree) 23 | import Control.Monad.Free (Free) 24 | 25 | import Data.Identity (Identity(..)) 26 | import Data.Newtype (unwrap) 27 | import Data.Profunctor (lcmap) 28 | import Data.Traversable (class Traversable, traverse) 29 | 30 | import Matryoshka.Algebra (Algebra, AlgebraM, GAlgebra, GAlgebraM) 31 | import Matryoshka.Class.Corecursive (class Corecursive, embed) 32 | import Matryoshka.Class.Recursive (class Recursive) 33 | import Matryoshka.Coalgebra (Coalgebra, CoalgebraM, GCoalgebra, GCoalgebraM) 34 | import Matryoshka.DistributiveLaw (DistributiveLaw, distAna, distCata, distFutu, distHisto) 35 | import Matryoshka.Fold (cata) 36 | import Matryoshka.Transform (Transform) 37 | import Matryoshka.Util (mapR) 38 | 39 | hylo 40 | :: forall f a b 41 | . Functor f 42 | => Algebra f b 43 | -> Coalgebra f a 44 | -> a 45 | -> b 46 | hylo f g = go 47 | where 48 | go a = f $ go <$> g a 49 | 50 | hyloM 51 | :: forall f m a b 52 | . Monad m 53 | => Traversable f 54 | => AlgebraM m f b 55 | -> CoalgebraM m f a 56 | -> a 57 | -> m b 58 | hyloM f g = go 59 | where 60 | go a = f =<< traverse go =<< g a 61 | 62 | ghylo 63 | :: forall f w n a b 64 | . Monad n 65 | => Comonad w 66 | => Functor f 67 | => DistributiveLaw f w 68 | -> DistributiveLaw n f 69 | -> GAlgebra w f b 70 | -> GCoalgebra n f a 71 | -> a 72 | -> b 73 | ghylo w n f g = extract <<< go <<< pure 74 | where 75 | go na = f <$> w ((duplicate <<< go <<< join) <$> n (g <$> na)) 76 | 77 | ghyloM 78 | :: forall f w n m a b 79 | . Monad m 80 | => Monad n 81 | => Comonad w 82 | => Traversable f 83 | => Traversable w 84 | => Traversable n 85 | => DistributiveLaw f w 86 | -> DistributiveLaw n f 87 | -> GAlgebraM w m f b 88 | -> GCoalgebraM n m f a 89 | -> a 90 | -> m b 91 | ghyloM w m f g = map extract <<< h <<< pure 92 | where 93 | h x = traverse f =<< w <$> (traverse (map duplicate <<< h <<< join) <<< m =<< traverse g x) 94 | 95 | transHylo 96 | :: forall t f g h u 97 | . Recursive t f 98 | => Corecursive u h 99 | => Functor g 100 | => Transform u g h 101 | -> Transform t f g 102 | -> t 103 | -> u 104 | transHylo f g = go 105 | where 106 | go t = mapR (f <<< map go <<< g) t 107 | 108 | dyna 109 | :: forall f a b 110 | . Functor f 111 | => GAlgebra (Cofree f) f b 112 | -> Coalgebra f a 113 | -> a 114 | -> b 115 | dyna f g = ghylo distHisto distAna f (map Identity <<< g) 116 | 117 | codyna 118 | :: forall f a b 119 | . Functor f 120 | => Algebra f b 121 | -> GCoalgebra (Free f) f a 122 | -> a 123 | -> b 124 | codyna f = ghylo distCata distFutu (lcmap (map unwrap) f) 125 | 126 | codynaM 127 | :: forall f m a b 128 | . Monad m 129 | => Traversable f 130 | => AlgebraM m f b 131 | -> GCoalgebraM (Free f) m f a 132 | -> a 133 | -> m b 134 | codynaM f = ghyloM distCata distFutu (lcmap (map unwrap) f) 135 | 136 | chrono 137 | :: forall f a b 138 | . Functor f 139 | => GAlgebra (Cofree f) f b 140 | -> GCoalgebra (Free f) f a 141 | -> a 142 | -> b 143 | chrono = ghylo distHisto distFutu 144 | 145 | convertTo :: forall t f r. Recursive t f => Corecursive r f => t -> r 146 | convertTo = cata embed 147 | -------------------------------------------------------------------------------- /src/Matryoshka/DistributiveLaw.purs: -------------------------------------------------------------------------------- 1 | {- 2 | Copyright 2016 SlamData, Inc. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | -} 16 | 17 | module Matryoshka.DistributiveLaw where 18 | 19 | import Prelude 20 | 21 | import Control.Comonad (class Comonad, extract) 22 | import Control.Comonad.Cofree (Cofree, buildCofree, tail) 23 | import Control.Comonad.Env.Trans (EnvT(..), runEnvT) 24 | import Control.Comonad.Trans.Class (lower) 25 | import Control.Monad.Except (ExceptT(..), runExceptT) 26 | import Control.Monad.Free (Free, liftF, resume) 27 | 28 | import Data.Distributive (class Distributive, distribute) 29 | import Data.Either (Either(..), either) 30 | import Data.Identity (Identity) 31 | import Data.Newtype (unwrap, wrap) 32 | import Data.Traversable (class Traversable, sequence) 33 | import Data.Tuple (Tuple(..), fst, snd) 34 | 35 | import Matryoshka.Algebra (Algebra) 36 | import Matryoshka.Class.Corecursive (class Corecursive, embed) 37 | import Matryoshka.Class.Recursive (class Recursive, project) 38 | import Matryoshka.Coalgebra (Coalgebra) 39 | 40 | type DistributiveLaw f g = forall a. f (g a) -> g (f a) 41 | 42 | distApplicative :: forall f g. Traversable f => Applicative g => DistributiveLaw f g 43 | distApplicative = sequence 44 | 45 | distDistributive :: forall f g. Traversable f => Distributive g => DistributiveLaw f g 46 | distDistributive = distribute 47 | 48 | distCata :: forall f. Functor f => DistributiveLaw f Identity 49 | distCata = wrap <<< map unwrap 50 | 51 | distPara :: forall t f. Corecursive t f => DistributiveLaw f (Tuple t) 52 | distPara = distZygo embed 53 | 54 | distParaT 55 | :: forall t f w 56 | . Corecursive t f 57 | => Comonad w 58 | => DistributiveLaw f w 59 | -> DistributiveLaw f (EnvT t w) 60 | distParaT = distZygoT embed 61 | 62 | distZygo :: forall f a. Functor f => Algebra f a -> DistributiveLaw f (Tuple a) 63 | distZygo g m = Tuple (g (map fst m)) (map snd m) 64 | 65 | distZygoT 66 | :: forall f w a 67 | . Functor f 68 | => Comonad w 69 | => Algebra f a 70 | -> DistributiveLaw f w 71 | -> DistributiveLaw f (EnvT a w) 72 | distZygoT g k fe = 73 | EnvT $ Tuple (g (fst <<< runEnvT <$> fe)) (k (lower <$> fe)) 74 | 75 | distHisto :: forall f. Functor f => DistributiveLaw f (Cofree f) 76 | distHisto = distGHisto identity 77 | 78 | distGHisto 79 | :: forall f h 80 | . Functor f 81 | => Functor h 82 | => DistributiveLaw f h 83 | -> DistributiveLaw f (Cofree h) 84 | distGHisto k = buildCofree \s -> Tuple (map extract s) (k $ map tail s) 85 | 86 | distAna :: forall f. Functor f => DistributiveLaw Identity f 87 | distAna = map wrap <<< unwrap 88 | 89 | distApo :: forall t f. Recursive t f => DistributiveLaw (Either t) f 90 | distApo = distGApo project 91 | 92 | distGApo :: forall f a. Functor f => Coalgebra f a -> DistributiveLaw (Either a) f 93 | distGApo f = either (map Left <<< f) (map Right) 94 | 95 | distGApoT 96 | :: forall f m a 97 | . Functor f 98 | => Functor m 99 | => Coalgebra f a 100 | -> DistributiveLaw m f 101 | -> DistributiveLaw (ExceptT a m) f 102 | distGApoT g k = map ExceptT <<< k <<< map (distGApo g) <<< runExceptT 103 | 104 | distFutu :: forall f. Functor f => DistributiveLaw (Free f) f 105 | distFutu = distGFutu identity 106 | 107 | distGFutu 108 | :: forall f h 109 | . Functor f 110 | => Functor h 111 | => DistributiveLaw h f 112 | -> DistributiveLaw (Free h) f 113 | distGFutu k f = case resume f of 114 | Left as -> join <<< liftF <$> k (distGFutu k <$> as) 115 | Right b -> pure <$> b 116 | -------------------------------------------------------------------------------- /src/Matryoshka/Unfold.purs: -------------------------------------------------------------------------------- 1 | {- 2 | Copyright 2016 SlamData, Inc. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | -} 16 | 17 | module Matryoshka.Unfold where 18 | 19 | import Prelude 20 | 21 | import Control.Monad.Free (Free, resume) 22 | 23 | import Data.Either (Either, either) 24 | import Data.Identity (Identity(..)) 25 | import Data.Traversable (class Traversable, traverse) 26 | 27 | import Matryoshka.Class.Corecursive (class Corecursive, embed) 28 | import Matryoshka.Class.Recursive (class Recursive, project) 29 | import Matryoshka.Coalgebra (Coalgebra, CoalgebraM, ElgotCoalgebra, GCoalgebra, GCoalgebraM) 30 | import Matryoshka.DistributiveLaw (DistributiveLaw, distAna, distFutu) 31 | import Matryoshka.Transform (CoalgebraicGTransform, Transform, TransformM) 32 | import Matryoshka.Util (mapR, traverseR) 33 | 34 | ana :: forall t f a. Corecursive t f => Coalgebra f a -> a -> t 35 | ana f = go 36 | where 37 | go a = embed (go <$> f a) 38 | 39 | anaM 40 | :: forall t f m a 41 | . Corecursive t f 42 | => Monad m 43 | => Traversable f 44 | => CoalgebraM m f a 45 | -> a 46 | -> m t 47 | anaM f = go 48 | where 49 | go a = f a >>= (map embed <<< traverse go) 50 | 51 | gana 52 | :: forall t f n a 53 | . Corecursive t f 54 | => Monad n 55 | => DistributiveLaw n f 56 | -> GCoalgebra n f a 57 | -> a 58 | -> t 59 | gana k f = go <<< pure <<< f 60 | where 61 | go a = embed $ map (go <<< map f <<< join) (k a) 62 | 63 | ganaM 64 | :: forall t f m n a 65 | . Corecursive t f 66 | => Monad m 67 | => Monad n 68 | => Traversable f 69 | => Traversable n 70 | => DistributiveLaw n f 71 | -> GCoalgebraM n m f a 72 | -> a 73 | -> m t 74 | ganaM k f = go <=< map pure <<< f 75 | where 76 | go a = embed <$> traverse (go <=< traverse f <<< join) (k a) 77 | 78 | elgotAna 79 | :: forall t f n a 80 | . Corecursive t f 81 | => Monad n 82 | => DistributiveLaw n f 83 | -> ElgotCoalgebra n f a 84 | -> a 85 | -> t 86 | elgotAna k f = go <<< f 87 | where 88 | go a = embed $ (go <<< (f =<< _)) <$> k a 89 | 90 | transAna 91 | :: forall t f u g 92 | . Recursive t f 93 | => Corecursive u g 94 | => Transform t f g 95 | -> t 96 | -> u 97 | transAna f = go 98 | where 99 | go t = mapR (map go <<< f) t 100 | 101 | transAnaT :: forall t f. Recursive t f => Corecursive t f => (t -> t) -> t -> t 102 | transAnaT f = go 103 | where 104 | go t = mapR (map go) (f t) 105 | 106 | transAnaM 107 | :: forall t f u g m 108 | . Recursive t f 109 | => Corecursive u g 110 | => Monad m 111 | => Traversable g 112 | => TransformM m t f g 113 | -> t 114 | -> m u 115 | transAnaM f = go 116 | where 117 | go t = traverseR (traverse go <=< f) t 118 | 119 | transAnaTM 120 | :: forall t f m 121 | . Recursive t f 122 | => Corecursive t f 123 | => Monad m 124 | => Traversable f 125 | => Coalgebra m t 126 | -> t 127 | -> m t 128 | transAnaTM f = go 129 | where 130 | go t = traverseR (traverse go) =<< f t 131 | 132 | postpro 133 | :: forall t f a 134 | . Recursive t f 135 | => Corecursive t f 136 | => (f ~> f) 137 | -> Coalgebra f a 138 | -> a 139 | -> t 140 | postpro f g = gpostpro distAna f (map Identity <<< g) 141 | 142 | gpostpro 143 | :: forall t f n a 144 | . Recursive t f 145 | => Corecursive t f 146 | => Monad n 147 | => DistributiveLaw n f 148 | -> (f ~> f) 149 | -> GCoalgebra n f a 150 | -> a 151 | -> t 152 | gpostpro f g h = go <<< pure 153 | where 154 | go na = embed $ (ana (g <<< project) <<< go <<< join) <$> f (h <$> na) 155 | 156 | transPostpro 157 | :: forall t f u g 158 | . Recursive t f 159 | => Recursive u g 160 | => Corecursive u g 161 | => (g ~> g) 162 | -> Transform t f g 163 | -> t 164 | -> u 165 | transPostpro f g = go 166 | where 167 | go t = mapR (map (transAna f <<< go) <<< g) t 168 | 169 | apo :: forall t f a. Corecursive t f => GCoalgebra (Either t) f a -> a -> t 170 | apo f = go 171 | where 172 | go a = embed $ either identity go <$> f a 173 | 174 | gapo 175 | :: forall t f a b 176 | . Corecursive t f 177 | => Coalgebra f b 178 | -> GCoalgebra (Either b) f a 179 | -> a 180 | -> t 181 | gapo f g = go 182 | where 183 | go a = embed $ either anaf go <$> g a 184 | anaf = ana f 185 | 186 | apoM 187 | :: forall t f m a 188 | . Corecursive t f 189 | => Monad m 190 | => Traversable f 191 | => GCoalgebraM (Either t) m f a 192 | -> a 193 | -> m t 194 | apoM f = go 195 | where 196 | go a = embed <$> (traverse (either pure go) =<< f a) 197 | 198 | elgotApo :: forall t f a. Corecursive t f => ElgotCoalgebra (Either t) f a -> a -> t 199 | elgotApo f = go 200 | where 201 | go a = either identity (embed <<< map go) $ f a 202 | 203 | transApo 204 | :: forall t f u g 205 | . Recursive t f 206 | => Corecursive u g 207 | => CoalgebraicGTransform (Either u) t f g 208 | -> t 209 | -> u 210 | transApo f = go 211 | where 212 | go t = mapR (map (either identity go) <<< f) t 213 | 214 | transApoT 215 | :: forall t f 216 | . Recursive t f 217 | => Corecursive t f 218 | => (t -> Either t t) 219 | -> t 220 | -> t 221 | transApoT f = go 222 | where 223 | go t = either identity (mapR (map go)) $ f t 224 | 225 | futu :: forall t f a. Corecursive t f => GCoalgebra (Free f) f a -> a -> t 226 | futu = gana distFutu 227 | 228 | elgotFutu :: forall t f a. Corecursive t f => ElgotCoalgebra (Free f) f a -> a -> t 229 | elgotFutu = elgotAna distFutu 230 | 231 | futuM 232 | :: forall t f m a 233 | . Corecursive t f 234 | => Monad m 235 | => Traversable f 236 | => GCoalgebraM (Free f) m f a 237 | -> a 238 | -> m t 239 | futuM f = go 240 | where 241 | go a = map embed <<< traverse loop =<< f a 242 | loop x = either (map embed <<< traverse loop) go (resume x) 243 | 244 | colambek :: forall t f. Recursive t f => Corecursive t f => f t -> t 245 | colambek = ana (map project) 246 | -------------------------------------------------------------------------------- /src/Matryoshka/Fold.purs: -------------------------------------------------------------------------------- 1 | {- 2 | Copyright 2016 SlamData, Inc. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | -} 16 | 17 | module Matryoshka.Fold where 18 | 19 | import Prelude 20 | 21 | import Control.Comonad (class Comonad, duplicate, extract) 22 | import Control.Comonad.Cofree (Cofree, mkCofree) 23 | import Control.Comonad.Env.Trans (EnvT) 24 | 25 | import Data.Foldable (class Foldable, foldMap) 26 | import Data.List (List) 27 | import Data.Monoid.Disj (Disj(..)) 28 | import Data.Newtype (alaF) 29 | import Data.Profunctor.Strong ((&&&)) 30 | import Data.Traversable (class Traversable, traverse) 31 | import Data.Tuple (Tuple(..)) 32 | 33 | import Matryoshka.Algebra (Algebra, AlgebraM, ElgotAlgebra, GAlgebra, GAlgebraM) 34 | import Matryoshka.Class.Corecursive (class Corecursive, embed) 35 | import Matryoshka.Class.Recursive (class Recursive, project) 36 | import Matryoshka.DistributiveLaw (DistributiveLaw, distGHisto, distHisto, distZygo, distZygoT) 37 | import Matryoshka.Util (mapR, traverseR) 38 | import Matryoshka.Transform (AlgebraicGTransform, Transform, TransformM) 39 | 40 | cata :: forall t f a. Recursive t f => Algebra f a -> t -> a 41 | cata f = go 42 | where 43 | go t = f $ map go $ project t 44 | 45 | cataM 46 | :: forall t f m a 47 | . Recursive t f 48 | => Monad m 49 | => Traversable f 50 | => AlgebraM m f a 51 | -> t 52 | -> m a 53 | cataM f = go 54 | where 55 | go t = f =<< traverse go (project t) 56 | 57 | gcata 58 | :: forall t f w a 59 | . Recursive t f 60 | => Comonad w 61 | => DistributiveLaw f w 62 | -> GAlgebra w f a 63 | -> t 64 | -> a 65 | gcata k g = g <<< extract <<< go 66 | where 67 | go t = k $ map (duplicate <<< map g <<< go) (project t) 68 | 69 | gcataM 70 | :: forall t f w m a 71 | . Recursive t f 72 | => Monad m 73 | => Comonad w 74 | => Traversable f 75 | => Traversable w 76 | => DistributiveLaw f w 77 | -> GAlgebraM w m f a 78 | -> t 79 | -> m a 80 | gcataM k g = g <<< extract <=< loop 81 | where 82 | loop t = k <$> traverse (map duplicate <<< traverse g <=< loop) (project t) 83 | 84 | elgotCata 85 | :: forall t f w a 86 | . Recursive t f 87 | => Comonad w 88 | => DistributiveLaw f w 89 | -> ElgotAlgebra w f a 90 | -> t 91 | -> a 92 | elgotCata k g = g <<< go 93 | where 94 | go t = k $ map (map g <<< duplicate <<< go) (project t) 95 | 96 | transCata 97 | :: forall t f u g 98 | . Recursive t f 99 | => Corecursive u g 100 | => Transform u f g 101 | -> t 102 | -> u 103 | transCata f = go 104 | where 105 | go t = mapR (f <<< map go) t 106 | 107 | transCataT 108 | :: forall t f 109 | . Recursive t f 110 | => Corecursive t f 111 | => (t -> t) 112 | -> t 113 | -> t 114 | transCataT f = go 115 | where 116 | go t = f $ mapR (map go) t 117 | 118 | transCataM 119 | :: forall t f u g m 120 | . Recursive t f 121 | => Corecursive u g 122 | => Monad m 123 | => Traversable f 124 | => TransformM m u f g 125 | -> t 126 | -> m u 127 | transCataM f = go 128 | where 129 | go t = traverseR (f <=< traverse go) t 130 | 131 | transCataTM 132 | :: forall t f m 133 | . Recursive t f 134 | => Corecursive t f 135 | => Monad m 136 | => Traversable f 137 | => (t -> m t) 138 | -> t 139 | -> m t 140 | transCataTM f = go 141 | where 142 | go t = f =<< traverseR (traverse go) t 143 | 144 | topDownCata 145 | :: forall t f a 146 | . Recursive t f 147 | => Corecursive t f 148 | => (a -> t -> Tuple a t) 149 | -> a 150 | -> t 151 | -> t 152 | topDownCata f = go 153 | where 154 | go a t = case f a t of 155 | Tuple a' tf -> mapR (map (go a')) tf 156 | 157 | topDownCataM 158 | :: forall t f m a 159 | . Recursive t f 160 | => Corecursive t f 161 | => Monad m 162 | => Traversable f 163 | => (a -> t -> m (Tuple a t)) 164 | -> a 165 | -> t 166 | -> m t 167 | topDownCataM f = go 168 | where 169 | go a t = f a t >>= case _ of 170 | Tuple a' tf -> traverseR (traverse (go a')) tf 171 | 172 | prepro 173 | :: forall t f a 174 | . Recursive t f 175 | => Corecursive t f 176 | => (f ~> f) 177 | -> Algebra f a 178 | -> t 179 | -> a 180 | prepro f g = go 181 | where 182 | go t = g $ (go <<< cata (embed <<< f)) <$> project t 183 | 184 | gprepro 185 | :: forall t f w a 186 | . Recursive t f 187 | => Corecursive t f 188 | => Comonad w 189 | => DistributiveLaw f w 190 | -> (f ~> f) 191 | -> GAlgebra w f a 192 | -> t 193 | -> a 194 | gprepro f g h = extract <<< go 195 | where 196 | go t = h <$> f (duplicate <<< go <<< cata (embed <<< g) <$> project t) 197 | 198 | transPrepro 199 | :: forall t f u g 200 | . Recursive t f 201 | => Corecursive t f 202 | => Corecursive u g 203 | => (f ~> f) 204 | -> Transform u f g 205 | -> t 206 | -> u 207 | transPrepro f g = go 208 | where 209 | go t = mapR (g <<< map (go <<< transCata f)) t 210 | 211 | para :: forall t f a. Recursive t f => GAlgebra (Tuple t) f a -> t -> a 212 | para f = go 213 | where 214 | go t = f (g <$> project t) 215 | g t = Tuple t (go t) 216 | 217 | paraM 218 | :: forall t f m a 219 | . Recursive t f 220 | => Monad m 221 | => Traversable f 222 | => GAlgebraM (Tuple t) m f a 223 | -> t 224 | -> m a 225 | paraM f = go 226 | where 227 | go t = f =<< traverse (map (Tuple t) <<< go) (project t) 228 | 229 | gpara 230 | :: forall t f w a 231 | . Recursive t f 232 | => Corecursive t f 233 | => Comonad w 234 | => DistributiveLaw f w 235 | -> GAlgebra (EnvT t w) f a 236 | -> t 237 | -> a 238 | gpara = gzygo embed 239 | 240 | elgotPara :: forall t f a. Recursive t f => ElgotAlgebra (Tuple t) f a -> t -> a 241 | elgotPara f = go 242 | where 243 | go t = f (Tuple t (go <$> project t)) 244 | 245 | transPara 246 | :: forall t f u g 247 | . Recursive t f 248 | => Corecursive u g 249 | => AlgebraicGTransform (Tuple t) u f g 250 | -> t 251 | -> u 252 | transPara f = go 253 | where 254 | go t = mapR (f <<< map (identity &&& go)) t 255 | 256 | transParaT 257 | :: forall t f 258 | . Recursive t f 259 | => Corecursive t f 260 | => (t -> t -> t) 261 | -> t 262 | -> t 263 | transParaT f = go 264 | where 265 | go t = f t (mapR (map go) t) 266 | 267 | zygo 268 | :: forall t f a b 269 | . Recursive t f 270 | => Algebra f b 271 | -> GAlgebra (Tuple b) f a 272 | -> t 273 | -> a 274 | zygo = gcata <<< distZygo 275 | 276 | gzygo 277 | :: forall t f w a b 278 | . Recursive t f 279 | => Comonad w 280 | => Algebra f b 281 | -> DistributiveLaw f w 282 | -> GAlgebra (EnvT b w) f a 283 | -> t 284 | -> a 285 | gzygo f w = gcata (distZygoT f w) 286 | 287 | elgotZygo 288 | :: forall t f a b 289 | . Recursive t f 290 | => Algebra f b 291 | -> ElgotAlgebra (Tuple b) f a 292 | -> t 293 | -> a 294 | elgotZygo = elgotCata <<< distZygo 295 | 296 | gElgotZygo 297 | :: forall t f w a b 298 | . Recursive t f 299 | => Comonad w 300 | => Algebra f b 301 | -> DistributiveLaw f w 302 | -> ElgotAlgebra (EnvT b w) f a 303 | -> t 304 | -> a 305 | gElgotZygo f w = elgotCata (distZygoT f w) 306 | 307 | mutu 308 | :: forall t f a b 309 | . Recursive t f 310 | => GAlgebra (Tuple a) f b 311 | -> GAlgebra (Tuple b) f a 312 | -> t 313 | -> a 314 | mutu f g = g <<< map go <<< project 315 | where 316 | go x = Tuple (mutu g f x) (mutu f g x) 317 | 318 | histo 319 | :: forall t f a 320 | . Recursive t f 321 | => GAlgebra (Cofree f) f a 322 | -> t 323 | -> a 324 | histo = gcata distHisto 325 | 326 | ghisto 327 | :: forall t f h a 328 | . Recursive t f 329 | => Functor h 330 | => DistributiveLaw f h 331 | -> GAlgebra (Cofree h) f a 332 | -> t 333 | -> a 334 | ghisto g = gcata (distGHisto g) 335 | 336 | elgotHisto 337 | :: forall t f a 338 | . Recursive t f 339 | => ElgotAlgebra (Cofree f) f a 340 | -> t 341 | -> a 342 | elgotHisto = elgotCata distHisto 343 | 344 | annotateTopDown 345 | :: forall t f a 346 | . Recursive t f 347 | => (a -> f t -> a) 348 | -> a 349 | -> t 350 | -> Cofree f a 351 | annotateTopDown f z = go 352 | where 353 | go t = 354 | let 355 | ft = project t 356 | in 357 | mkCofree (f z ft) (map go ft) 358 | 359 | annotateTopDownM 360 | :: forall t f m a 361 | . Recursive t f 362 | => Monad m 363 | => Traversable f 364 | => (a -> f t -> m a) 365 | -> a 366 | -> t 367 | -> m (Cofree f a) 368 | annotateTopDownM f z = go 369 | where 370 | go t = 371 | let 372 | ft = project t 373 | in 374 | (flip map (traverse go ft) <<< mkCofree) =<< f z ft 375 | 376 | isLeaf :: forall t f. Recursive t f => Foldable f => t -> Boolean 377 | isLeaf t = alaF Disj foldMap (const true) (project t) 378 | 379 | children :: forall t f. Recursive t f => Foldable f => t -> List t 380 | children = foldMap pure <<< project 381 | 382 | universe :: forall t f. Recursive t f => Foldable f => t -> List t 383 | universe t = universe =<< children t 384 | 385 | lambek :: forall t f. Recursive t f => Corecursive t f => t -> f t 386 | lambek = cata (map embed) 387 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | --------------------------------------------------------------------------------