简体   繁体   中英

How can I find out the type of operator “+”?

In GHCi, version 8.6.3 ( https://repl.it/languages/haskell ), I would like to know how to find out the type of operator "+". I want to see if its type is num a, b,c => a -> b -> c or num a, b,c => (a,b) -> c .

But I can't find its type out. It also affects the next expression in an unknown way. Why do I fail and what shall I do then?

   :type +
   :type not
<interactive>:1:1: error: parse error on input ‘+’
   :type not
not :: Bool -> Bool
=> "12"

This way:

> :type (+)
(+) :: Num a => a -> a -> a

And also

> :t (+) 4
(+) 4 :: Num a => a -> a

> :t (+) 4 3
(+) 4 3 :: Num a => a

In the Haskell's console, you have to use the :t key giving to it a value such as you would use it in your program, some examples:

plus = +

will give an error

plus = (+)

it's ok:

ghci> :t plus
ghci> :t (+)

Num a => a -> a -> b

so:

plusOne = + 1

will give you an error also

but

plusOne  = (+ 1)
plusOne' = (1 +)

it's Ok:

:t plusOne'
:t plusOne
:t (1 +)
:t (+ 1)

Num a => a -> a

and finally:

twoPlusOne = 2 + 1

:t twoPlusOne
:t 2 + 1

Num a => b

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM