Haskell, multiple type classes for one argument

This is an example in Learn You A Haskell, chapter on higher order functions: compareWithHundred :: (Num a, Ord a) => a -> Ordering compareWithHundred x = compare 100 x While the idea of the function is clear for me, I'm not sure why type signature is (Num a, Ord a). We only pass integer that is to be compared to the function, of type Int. What Ord stands for here, and why is im

Haskell,一个参数的多个类型类

这是Learn You Haskell的一个例子,关于高阶函数的章节: compareWithHundred :: (Num a, Ord a) => a -> Ordering compareWithHundred x = compare 100 x 虽然函数的思想对我来说很清楚,但我不确定为什么类型签名是(数字a,数字a)。 我们只传递要与Int类型的函数进行比较的整数。 Ord在这里代表什么,为什么在类型签名中隐含地传递了参数? 这不是签名的唯一可能签名。 它恰好是最普遍的一个。 compareWit

Why does this function fail to typecheck?

During a lecture on functional programming we saw the following Haskell function: f :: Bool -> Int -> (a -> Int) -> Int f x y z = if x then y + y else (z x) + (z y) It is expected that this function will fail to typecheck. However, the reason why this happens was not explained. When trying it out in GHCI I got the following output: Prelude> :l test [1 of 1] Compiling Main

为什么此功能无法检测?

在函数式编程的讲座中,我们看到了以下Haskell函数: f :: Bool -> Int -> (a -> Int) -> Int f x y z = if x then y + y else (z x) + (z y) 预计此功能将无法检测。 但是,这种情况发生的原因没有解释。 在GHCI中试用时,我得到了以下输出: Prelude> :l test [1 of 1] Compiling Main ( test.hs, interpreted ) test.hs:2:35: Couldn't match expected type `a' with actual type `Bool'

Does Haskell have return type overloading?

Based on what I've read about Haskell, and the experimentation I've done with GHC, it seems like Haskell has return type overloading (aka ad hoc polymorphism). One example of this is the fromInteger function which can give you a Double or an Integer depending on where the result is used. For example: fd :: Double -> String fd x = "Double" fi :: Integer -> String fi x = "Integer"

Haskell是否有返回类型重载?

根据我读过的关于Haskell的内容,以及我用GHC所做的实验,Haskell似乎有返回类型重载(又名ad hoc多态)。 其中一个例子是fromInteger函数,它可以给你一个Double或Integer取决于结果的使用位置。 例如: fd :: Double -> String fd x = "Double" fi :: Integer -> String fi x = "Integer" fd (fromInteger 5) -- returns "Double" fi (fromInteger 5) -- returns "Integer" Haskell的一个温柔的介绍似乎同意这一

Effects of monomorphism restriction on type class constraints

This code breaks when a type declaration for baz is added: baz (x:y:_) = x == y baz [_] = baz [] baz [] = False A common explanation (see Why can't I declare the inferred type? for an example) is that it's because of polymorphic recursion. But that explanation doesn't explain why the effect disappears with another polymorphically recursive example: foo f (x:y:_) = f x y foo f [_]

单态限制对类型约束的影响

此代码在添加baz的类型声明时中断: baz (x:y:_) = x == y baz [_] = baz [] baz [] = False 一个常见的解释(请参阅为什么我不能声明推断的类型?例如)是因为多态递归。 但是这个解释并不能解释为什么这个效应会以另一个多态递归的例子消失: foo f (x:y:_) = f x y foo f [_] = foo f [] foo f [] = False 它也不能解释为什么GHC认为递归没有类型声明是单形的。 与示例的解释可以reads在http://www.haskell.org/onlin

Mapping to String

I am new to Haskell, so maybe I am missing some fundamental concepts here (or maybe failed to find the appropriate extension). I was wondering if there was a way to optimize or further abstract the following scenario. This code seems very redundant. Let's say I have the following data classes: data Person = Person { personName :: !String , personAge :: !Int

映射到字符串

我是Haskell的新手,所以也许我在这里错过了一些基本概念(或者可能找不到合适的扩展名)。 我想知道是否有一种方法来优化或进一步提取以下方案。 这段代码看起来非常冗余。 假设我有以下数据类: data Person = Person { personName :: !String , personAge :: !Int } deriving Show data Dog = Dog { dogName :: !String , dogAge :: !Int

Haskell just using the read function signals an error

Can anybody explain, why it is valid to read a number to add it to a another number, although reading just a number is not valid? Prelude> read "5" + 3 8 Prelude> read "5" :33:1: No instance for (Read a0) arising from a use of `read' The type variable `a0' is ambiguous Possible fix: add a type signature that fixes these type variable(s) Note: there are several potential instanc

