Need Help

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Need Help

kaushal Pathak
Hi There,
   Here is a very simple haskell program I have written to find whether a
number is prime or not, which doesn't give me any compilation error but
gives following run-time error :

Any help would help me move ahead, I know that the type concepts is what I
am lacking, anly pointer to simple/good article/paper would definitely help.

Lots of thannx jut even to look at the mail :-)

Regards
Kaushal

*Run Time Error :*

kaushal > isPrime1 171

<interactive>:1:0:
    Ambiguous type variable `t' in the constraints:
      `Integral t'
        arising from a use of `isPrime1' at <interactive>:1:0-11
      `Floating t'
        arising from a use of `isPrime1' at <interactive>:1:0-11
      `RealFrac t'
        arising from a use of `isPrime1' at <interactive>:1:0-11
    Probable fix: add a type signature that fixes these type variable(s)
kaushal >

*Program :*

isPrime1 x =
    let canDivide num 0 = 0
        canDivide num 1 = 0
        canDivide num divisor = if ((mod num divisor) == 0) then 1
                                else canDivide num (divisor - 1)
    in
        if ( x == 1 )
        then putStrLn("1 is Neither prime nor composite!!!")
        else if ((canDivide x first_div) == 1)
             then putStrLn(show(x) ++ " is not a Prime Number!!!")
             else putStrLn(show(x) ++ " is a Prime Number!!!")
        where
        first_div :: Integral a => a ; first_div = round (sqrt x)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090411/f855590c/attachment.htm
Reply | Threaded
Open this post in threaded view
|

Need Help

Michael Snoyman
On Sat, Apr 11, 2009 at 3:16 PM, kaushal Pathak <[hidden email]> wrote:

>
>         first_div :: Integral a => a ; first_div = round (sqrt x)
>

Should in fact be

        first_div :: Integral a => a ; first_div = round (sqrt $
fromIntegral x)

Basically, sqrt has the type signature sqrt :: (Floating a) => a -> a,
meaning it only works on things like doubles. The rest of your code only
works on integral types. Your code compiles just fine because, in theory,
you could have a data structure which in an instance of both Integral and
Floating, but the argument you provide is probably just a regular old Int or
something like that (I can't be certain, as you have not provided the code
that calls isPrime1).

Let me know if you'd like further clarification.

Michael
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090413/39540c00/attachment.htm
Reply | Threaded
Open this post in threaded view
|

Need Help

Rahul Kapoor
In reply to this post by kaushal Pathak
> ?Here is a very simple haskell program I have written to find whether a
> number is prime or not, which doesn't give me any compilation error but
> gives following run-time error :
>

The type of isPrime1 function you have defined is inferred to be

isPrime1 :: (Integral a, RealFrac a, Floating a) => a -> IO ()

You can check this quickly using :t isPrime1 in GHCI.

When you try to run isPrime1 in GHCI you get a compile error
since the argument isPrime1 is an integer which does not
satisfy the three constraints (Integral, RealFrac and Floating).

You probably wanted to define isPrime1 to have the type
(Integer a) => a -> IO ()

Cheers,
Rahul


www.artquiver.com
What kind of art do you like?
Reply | Threaded
Open this post in threaded view
|

Need Help

Daniel Fischer-4
In reply to this post by kaushal Pathak
Am Samstag 11 April 2009 14:16:47 schrieb kaushal Pathak:

> Hi There,
>    Here is a very simple haskell program I have written to find whether a
> number is prime or not, which doesn't give me any compilation error but
> gives following run-time error :
>
> Any help would help me move ahead, I know that the type concepts is what I
> am lacking, anly pointer to simple/good article/paper would definitely
> help.
>
> Lots of thannx jut even to look at the mail :-)
>
> Regards
> Kaushal
>
> *Run Time Error :*
>
> kaushal > isPrime1 171
>
> <interactive>:1:0:
>     Ambiguous type variable `t' in the constraints:
>       `Integral t'
>         arising from a use of `isPrime1' at <interactive>:1:0-11
>       `Floating t'
>         arising from a use of `isPrime1' at <interactive>:1:0-11
>       `RealFrac t'
>         arising from a use of `isPrime1' at <interactive>:1:0-11
>     Probable fix: add a type signature that fixes these type variable(s)
> kaushal >
>
> *Program :*
>
> isPrime1 x =
>     let canDivide num 0 = 0
>         canDivide num 1 = 0
>         canDivide num divisor = if ((mod num divisor) == 0) then 1
>                                 else canDivide num (divisor - 1)
>     in
>         if ( x == 1 )
>         then putStrLn("1 is Neither prime nor composite!!!")
>         else if ((canDivide x first_div) == 1)
>              then putStrLn(show(x) ++ " is not a Prime Number!!!")
>              else putStrLn(show(x) ++ " is a Prime Number!!!")
>         where
>         first_div :: Integral a => a ; first_div = round (sqrt x)

