Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Investigating (->) with ghci and trying to get to its roots

Tags:

haskell

ghci

I am trying to use ghci to investigate type (->).

I'd love to understand why I can ask :t (+), but not :t (->):

Prelude> :t (->)
<interactive>:1:2: error: parse error on input ‘->’

Luckily, both operators allow investigation using :i, so I presume it's all because (+) is a method of class Num, whereas (->) is a data.

Diving deeper into (->):

Prelude> :i (->)
data (->) (a :: TYPE q) (b :: TYPE r)   -- Defined in ‘GHC.Prim’
infixr 0 ->
instance Applicative ((->) a) -- Defined in ‘GHC.Base’
instance Functor ((->) r) -- Defined in ‘GHC.Base’
instance Monad ((->) r) -- Defined in ‘GHC.Base’
instance Monoid b => Monoid (a -> b) -- Defined in ‘GHC.Base’
instance Semigroup b => Semigroup (a -> b) -- Defined in ‘GHC.Base’

But there is no trace of data (->) in the Hackage page for GHC.Prim. Possibly I am checking the wrong language version, but AFAIS 0.5.3 is the most recent one, and my ghci version is the latest.

Where can I find the declaration of data (->)?

Eventually, I'd like to read about TYPE, but all the pages I retrieve on Google are talking of type.

Where can I find information about TYPE?

like image 607
Marco Faustinelli Avatar asked Sep 12 '19 08:09

Marco Faustinelli


People also ask

What is the difference between GHC and Ghci?

Introduction. GHCi is GHC's interactive environment, in which Haskell expressions can be interactively evaluated and programs can be interpreted.

How do I run Ghci in terminal?

If you have installed the Haskell Platform, open a terminal and type ghci (the name of the executable of the GHC interpreter) at the command prompt. Alternatively, if you are on Windows, you may choose WinGHCi in the Start menu. And you are presented with a prompt. The Haskell system now attentively awaits your input.

How do I run a Haskell file with Ghci?

Open a command window and navigate to the directory where you want to keep your Haskell source files. Run Haskell by typing ghci or ghci MyFile. hs. (The "i" in "GHCi" stands for "interactive", as opposed to compiling and producing an executable file.)


1 Answers

Since answers have been given in the comments, I sum them up here:

  • (->) is a type constructor. You can investigate it with :k (->) @Willem van Onsem

  • GHC.Prim has no source code anywhere. It is completely generated by the compiler and you don't need to bother looking at it. When GHCi tells you something is defined there, read that as saying it comes "from the sky" @dfeuer

like image 187
Marco Faustinelli Avatar answered Oct 14 '22 15:10

Marco Faustinelli