Skip to content

Instantly share code, notes, and snippets.

@witt3rd
Last active October 13, 2020 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save witt3rd/e4226e5af3ae1eba6fc2c59a732dddcb to your computer and use it in GitHub Desktop.
Save witt3rd/e4226e5af3ae1eba6fc2c59a732dddcb to your computer and use it in GitHub Desktop.
Type-Driven Development with Idris: Reading Group Questions and Challenges

Type-Driven Development with Idris: Reading Group Questions and Challenges

Chapter 3. Interactive development with types

  • Functions in Idris are defined by collections of pattern-matching equations.
  • Patterns arise from the constructors of a data type.
  1. Describe "type declaration", "type definition", "equation", "expression"
  2. Are type declarations required? When (not)?
  3. What is referential transparency?
  4. Can pattern matching match a specific value?
  5. How would you recurse from 0 to n?
  • The Atom text editor provides an interactive editing mode that uses the type to help direct function implementation. Similar modes are available for Emacs and Vim.
  • Interactive editing commands provide natural tools for following the process of type, define, refine.
  • Interactive editing provides a command for searching for a valid expression that satisfies the type of a hole.
  1. What are the interactive editor commands that support type, define, refine?
  • More-precise types, such as Vect, give more information to the compiler both to help check that a function is correct, and to help constrain expression searches.
  • Matrices are two-dimensional vectors, with the dimensions encoded in the type. Operations on matrices such as addition and multiplication can be given types that precisely describe how the operations affect the dimensions.
  1. What are type-level variables?
  2. What other type-level constructs do you imagine might exist?
  • The type-define-refine process helps you to implement matrix operations with precise types by using the type to direct the implementation of each subexpression and create appropriate helper functions.
  • 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 {}.
  1. Why would you want to bring implicit arguments into scope explicitly?

Challenge: Diagonal Difference

Given a square matrix, calculate the absolute difference between the sums of its diagonals.

Sample Input

3
11 2 4
4 5 6
10 8 -12

Expected Result

15

Chapter 4. User-defined data types

  • Data types are defined in terms of a type constructor and data constructors.
  1. What is the difference between a 'type constructor' and a 'data constructor'?
  • Enumeration types are defined by listing the data constructors of the type.
  • Union types are defined by listing the data constructors of the type, each of which may carry additional information.
  • Generic types are parameterized over some other type. In a generic type definition, variables stand in place of concrete types.
  • Dependent types can be indexed over any other value.
  1. What does it mean for a type to be 'indexed' over any other value?
  • Using dependent types, you can classify a larger family of types (such as vehicles) into smaller subgroups (such as vehicles powered by petrol and those powered by pedal) in the same declaration.
  1. What is a 'type family'?
  • Dependent types allow safety checks to be guaranteed at compile time, such as guaranteeing that all vector accesses are within the bounds of the vector.
  • You can write larger programs in the type-driven style, creating new data types where appropriate to help describe components of the system.
  • Interactive programs that involve state can be written using the replWith function.

Challenge

Discuss Interface Patterns

Sample Input

N/A

Expected Result

N/A

Chapter 5. Interactive programs: input and output processing

  • Idris provides a generic IO type for describing interactive actions.
  • Idris distinguishes between evaluation of pure functions and execution of interactive actions. The :exec command at the REPL executes interactive actions.
  • You can sequence interactive actions using do notation, which translates to applications of the >>= operator.
  1. Monads are just monoids in the category of endofunctors, what's the problem?
  • You can produce pure values from interactive definitions by using the pure function.
  • Idris provides a concise notation for pattern-matching on the result of an interactive action.
  • Dependent types express assumptions about inputs to functions, so you need to validate user inputs to check that they satisfy those assumptions.
  • Dependent pairs allow you to pair two values, where the type of the second value is computed from the first value.
  • You can use dependent pairs to express that a type’s argument, such as the length of a vector, will not be known until the user enters some input.
  1. What other uses of dependent pairs can you think of?

Challenge

  • Implement the IO monad (hint)

Sample Input

Expected Result

Chapter 6. Programming with first-class types

  • Type synonyms are alternative names for existing types, which allow you to give more-descriptive names to types.
  • Type-level functions are functions that can be used anywhere that Idris is expecting a Type. Type synonyms are a special case of type-level functions.
  • Type-level functions can be used to compute types, and they allow you to write functions with varying numbers of arguments, such as printf.
  • You can represent a schema for a data store as a type, and then use type-level functions to calculate the types of operations, such as parsing and displaying entries in the data store from the schema description.
  • A record is a data type with only one constructor, and with automatically generated functions for projecting fields from the record.
  • After refining a data type, you can use holes to correct compilation errors temporarily.
  • Using do notation, you can sequence computations that use Maybe to capture the possibility of errors.

Challenge

Implement scanf to mirror printf.

Sample Input

Expected Result

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment