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

Idris Study Group - The Idris Book Ch1 to Ch6

Fd9f21969cd8a0cbf0b2f48a9af0cc28?s=47 cybai
September 09, 2020

Idris Study Group - The Idris Book Ch1 to Ch6

Fd9f21969cd8a0cbf0b2f48a9af0cc28?s=128

cybai

September 09, 2020
Tweet

Transcript

  1. Idris Study Group Ch1 ~ Ch6 @_cybai

  2. What is Idris?

  3. 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
  4. What is Type-Driven Development?

  5. 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.
  6. Types as Models

  7. Types allow you to make these models explicit in code

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

    . + ⎝ ⎜ ⎛ 1 3 5 2 4 6 ⎠ ⎟ ⎞ = ( 7 9 8 10 ) ???
  9. Matrix = M x N of numbers

  10. 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
  11. None
  12. Matrix : Nat -> Nat -> Type Matrix x y

    = Vect x (Vect y Int)
  13. 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
  14. Total and partial functions

  15. A total function is guaranteed to produce a finite prefix

    of a potentially infinite result.
  16. None
  17. Total add : Matrix m n -> Matrix m n

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

    -> Matrix m n add (x :: xs) (y :: ys) = zipWith (+) x y :: add xs ys
  19. 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.
  20. Quick demo with REPL

  21. Basic Types in Idris

  22. Numeric types and values

  23. 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.
  24. None
  25. None
  26. 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]
  27. doubleInt : Int -> Int doubleInt x = x +

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

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

    x doubleInteger : Integer -> Integer doubleInteger x = x + x double : ty -> ty double x = x + x
  30. 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
  31. double : Num ty => ty -> ty double x

    = x + x
  32. 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
  33. 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
  34. Comments -- Single line comments {- - Multi-line comments -

    - You can write it like this. -}
  35. 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.
  36. Interactive development with types

  37. None
  38. (Go to Atom)

  39. Implicit Arguments

  40. *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
  41. 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.
  42. 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 {}.
  43. 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.
  44. (Go to Atom)

  45. Thank you!