Idris: Implementing a Dependently Typed Programming Language

Similar documents
Type checking by theorem proving in IDRIS

Idris. Programming with Dependent Types. Edwin Brady University of St Andrews, Scotland,

Idris, a language with dependent types Extended Abstract

Embedded Domain Specific Language Implementation using Dependent Types

Type Driven Development in Idris

Type-driven Development of Communicating Systems in Idris

c constructor P, Q terms used as propositions G, H hypotheses scope identifier for a notation scope M, module identifiers t, u arbitrary terms

Coq quick reference. Category Example Description. Inductive type with instances defined by constructors, including y of type Y. Inductive X : Univ :=

Coq with Classes. Matthieu Sozeau. Journées PPS 2011 September 5th 2011 Trouville, France. Project Team πr 2 INRIA Paris

EDIT-TIME TACTICS IN IDRIS

Ideas over terms generalization in Coq

Embedded Domain Specific Languages in Idris

Simon Peyton Jones Microsoft Research August 2013

Dependently Typed Functional Programming with Idris

Advanced Functional Programming

First-Class Type Classes

Type Processing by Constraint Reasoning

Provably Correct Software

Autosubst Manual. May 20, 2016

Ivor, a Proof Engine

Generic Constructors and Eliminators from Descriptions

Lecture 8: Summary of Haskell course + Type Level Programming

The design of a programming language for provably correct programs: success and failure

Embedded Domain Specific Languages in Idris Lecture 3: State, Side Effects and Resources

Dependent Polymorphism. Makoto Hamana

Dependent Types in the Idris Programming Language

Documentation for the Idris Language. Version 1.0

Correct-by-Construction Concurrency: using Dependent Types to Verify Implementations of Effectful Resource Usage Protocols

Programming Up-to-Congruence, Again. WG 2.8 Estes Park

Side Effects (3A) Young Won Lim 1/13/18

Adam Chlipala University of California, Berkeley ICFP 2006

Heq: a Coq library for Heterogeneous Equality

Introduction to ML. Based on materials by Vitaly Shmatikov. General-purpose, non-c-like, non-oo language. Related languages: Haskell, Ocaml, F#,

Resource-dependent Effects for Communication Protocols

Equations: a tool for dependent pattern-matching

Encoding functions in FOL. Where we re at. Ideally, would like an IF stmt. Another example. Solution 1: write your own IF. No built-in IF in Simplify

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group

G Programming Languages - Fall 2012

IA014: Advanced Functional Programming

Lightweight Invariants with Full Dependent Types

Coq projects for type theory 2018

An experiment with variable binding, denotational semantics, and logical relations in Coq. Adam Chlipala University of California, Berkeley

COMP 3141 Software System Design and Implementation

Principles of Programming Languages

Side Effects (3B) Young Won Lim 11/20/17

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

Programming with dependent types: passing fad or useful tool?

CS 320: Concepts of Programming Languages

Combining Proofs and Programs in a Dependently Typed Language. Certification of High-level and Low-level Programs

The Idris Tutorial. Release The Idris Community

Side Effects (3B) Young Won Lim 11/23/17

A totally Epic backend for Agda

Lambda Calculus and Type Inference

Lecture #23: Conversion and Type Inference

Side Effects (3B) Young Won Lim 11/27/17

3 Pairs and Lists. 3.1 Formal vs. Informal Proofs

Documentation for the Idris Language. Version 0.99

Structures in Coq January 27th 2014

Formally Certified Satisfiability Solving

CSCI-GA Scripting Languages

Inheritance and Overloading in Agda

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Type Soundness. Type soundness is a theorem of the form If e : τ, then running e never produces an error

Induction in Coq. Nate Foster Spring 2018

idris-mode: Idris interaction with emacs

Reasoning with the HERMIT

Intrinsically Typed Reflection of a Gallina Subset Supporting Dependent Types for Non-structural Recursion of Coq

Introduction to dependent types in Coq

Derivation of a Pattern-Matching Compiler

Programming in IDRIS: A Tutorial

Lambda Calculus and Type Inference

MoreIntro_annotated.v. MoreIntro_annotated.v. Printed by Zach Tatlock. Oct 04, 16 21:55 Page 1/10

CS 11 Haskell track: lecture 1

Types Summer School Gothenburg Sweden August Dogma oftype Theory. Everything has a type

Function compose, Type cut, And the Algebra of logic

Lean 2 Quick Reference

Isabelle s meta-logic. p.1

Introduction to Homotopy Type Theory

Milawa an extensible proof checker

Type Declarations. Γ <num> : num [... <id> τ... ] <id> : τ Γ true : bool Γ false : bool Γ e 1 : num Γ e 2 : num Γ. {+ e 1 e 2 } : num

10 Years of Partiality and General Recursion in Type Theory

Chemistry Studio. B.Tech Project. Ashish Gupta (Y8140) Akshay Mittal (Y8056)

The Isar Proof Language in 2016

Overview. A Compact Introduction to Isabelle/HOL. Tobias Nipkow. System Architecture. Overview of Isabelle/HOL

A BI-DIRECTIONAL REFINEMENT ALGORITHM FOR THE CALCULUS OF (CO)INDUCTIVE CONSTRUCTIONS

Inductive data types

A Verified Compiler from Isabelle/HOL to CakeML

Liquidate your assets

Principles of Programming Languages

Towards Reasoning about State Transformer Monads in Agda. Master of Science Thesis in Computer Science: Algorithm, Language and Logic.

CMSC 331 Final Exam Section 0201 December 18, 2000

Computing Fundamentals 2 Introduction to CafeOBJ

Overloading, Type Classes, and Algebraic Datatypes

(Refer Slide Time: 4:00)

register lock_guard(mtx_); string_view s = register to_string(42); We propose register-expression to grant the temporary objects scope lifetimes.

Static and User-Extensible Proof Checking. Antonis Stampoulis Zhong Shao Yale University POPL 2012

An Introduction to Programming and Proving in Agda (incomplete draft)

Assistant for Language Theory. SASyLF: An Educational Proof. Corporation. Microsoft. Key Shin. Workshop on Mechanizing Metatheory

Monad Background (3A) Young Won Lim 11/18/17

Alonzo a Compiler for Agda

Transcription:

Idris: Implementing a Dependently Typed Programming Language Edwin Brady University of St Andrews ecb10@st-andrews.ac.uk @edwinbrady Type Inference and Automated Proving, Dundee, 12th May 2015 1 / 25

Idris is a pure functional programming language with first class dependent types http://idris-lang.org In this talk: The core language (TT) and elaboration, or... 2 / 25

Idris is a pure functional programming language with first class dependent types http://idris-lang.org In this talk: The core language (TT) and elaboration, or...... how I tried to write a type checker, but accidentally wrote a theorem prover instead 2 / 25

Elaboration Example Vectors, high level Idris data Vect : Nat -> Type -> Type where Nil : Vect Z a (::) : a -> Vect k a -> Vect (S k) a Vectors, TT Nil : (a : Type) -> Vect a Z (::) : (a : Type) -> (k : Nat) -> a -> Vect k a -> Vect (S k) a 3 / 25

Elaboration Example Vectors, high level Idris data Vect : Nat -> Type -> Type where Nil : Vect Z a (::) : a -> Vect k a -> Vect (S k) a Vectors, TT Nil : (a : Type) -> Vect a Z (::) : (a : Type) -> (k : Nat) -> a -> Vect k a -> Vect (S k) a Example (::) Char (S Z) a ((::) Char Z b (Nil Char)) -- [ a, b ] 3 / 25

Elaboration Example Pairwise addition, high level Idris vadd : Num a => Vect n a -> Vect n a -> Vect n a vadd Nil Nil = Nil vadd (x :: xs) (y :: ys) = x + y :: vadd xs ys 4 / 25

Elaboration Example Step 1: Add implicit arguments vadd : (a : _) -> (n : _) -> (Num a) -> Vect n a -> Vect n a -> Vect n a vadd c (Nil _) (Nil _) = Nil _ vadd c ((::) x xs) ((::) y ys) = (::) ((+) _ x y) (vadd _ xs ys) 5 / 25

Elaboration Example Step 2: Solve implicit arguments vadd : (a : Type) -> (n : Nat) -> (Num a) -> Vect n a -> Vect n a -> Vect n a vadd a Z c (Nil a) (Nil a) = Nil a vadd a (S k) c ((::) a k x xs) ((::) a k y ys) = (::) a k ((+) c x y) (vadd a k c xs ys) 6 / 25

Elaboration Example Step 3: Make pattern bindings explicit vadd : (a : Type) -> (n : Nat) -> (Num a) -> Vect n a -> Vect n a -> Vect n a pat a : Type, c : Num a. vadd a Z c (Nil a) (Nil a) = Nil a pat a : Type, k : Nat, c : Num a. pat x : a, xs : Vect a k, y : a, ys : Vect a k. vadd a (S k) c ((::) a k x xs) ((::) a k y ys) = (::) a k ((+) c x y) (vadd a k c xs ys) 7 / 25

