free-functors 0 → 0.1
raw patch · 5 files changed
+39/−11 lines, 5 filesdep +voidPVP ok
version bump matches the API change (PVP)
Dependencies added: void
API changes (from Hackage documentation)
+ Data.Functor.Free: convertClosed :: c x => Free c Void -> x
Files
- examples/FreeNum.hs +32/−0
- examples/NonEmptyList.hs +1/−4
- free-functors.cabal +3/−2
- src/Data/Functor/Free.hs +3/−0
- src/Data/Functor/HFree.hs +0/−5
+ examples/FreeNum.hs view
@@ -0,0 +1,32 @@+{-# LANGUAGE FlexibleInstances #-}+module FreeNum where++import Data.Functor.Free++import Control.Applicative++instance Num (Free Num a) where+ Free l + Free r = Free $ (+) <$> l <*> r+ Free l * Free r = Free $ (*) <$> l <*> r+ Free l - Free r = Free $ (-) <$> l <*> r+ negate (Free f) = Free $ negate <$> f+ abs (Free f) = Free $ abs <$> f+ signum (Free f) = Free $ signum <$> f+ fromInteger i = Free $ pure (fromInteger i)++x, y :: Free Num String+x = return "x"+y = return "y"++expr :: Free Num String+expr = 1 + x * (3 - y)++-- Monadic bind is variable substitution+subst :: Free Num String -> Free Num a+subst e = e >>= \v -> case v of + "x" -> 10+ _ -> 2++-- Closed expressions can be evaluated to any other instance of Num+result :: Int+result = convertClosed (subst expr)
examples/NonEmptyList.hs view
@@ -1,7 +1,4 @@-{-# LANGUAGE- MultiParamTypeClasses- , FlexibleInstances- #-}+{-# LANGUAGE FlexibleInstances #-} module NonEmptyList where import Data.Functor.Free
free-functors.cabal view
@@ -1,5 +1,5 @@ name: free-functors-version: 0+version: 0.1 synopsis: Provides free functors that are adjoint to functors that forget class constraints. description: A free functor is a left adjoint to a forgetful functor. It used to be the case that the only category that was easy to work with in Haskell was Hask itself, so@@ -42,7 +42,8 @@ base >= 4.4 && < 5, constraints >= 0.3.2 && < 0.4, transformers >= 0.2.0.0 && < 0.4,- comonad >= 3.0 && < 3.1+ comonad >= 3.0 && < 3.1,+ void >= 0.4 && < 0.6 source-repository head type: git
src/Data/Functor/Free.hs view
@@ -32,6 +32,7 @@ import Data.Functor.Compose import Data.Foldable import Data.Traversable+import Data.Void -- | The free functor for constraint @c@.@@ -88,3 +89,5 @@ convert :: (c (f a), Applicative f) => Free c a -> f a convert = rightAdjunct pure +convertClosed :: c x => Free c Void -> x+convertClosed = rightAdjunct absurd
src/Data/Functor/HFree.hs view
@@ -3,10 +3,6 @@ , RankNTypes , TypeOperators , FlexibleInstances- , GADTs- , MultiParamTypeClasses- , UndecidableInstances- , ScopedTypeVariables #-} ----------------------------------------------------------------------------- -- |@@ -26,7 +22,6 @@ ----------------------------------------------------------------------------- module Data.Functor.HFree where -import Control.Monad import Control.Applicative import Control.Monad.Trans.Class