Einführung in Agda. Peter Thiemann. Albert-Ludwigs-Universität Freiburg. University of Freiburg, Germany

Similar documents
Artificial Intelligence Programming Probability

Foundations of AI. 10. Knowledge Representation: Modeling with Logic. Concepts, Actions, Time, & All the Rest

Durkheim. Durkheim s fundamental task in Rules of the Sociological Method is to lay out

Coherence Theory of Truth as Base for Knowledge Based Systems

Phenomenal content. PHIL April 15, 2012

GPP 501 Microeconomic Analysis for Public Policy Fall 2017

ARTIFICIAL INTELLIGENCE

Cayley Graphs. Ryan Jensen. March 26, University of Tennessee. Cayley Graphs. Ryan Jensen. Groups. Free Groups. Graphs.

Wason's Cards: What is Wrong?

AGENT-BASED SYSTEMS. What is an agent? ROBOTICS AND AUTONOMOUS SYSTEMS. Today. that environment in order to meet its delegated objectives.

Review Questions in Introductory Knowledge... 37

Some Thoughts on the Principle of Revealed Preference 1

Connectivity in a Bipolar Fuzzy Graph and its Complement

The effect of premise order in conditional reasoning: a test of the mental model theory

CPSC 121 Some Sample Questions for the Final Exam

Cognitive domain: Comprehension Answer location: Elements of Empiricism Question type: MC

Chapter 3 Software Packages to Install How to Set Up Python Eclipse How to Set Up Eclipse... 42

Incorporating Action into Diagnostic Problem Solving (An Abridged Report)

Chapter 2. Knowledge Representation: Reasoning, Issues, and Acquisition. Teaching Notes

To complete this task, students must write an appropriate response and cite evidence to support an inference about a character in a literary text.

Advanced Logical Thinking Skills (1) Revisit the concepts of logic

METHODOLOGY FOR DISSERTATION

TRANSLATING RESEARCH INTO PRACTICE

SOCQ121/BIOQ121. Session 2. Evidence and Research. Department of Social Science. endeavour.edu.au

What Happened to Bob? Semantic Data Mining of Context Histories

Hoare Logic and Model Checking. LTL and CTL: a perspective. Learning outcomes. Model Checking Lecture 12: Loose ends

Evolutionary Approach to Investigations of Cognitive Systems

Validity and Quantitative Research. What is Validity? What is Validity Cont. RCS /16/04

Including Quantification in Defeasible Reasoning for the Description Logic EL

The shallow processing of logical negation

On the constructive nature of natural language quantifiers. Allan Ramsay School of Computer Science, University of Manchester, Manchester M13 9PL, UK

Plan Recognition through Goal Graph Analysis

Peer review on manuscript "Multiple cues favor... female preference in..." by Peer 407

1 What is an Agent? CHAPTER 2: INTELLIGENT AGENTS

Module 28 - Estimating a Population Mean (1 of 3)

Inferencing in Artificial Intelligence and Computational Linguistics

Section 6: Analysing Relationships Between Variables

ACT Aspire. Science Test Prep

COMP329 Robotics and Autonomous Systems Lecture 15: Agents and Intentions. Dr Terry R. Payne Department of Computer Science

Reading Comprehension Strategies

One-Way Independent ANOVA

CFSD 21 st Century Learning Rubric Skill: Critical & Creative Thinking

Plan Recognition through Goal Graph Analysis

Is it possible to gain new knowledge by deduction?

Study on Gender in Physics

Research Questions, Variables, and Hypotheses: Part 2. Review. Hypotheses RCS /7/04. What are research questions? What are variables?

THIS PROBLEM HAS BEEN SOLVED BY USING THE CALCULATOR. A 90% CONFIDENCE INTERVAL IS ALSO SHOWN. ALL QUESTIONS ARE LISTED BELOW THE RESULTS.

Lecture 9: Lab in Human Cognition. Todd M. Gureckis Department of Psychology New York University

INVESTIGATING FIT WITH THE RASCH MODEL. Benjamin Wright and Ronald Mead (1979?) Most disturbances in the measurement process can be considered a form

Appendix: Instructions for Treatment Index B (Human Opponents, With Recommendations)

INFERENCING STRATEGIES

Learning with Rare Cases and Small Disjuncts

Making comparisons. Previous sessions looked at how to describe a single group of subjects However, we are often interested in comparing two groups

Volker Halbach, Axiomatic Theories of Truth, Cambridge: Cambridge. University Press, 2011, ix+365pp., $85.00, ISBN

Unit 1: Day 11 and 12: Summative Task

9 research designs likely for PSYC 2100

Science is a way of learning about the natural world by observing things, asking questions, proposing answers, and testing those answers.

Chapter 20 Confidence Intervals with proportions!

COHERENCE: THE PRICE IS RIGHT

BLUE JAY MANUSCRIPT REVIEW HISTORY REVIEWS (ROUND 1) Editor Decision Letter

Response to the ASA s statement on p-values: context, process, and purpose

DA SCIENTIFIC METHOD SCIENTISTS USE A FORM OF DEDUCTIVE REASONING KNOWN AS THE SCIENTIFIC METHOD. THE SCIENTIFIC METHOD HAS 8 STEPS:

Normality and Faults in Logic-Based Diagnosis*

PARADIGMS, THEORY AND SOCIAL RESEARCH

The Logic of Causal Order Richard Williams, University of Notre Dame, Last revised February 15, 2015

Louie s Leadership Lessons Sept LOUIE S LEADERSHIP LESSONS: Quick Reference:

Managing Your Emotions

Ontology of Observing: The Biological Foundations of Self-Consciousness and of The Physical Domain of Existence

In Class Problem Discovery of Drug Side Effect Using Study Designer

Insight Assessment Measuring Thinking Worldwide

Chapter 02 Developing and Evaluating Theories of Behavior

Marriage Matching with Correlated Preferences

Jazyková kompetence I Session II

for DUMMIES How to Use this Guide Why Conduct Research? Research Design - Now What?

MTAT Bayesian Networks. Introductory Lecture. Sven Laur University of Tartu

Choosing Life: Empowerment, Action, Results! CLEAR Menu Sessions. Substance Use Risk 2: What Are My External Drug and Alcohol Triggers?

INTRODUCTION. Study and Practice

RISK COMMUNICATION FLASH CARDS. Quiz your knowledge and learn the basics.

A SYSTEM FOR COMPUTER-AIDED DIAGNOSIS

Long Essay Question: Evidence/Body Paragraphs

Sawtooth Software. The Number of Levels Effect in Conjoint: Where Does It Come From and Can It Be Eliminated? RESEARCH PAPER SERIES

Chapter Eight: Multivariate Analysis

Minds or Machines. John Beloff (1988) Intro to Philosophy Professor Douglas Olena

6. A theory that has been substantially verified is sometimes called a a. law. b. model.

How to Manage Seemingly Contradictory Facet Results on the MBTI Step II Assessment

Recognizing Stress. Why you need to manage Stress

My Notebook. A space for your private thoughts.

Problem Set 2: Computer Psychiatrist

Module 4: Technology: PsycINFO, APA Format and Critical Thinking

Vagueness, Context Dependence and Interest Relativity

CHAPTER - 6 STATISTICAL ANALYSIS. This chapter discusses inferential statistics, which use sample data to

Tip sheet. A quick guide to the dos and don ts of mental health care and inclusion. 1. Ask questions. Practical tips

ECON320 Notes II: Lecture Notes on Social Choice Theory Part I

The Structure of Implicit Social Attitudes. Alex Madva Eastern Division APA December 2012

OBSERVATION-BASED PROACTIVE COMMUNICATION IN MULTI-AGENT TEAMWORK

Demystification of Cognitive Insight: Opportunistic Assimilation and the Prepared- Mind Perspective (Seifert et al., 1993)

Chapter Eight: Multivariate Analysis

MITOCW ocw f99-lec20_300k

A Computational Theory of Belief Introspection

Transcription:

Einführung in Agda https://tinyurl.com/bobkonf17-agda Albert-Ludwigs-Universität Freiburg Peter Thiemann University of Freiburg, Germany thiemann@informatik.uni-freiburg.de 23 Feb 2018

Programs that work the dependent stairway Thiemann Agda 2018-02-23 2 / 38

Programs that work the dependent stairway Choose an expressive type system Thiemann Agda 2018-02-23 2 / 38

Programs that work the dependent stairway Choose an expressive type system Express your specification as a type Thiemann Agda 2018-02-23 2 / 38

Programs that work the dependent stairway Choose an expressive type system Express your specification as a type Write the only possible program of this type Thiemann Agda 2018-02-23 2 / 38

Thiemann Agda 2018-02-23 3 / 38

Why does it work? The Curry-Howard Correspondence Thiemann Agda 2018-02-23 4 / 38

Why does it work? The Curry-Howard Correspondence Propositions as types Thiemann Agda 2018-02-23 4 / 38

Why does it work? The Curry-Howard Correspondence Propositions as types Proofs as programs Thiemann Agda 2018-02-23 4 / 38

Why does it work? The Curry-Howard Correspondence Propositions as types Proofs as programs Central insight Write program of this type = Find a proof for this proposition Thiemann Agda 2018-02-23 4 / 38

In Agda Remember Curry-Howard A type corresponds to a proposition Elements of the type are proofs for that proposition Thiemann Agda 2018-02-23 5 / 38

In Agda Remember Curry-Howard A type corresponds to a proposition Elements of the type are proofs for that proposition The role of functions A function f : A B... Thiemann Agda 2018-02-23 5 / 38

In Agda Remember Curry-Howard A type corresponds to a proposition Elements of the type are proofs for that proposition The role of functions A function f : A B... transforms an element of A to an element of B Thiemann Agda 2018-02-23 5 / 38

In Agda Remember Curry-Howard A type corresponds to a proposition Elements of the type are proofs for that proposition The role of functions A function f : A B... transforms an element of A to an element of B transforms a proof of A to a proof of B Thiemann Agda 2018-02-23 5 / 38

In Agda Remember Curry-Howard A type corresponds to a proposition Elements of the type are proofs for that proposition The role of functions A function f : A B... transforms an element of A to an element of B transforms a proof of A to a proof of B shows: if we have a proof of A, then we have a proof of B Thiemann Agda 2018-02-23 5 / 38

In Agda Remember Curry-Howard A type corresponds to a proposition Elements of the type are proofs for that proposition The role of functions A function f : A B... transforms an element of A to an element of B transforms a proof of A to a proof of B shows: if we have a proof of A, then we have a proof of B is a proof of the logical implication A B Thiemann Agda 2018-02-23 5 / 38

Plan 1 Prelude 2 Logic 3 Numbers 4 Vectors 5 Going further Thiemann Agda 2018-02-23 6 / 38

Logic in Agda Defining types: the true proposition -- Truth data : Set where tt : Thiemann Agda 2018-02-23 7 / 38

Logic in Agda Defining types: the true proposition -- Truth data : Set where tt : Explanation (cf. data in Haskell) Truth a comment data defines a new datatype is the name of the type Set is its kind tt is the single element of Thiemann Agda 2018-02-23 7 / 38

Logic in Agda Conjunction is really just a pair -- Conjunction data _ _ (P Q : Set) : Set where _,_ : P Q (P Q) Thiemann Agda 2018-02-23 8 / 38

Logic in Agda Conjunction is really just a pair -- Conjunction data _ _ (P Q : Set) : Set where _,_ : P Q (P Q) Explanation _ _ the name of an infix type constructor the underlines indicate the positions of the arguments (P Q : Set) parameters of the type _,_ data constructor with two parameters Thiemann Agda 2018-02-23 8 / 38

Logic in Agda Disjunction is really just Either -- Disjunction data _ _ (P Q : Set) : Set where inl : P (P Q) inr : Q (P Q) Thiemann Agda 2018-02-23 9 / 38

Logic in Agda Disjunction is really just Either -- Disjunction data _ _ (P Q : Set) : Set where inl : P (P Q) inr : Q (P Q) Explanation two data constructors everything covered Thiemann Agda 2018-02-23 9 / 38

A first program in Agda Specification -- Conjunction is commutative commconj1 : (P : Set) (Q : Set) (P Q) (Q P) Thiemann Agda 2018-02-23 10 / 38

A first program in Agda Specification -- Conjunction is commutative commconj1 : (P : Set) (Q : Set) (P Q) (Q P) Explanation (P : Set) an argument of type Set with name P to be used later in the type (P : Set) and (Q : Set) declare that P and Q are types (propositions) (P Q) (Q P) is the proposition we want to prove = the type of the program we want to write Thiemann Agda 2018-02-23 10 / 38

A first program in Agda Specification -- Conjunction is commutative commconj1 : (P : Set) (Q : Set) (P Q) (Q P) Let s write it interactively Thiemann Agda 2018-02-23 10 / 38

Should start with a screen like this Thiemann Agda 2018-02-23 11 / 38

Variations on the specification Fully explicit -- Conjunction is commutative commconj1 : (P : Set) (Q : Set) (P Q) (Q P) commconj1 P Q p, q = q, p arguments P and Q are not used and Agda can infer them Thiemann Agda 2018-02-23 12 / 38

Variations on the specification Fully explicit -- Conjunction is commutative commconj1 : (P : Set) (Q : Set) (P Q) (Q P) commconj1 P Q p, q = q, p arguments P and Q are not used and Agda can infer them With inferred parameters -- Conjunction is commutative commconj2 : (P Q : Set) (P Q) (Q P) commconj2 p, q = q, p just put _ for inferred arguments Thiemann Agda 2018-02-23 12 / 38

Variations on the specification Implicit parameters -- Conjunction is commutative commconj : {P Q} (P Q) (Q P) commconj p, q = q, p Thiemann Agda 2018-02-23 13 / 38

Variations on the specification Implicit parameters -- Conjunction is commutative commconj : {P Q} (P Q) (Q P) commconj p, q = q, p Explanation {P Q} is short for {P Q : Set} {P Q : Set} indicates that P and Q are implicit parameters: they need not be provided and Agda tries to infer them Successful here, but we get an obscure error message if Agda cannot infer implicit parameters Thiemann Agda 2018-02-23 13 / 38

A second program in Agda Specification -- Disjunction is commutative commdisj : {P Q} (P Q) (Q P) Thiemann Agda 2018-02-23 14 / 38

A second program in Agda Specification -- Disjunction is commutative commdisj : {P Q} (P Q) (Q P) Let s write it interactively Thiemann Agda 2018-02-23 14 / 38

Logic in Agda Negation at last -- Falsity data : Set where -- Negation : Set Set P = P Thiemann Agda 2018-02-23 15 / 38

Logic in Agda Negation at last -- Falsity data : Set where -- Negation : Set Set P = P Explanation The type has no elements, hence no constructors Negation is defined by reductio ad absurdum: P i.e., having a proof for P would lead to a contradiction Thiemann Agda 2018-02-23 15 / 38

De Morgan s laws Specification -- DeMorgan s laws demnd1 : {P Q} (P Q) ( P Q) demnd2 : {P Q} ( P Q) (P Q) Thiemann Agda 2018-02-23 16 / 38

De Morgan s laws Specification -- DeMorgan s laws demnd1 : {P Q} (P Q) ( P Q) demnd2 : {P Q} ( P Q) (P Q) Interaction time Thiemann Agda 2018-02-23 16 / 38

Plan 1 Prelude 2 Logic 3 Numbers 4 Vectors 5 Going further Thiemann Agda 2018-02-23 17 / 38

Numbers in Agda Surprise Numbers are not predefined in Agda We have to define them ourselves (But there is a library) Thiemann Agda 2018-02-23 18 / 38

Numbers in Agda Surprise Numbers are not predefined in Agda We have to define them ourselves (But there is a library) Let s try Thiemann Agda 2018-02-23 18 / 38

Peano s axioms 1 Giuseppe Peano says... 1 zero is a natural number 2 If n is a natural number, then suc n is also a natural number 3 All natural numbers can be (and must be) constructed from 1. and 2. 1 Image Attribution: By Unknown - School of Mathematics and Statistics, University of St Andrews, Scotland [1], Public Domain, https://commons.wikimedia.org/w/index.php?curid=2633677 Thiemann Agda 2018-02-23 19 / 38

