What is the resolution to this "No Instance" error? - haskell

I have created new type describing Binary Tree
data BinTree a = Null | Num a (BinTree a) (BinTree a)
deriving (Show)
And have created the following function:
treehandle :: BinTree a -> Bool
treehandle a = True
to check at least inputing values.
When I input value Null, program outputs result successfully, but I can't input binary tree. I try thus:
treehandle (5 (Null) (Null))
but obtain:
<interactive>:66:13:
No instance for (Num (BinTree a1 -> BinTree a2 -> BinTree a0))
(maybe you haven't applied enough arguments to a function?)
arising from the literal ‘5’
In the expression: 5
In the first argument of ‘treehandle’, namely ‘(5 (Null) (Null))’
In the expression: treehandle (5 (Null) (Null))
Why?

You forgot the value constructor's name
treehandle (Num 5 (Null) (Null))

I would find a different naming for the data constructors if I was you. Num is also the name of a type class, this can be very confusing when looking at error messages.
Also deriving Show is not correctly indented, and you forgot the data constructor in treehandle (5 (Null) (Null)). Here is a working version.
data BinTree a = Leaf | Node a (BinTree a) (BinTree a) deriving Show
treehandle :: BinTree a -> Bool
treehandle _ = True
test = treehandle $ Node 5 Leaf Leaf
treehandle wants a value of type BinTree a and all you gave it was an Int and two empty BinTree's, it actually tried to apply the Int with the two empty BinTree's and failed. You have to make a Node to get a single BinTree a which you can pass to treehandle

I thank for your replies. I appreciate it. Haskell is "wild" language being mix of Lisp and Prolog. Gradually I begin to get used to it. I would share with my results. So, that's BinTree declaration:
data BinTree a = Leaf | Val a (BinTree a) (BinTree a)
deriving (Show)
It resembles on Prolog unification algorithm - I mean about definition of "reverse" as below:
reverse :: BinTree a -> BinTree a
reverse (Val a1 (a2) (a3)) = (Val a1 (reverse a3) (reverse a2))
reverse Leaf = Leaf
It works!

Related

How to Access Fields of Custom Data Types without Record Syntax in Haskell?

I'd like to understand how to access fields of custom data types without using the record syntax. In LYAH it is proposed to do it like this:
-- Example
data Person = Subject String String Int Float String String deriving (Show)
guy = Subject "Buddy" "Finklestein" 43 184.2 "526-2928" "Chocolate"
firstName :: Person -> String
firstName (Subject firstname _ _ _ _ _) = firstname
I tried applying this way of accessing data by getting the value of a node of a BST:
data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show, Read, Eq)
singleton :: a -> Tree a
singleton x = Node x EmptyTree EmptyTree
treeInsert :: (Ord a) => a -> Tree a -> Tree a
treeInsert x EmptyTree = singleton x
treeInsert x (Node a left right)
| x == a = Node x left right
| x < a = Node a (treeInsert x left) right
| x > a = Node a left (treeInsert x right)
getValue :: Tree -> a
getValue (Node a _ _) = a
But I got the following error:
Could someone explain how to access the field correctly without using the record syntax and what the error message means? Please note that I'm a beginner in Haskell. My purpose is to understand why this particular case throws an error and how to do it it correctly. I'm not asking this to be pointed to more convenient ways of accessing fields (like the record syntax). If someone asked a similar question before: Sorry! I really tried finding an answer here but could not. Any help is appreciated!
You forgot to add the type parameter to Tree in your function's type signature
getValue :: Tree -> a
should be
getValue :: Tree a -> a
Expecting one more argument to `Tree'
means that Tree in the type signature is expecting a type argument, but wasn't provided one
Expected a type, but Tree has a kind `* -> *'
Tree a is a type, but Tree isn't (?) because it is expecting a type argument.
A kind is like a type signature for a type.
A kind of * means the type constructor does not expect any type of argument.
data Tree = EmptyTree | Tree Int Tree Tree
has a kind of *
It is like the type signature of a no-argument function (technically this is not really called a function, I think)
f :: Tree Int
f = Node 0 EmptyTree EmptyTree
A kind of * -> * means the type constructor expects an argument.
Your Tree type has a kind of * -> *, because it takes one type argument, the a on the left hand side of =.
A kind of * -> * is sort of like a function that takes one argument:
f :: Int -> Tree Int
f x = Node x EmptyTree EmptyTree

How to convert a String to an Enumerated Data Type in Haskell?

I have an enumerated data type that looks like such:
data MathExpr a = X
| Coef a
| Sum (MathExpr a) (MathExpr a)
| Prod (MathExpr a) (MathExpr a)
| Quot (MathExpr a) (MathExpr a)
| Exp (MathExpr a)
| Log (MathExpr a)
deriving (Eq,Show,Read)
I'm trying to convert a String to this type. I want to use the read function thats available within the prelude. To do so I created another function as such:
readMathExpr :: String -> MathExpr a
readMathExpr = read
This gives me an error while compiling saying that there is no instance for (Read a) arising from use of 'read'. If anybody could point me in the right direction or link a useful tutorial pertaining to the read function I'd be extremely grateful. Thank you !
The problem is simply that your proposed type signature is too general. The derived instance of Read for MathExpr a only works when there is already an instance of Read for a. That is, if you were to write out the instance yourself it would start:
instance (Read a) => Read (MathExpr a) where ...
I don't imagine that in practice you will wish to read values of type MathExpr a for types a which aren't themselves instances of Read, so the fix is simply to add the necessary type-class constraint to the signature:
readMathExpr :: (Read a) => String -> MathExpr a
readMathExpr = read

'Cannot find "show" function' error when parameter is instance of Show

The following error is triggered:
ERROR - Cannot find "show" function for:
*** Expression : debugFunction getDebugTree
*** Of type : Tree (Tree Int) -> Bool
when running debugFunction getDebugTree
data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Eq, Show)
getDebugTree :: Tree Int
getDebugTree = (Node 1 (Empty) (Node 2 Empty Empty))
debugFunction :: Show a => a -> Tree a -> Bool
debugFunction _ _ = True
I've read that
reverse []
ERROR: Cannot find "show" function for:
* expression : reverse []
* of type : [a]
The top-level system for Hugs can only print values which belong to a
type which can be shown, that is a type which belongs to the Show
class. Now, the list type is an instance of Show, so what is wrong
with reverse [] (which evaluates to [])? The problem is that the type
of [] is polymorphic: [] :: [a] for all a. Not knowing a Hugs refuses
to print a value of type [a]. Note that this behaviour applies to all
polymorphic values. Given the definition
data Tree a = Empty | Node (Tree a) a (Tree a)
we have, on evaluating
Empty
the error message
ERROR: Cannot find "show" function for:
* expression : Empty
* of type : Tree a
Functions can be shown, but not very helpfully; printing any function
results in
<< function>>
As far as I understand Hugs doesn't know a so it refuses to print it (even though I'm printing a Bool?) so I've tried to make a be an instance of Show, but it still doesn't work, what's the problem here?
Look at the type of debugFunction.
debugFunction :: Show a => a -> Tree a -> Bool
debugFunction expects two arguments, the second of which is a Tree a. You're only passing it one. The type checker sees that you're passing a Tree Int as the first argument and infers a ~ Tree Int. So debugFunction getDebugTree is a function waiting for a second argument of type Tree (Tree Int).
debugFunction getDebugTree :: Tree (Tree Int) -> Bool
I suspect you intended to use getDebugTree as the second argument to debugFunction, which means you need to come up with an Int to use as the first one.
debugFunction 0 getDebugTree :: Bool
The error message about the missing Show instance comes from the REPL itself. It's attempting to print out the result of debugFunction getDebugTree, but it can't because functions can't be shown.

How does this instance declaration result in an ambiguous case?

The following snippet
data Tree k v = ETree | Node { leftTreeOf :: Tree k v,
rightTreeOf :: Tree k v,
tKey :: k,
tVal :: v
}
instance Show s => Show (Tree s s) where
show = showTree 0
yields
Illegal instance declaration for `Show (Tree s s)'
(All instance types must be of the form (T a1 ... an)
where a1 ... an are *distinct type variables*,
and each type variable appears at most once in the instance head.
Use -XFlexibleInstances if you want to disable this.)
In the instance declaration for `Show (Tree s s)'
I looked it up, and the restriction that -XFlexibleInstances lifts is in place to prevent ambiguous instances from being declared.
How does having two type variables allow an ambiguous case though?
instance Show s => Show (Tree s) where
show = showTree 0
worked fine when I only needed one type variable.
Sorry, I didn't think it through.
If anyone else is having this problem, it requires 2 different type variable to be supplied to allow 2 different Show-able types:
instance (Show sk, Show sv) => Show (Tree sk sv) where
show = showTree 0
Then any contained functions (in this case showTree), need a similar signature.

Why can't Haskell infer Tree type?

I followed the book to define Tree data type, but show doesn't work correctly. Why?
data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show)
test = show EmptyTree
gives the error message:
No instance for (Show a0) arising from a use of ???show???
The type variable ???a0??? is ambiguous
Note: there are several potential instances:
instance Show a => Show (Tree a)
-- Defined at /Users/gzhao/Documents/workspace/hsTest2/src/Tree.hs:3:62
instance Show Double -- Defined in ???GHC.Float???
instance Show Float -- Defined in ???GHC.Float???
...plus 25 others
In the expression: show EmptyTree
In an equation for ???test???: test = show EmptyTree
The problem is that EmptyTree has type Tree a for any type a. Even though it won't actually affect the final output, the compiler wants to know which a you mean.
The simplest fix is to pick a specific type, e.g. with show (EmptyTree :: Tree ()). This uses the unit type (), which is in some sense the simplest possible type, but you can also use any other type that has a Show instance, like Int, String etc.

Resources