Make it first_div = round (sqrt $ fromIntegral x)

And use Bool for boolean expressions:

canDivide num 0 = False
canDivide num 1 = False
canDivide num divisor = num `mod` divisor == 0 || canDivide num (divisor -1)

The error happens because Haskell does no automatic type conversions.
In canDivide, you use mod on x, since the type of mod is

Prelude> :t mod
mod :: (Integral a) => a -> a -> a

both arguments of canDivide must belong to the same type which must be an Integral type.
The second argument of canDivide is first_div, which you defined as

first_div = round (sqrt x)

The type of sqrt is

Prelude> :t sqrt
sqrt :: (Floating a) => a -> a

so to call sqrt x, the type of x must belong to the Floating class, the result of sqrt has
the same type as its argument.

Then you round the result of sqrt, the type of round is

Prelude> :t round
round :: (RealFrac a, Integral b) => a -> b

The argument of round is the result of sqrt, which has the same type as x, which also must
belong to the class RealFrac for this to work.

So, for your function to work, its argument must have a type belonging to the three
classes
Integral
Floating
RealFrac

That's perfectly legal, so the code compiles and isPrime1 has the type

isPrime1 :: (Integral a, Floating a, RealFrac a) => a -> IO ().

And when you type

isPrime1 171

at the prompt, ghci doesn't really know what to do, to know that, it must know the exact
type of 171 (different types could expose different behaviour and might even lead to
different results). Since all classes are standard classes and at least one of them is
numeric, ghci tries then to default the type, looking for a type satisfying these
constraints in the default list (Integer, Double is the default-default).

But there is no type which belongs to all three classes (unless you provide the instances
for some type, and it wouldn't really make sense for a type to be Integral as well as
Floating), so ghci gives up and reports that it couldn't resolve the type variable.

In situations like this, usually the remedy is inserting a few explicit conversion
functions.

Reply | Threaded
Open this post in threaded view
|

Need Help

kaushal Pathak
In reply to this post by Rahul Kapoor
Here is another one liner I am stuck at(Find permuation of string), here is
my one line code :

permute myStr = foldr(\x acc -> (zipWith (\x1 y1 -> x1 ++ [x] ++ y1) (inits
acc) (tails acc))) [] myStr

and here is 5 line error ;-(

    Occurs check: cannot construct the infinite type: a = [a]
      Expected type: [a]
      Inferred type: [[a]]
    In the expression:
        (zipWith (\ x1 y1 -> x1 ++ x ++ y1) (inits acc) (tails acc))
    In the first argument of `foldr', namely
        `(\ x acc
              -> (zipWith (\ x1 y1 -> x1 ++ x ++ y1) (inits acc) (tails
acc)))'


Will really appreciate your help in moving ahead

Regards
Kaushal
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090429/39380333/attachment-0001.htm
Reply | Threaded
Open this post in threaded view
|

Need Help

kaushal Pathak
Whats the issue with following pgm to find permutation of a string

permute myStr = foldl(\acc x1 ->
                      (foldr(\x2 acc1 ->
                                 (zipWith (\x3 y3 -> x3 ++ [x1] ++ y3)
(inits acc1) (tails acc1)))
                       [] acc))
                    [] myStr


On Wed, Apr 29, 2009 at 6:35 PM, kaushal Pathak <[hidden email]> wrote:

> Here is another one liner I am stuck at(Find permuation of string), here is
> my one line code :
>
> permute myStr = foldr(\x acc -> (zipWith (\x1 y1 -> x1 ++ [x] ++ y1) (inits
> acc) (tails acc))) [] myStr
>
> and here is 5 line error ;-(
>
>     Occurs check: cannot construct the infinite type: a = [a]
>       Expected type: [a]
>       Inferred type: [[a]]
>     In the expression:
>         (zipWith (\ x1 y1 -> x1 ++ x ++ y1) (inits acc) (tails acc))
>     In the first argument of `foldr', namely
>         `(\ x acc
>               -> (zipWith (\ x1 y1 -> x1 ++ x ++ y1) (inits acc) (tails
> acc)))'
>
>
> Will really appreciate your help in moving ahead
>
> Regards
> Kaushal
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090429/2442e1d8/attachment.htm