Peano s axioms 1 Giuseppe Peano says... 1 zero is a natural number 2 If n is a natural number, then suc n is also a natural number 3 All natural numbers can be (and must be) constructed from 1. and 2. An inductive definition 1 Image Attribution: By Unknown - School of Mathematics and Statistics, University of St Andrews, Scotland [1], Public Domain, https://commons.wikimedia.org/w/index.php?curid=2633677 Thiemann Agda 2018-02-23 19 / 38

Inductive definition in Agda Natural numbers data N : Set where zero : N suc : N N Thiemann Agda 2018-02-23 20 / 38

Inductive definition in Agda Natural numbers data N : Set where zero : N suc : N N Explanation Defines zero and suc just like demanded by Peano Define functions on N by induction and pattern matching on the constructors Thiemann Agda 2018-02-23 20 / 38

Functional programming Addition add : N N N add zero n = n add (suc m) n = suc (add m n) Thiemann Agda 2018-02-23 21 / 38

Functional programming Addition add : N N N add zero n = n add (suc m) n = suc (add m n) Subtraction sub : N N N sub m zero = m sub zero (suc n) = zero sub (suc m) (suc n) = sub m n Thiemann Agda 2018-02-23 21 / 38

Why specify properties? Deficiency of Testing Testing shows the presence, not the absence of bugs. E.W. Dijkstra Thiemann Agda 2018-02-23 22 / 38

What can we specify? Properties of addition all require equality on numbers Thiemann Agda 2018-02-23 23 / 38

What can we specify? Properties of addition all require equality on numbers Next surprise Equality is not predefined in Agda We have to define it ourselves (But there is a library) Thiemann Agda 2018-02-23 23 / 38

What can we specify? Properties of addition all require equality on numbers Next surprise Equality is not predefined in Agda We have to define it ourselves (But there is a library) Let s try Thiemann Agda 2018-02-23 23 / 38

Inductive definition of equality Equality on natural numbers data _ _ : N N Set where z z : zero zero s s : {m n : N} m n suc m suc n Explanation Unusual: datatype parameterized by two numbers The constructor s s takes a proof that m n and thus becomes a proof that suc m suc n Thiemann Agda 2018-02-23 24 / 38

Properties of equality Equality is... -- reflexive refl- : (n : N) n n -- transitive trans- : {m n o : N} m n n o m o -- symmetric symm- : {m n : N} m n n m Thiemann Agda 2018-02-23 25 / 38

Properties of equality Reflexivity Need to define a function that given some n returns a proof of (element of) n n Straightforward programming exercise Use pattern matching / induction Agda can do it automatically Thiemann Agda 2018-02-23 26 / 38

Properties of equality Reflexivity Need to define a function that given some n returns a proof of (element of) n n Straightforward programming exercise Use pattern matching / induction Agda can do it automatically Interaction time Thiemann Agda 2018-02-23 26 / 38

Properties of equality Symmetry m n n m Symmetry can be proved by induction on m and n Introduces a new concept: absurd patterns Less cumbersome alternative: pattern matching on equality proof Thiemann Agda 2018-02-23 27 / 38

Properties of equality Symmetry m n n m Symmetry can be proved by induction on m and n Introduces a new concept: absurd patterns Less cumbersome alternative: pattern matching on equality proof Interaction time Thiemann Agda 2018-02-23 27 / 38

Properties of addition Zero is neutral element of addition neutraladd0l : (m : N) add zero m m neutraladd0r : (m : N) add m zero m Thiemann Agda 2018-02-23 28 / 38

Properties of addition Zero is neutral element of addition neutraladd0l : (m : N) add zero m m neutraladd0r : (m : N) add m zero m Addition is associative assocadd : (m n o : N) add m (add n o) add (add m n) o Thiemann Agda 2018-02-23 28 / 38

Properties of addition Zero is neutral element of addition neutraladd0l : (m : N) add zero m m neutraladd0r : (m : N) add m zero m Addition is associative assocadd : (m n o : N) add m (add n o) add (add m n) o Addition is commutative commadd : (m n : N) add m n add n m Thiemann Agda 2018-02-23 28 / 38

Properties of addition Proving... Neutral element and associativity are straightforward Commutativity is slightly more involved Requires an auxiliary function Thiemann Agda 2018-02-23 29 / 38