Implementing Elaboration Idris programs may contain several high level constructs not present in TT: Implicit arguments, type classes where clauses, with and case structures, pattern matching let,... Incomplete terms (metavariables) Types often left locally implicit We want the high level language to be as expressive as possible, while remaining translatable to TT. 8 / 25

An observation Consider Coq style theorem proving (with tactics) and Agda style (by pattern matching). Pattern matching is a convenient abstraction for humans to write programs Tactics are a convenient abstraction for building programs by refinement i.e. explaining programming to a machine 9 / 25

An observation Consider Coq style theorem proving (with tactics) and Agda style (by pattern matching). Pattern matching is a convenient abstraction for humans to write programs Tactics are a convenient abstraction for building programs by refinement i.e. explaining programming to a machine Idea: High level program structure directs tactics to build TT programs by refinement 9 / 25

Elaboration Elaborating terms build :: Pattern -> PTerm -> Elab Term runelab :: Name -> Type -> Elab a -> Idris a 10 / 25

Elaboration Elaborating terms build :: Pattern -> PTerm -> Elab Term runelab :: Name -> Type -> Elab a -> Idris a Elaboration is type-directed The Idris monad encapsulates system state The Elab monad encapsulates proof state The Pattern argument indicates whether this is the left hand side of a definition PTerm is the representation of the high-level syntax Term and Type are representations of TT 10 / 25

Implementing Elaboration Proof State The proof state is encapsulated in a monad, Elab, and contains: Current proof term (including holes) Holes are incomplete parts of the proof term (i.e. sub-goals) Unsolved unification problems (e.g. f x = g y) Sub-goal in focus Global context (definitions) 11 / 25

Implementing Elaboration Operations Some primitive operations: Type checking check :: Raw -> Elab (Term, Type) Normalisation normalise :: Term -> Elab Term Unification unify :: Term -> Term -> Elab [(Name, Term)] 12 / 25

Implementing Elaboration Operations Some primitive operations: Type checking check :: Raw -> Elab (Term, Type) Normalisation normalise :: Term -> Elab Term Unification unify :: Term -> Term -> Elab [(Name, Term)] Querying proof state goal :: Elab Type get_env :: Elab [(Name, Type)] get_proofterm :: Elab Term 12 / 25

Implementing Elaboration Tactics A tactic is a function which updates a proof state, for example by: Updating the proof term Solving a sub-goal Changing focus For example: focus :: Name -> Elab () claim :: Name -> Raw -> Elab () forall :: Name -> Raw -> Elab () exact :: Raw -> Elab () apply :: Raw -> [Raw] -> Elab () 13 / 25

Implementing Elaboration Tactics Tactics can be combined to make more complex tactics By sequencing, with do-notation By combinators: try :: Elab a -> Elab a -> Elab a If first tactic fails, use the second tryall :: [Elab a] -> Elab a Try all tactics, exactly one must succeed Used to disambiguate overloaded names Effectively, we can use the Elab monad to write proof scripts (c.f. Coq s Ltac language) 14 / 25

Elaborating Applications Append (++) : {a : Type} -> {n : Nat} -> {m : Nat} -> Vect n a -> Vect m a -> Vect (n + m) a How do we build an application Nil ++ (1 :: 2 :: Nil)? 15 / 25

Elaborating Applications Append (++) : {a : Type} -> {n : Nat} -> {m : Nat} -> Vect n a -> Vect m a -> Vect (n + m) a How do we build an application Nil ++ (1 :: 2 :: Nil)? Tactic script do claim a Type ; claim n Nat ; claim m Nat claim xs (Vect n a) ; claim ys (Vect m a) 15 / 25

Elaborating Applications Append (++) : {a : Type} -> {n : Nat} -> {m : Nat} -> Vect n a -> Vect m a -> Vect (n + m) a How do we build an application Nil ++ (1 :: 2 :: Nil)? Tactic script do claim a Type ; claim n Nat ; claim m Nat claim xs (Vect n a) ; claim ys (Vect m a) apply ((++) a n m xs ys) 15 / 25

Elaborating Applications Append (++) : {a : Type} -> {n : Nat} -> {m : Nat} -> Vect n a -> Vect m a -> Vect (n + m) a How do we build an application Nil ++ (1 :: 2 :: Nil)? Tactic script do claim a Type ; claim n Nat ; claim m Nat claim xs (Vect n a) ; claim ys (Vect m a) apply ((++) a n m xs ys) focus xs; elab Nil focus ys; elab (1 :: 2 :: Nil) 15 / 25