Haskell只是使用read函数发出错误信号

任何人都可以解释,为什么阅读一个数字将其添加到另一个数字是有效的,但只读一个数字是无效的? Prelude> read "5" + 3 8 Prelude> read "5" :33:1: No instance for (Read a0) arising from a use of `read' The type variable `a0' is ambiguous Possible fix: add a type signature that fixes these type variable(s) Note: there are several potential instances: instance Read () -- Defined i

Fixing type inference in HLists

I've been trying to get the some code to compile. It's meant to take a HList , extract out the strings and concatenate them together. {-# LANGUAGE RankNTypes #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE TypeSynonymInstances #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE MultiParamTypeClasses #-} module Lib ( y ) where import Data.HList data HConcat2 = HConcat2 instance A

HLists中的修复类型推断

我一直试图让一些代码来编译。 它的意思是采取一个HList ,提取出字符串并将它们连接在一起。 {-# LANGUAGE RankNTypes #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE TypeSynonymInstances #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE MultiParamTypeClasses #-} module Lib ( y ) where import Data.HList data HConcat2 = HConcat2 instance ApplyAB HConcat2 (String, String) String where applyA

Folding over a polymorphic list in Haskell

I have a collection of records spread across a number of types in a large Haskell application that reference each other. All of the types involved implement a common typeclass. The typeclass contains functions that work over a variable and all of its children, very much like uniplate's para function. This is a simplified code sample of what I'd like to build. Is it possible (and reas

在Haskell中折叠多态列表

我有一个散布在大量Haskell应用程序中的多个类型的记录集合,这些应用程序可以相互引用。 所有涉及的类型都实现了一个常见的类型类。 类型类包含对变量及其所有子元素起作用的函数,非常类似于uniplate的para函数。 这是我想要构建的简化代码示例。 是否有可能(和合理的)获得泛型功能来折叠在GHC中实现给定类型类型的记录字段... {-# LANGUAGE RankNTypes #-} myPara :: forall a r . (Data a, Foo a) => (for

Why are polymorphic values not inferred in Haskell?

Numeric literals have a polymorphic type: *Main> :t 3 3 :: (Num t) => t But if I bind a variable to such a literal, the polymorphism is lost: x = 3 ... *Main> :t x x :: Integer If I define a function, on the other hand, it is of course polymorphic: f x = 3 ... *Main> :t f f :: (Num t1) => t -> t1 I could provide a type signature to ensure the x remains polymorphic: x :: N

为什么在Haskell中不推断多态值?

数字文字具有多态类型: *Main> :t 3 3 :: (Num t) => t 但是,如果我将一个变量绑定到这样的文字上,多态性就会丢失: x = 3 ... *Main> :t x x :: Integer 如果我定义一个函数,另一方面,它当然是多态的: f x = 3 ... *Main> :t f f :: (Num t1) => t -> t1 我可以提供一个类型签名来确保x保持多态: x :: Num a => a x = 3 ... *Main> :t x x :: (Num a) => a 但为什么这是必要的? 为

How to create a polyvariadic haskell function?

I need a function which takes an arbitrary number of arguments (All of the same type), does something with them and afterwards gives a result back. A list of arguments is impracticable in my specific case. As I looked through the haskell libs, I saw that the function printf (from module Text.Printf ) uses a similar trick. Unfortunately, I couldn't understand that magic by looking at the s

如何创建一个polyvariadic haskell函数?

我需要一个函数,它接受任意数量的参数(所有相同类型),对它们做一些事情,然后再给出结果。 在我的具体情况下,争议清单是不切实际的。 当我查看haskell库时,我看到函数printf (来自模块Text.Printf )使用类似的技巧。 不幸的是,我无法通过查看源代码来理解这种魔法。 有人可以解释如何实现这个目标,或者至少有一些网页/论文/无论我在哪里可以找到一个好的描述? 动机: 我需要这个的原因非常简单。 对于学校