总和类型-为什么在Haskell中show(Int | Double)与`(show Int)|不同? (显示双)` [英] Sum types - Why in Haskell is `show (Int | Double)` different than `(show Int) | (show Double)`

查看:99
本文介绍了总和类型-为什么在Haskell中show(Int | Double)与`(show Int)|不同? (显示双)`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这些不相等?

show $ if someCondition then someInt else some double

if someCondition then show someInt else show someDouble

如果您将第一个示例中的 if ... else 部分单独隔离为一个表达式,那么您就无法用匿名的总和类型( Int | Double ,就像您可以在TypeScript中轻松进行的操作一样(提及TypeScript,因为它是我经常使用的语言,并且支持Sum类型),因此必须使用 Either 然后基于它的数据将调用 show

I understand that if you isolate the if ... else part in the first example to an expression by itself then you can't represent its type with an anonymous sum type, the kind of Int | Double, like something you could do easily in TypeScript (mentioning TypeScript because it is the langauge I used often and that supports Sum types), and would have to resort to using the Either data then based on it would call show.

我在这里给出的示例是琐碎,但对我而言,认为好吧,我们将要显示一些东西,而某些东西取决于 someCondition ,而不是好吧,如果someCondition为真,则显示someInt否则显示someDouble,并且还减少了代码重复(此处的显示重复了两次,但这也可能是一个长函数应用程序,而不是 if ... else 可能要考虑> 2个分支)

The example I gave here is trivial but to me it makes more sense to think "Okay we are going to show something, and that something depends on someCondition" rather than "Okay if someCondition is true then show someInt otherwise show someDouble", and also allows for less code duplication (here the show is repeated twice but it could also be a long function application and instead of an if ... else there could be >2 branches to consider)

在我看来,编译器应该很容易检查是否每个类型都构成求和类型(此处 Int | Double )可用作 show 函数的参数,并确定类型是否正确或不。更好的是,无论参数的类型如何, show 函数始终返回 string ,因此编译器不会必须携带所有可能的分支(因此所有可能的类型)。

In my mind it should be easy for the compiler to check if each of the types that make the sum type (here Int | Double) could be used as a parameter to show function and decides if the types are correct or not. Even better is that show function always returns a string no matter the types of the parameters, so the compiler doesn't have to carry with it all the possible "branches" (so all the possible types).

是否选择不存在这样的功能?还是我想实现起来更困难?

Is it by choice that such a feature doesn't exist? Or is implementing it harder that I think?

推荐答案

表达式的所有部分都必须正确键入。 的类型,如果someCondition然后someInt else someDouble 必须像存在。显示一个=> a ,但是Haskell不支持这种存在性量化。

All parts of an expression must be well-typed. The type of if someCondition then someInt else someDouble would have to be something like exists a. Show a => a, but Haskell doesn't support that kind of existential quantification.

更新:为 chi在评论中指出,如果Haskell支持联合/交叉类型(与求和/乘积类型不同),也可以,但是不幸的是,并非如此。

Update: As chi points out in a comment, this would also be possible if Haskell had support for union/intersection types (which are not the same as sum/product types), but it unfortunately doesn't.

这篇关于总和类型-为什么在Haskell中show(Int | Double)与`(show Int)|不同? (显示双)`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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