Elaborating Applications Append (++) : {a : Type} -> {n : Nat} -> {m : Nat} -> Vect n a -> Vect m a -> Vect (n + m) a How do we build an application Nil ++ (1 :: 2 :: Nil)? Tactic script do claim a Type ; claim n Nat ; claim m Nat claim xs (Vect n a) ; claim ys (Vect m a) apply ((++) a n m xs ys) focus xs; elab Nil focus ys; elab (1 :: 2 :: Nil) Elaborating each sub-term (and running apply) also runs the unify operation, which fills in the implicit arguments. 15 / 25

Elaborating Applications Given an Idris application of a function f to arguments args: Type check f Yields types for each argument, ty i 16 / 25

Elaborating Applications Given an Idris application of a function f to arguments args: Type check f Yields types for each argument, ty i For each arg i : ty i, invent a name n i and run the tactic claim n i ty i 16 / 25

Elaborating Applications Given an Idris application of a function f to arguments args: Type check f Yields types for each argument, ty i For each arg i : ty i, invent a name n i and run the tactic claim n i ty i Apply f to ns 16 / 25

Elaborating Applications Given an Idris application of a function f to arguments args: Type check f Yields types for each argument, ty i For each arg i : ty i, invent a name n i and run the tactic claim n i ty i Apply f to ns For each non-placeholder arg, focus on the corresponding n and elaborate arg. 16 / 25

Elaborating Applications Given an Idris application of a function f to arguments args: Type check f Yields types for each argument, ty i For each arg i : ty i, invent a name n i and run the tactic claim n i ty i Apply f to ns For each non-placeholder arg, focus on the corresponding n and elaborate arg. (Note: elaborating an argument may affect the type of another argument!) 16 / 25

Elaborating Bindings How do we build a function type, e.g. (n : Nat) -> Vect n Int? 17 / 25

Elaborating Bindings How do we build a function type, e.g. (n : Nat) -> Vect n Int? Tactic script do claim n_s Type forall n n_s focus n_s; elab Nat elab (Vect n Int) 17 / 25

Elaborating Bindings In general, given a binder and its scope, say (x : Check that the current goal type is a Type Create a hole for S claim n S Type Create a binder with forall x n S Elaborate S and T S) -> T 18 / 25

Elaborating Declarations Top level declarations f : S1 ->... -> Sn -> T f x1... xn = e 19 / 25

Elaborating Declarations Top level declarations f : S1 ->... -> Sn -> T f x1... xn = e Elaborate the type, and add f to the context Elaborate the lhs Any out of scope names are assumed to be pattern variables Elaborate the rhs in the scope of the pattern variables from the lhs Check that the lhs and rhs have the same type 19 / 25

Elaborating where Function with where block f : S1 ->... -> Sn -> T f x1... xn = e where f_aux =... 20 / 25

Elaborating where Function with where block f : S1 ->... -> Sn -> T f x1... xn = e where f_aux =... Elaborate the lhs of f Lift the auxiliary definitions to top level functions by adding the pattern variables from the lhs Elaborate the auxiliary definitions Elaborate the rhs of f as normal 20 / 25

Elaborating Type Classes High level Idris class Show a where show : a -> String instance Show Nat where show Z = "Z" show (S k) = "s" ++ show k 21 / 25

Elaborating Type Classes Elaborated TT data Show : (a : Set) -> Set where ShowInstance : (show : a -> String) -> Show a show : (Show a) -> a -> String show (ShowInstance show ) x = show x instanceshownat : Show Nat instanceshownat = ShowInstance show where show : Nat -> String show Z = "Z" show (S k) = "s" ++ show k 22 / 25

Elaborating Type Classes Type class constraints are a special kind of implicit argument (c.f. Agda s instance arguments) Ordinary implicit arguments solved by unification Constraint arguments solved by a tactic resolvetc :: Elab () Looks for a local solution first Then looks for globally defined instances May give rise to further constraints 23 / 25

Summary Idris is a high level language, elaborating to TT via: Tactics to build TT terms A top level monad for adding declarations Everything is translated to a top-level declaration Add a high level feature (e.g. classes) by translating to declarations with and case constructs, records, type providers... Adding new features has proved straightforward! Full details: JFP 23(5): Idris, a general-purpose dependently typed programming language: Design and implementation 24 / 25

For more information http://idris-lang.org/documentation The mailing list idris-lang@groups.google.com The IRC channel, #idris, on irc.freenode.net 25 / 25