lacks an accompanying binding - What does it mean? How it works?
Asked Answered
C

3

12

I am practising from LYAH.

phoneBook.hs file contains following code:

phoneBook :: [(String, String)]

While trying to compile the above-mentioned code I am getting following error:

*Main> :load "/home/optimight/phoneBook.hs"
[1 of 1] Compiling Main             ( /home/optimight/phoneBook.hs, interpreted )

/home/optimight/phoneBook.hs:1:1:
    The type signature for `phoneBook' lacks an accompanying binding
Failed, modules loaded: none.

Question added after brano's answer and subsequent comment to this answer: How do we provide implementation for above-mentioned type signature?

If I add this :

type phoneBook = [(String, String)]

I am getting following error:

Prelude> :load "/home/optimight/phoneBook.hs"
[1 of 1] Compiling Main             ( /home/optimight/phoneBook.hs, interpreted )

/home/optimight/phoneBook.hs:2:6:
    Malformed head of type or class declaration: phoneBook
Failed, modules loaded: none
Consultative answered 27/7, 2012 at 7:37 Comment(0)
B
11

You need to provide an implementation for phoneBook.

phoneBook :: [(String, String)] is just the signature.

Boycott answered 27/7, 2012 at 7:41 Comment(14)
Why it is compulsory to provide an implementation? If I am developing a haskell application and if phoneBook is part of it, I will never bundle any data in it. It will be a blank phoneBook.Consultative
How do we provide implementation for this?Consultative
@Consultative e.g. phoneBook = []. Whatever value you choose to put here will be the value during the entire program execution. Instead of defining a top level value, phoneBook should probably be a function parameter or (part of) the state in a State or a StateT function.Zurek
with your implementation you are sayin that phoneBook is a function returning a list of string pair. You need to specify the implementation in order to use it. Maybe this is not what you want to do. Do you want to make a type definition? In that case you should type "type phoneBook = [(String,String)]Boycott
@dave4420: I could not understand your comment due to my very limited knowledge of haskell. Can you please give elaborate explanation along with the code, you are suggesting.Consultative
brano: With type phoneBook = [(String, String)] , I am getting this error: Prelude> :load "/home/optimight/phoneBook.hs" [1 of 1] Compiling Main ( /home/optimight/phoneBook.hs, interpreted ) /home/optimight/phoneBook.hs:2:6: Malformed head of type or class declaration: phoneBook Failed, modules loaded: none.Consultative
could you please provide the whole code? And also explain what you are trying to acchieve?Boycott
when you define a type in haskell it needs to start with a capital letter. so type PhoneBook = [(String,String)]. This essentially means that each time you refer PhoneBook you will actually refer to a list of string pairs.Boycott
@Boycott With the given type signature phoneBook is not a function that returns a list of pairs, it is a list of pairs. If it was a function, its type would include a ->.Unchaste
@Unchaste yes i know ;) if you read the comments above i just gave him the type definition if that was the case he wanted. Did I state anywhere that it was a function :SBoycott
@Boycott Yes, you said: "with your implementation you are sayin that phoneBook is a function returning a list of string pair."Unchaste
@sepp2k. If you look at his first post he writes phoneBook::[(String,String)] this is a function returning a list of string pairs. This function does not take any arguments but it is still a function that needs an implementation. If you want the phoneBook to be a list of pairs you need to write type PhoneBook = [(String,String)] then PhoneBook IS a list of string pairs.Boycott
@Boycott That's utterly and entirely false. There is no such thing as a function of no arguments in Haskell. Any value whose type does not contain a ->, is not a function. If you write type PhoneBook = [(String, String)] then PhoneBook is the type of lists of pairs, but it's not itself a list or any other kind of value - it's a type.Unchaste
let us continue this discussion in chatBoycott
B
1

If you want to declare a type, it must have initial upper case i.e. type PhoneBook = [(String, String)].

If you want to declare a function then you need to provide either just its definition (the binding) or both its definition and its type signature. The minimal effort to compile your code is:

phoneBook :: [(String, String)]
phoneBook = undefined

Then you can replace undefined with any value of type [(String, String)] e.g. [("Person","Number")].

Braswell answered 27/7, 2012 at 8:53 Comment(0)
F
1

I had the same problem and this was what I was doing initially

lst3 :: [a] -> (a , a , a)
lst  xs = if (length xs /= 3)
          then error "Undefined"      
          else (xs !! 0,xs !! 1,xs !! 2)          

As you can see the Function signature doesn't correspond to when I'm passing it the parameters and this is to be written as:

lst3 :: [a] -> (a , a , a)
lst3  xs = if (length xs /= 3)
          then error "Undefined"
          else (xs !! 0,xs !! 1,xs !! 2)
Fraga answered 12/11, 2022 at 8:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.