Trouble understanding types in Haskell - haskell

I'm given the following where:
data Card = Card Suit Rank
deriving (Eq, Ord, Show)
type BidFunc
= Card -- ^ trump card
-> [Card] -- ^ list of cards in the player's hand
-> Int -- ^ number of players
-> [Int] -- ^ bids so far
-> Int -- ^ the number of tricks the player intends to win
where I'm required to write a function of
makeBid :: BidFunc
makeBid = (write here)
The problem I'm having is that i couldnt understand the syntax of the function type declared which is BidFunc. I'm new to Haskell so i would appreciate if someone could give me an explanation clear enough on the function type above.
In particularly, why is there a '=' Card, followed by -> [Card] etc? Am i supposed to pass in arguments to the function type?

makeBid :: BidFunc is exactly the same as makeBid :: Car -> [Card] -> Int -> [Int] -> Int, so you would define the function in exactly the same way:
makeBid :: BidFunc
-- makeBid :: Card -> [Card] -> Int -> [Int] -> Int
makeBid c cs n bs = ...
As for the formatting of the type definition, it's just that: formatting. IMO, it would be a little clearer written as
type BidFunc = Card -- ...
-> [Card] -- ...
-> Int -- ...
-> [Int] -- ...
-> Int -- ...
if you want to comment on each argument and the return value. Without comments, it can of course be written on one line:
type BidFunc = Card -> [Card] -> Int -> [Int] -> Int
In general, type <lhs> = <rhs> just remeans that <lhs> is a name that can refer to whatever type <rhs> specifies.
As to why one might feel the need to define a type alias for something that isn't going to be reused often, I couldn't say. Are they any other functions beside makeBid that would have the same type?

Related

Strict type alias in Haskell

Suppose I have a recursive function taking 3 integers, each having a different meaning, e.g.
func :: Int -> Int -> Int -> SomeType1 -> SomeType2
What I want is to prevent myself from mistyping the order of the arguments like this (somewhere in the func implementation):
func a b c t = f b a c ( someProcessing t )
The easiest way I've come up with is to define type aliases like
type FuncFirstArg = Int
type FuncSecondArg = Int
type FuncThirdArg = Int
And change func signature:
func :: FuncFirstArg -> FuncSecondArg -> FuncThirdArg -> SomeType1 -> SomeType2
But it seems like this approach doesn't work as I intended. Why does Haskell still allow me to pass FuncSecondArg as a first argument and so on. Is there a way to do what I want without declaring datatypes?
type in Haskell is a rename of an existing type. Just like String and [Char] are fully exchangeable, so are FuncFirstArg and Int, and by transition FuncSecondArg as well.
The most normal solution is to use a newtype which was introduced exactly for the purpose of what you try to achieve. For convenience, it is good to declare it as a record:
newtype FuncFirstArg = FuncFirstArg {unFuncFirstArg :: Int}
Note that newtype is entirely reduced during compilation time, so it has no overhead on the runtime.
However, if you have many arguments like in your example, a common strategy is to create a dedicated type for all of the parameters supplied to the function:
data FuncArgs = FuncArgs
{ funcA :: Int
, funcB :: Int
, funcC :: Int
, funcT :: Sometype1
}
f :: FuncArgs -> Sometype2
Yes, it has some bad impact on currying and partial application, but in many cases you can deal with it by providing predefined argument packs or even uncurry the function:
defaultArgs :: Sometype1 -> FuncArgs
defaultArgs t = FuncArgs {a = 0, b = 0, c = 0, t = t}
fUnc :: Int -> Int -> Int -> SomeType1 -> SomeType2
fUnc a b c t = f $ FuncArgs a b c t
Conclusion
For the typechecker to distinguish types, the types have to be actually different. You can't skip defining new types, therefore.

How can I find the definition of a Prelude function?

