type-signature

What's going on in this type signature? (Vector.Mutable modifiers in Haskell)

杀马特。学长 韩版系。学妹 提交于 2020-01-01 08:45:59
问题 Mutable vectors in Haskell have three element-level mutators: read :: PrimMonad m => MVector (PrimState m) a -> Int -> m a write :: PrimMonad m => MVector (PrimState m) a -> Int -> a -> m () swap :: PrimMonad m => MVector (PrimState m) a -> Int -> Int -> m () Now I can use these fine -- import Data.Vector import Data.Vector.Mutable import Control.Monad.ST import Control.Monad.Primitive incrAt :: Vector Double -> Int -> Vector Double incrAt vec i = runST $ do mvec <- thaw vec oldval <- read

What's going on in this type signature? (Vector.Mutable modifiers in Haskell)

拈花ヽ惹草 提交于 2020-01-01 08:45:27
问题 Mutable vectors in Haskell have three element-level mutators: read :: PrimMonad m => MVector (PrimState m) a -> Int -> m a write :: PrimMonad m => MVector (PrimState m) a -> Int -> a -> m () swap :: PrimMonad m => MVector (PrimState m) a -> Int -> Int -> m () Now I can use these fine -- import Data.Vector import Data.Vector.Mutable import Control.Monad.ST import Control.Monad.Primitive incrAt :: Vector Double -> Int -> Vector Double incrAt vec i = runST $ do mvec <- thaw vec oldval <- read

What are the possible operators for traits in a where clause in Rust?

。_饼干妹妹 提交于 2019-12-24 09:28:19
问题 I'm learning Rust and got to the chapter on trait bounds. In that chapter, they give an example with the + (plus) operator to enumerate all required traits in a where clause. What other operators are allowed on traits in Rust? I searched but I wasn't able to find any documentation about this. Does Rust support 'or' notation, brackets, negation? 回答1: 'or' notation No. Such a thing doesn't make sense to me — what would the code do if something could be A OR B ? brackets No, unless you count the

Haskell `forever` type signature

人走茶凉 提交于 2019-12-23 07:29:34
问题 In Haskell why is type-signature of forever forever :: Monad m => m a -> m b Specifically why isn't it just :: Monad m => m a -> m a ? Surely the type of monad we are acting upon doesn't change half way through forever ? A function such as: forever' :: Monad m => m a -> m a forever' = forever seems to work exactly the same. 回答1: The type signature of forever is crafted to suggest that its result runs forever. Specifically, there is no way to write an action of type m b (polymorphic in its

Haskell `forever` type signature

我的未来我决定 提交于 2019-12-23 07:29:09
问题 In Haskell why is type-signature of forever forever :: Monad m => m a -> m b Specifically why isn't it just :: Monad m => m a -> m a ? Surely the type of monad we are acting upon doesn't change half way through forever ? A function such as: forever' :: Monad m => m a -> m a forever' = forever seems to work exactly the same. 回答1: The type signature of forever is crafted to suggest that its result runs forever. Specifically, there is no way to write an action of type m b (polymorphic in its

Should one specify a type signature for main or not? Why / why not?

爷,独闯天下 提交于 2019-12-22 01:53:14
问题 I learned from chapter 9 of Learn You A Haskell For Great Good that By convention, we don't usually specify a type declaration for main . As far as I can tell, this convention is widespread. However, if I compile, using the -Wall flag, a program that lacks a type signature for main , such as -- test.hs -- main :: IO () main = print (1 :: Int) GHC does issue a warning: $ ghc -Wall test.hs [1 of 1] Compiling Main ( test.hs, test.o ) test.hs:2:1: Warning: Top-level binding with no type signature

Why is it so uncommon to use type signatures in where clauses?

被刻印的时光 ゝ 提交于 2019-12-22 01:26:46
问题 Does it help the compiler to optimise, or is it just surplus work to add additional type signatures? For example, one often sees: foo :: a -> b foo x = bar x where bar x = undefined Rather than: foo :: a -> b foo x = bar x where bar :: a -> b bar x = undefined If I omit the top level type signature, GHC gives me a warning, so if I don't get warnings I am quite confident my program is correct. But no warnings are issued if I omit the signature in a where clause. 回答1: Often definitions in where

Type signatures that never make sense

╄→尐↘猪︶ㄣ 提交于 2019-12-21 11:05:30
问题 Consider (a->a) -> [a] -> Bool Is there any meaningful definition for this signature? That is, a definition that not simply ignores the argument? x -> [a] -> Bool It seems there are many such signatures that can be ruled out immediately. 回答1: Carsten König suggested in a comment to use the free theorem. Let's try that. Prime the cannon We start by generating the free theorem corresponding to the type (a->a) -> [a] -> Bool . This is a property that every function with that type must satisfy,

Overloading function signatures haskell

只谈情不闲聊 提交于 2019-12-20 16:57:33
问题 I get the following error message when I compile: Duplicate type signature: weightedMedian.hs:71:0-39: findVal :: [ValPair] -> Double -> Double weightedMedian.hs:68:0-36: findVal :: [ValPair] -> Int -> Double My solution is to have findValI and findValD. However, findValI just converts the Int type to a Double and calls findValD. Also I can't pattern match on types of Num (Int, Double) so I can't just change the type signature to findVal :: [ValPair] -> Num -> Double In many languages I

Type error when testing a function with a negative number

☆樱花仙子☆ 提交于 2019-12-18 09:16:11
问题 I am following along with the Learn you a Haskell for great good , I have implemented take' : take' :: (Ord i, Num i) => i -> [a] -> [a] take' n _ | n <= 0 = [] take' _ [] = [] take' n (x:xs) = x: take' (n-1) xs When testing the function with: take' -2 [2] instead of getting an empty list, I have this message: Non type-variable argument in the constraint: Num (i -> [a] -> [a]) (Use FlexibleContexts to permit this) When checking that ‘it’ has the inferred type it :: forall i a t. (Num i, Num t