forked from unisonweb/unison
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move recursion schemes to separate package
- Loading branch information
Showing
25 changed files
with
223 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,4 +69,5 @@ library | |
, unison-core | ||
, unison-hash | ||
, unison-prelude | ||
, unison-util-recursion | ||
default-language: GHC2021 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,4 +64,5 @@ library | |
, text | ||
, unison-hash | ||
, unison-prelude | ||
, unison-util-recursion | ||
default-language: Haskell2010 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
name: unison-util-recursion | ||
github: unisonweb/unison | ||
copyright: Copyright (C) 2013-2022 Unison Computing, PBC and contributors | ||
|
||
ghc-options: -Wall | ||
|
||
dependencies: | ||
- base | ||
- free | ||
|
||
library: | ||
source-dirs: src | ||
when: | ||
- condition: false | ||
other-modules: Paths_unison_util_recursion | ||
|
||
default-extensions: | ||
- ApplicativeDo | ||
- BangPatterns | ||
- BlockArguments | ||
- DeriveAnyClass | ||
- DeriveFoldable | ||
- DeriveFunctor | ||
- DeriveGeneric | ||
- DeriveTraversable | ||
- DerivingStrategies | ||
- DerivingVia | ||
- DoAndIfThenElse | ||
- DuplicateRecordFields | ||
- FlexibleContexts | ||
- FlexibleInstances | ||
- FunctionalDependencies | ||
- GeneralizedNewtypeDeriving | ||
- ImportQualifiedPost | ||
- LambdaCase | ||
- MultiParamTypeClasses | ||
- NamedFieldPuns | ||
- OverloadedStrings | ||
- PatternSynonyms | ||
- RankNTypes | ||
- ScopedTypeVariables | ||
- StandaloneDeriving | ||
- TupleSections | ||
- TypeApplications | ||
- TypeFamilies | ||
- ViewPatterns |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{-# LANGUAGE DefaultSignatures #-} | ||
{-# LANGUAGE QuantifiedConstraints #-} | ||
{-# LANGUAGE UndecidableInstances #-} | ||
|
||
module Unison.Util.Recursion | ||
( Algebra, | ||
Recursive (..), | ||
cataM, | ||
para, | ||
Fix (..), | ||
Cofree' (..), | ||
) | ||
where | ||
|
||
import Control.Arrow ((&&&)) | ||
import Control.Comonad.Cofree (Cofree ((:<))) | ||
import Control.Monad ((<=<)) | ||
|
||
type Algebra f a = f a -> a | ||
|
||
class Recursive t f | t -> f where | ||
cata :: (Algebra f a) -> t -> a | ||
default cata :: (Functor f) => (f a -> a) -> t -> a | ||
cata φ = φ . fmap (cata φ) . project | ||
project :: t -> f t | ||
default project :: (Functor f) => t -> f t | ||
project = cata (fmap embed) | ||
embed :: f t -> t | ||
{-# MINIMAL embed, (cata | project) #-} | ||
|
||
cataM :: (Recursive t f, Traversable f, Monad m) => (f a -> m a) -> t -> m a | ||
cataM φ = cata $ φ <=< sequenceA | ||
|
||
para :: (Recursive t f, Functor f) => (f (t, a) -> a) -> t -> a | ||
para φ = snd . cata (embed . fmap fst &&& φ) | ||
|
||
newtype Fix f = Fix (f (Fix f)) | ||
|
||
deriving instance (forall a. (Show a) => Show (f a)) => Show (Fix f) | ||
|
||
deriving instance (forall a. (Eq a) => Eq (f a)) => Eq (Fix f) | ||
|
||
deriving instance (Eq (Fix f), forall a. (Ord a) => Ord (f a)) => Ord (Fix f) | ||
|
||
instance (Functor f) => Recursive (Fix f) f where | ||
embed = Fix | ||
project (Fix f) = f | ||
|
||
data Cofree' f a x = a :<< f x | ||
deriving (Foldable, Functor, Traversable) | ||
|
||
-- | | ||
-- | ||
-- __NB__: `Cofree` from “free” is lazy, so this instance is technically partial. | ||
instance (Functor f) => Recursive (Cofree f a) (Cofree' f a) where | ||
embed (a :<< fco) = a :< fco | ||
project (a :< fco) = a :<< fco |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
cabal-version: 1.12 | ||
|
||
-- This file has been generated from package.yaml by hpack version 0.36.0. | ||
-- | ||
-- see: https://github.com/sol/hpack | ||
|
||
name: unison-util-recursion | ||
version: 0.0.0 | ||
homepage: https://github.com/unisonweb/unison#readme | ||
bug-reports: https://github.com/unisonweb/unison/issues | ||
copyright: Copyright (C) 2013-2022 Unison Computing, PBC and contributors | ||
build-type: Simple | ||
|
||
source-repository head | ||
type: git | ||
location: https://github.com/unisonweb/unison | ||
|
||
library | ||
exposed-modules: | ||
Unison.Util.Recursion | ||
hs-source-dirs: | ||
src | ||
default-extensions: | ||
ApplicativeDo | ||
BangPatterns | ||
BlockArguments | ||
DeriveAnyClass | ||
DeriveFoldable | ||
DeriveFunctor | ||
DeriveGeneric | ||
DeriveTraversable | ||
DerivingStrategies | ||
DerivingVia | ||
DoAndIfThenElse | ||
DuplicateRecordFields | ||
FlexibleContexts | ||
FlexibleInstances | ||
FunctionalDependencies | ||
GeneralizedNewtypeDeriving | ||
ImportQualifiedPost | ||
LambdaCase | ||
MultiParamTypeClasses | ||
NamedFieldPuns | ||
OverloadedStrings | ||
PatternSynonyms | ||
RankNTypes | ||
ScopedTypeVariables | ||
StandaloneDeriving | ||
TupleSections | ||
TypeApplications | ||
TypeFamilies | ||
ViewPatterns | ||
ghc-options: -Wall | ||
build-depends: | ||
base | ||
, free | ||
default-language: Haskell2010 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.