3-idris.ltx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. \s{Idris}
  2. This section provides a ``working example'' of the above in Idris. If you don't
  3. know what that is, you are a bad person. Go back and read \cref{intro-idris}!
  4. Open up an Idris prompt, and enter \code{:type True}. That is basically asking
  5. Idris ``what type of thing is \truenm?'' Idris will tell you. Also do the same
  6. thing for \falsenm. Here's what happens when I do it:
  7. \begin{lstlisting}
  8. Idris> :type True
  9. Prelude.Bool.True : Bool
  10. Idris> :type False
  11. Prelude.Bool.False : Bool
  12. \end{lstlisting}
  13. If you ask Idris what the type of \code{True} or \code{False} is, it will tell
  14. you that they are \code{Bool}s.\footnote{For the most part, you can ignore all
  15. the weird stuff on the left-hand-side, for the time being. For instance, when
  16. I ran \code{:type True}, Idris switched \code{True} to
  17. \code{Prelude.Bool.True}. These are odd caveats of Idris's syntax, which I
  18. don't have time to explain right now. We'll get to them later, I promise.}
  19. You're probably thinking ``what the hell is a \code{Bool}?'' \code{Moreover, why
  20. the hell is this guy printing all this stuff in monospace}? Well, explaining
  21. this sorta stuff, that's what I'm here for. \code{Bool} is short for Boolean,
  22. which is what this chapter is about.
  23. The reason I'm printing stuff \code{in monospace} is to say ``hey, this is
  24. code.'' More importantly, printing stuff in monospace eschews some formatting
  25. flukes caused by variable-width text. In normal paragraph text, these flukes are
  26. fine --- they are even helpful --- but they cause some ambiguities in code. For
  27. that reason, the standard thing to do is to write code in monospace.
  28. In Idris, and in most programming languages, the $\land$ operator is replaced
  29. with \code{\&\&}. We know that, in Idris \truenm\ and \falsenm\ are both Boolean
  30. values. What about $\true \land \false$?
  31. \begin{lstlisting}
  32. Idris> :type (True && False)
  33. True && Delay False : Bool
  34. \end{lstlisting}
  35. \xti{So, wait, \truenm\ and \falsenm\ are both Boolean values, but
  36. $\true \land \false$ is also a Boolean value?}
  37. Yes, Timmy, now try to keep up.
  38. Now, from the explanation of $\land$ in \cref{s: Basic Logic},
  39. $\true \land \false$ should only be true if both \truenm\ and \falsenm\ are
  40. \truenm. Well, that's obviously not the case, so $\true \land \false$ should
  41. turn out to be \falsenm, right? Let's see!
  42. \begin{lstlisting}
  43. Idris> True && False
  44. False : Bool
  45. \end{lstlisting}
  46. What about $\lor$? In Idris --- and most programming languages --- $\lor$ is
  47. replaced with \code{||}.
  48. \begin{lstlisting}
  49. Idris> True || False
  50. True : Bool
  51. \end{lstlisting}