什么是一些区分类别的类型? [英] What are some types that discriminate between categories?

查看:172
本文介绍了什么是一些区分类别的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然熟悉所有这些类别理论的东西,几乎每个我看到的例子都是用一个Maybe或一个数组。但我还没有发现任何区分这些类别的例子。例如,以下是我仍然无法回答的一些问题:


  1. 什么是半群也不是一个Monoid ?

  2. 什么是可折叠的,它也不是可穿越的? [Duplicate]

  3. 什么是Functor也不是应用程序?

  4. 什么是应用程序也不是应用程序?
  5. >
  6. 什么是应用,这不是一个链?

  7. 什么是一个链也不是一个单子?

  8. 什么是不是Monad的应用程序? [Duplicate]

我通过 JavaScript FantasyLand ,所以这就是我从中得到的术语 - 我知道这些东西有不同的词。



解决方案

1。什么是不是半群的半群?



仙人掌给出了半群的一个很好的例子,它不是一个幺半群。任何类型的非空(有限)列表表示该类型的自由半群。另一个例子是 Data.Void.Void ,它不是 Monoid ,因为它没有任何元素,因此没有身份要素。另一个例子是加法下的一组正整数。

3。



其中一个 Functor 不是应用处理程序

 数据处理程序a where 
Handler :: Exception e => (e - > IO a) - > Handler a

实例Functor Handler其中
fmap f(Handler h)= Handler(\e-> f< $> $)
Handler f :: Handler(a - > b)
和<$ c $ b>

$ c> Handler g :: Handler a ,你有

  f :: e1  - > IO(a  - > b)
g :: e2 - > IO a

其中 e1 e2 是(可能不同)异常类型。您需要创建 h :: e3 - > IO b 用于某些异常类型 e3 。没有真正的 方法来执行此操作。



似乎很难找到 Functor 应用 不能 c>只有一条法律,因此承认 Applicative 会被拒绝的各种奇怪的事情。



4。 Whats an Apply that is not also also a Applicative?



6。什么是不是Monad的应用程序?



Map k IntMap 。当 a 时,(,)a Const a Semigroup ,但不是 Monoid 。类似地,其他一些类型适合接受 Apply 和/或绑定的较弱上下文的模式比 Applicative Monad



5。什么是申请,这不是一个链?



ZipList 是一个申请但不是绑定。我不知道 Chain 是什么。






* *一种半合理的方式可能看起来像这样:

pre $ y $ P $ y $ P $ b实例(异常x,异常y)=>
例外(P x y)
实例应用处理程序其中
Handler f<。> Handler g =
Handler(\(P e1 e2) - > f e1 * g e2)



think 这个服从 Apply 定律,但我还没完全确定。


I'm still getting familiar with all this category theory stuff, and just about every example I see is with a Maybe or an Array. But I haven't found any examples that discriminate between these categories. For example, here are some of the questions I've still been unable to answer:

  1. whats a Semigroup that isn't also a Monoid?
  2. whats a Foldable that isn't also a Traversable? [Duplicate]
  3. whats an Functor that isn't also an Apply?
  4. whats an Apply that isn't also an Applicative?
  5. whats an Apply that isn't also a Chain?
  6. whats a Chain that isn't also a Monad?
  7. what an Applicative that isn't also a Monad? [Duplicate]

I'm learning this stuff through the context of JavaScript FantasyLand so that's where I'm getting the lingo from -- I understand there are different words for these things.

解决方案

1. Whats a Semigroup that isn't also a Monoid?

Cactus gives a great example of a semigroup that's not a monoid. Non-empty (finite) lists of any type represent the free semigroup over that type. Another example is Data.Void.Void, which isn't a Monoid because it doesn't have any elements and therefore doesn't have an identity element. Yet another example is the set of positive integers under addition.

3. Whats an Functor that isn't also an Apply?

One Functor that's not an Apply is Handler.

data Handler a where
  Handler :: Exception e => (e -> IO a) -> Handler a

instance Functor Handler where
  fmap f (Handler h) = Handler (\e -> f <$> h e)

Given Handler f :: Handler (a -> b) and Handler g :: Handler a, you have

f :: e1 -> IO (a -> b)
g :: e2 -> IO a

Where e1 and e2 are (possibly different) types of exception. You need to create h :: e3 -> IO b for some exception type e3. There is no really sensible way to do this**.

It seems harder to find Functors that can't be made into law-abiding Apply instances, because Apply has just one law and therefore admits all sorts of weird things that Applicative would reject.

4. Whats an Apply that isn't also an Applicative?

6. What an Applicative that isn't also a Monad?

Map k and IntMap. Also, (,) a and Const a when a is a Semigroup but not a Monoid. Similarly, some other types fit the pattern of accepting a weaker context for Apply and/or Bind than for Applicative or Monad, respectively.

5. Whats an Apply that isn't also a Chain?

ZipList is an Apply but not a Bind. I don't know what a Chain is.


** One semi-sensible way might look like this:

data P x y = P x y deriving (Show, Typeable)
instance (Exception x, Exception y) =>
            Exception (P x y)
instance Apply Handler where
  Handler f <.> Handler g =
    Handler (\(P e1 e2) -> f e1 <*> g e2)

I think this obeys the Apply law, but I'm not yet completely certain.

这篇关于什么是一些区分类别的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