Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Idris Study Group - The Idris Book Ch1 to Ch6

cybai
September 09, 2020

Idris Study Group - The Idris Book Ch1 to Ch6

cybai

September 09, 2020
Tweet

More Decks by cybai

Other Decks in Programming

Transcript

  1. Created by Edwin Brady Developed for Type-Driven Development A pure

    functional programming language Idris 2 has been released this year (2020) I haven't checked what's the difference between 1 and 2; anyone who is familiar with that, feel free to share later
  2. In Idris, types are a first-class language construct. Types can

    be manipulated, used, passed as arguments to functions, and returned from functions just like any other value, such as numbers, strings, or lists.
  3. Types allow you to make these models explicit in code

    and ensure that your implementation of a program matches the model in your head.
  4. The classical example (also from the book) is Matrix calculation

    . + ⎝ ⎜ ⎛ 1 3 5 2 4 6 ⎠ ⎟ ⎞ = ( 7 9 8 10 ) ???
  5. Behavior Input Output Addition M x N + M x

    N M x N Multiplication M x N * N x Q M x Q Transpose M x N N x M
  6. Matrix : Nat -> Nat -> Type Matrix x y

    = Vect x (Vect y Int) add : Matrix m n -> Matrix m n -> Matrix m n mul : Matrix m n -> Matrix n q -> Matrix m q transpose : Matrix m n -> Matrix n m
  7. Total add : Matrix m n -> Matrix m n

    -> Matrix m n add [] [] = [] add (x :: xs) (y :: ys) = zipWith (+) x y :: add xs ys
  8. Partial add : Matrix m n -> Matrix m n

    -> Matrix m n add (x :: xs) (y :: ys) = zipWith (+) x y :: add xs ys
  9. The Idris system consists of an interactive environment and a

    batch mode compiler. In the interactive environment, you can load and type-check source files, evaluate expressions, search libraries, browse documentation, and compile and run complete programs.
  10. Type Int A fixed-width signed integer type. Integer An unbounded

    signed integer type. Nat An unbounded unsigned integer type. Double A double-precision floating-point type.
  11. Type String String "Hello World!" Char Char 'C' Bool Bool

    True or False Tuple (Integer, String) (2020, "Idris") List List Integer [1, 2, 3, 4]
  12. doubleInt : Int -> Int doubleInt x = x +

    x *Double> double 47 94 : Int *Double> double (double 15) 60 : Int
  13. Idris, by default, will evaluate the innermost expression first. In

    other words, it will evaluate function arguments before function definitions.
  14. doubleNat : Nat -> Nat doubleNat x = x +

    x doubleInteger : Integer -> Integer doubleInteger x = x + x double : ty -> ty double x = x + x
  15. Type checking ./Double.idr Double.idr:4:12-16: | 4 | double x =

    x + x | ~~~~~ When checking right hand side of double with expected type ty ty is not a numeric type
  16. let & where for local variables / functions longer :

    String -> String -> Nat longer word1 word2 = let len1 = length word1 len2 = length word2 in if len1 > len2 then len1 else len2 pythagoras : Double -> Double -> Double pythagoras x y = sqrt (square x + square y) where square : Double -> Double square x = x * x
  17. Keyword export Allow the function to be exported mutual Allow

    functions to call each other mutually total Always check the function as total by default
  18. Documentation Comments ||| Calculate the average length of words in

    a string. ||| @str a string containing words separated by whitespace. average : (str : String) -> Double It will be shown with :doc in REPL or in IDE.
  19. *Append> append Char 2 2 ['a','b'] ['c','d'] ['a', 'b', 'c',

    'd'] : Vect 4 Char *Append> append' _ _ _ ['a','b'] ['c','d'] ['a', 'b', 'c', 'd'] : Vect 4 Char
  20. The notation {x : S} -> T denotes a function

    type where the argument is intended to be inferred by Idris, rather than written directly by the programmer.
  21. Type-level variables are implicit arguments to functions, which can be

    brought into scope and used like any other arguments by enclosing them in braces {}.
  22. Type-driven development involves not only giving precise types to functions

    but also thinking about exactly how data is structured. In a sense, programming (pure functional programming, in particular) is about transforming data from one form to another.