category-theory

Scala — How to use Functors on non-Function types?

感情迁移 提交于 2019-12-03 12:49:14
While reading the description of Functors on this blog: https://hseeberger.wordpress.com/2010/11/25/introduction-to-category-theory-in-scala/ there is a generic definition of Functor and a more specific one: trait GenericFunctor[->>[_, _], ->>>[_, _], F[_]] { def fmap[A, B](f: A ->> B): F[A] ->>> F[B] } trait Functor[F[_]] extends GenericFunctor[Function, Function, F] { final def fmap[A, B](as: F[A])(f: A => B): F[B] = fmap(f)(as) } Clearly this means Functors can be used with other higher-kinded types besides Function objects. Could someone please give an example or explain how or why or in

Initial algebra for rose trees

人盡茶涼 提交于 2019-12-03 12:41:25
As far as I understand, recursive data types from Haskell correspond to initial algebras of endofunctors from the Hask category [ 1 , 2 ]. For example: Natural numbers, data Nat = Zero | Succ Nat , correspond to the initial algebra of the endofunctor F(-) = 1 + (-) . Lists, data List a = Nil | Cons a (List a) , correspond to the initial algebra of the endofunctor F(A, -) = 1 + A × (-) . However, it's not clear to me what the endofunctor corresponding to the rose trees should be: data Rose a = Node a (List (Rose a)) What confuses me is that there are two recursions: one for the rose tree and

Does * in (<*>) have a special meaning?

家住魔仙堡 提交于 2019-12-03 12:13:39
问题 Trying to expand my understanding about symbols in Haskell : ($) : Function Application operator (Allow you to apply arguments over a function) (&) : flipped version of Function Application Operator? (&) = flip ($) (<>) : associative operator (You'll find it in Semigroups and Monoids) (<$>) : function application ($) lifted over a Functor structure (<&>) : flipped functor map Can we make a link between (*) and (<*>) ? I don't understand the meaning of * actually... 回答1: This is deliberate. <*

Relation between `DList` and `[]` with Codensity

こ雲淡風輕ζ 提交于 2019-12-03 11:01:54
问题 I've been experimenting with Codensity lately which is supposed to relate DList with [] among other things. Anyway, I've never found code that states this relation. After some experiments I ended up with this: {-# LANGUAGE RankNTypes #-} module Codensity where newtype Codensity f a = Codensity { runCodensity :: forall b. (a -> f b) -> f b } type DList a = Codensity [] [a] nil :: DList a nil = Codensity ($ []) infixr 5 `cons` cons :: a -> DList a -> DList a cons x (Codensity xs) = Codensity ($

Do all Type Classes in Haskell Have a Category Theoretic Analogue?

我与影子孤独终老i 提交于 2019-12-03 10:48:29
问题 Consider a type class whose members are of type * -> * . For example: the Functor typeclass. It is a well-known fact that, in Haskell, there is a correspondence between this typeclass and its mathematical (i.e., Category Theoretic) analogue. Generalizing: Question 1: Does every every typeclass in Haskell whose members are of kind * -> * correspond to some function between categories? Now consider a typeclass whose members are of type * . For example, one could imagine a type class Group which

Structurally enforced Free Alternative, without left distributivity

只谈情不闲聊 提交于 2019-12-03 10:44:44
There is a nice Free Alternative in the great free package, which lifts a Functor to a left-distributive Alternative. That is, the claim is that: runAlt :: Alternative g => (forall x. f x -> g x) -> Alt f a -> g a is an Alternative homomorphism , with liftAlt . And, indeed, it is one, but only for left-distributive Alternative instances. Of course, in reality, very few Alternative instances are actually left-distributive. Most of the alternative instances that actually matter (parsers, MaybeT f for most Monad f, etc.) are not left-distributive. This fact can be shown by an example where runAlt

List based on right Kan extension

时光总嘲笑我的痴心妄想 提交于 2019-12-03 10:36:38
In the `` Kan Extensions for Program Optimisation '' by Ralf Hinze there is the definition of List type based on right Kan extension of the forgetful functor from the category of monoids along itself (section 7.4). The paper gives Haskell implementation as follows: newtype List a = Abstr { apply :: forall z . (Monoid z) => (a -> z) -> z } I was able to define usual nil and cons constructors: nil :: List a nil = Abstr (\f -> mempty) cons :: a -> List a -> List a cons x (Abstr app) = Abstr (\f -> mappend (f x) (app f)) With the following instance of Monoid class for Maybe functor, I managed to

Open Type Level Proofs in Haskell/Idris

人走茶凉 提交于 2019-12-03 08:08:08
问题 In Idris/Haskell, one can prove properties of data by annotating the types and using GADT constructors, such as with Vect, however, this requires hardcoding the property into the type (e.g. a Vect has to be a separate type from a List). Is it possible to have types with an open set of properties (such as list carrying both a length and running average), for example by overloading constructors or using something in the vein of Effect? 回答1: I believe that McBride has answered that question (for

How to define equality for Category instances?

别说谁变了你拦得住时间么 提交于 2019-12-03 06:22:16
In order to prove that for instance the Category laws hold for some operations on a data type, how do one decide how to define equality? Considering the following type for representing boolean expressions: data Exp = ETrue | EFalse | EAnd Exp Exp deriving (Eq) Is it feasible trying to prove that Exp forms a Category with identity ETrue and operator: (<&>) = EAnd without redefining the Eq instance? Using the default instance of Eq the left-identity law breaks, i.e: ETrue <&> e == e evaluates to False . However, defining an eval function : eval ETrue = True eval EFalse = False eval (EAnd e1 e2)

Do monad transformers, generally speaking, arise out of adjunctions?

强颜欢笑 提交于 2019-12-03 05:14:29
In Adjoint functors determine monad transformers, but where's lift? , Simon C has shown us the construction... newtype Three u f m a = Three { getThree :: u (m (f a)) } ... which, as the answers there discuss, can be given an instance Adjunction f u => MonadTrans (Three u f) ( adjunctions provides it as AdjointT ). Any Hask/Hask adjunction thus leads to a monad transformer; in particular, StateT s arises in this manner from the currying adjunction between (,) s and (->) s . My follow-up question is: does this construction generalise to other monad transformers? Is there a way to derive, say,