Finding type signatures in Haskell

I'm playing with a small server in Haskell. As an exercise, I am manually adding type signatures. import System.Posix (installHandler, sigPIPE, getFileStatus, fileSize, Handler(..)) import Network (listenOn, accept, PortID(..)) import qualified Data.ByteString.Lazy as B import Data.Binary import Network.Socket (close) import Control.Monad (forever, replicateM_) import System.Environment (ge

在Haskell中查找类型签名

我正在玩Haskell中的一个小型服务器。 作为练习,我手动添加类型签名。 import System.Posix (installHandler, sigPIPE, getFileStatus, fileSize, Handler(..)) import Network (listenOn, accept, PortID(..)) import qualified Data.ByteString.Lazy as B import Data.Binary import Network.Socket (close) import Control.Monad (forever, replicateM_) import System.Environment (getArgs) import System.IO (hGetConten

Type inference of instance functions

The Problem I want to be able to create 2 data types : A and B and to create 2 functions f : f :: A -> Int -> Int f :: B -> String -> String -> String The only way I can do it (so far as I know) is to use type classes and instances . The problem is, that I do not want to explicit write f signatures - I want type checker to infer it for me. Is it possible? Example code

输入实例函数的推理

问题 我希望能够创建2种data types : A和B并创建2个函数f : f :: A -> Int -> Int f :: B -> String -> String -> String 我能做到的唯一方法就是使用type classes和instances 。 问题是,我不想明确写出f签名 - 我想让类型检查器为我推断它。 可能吗? 示例代码 {-# LANGUAGE FlexibleInstances, FunctionalDependencies, UndecidableInstances #-} data A = A{ax::Int} deriving(Show) data B

What is a type "t" in an inferred Haskell type signature?

I wrote a function f , which I use in a foldM : foldM (f xs) [] ids ... f xs acc id = case lookup id xs of Just x -> return $ acc ++ [(id, x)] Nothing -> throwError $ TypeError "Cannot project nonexisting field" the type signature I wrote for it is: [(String, Value)] -> [(String, Value)] -> String -> EvalMonad [(String, Value)] then I decided to delete the type signatur

推断的Haskell类型签名中的类型“t”是什么?

我写了一个函数f ,这是我在使用foldM : foldM (f xs) [] ids ... f xs acc id = case lookup id xs of Just x -> return $ acc ++ [(id, x)] Nothing -> throwError $ TypeError "Cannot project nonexisting field" 我为它写的类型签名是: [(String, Value)] -> [(String, Value)] -> String -> EvalMonad [(String, Value)] 那么我决定删除类型签名,因为这个函数很简单并且足够描述。 当我使

GHC type inference woes

Question. Is there any way to make this code work without an explicit type signature? Code. First, I have a in-practice-much-nicer alternate MonadTrans class, inspired by Data.Newtype . It looks like this, {-# LANGUAGE FlexibleContexts, TypeFamilies #-} module Alt.Control.Monad.Trans where import Control.Monad class (Monad

GHC类型推断困境

题。 有没有什么办法可以让这段代码在没有显式类型签名的情况下工作? 码。 首先,我从Data.Newtype获得了一个实践中更好的替代MonadTrans类。 它看起来像这样, {-# LANGUAGE FlexibleContexts, TypeFamilies #-} module Alt.Control.Monad.Trans where import Control.Monad class (Monad

Perform Monadic Computation N times

I'm implementing a function to perform a monadic computation N times. I've written the following code. performN :: Monad m => Int -> m t -> m [t] performN 0 m = return [] performN n m = do x1<- m x2<- if n == 1 then return [] else (m:performN (n-2) m) return (x1:x2) I get the following error. :264:44: error: • Couldn't match type 'm' with 

单次计算N次

我正在实现一个函数来执行单次计算N次。 我写了下面的代码。 performN :: Monad m => Int -> m t -> m [t] performN 0 m = return [] performN n m = do x1<- m x2<- if n == 1 then return [] else (m:performN (n-2) m) return (x1:x2) 我收到以下错误。 :264:44:错误: •无法将类型“m”与“[]”匹配 'm'是一个刚性类型变量 类型签名为: performN :: forall(m :: * - >

Haskell GADT 'Show'

This code {-# LANGUAGE GADTs #-} data Expr a where Val :: Num a => a -> Expr a Eq :: Eq a => Expr a -> Expr a -> Expr Bool eval :: Expr a -> a eval (Val x) = x eval (Eq x y) = (eval x) == (eval y) instance Show a => Show (Expr a) where show (Val x) = "Val " ++ (show x) show (Eq x y) = "Eq (" ++ (show y) ++ ") (" ++ (show x) ++ ")" fails to compile with th

Haskell GADT'显示'

这个代码 {-# LANGUAGE GADTs #-} data Expr a where Val :: Num a => a -> Expr a Eq :: Eq a => Expr a -> Expr a -> Expr Bool eval :: Expr a -> a eval (Val x) = x eval (Eq x y) = (eval x) == (eval y) instance Show a => Show (Expr a) where show (Val x) = "Val " ++ (show x) show (Eq x y) = "Eq (" ++ (show y) ++ ") (" ++ (show x) ++ ")" 无法编译时出现以下错误消

Seemingly correct type signature refused in where block

I have been messing around with continuation passing style in Haskell. Taking the Cont monad apart and removing the type wrappers helped me in understanding the implementation. Here is the code: {-# LANGUAGE ScopedTypeVariables #-} import Control.Monad (ap) newtype Cont r a = Cont {runCont :: (a -> r) -> r} instance Functor (Cont r) where fmap f ka = ka >>= pure . f instance

看起来正确的类型签名被拒绝在哪里

我一直在Haskell中继续传递样式。 将Cont monad分开并移除类型包装器有助于我理解实现。 代码如下: {-# LANGUAGE ScopedTypeVariables #-} import Control.Monad (ap) newtype Cont r a = Cont {runCont :: (a -> r) -> r} instance Functor (Cont r) where fmap f ka = ka >>= pure . f instance Applicative (Cont r) where (<*>) = ap pure = return instance Monad (Cont r) where ka &

Why is my implementation of a map on a custom type not correct?

I'm following the List exercise from https://github.com/NICTA/course The below snippet copied from part of https://github.com/NICTA/course/blob/master/src/Course/List.hs data List t = Nil | t :. List t deriving (Eq, Ord) map :: (a -> b) -> List a -> List b map f a = filter (listElement -> listElement /= Nil) a The above gives me the below error: Couldn't mat

为什么我的自定义类型的地图实现不正确?

我正在从https://github.com/NICTA/course进行List练习 以下摘录自https://github.com/NICTA/course/blob/master/src/Course/List.hs的一部分 data List t = Nil | t :. List t deriving (Eq, Ord) map :: (a -> b) -> List a -> List b map f a = filter (listElement -> listElement /= Nil) a 以上给出了以下错误: 无法与实际类型'List t0'匹配预期类型'b''b'是由ma

Type signature is too general; missing constraint?

So I tried to ask a general "how do you debug type-level programming problems?" question, and it seems either the question is too general to answer, or maybe it's impossible to debug such things. :-} On this particular day, the problem that's irking me is a moderately large, complex program. I've managed to boil it down to a reasonably small core that still goes wrong.

类型签名过于笼统; 缺少约束?

所以我试着问一个将军:“你怎么调试类型编程问题?” 问题,似乎或者这个问题太笼统,无法回答,或者也许不可能调试这样的事情。 : - } 在这个特定的日子,令我感到厌烦的是一个中等大小,复杂的程序。 我已经设法将它归结为一个仍然出错的合理小核心。 开始了: {-# LANGUAGE GADTs, MultiParamTypeClasses, FlexibleInstances, FlexibleContexts, RankNTypes, KindSignatures #-} module Minimal where

implicitly coerce types in a Haskell do block?

My question relates to the answer to another question: https://stackoverflow.com/a/11766789/3212958 In his answer, ertes writes the following type signature select :: [a] -> [(a, [a])] However, when select is actually used, ertes writes the following inside of a do block (y, ys) <- select xs Please help me shed some light on how the tuple (y, ys) matches the return type of select, nam

在Haskell do块中隐式强制类型?

我的问题与另一个问题的答案有关:https://stackoverflow.com/a/11766789/3212958 在他的回答中,ertes写下了以下类型的签名 select :: [a] -> [(a, [a])] 但是,实际使用select时,ertes在do块中写入以下内容 (y, ys) <- select xs 请帮助我了解元组(y, ys)匹配select的返回类型,即[(a, [a])] 。 Haskell在某些时候强制类型? (是否曾经哈斯克尔强制类型?)是<-提取类型的元组(a, [a])从列表单子是select回