functor

Are Functor instances unique?

天涯浪子 提交于 2019-11-27 21:16:21
I was wondering to what extent Functor instances in Haskell are determined (uniquely) by the functor laws. Since ghc can derive Functor instances for at least "run-of-the-mill" data types, it seems that they must be unique at least in a wide variety of cases. For convenience, the Functor definition and functor laws are: class Functor f where fmap :: (a -> b) -> f a -> f b fmap id = id fmap (g . h) = (fmap g) . (fmap h) Questions: Can one derive the definition of map starting from the assumption that it is a Functor instance for data List a = Nil | Cons a (List a) ? If so, what assumptions have

To what extent are Applicative/Monad instances uniquely determined?

旧巷老猫 提交于 2019-11-27 19:17:12
问题 As described this question/answers, Functor instances are uniquely determined, if they exists. For lists, there are two well know Applicative instances: [] and ZipList. So Applicative isn't unique (see also Can GHC derive Functor and Applicative instances for a monad transformer? and Why is there no -XDeriveApplicative extension?). However, ZipList needs infinite lists, as its pure repeats a given element indefinitely. Are there other, perhaps better examples of data structures that have at

An example of a Foldable which is not a Functor (or not Traversable)?

徘徊边缘 提交于 2019-11-27 18:27:34
A Foldable instance is likely to be some sort of container, and so is likely to be a Functor as well. Indeed, this says A Foldable type is also a container (although the class does not technically require Functor , interesting Foldable s are all Functor s). So is there an example of a Foldable which is not naturally a Functor or a Traversable ? (which perhaps the Haskell wiki page missed :-) ) Sjoerd Visscher Here's a fully parametric example: data Weird a = Weird a (a -> a) instance Foldable Weird where foldMap f (Weird a b) = f $ b a Weird is not a Functor because a occurs in a negative

Monads as adjunctions

这一生的挚爱 提交于 2019-11-27 17:01:07
I've been reading about monads in category theory. One definition of monads uses a pair of adjoint functors. A monad is defined by a round-trip using those functors. Apparently adjunctions are very important in category theory, but I haven't seen any explanation of Haskell monads in terms of adjoint functors. Has anyone given it a thought? Edit : Just for fun, I'm going to do this right. Original answer preserved below The current adjunction code for category-extras now is in the adjunctions package: http://hackage.haskell.org/package/adjunctions I'm just going to work through the state monad

How can it be useful to overload the “function call” operator?

我只是一个虾纸丫 提交于 2019-11-27 12:42:59
I recently discovered that in C++ you can overload the "function call" operator, in a strange way in which you have to write two pair of parenthesis to do so: class A { int n; public: void operator ()() const; }; And then use it this way: A a; a(); When is this useful? This can be used to create "functors" , objects that act like functions: class Multiplier { public: Multiplier(int m): multiplier(m) {} int operator()(int x) { return multiplier * x; } private: int multiplier; }; Multiplier m(5); cout << m(4) << endl; The above prints 20 . The Wikipedia article linked above gives more

How to return a pure value from a impure method

让人想犯罪 __ 提交于 2019-11-27 08:13:52
问题 I know it must sound trivial but I was wondering how you can unwrap a value from a functor and return it as pure value? I have tried: f::IO a->a f x=(x>>=) f= >>= What should I place in the right side? I can't use return since it will wrap it back again. 回答1: It's a frequently asked question: How do I extract 'the' value from my monad , not only in Haskell, but in other languages as well. I have a theory about why this question keeps popping up, so I'll try to answer according to that; I hope

passing functor as function pointer

为君一笑 提交于 2019-11-27 07:12:36
I'm trying to use a C library in a C++ app and have found my self in the following situation (I know my C, but I'm fairly new to C++). On the C side I have a collection of functions that takes a function pointer as their argument. On the C++ side I have objects with a functor which has the same signature as the function pointer needed by the C function. Is there any way to use the C++ functor as a function pointer to pass to the C function? You cannot directly pass a pointer to a C++ functor object as a function pointer to C code (or even to C++ code). Additionally, to portably pass a callback

Help with understanding a function object or functor in Java

此生再无相见时 提交于 2019-11-27 06:49:51
Can someone explain what a functor is and provide a simple example? Philip JF A function object is just that. Something which is both an object and a function. Aside: calling a function object a "functor" is a serious abuse of the term: a different kind of "functors" are a central concept in mathematics, and one that has a direct role in computer science (see "Haskell Functors"). The term is also used in a slightly different way in ML, so unless you are implementing one of these concepts in Java (which you can!) please stop using this terminology. It makes simple things complicated. Back to

F# changes to OCaml [closed]

一个人想着一个人 提交于 2019-11-27 05:51:51
F# is derived from OCaml, but what major items are missing or added? Specifically I'm curious as to whether the resources available for learning OCaml are also useful to someone who wants to learn F#. Chris Conway The main differences are that F# does not support: functors OCaml-style objects polymorphic variants the camlp4/5 preprocessor or extension points (ppx) In addition, F# has a different syntax for labeled and optional parameters. In theory, OCaml programs that don't use these features can be compiled with F#. Learning OCaml is a perfectly reasonable introduction to F# (and vice versa,

What monads can be expressed as Free over some functor?

一曲冷凌霜 提交于 2019-11-27 05:25:10
问题 The documentation for Free says: A number of common monads arise as free monads, Given data Empty a , Free Empty is isomorphic to the Identity monad. Free Maybe can be used to model a partiality monad where each layer represents running the computation for a while longer. What other monads are expressible using Free ? I could think only of one more: I believe Free (Const e) is isomorphic to Either e . Edit: What monads are not expressible using Free and why? 回答1: Almost all of them (up to