I'm currently trying to find the definition of the words function to help get an idea for a similar function I'm writing. So I was wondering is there somewhere that has all the definitions of the Prelude functions? Maybe a GHCi command to show the definition of one, or something on the Haskell wiki, I'm not sure.
Or if there isn't somewhere I can find that do any of y'all know what the definitions of words is?
Most packages on Hackage come with documentation that also includes a link to the source code for every function. You can usually find the function via Hoogle.
In the case of words, the Prelude documentation is found here, and the source is found at https://hackage.haskell.org/package/base-4.16.3.0/docs/src/Data.OldList.html#words:
-- | 'words' breaks a string up into a list of words, which were delimited
-- by white space.
--
-- >>> words "Lorem ipsum\ndolor"
-- ["Lorem","ipsum","dolor"]
words :: String -> [String]
{-# NOINLINE [1] words #-}
words s = case dropWhile {-partain:Char.-}isSpace s of
"" -> []
s' -> w : words s''
where (w, s'') =
break {-partain:Char.-}isSpace s'
This should in general also work with local documentation.
For ghci specifically, there are :info and :list commands, but :list words only produces an error message ("cannot list source code for words: module base-…:Data.OldList is not interpreted") for me.
Documentation of a function.
To search for the definition, look at hackage. In the case of words: https://hackage.haskell.org/package/base-4.16.3.0/docs/Prelude.html#v:words
Finding a function
To find a function, use hoogle. It is the de-facto tool for finding a function.
Example:
This is inside the shell. Hoogle can be installed at the samne time as when ghc is.
Find a function like so:
> hoogle "subtract"
Prelude subtract :: Num a => a -> a -> a
GHC.Num subtract :: Num a => a -> a -> a
Distribution.Compat.Prelude.Internal subtract :: Num a => a -> a -> a
GHC.Prelude subtract :: Num a => a -> a -> a
Hedgehog.Internal.Prelude subtract :: Num a => a -> a -> a
BasePrelude subtract :: Num a => a -> a -> a
RIO.Prelude subtract :: Num a => a -> a -> a
System.Metrics.Gauge subtract :: Gauge -> Int64 -> IO ()
ClassyPrelude subtract :: Num a => a -> a -> a
Algebra.Additive subtract :: C a => a -> a -> a
-- plus more results not shown, pass --count=20 to see more
The cool thing is, you can also search for types and functions with certain types. For example, here is the list of functions which will take an Int and return an Int.
hoogle "Int -> Int"
GHC.Unicode wgencat :: Int -> Int
System.Win32.DebugApi dr :: Int -> Int
Codec.Picture.Jpg.Internal.Common toBlockSize :: Int -> Int
Statistics.Function nextHighestPowerOfTwo :: Int -> Int
Numeric.SpecFunctions log2 :: Int -> Int
Math.NumberTheory.Logarithms intLog2 :: Int -> Int
Math.NumberTheory.Logarithms intLog2' :: Int -> Int
Streamly.Internal.Data.Array.Foreign.Mut.Type roundUpToPower2 :: Int -> Int
Streamly.Internal.System.IO arrayPayloadSize :: Int -> Int
Data.Array.Comfort.Shape triangleSize :: Int -> Int
-- plus more results not shown, pass --count=20 to see more

Syntax in Functions (Expected kind `* -> Constraint', but `Int' has kind `*') [duplicate]

This question already has answers here:
`String' is applied to too many type arguments
(2 answers)
Closed 2 years ago.
I'm starting to learn Haskell and I'm struggling with syntax in Functions. I'm trying to create a function that receives a Number 'e' and a Number 'n', returning a list with of 'n' times 'e'.
Example: repn 3 5 would return [3,3,3,3,3]:
repn :: Int a => a -> a -> [a]
repn e n
| n >= 1 = (take n (repeat e))
| otherwise = []
But I'm getting this error:
* Expected kind `* -> Constraint', but `Int' has kind `*'
* In the type signature: replica :: Int a => a -> a
As a rule of thumb, writing something of the form Foo a => ... only makes sense of Foo is a type class. Unlike in OO languages, a class and a type in Haskell are completely different things. Int is a type, therefore it cannot be used in this way; instead you should simply use
repn :: Int -> Int -> [Int]
...though actually there's no reason to restrict the list-element type at all, the signature could as well be
repn :: a -> Int -> [a]
Alternatively, you can use a type class: the class of “int-like types” is Integral. Then you can't use take directly, as that is restricted to Int for the counter argument; however you can convert any integral type to Int:
repn :: Integral n => a -> n -> [a]
repn e n = take (fromIntegral n) $ repeat e
-- Note no case distinction is needed, because `take` already yields
-- an empty list if `n<1`.
Your type definition is not correct. The double arrow is used to show required typeclasses (aka kind * -> Constraint), or a constraint on types. However, Int is itself a type (aka kind *).
You can't specify types in this way. In your implementation, n must be an Int, but e could be anything. Since you specify that it should be a number, though, you can constrain it with the Num typeclass. The correct version of your signature therefore is:
repn :: Num a => a -> Int -> [a]
If you want e to be constrained to an Int, then your signature should be
repn :: Int -> Int -> [Int]
If you don't need any constraints on e, your signature can be
repn :: a -> Int -> [a]
These are all valid type signatures for the function you have written. (Though in actual code, I would just point you to the builtin replicate function.)

what does ((Ord, Ord) -> Bool) -> [String] -> String mean in haskell

I changed this question to make it more clear. shortestStringHelper has type (Int -> Int -> Bool) -> [String] -> String. Note the String may appear as [Char] instead depending on how Haskell feels at the time. This function will look a lot like shortestString and shortestString' but it is more general because it takes a function as an argument (Int -> Int -> Bool). Note: Depending on how you write it, you may get (Int -> Int -> Bool) -> [[a]] -> [a], which is also ok.
I know how to write the find the shortest string
shortestString :: [String] -> String
shortestString lst = if null lst then "" else foldl (\shortest x -> if length x < length shortest then x else shortest) (head lst) lst
shortestString' :: [String] -> String
shortestString' lst = if null lst then "" else foldl (\shortest x -> if length x <= length shortest then x else shortest) (head lst) lst
and they work, but I am not sure if I understand this question, and I don't know what does ((Ord, Ord) -> Bool) means.
Ord means something incompatible with that in the Prelude, but ordinarily the type signature you give would be a function with two arguments.
The first argument has type (Ord, Ord) -> Bool, a function that takes a pair of Ord and returns a Bool. In context, this might be intended as a generalized comparison function, such as one that compares the lengths of two strings expressed as whatever an Ord is here. This is unusual, because you would expect something like that to be written like isShorter :: String -> String -> Bool, or maybe isShorter :: [a] -> [a] -> Bool.
The second argument has type [String], a list of String objects. The function returns a single String.
However, you haven’t given us enough information to understand what’s going on in this specific assignment.
Edit: You’ve updated your question so that the type of the first argument is (Int, Int) -> Bool rather than (Ord, Ord) -> Bool, which makes more sense. This might be a way to wrap a comparison on two arguments, such as the lengths of two strings, for a higher-order function that expects one argument.

What to do with “Inferred type is less polymorphic than expected”?

I need the Numeric.FAD library, albeit still being completely puzzled by existential types.
This is the code:
error_diffs :: [Double] -> NetworkState [(Int, Int, Double)]
error_diffs desired_outputs = do diff_error <- (diff_op $ error' $ map FAD.lift desired_outputs)::(NetworkState ([FAD.Dual tag Double] -> FAD.Dual tag Double))
weights <- link_weights
let diffs = FAD.grad (diff_error::([FAD.Dual tag a] -> FAD.Dual tag b)) weights
links <- link_list
return $ zipWith (\link diff ->
(linkFrom link, linkTo link, diff)
) links diffs
error' runs in a Reader monad, ran by diff_op, which in turn generates an anonymous function to take the current NetworkState and the differential inputs from FAD.grad and stuffs them into the Reader.
Haskell confuses me with the following:
Inferred type is less polymorphic than expected
Quantified type variable `tag' is mentioned in the environment:
diff_error :: [FAD.Dual tag Double] -> FAD.Dual tag Double
(bound at Operations.hs:100:33)
In the first argument of `FAD.grad', namely
`(diff_error :: [FAD.Dual tag a] -> FAD.Dual tag b)'
In the expression:
FAD.grad (diff_error :: [FAD.Dual tag a] -> FAD.Dual tag b) weights
In the definition of `diffs':
diffs = FAD.grad
(diff_error :: [FAD.Dual tag a] -> FAD.Dual tag b) weights
this code gives the same error as you get:
test :: Int
test =
(res :: Num a => a)
where
res = 5
The compiler figured that res is always of type Int and is bothered that for some reason you think res is polymorphic.
this code, however, works fine:
test :: Int
test =
res
where
res :: Num a => a
res = 5
here too, res is defined as polymorphic but only ever used as Int. the compiler is only bothered when you type nested expressions this way. in this case res could be reused and maybe one of those uses will not use it as Int, in contrast to when you type a nested expression, which cannot be reused by itself.
If I write,
bigNumber :: (Num a) => a
bigNumber = product [1..100]
then when bigNumber :: Int is evaluated,
it's evaluating (product :: [Int] -> Int) [(1 :: Int) .. (100 :: Int)],
and when bigNumber :: Integer is evaluated,
it's evaluating (product :: [Integer] -> Integer) [(1 :: Integer) .. (100 :: Integer)].
Nothing is shared between the two.
error_diffs has a single type, that is: [Double] -> NetworkState [(Int, Int, Double)]. It must evaluate in exactly one way.
However, what you have inside:
... :: NetworkState ([FAD.Dual tag Double] -> FAD.Dual tag Double)
can be evaluated in different ways, depending on what tag is.
See the problem?

Resources