Properties of addition Proving... Neutral element and associativity are straightforward Commutativity is slightly more involved Requires an auxiliary function Interaction time Thiemann Agda 2018-02-23 29 / 38

Plan 1 Prelude 2 Logic 3 Numbers 4 Vectors 5 Going further Thiemann Agda 2018-02-23 30 / 38

Vectors in Agda Vectors with static bounds checks Flagship application of dependent typing All vector operations proved safe at compile time Key: define vector type indexed by its length Thiemann Agda 2018-02-23 31 / 38

The vector type data Vec (A : Set) : (n : N) Set where Nil : Vec A zero Cons : {n : N} (a : A) Vec A n Vec A (suc n) Thiemann Agda 2018-02-23 32 / 38

The vector type data Vec (A : Set) : (n : N) Set where Nil : Vec A zero Cons : {n : N} (a : A) Vec A n Vec A (suc n) concat : {A m n} Vec A m Vec A n Vec A (add m n) concat Nil ys = ys concat (Cons a xs) ys = Cons a (concat xs ys) Thiemann Agda 2018-02-23 32 / 38

Safe vector access avoid out of bound indexes Trick #1 Type of get depends on length of vector n and index m... and a proof that m < n Thiemann Agda 2018-02-23 33 / 38

Safe vector access avoid out of bound indexes Trick #1 Type of get depends on length of vector n and index m... and a proof that m < n get : {A n} Vec A n (m : N) suc m n A Thiemann Agda 2018-02-23 33 / 38

Safe vector access avoid out of bound indexes Trick #1 Type of get depends on length of vector n and index m... and a proof that m < n get : {A n} Vec A n (m : N) suc m n A Trick #2... type restricts the index to m < n Thiemann Agda 2018-02-23 33 / 38

Safe vector access avoid out of bound indexes Trick #1 Type of get depends on length of vector n and index m... and a proof that m < n get : {A n} Vec A n (m : N) suc m n A Trick #2... type restricts the index to m < n get1 : {A n} Vec A n Fin n A Thiemann Agda 2018-02-23 33 / 38

Finite set type data Fin : N Set where zero : {n : N} Fin (suc n) suc : {n : N} Fin n Fin (suc n) Thiemann Agda 2018-02-23 34 / 38

Finite set type data Fin : N Set where zero : {n : N} Fin (suc n) suc : {n : N} Fin n Fin (suc n) Explanation Overloading of constructors ok Fin zero = (empty set) Fin (suc zero) = {0} Fin (suc (suc zero)) = {0, 1} etc Thiemann Agda 2018-02-23 34 / 38

Finite set type data Fin : N Set where zero : {n : N} Fin (suc n) suc : {n : N} Fin n Fin (suc n) Explanation Overloading of constructors ok Fin zero = (empty set) Fin (suc zero) = {0} Fin (suc (suc zero)) = {0, 1} etc Interaction time Thiemann Agda 2018-02-23 34 / 38

Splitting a vector We know this type already... -- Pair data _ _ (A B : Set) : Set where _,_ : (a : A) (b : B) (A B) -- split a vector in two parts split : {A n} Vec A n (m : N) m n Vec A m Vec A (sub n m) Solution introduces a new feature: with matching This operation can also be defined with Fin... Thiemann Agda 2018-02-23 35 / 38

Splitting a vector We know this type already... -- Pair data _ _ (A B : Set) : Set where _,_ : (a : A) (b : B) (A B) -- split a vector in two parts split : {A n} Vec A n (m : N) m n Vec A m Vec A (sub n m) Solution introduces a new feature: with matching This operation can also be defined with Fin... Thiemann Agda 2018-02-23 35 / 38

Plan 1 Prelude 2 Logic 3 Numbers 4 Vectors 5 Going further Thiemann Agda 2018-02-23 36 / 38

Going further http://learnyouanagda.liamoc.net/ nicely paced tutorial, some more background http://wiki.portal.chalmers.se/agda/pmwiki.php?n= Main.HomePage definitive resource http://wiki.portal.chalmers.se/agda/pmwiki.php?n= Main.Othertutorials with a load of links to tutorials Thiemann Agda 2018-02-23 37 / 38

Questions? Thiemann Agda 2018-02-23 38 / 38