30

I'd appreciate if someone could point to docs on what "let" does in GHCi, or failing that, explain it convincingly.

So far as I can tell, "let" (without "in") is not part of the Haskell language per se, and on the other hand, it doesn't appear to be a GHCi command either, as it's not prefixed by colon.

devinho
  • 404
  • 4
  • 18
gwideman
  • 2,705
  • 1
  • 24
  • 43
  • 1
    Short answer: `let` (without `in`) *is* part of the Haskell language, when inside a `do` block (and writing in GHCi resembles writing in the `IO` monad). – MasterMastic Mar 08 '15 at 23:26

4 Answers4

34

While programming in GHCi, you're like programming in the IO monad with do syntax, so for example you can directly execute an IO action, or use monadic bind syntax like r <- someIOFun.

let is also a part of do so you can also use this. I think it's being desugared into let .. in <rest of the computation>, so for example when you do this:

ghci> let a = 1
ghci> someFun
ghci> someFun2

It's like:

let a = 1 in
do someFun
   someFun2
SwiftsNamesake
  • 1,540
  • 2
  • 11
  • 25
sinan
  • 6,809
  • 6
  • 38
  • 67
12

Here's the relevant part of the documentation.

GHCI statements are executed as IO computation. So let is same as what you have inside an IO monad where you bind non-IO expression using let.

isomorphismes
  • 8,233
  • 9
  • 59
  • 70
Satvik
  • 11,238
  • 1
  • 38
  • 46
  • 4
    Thanks for pointing to the docs, I hadn't found that. As a side comment, that doc is rather disappointing, since its explanation of let is sort of premised on understanding monads. New users encounter let almost immediately as it's frequently presented in tutorials on beginning Haskell concepts, yet it doesn't work if copied to a haskell source file, and explanations assuming understanding of monads are not too useful. Nonetheless, with the collection of answers here I at least see how these features fit together. – gwideman Dec 27 '12 at 10:23
  • @gwideman Yeah, I understand that. You can not directly copy what you write as ghci statements in a hs file and expect them to work. Well you can ofcourse do `main = do `. It might not work always as expected as ghci prints the output of a statement by using its `Show` instance. – Satvik Dec 27 '12 at 13:02
9

For more down-to-the-code details, this comment in TcRnDriver.lhs might be enlighting:

--------------------------------------------------------------------------
                Typechecking Stmts in GHCi

Here is the grand plan, implemented in tcUserStmt

        What you type The IO [HValue] that hscStmt returns
        ------------- ------------------------------------
        let pat = expr ==> let pat = expr in return [coerce HVal x, coerce HVal y, ...]
                                        bindings: [x,y,...]

        pat <- expr ==> expr >>= \ pat -> return [coerce HVal x, coerce HVal y, ...]
                                        bindings: [x,y,...]

        expr (of IO type) ==> expr >>= \ it -> return [coerce HVal it]
          [NB: result not printed] bindings: [it]

        expr (of non-IO type, ==> let it = expr in print it >> return [coerce HVal it]
          result showable) bindings: [it]

        expr (of non-IO type,
          result not showable) ==> error

So a command at the GHCi prompt can have up to three effects: Some code is evaluated, some things are printed, and some variable names are bound. Your case (the first one in the comment) binds variables, but does not print.

The syntax does resemble the do-notation, so @sinan’s answer is somewhat right, but it is not really what is happening under the hood – otherwise, for example, nothing would ever be printed.

Joachim Breitner
  • 25,395
  • 6
  • 78
  • 139
  • I'm pretty sure I don't understand all of that at this stage, but it is very useful to see the degree to which one should not learn from GHCi :-). Thanks! – gwideman Dec 28 '12 at 12:56
4

GHCI commands are executed in the IO monad and uses do syntax, so the desugaring rules apply. From Real World Haskell

doNotation4 =
    do let val1 = expr1
           val2 = expr2
           {- ... etc. -}
           valN = exprN
       act1
       act2
       {- ... etc. -}
       actN

translates to:

translated4 =
    let val1 = expr1
        val2 = expr2
        {- ... etc. -}
        valN = exprN
    in do act1
          act2
          {- ... etc. -}
          actN
Boris
  • 5,094
  • 4
  • 45
  • 71