129
2. Red. Systems & Term Rewriting (a) Reductions in Agda. (b) Reduction systems. (c) Termination, confluence, normalisation. (d) Term rewriting systems. CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sect. 2 2-1

2. Red. Systems & Term Rewriting

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

2. Red. Systems & Term Rewriting

(a) Reductions in Agda.(b) Reduction systems.(c) Termination, confluence, normalisation.(d) Term rewriting systems.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sect. 2 2-1

(a) Reductions in Agda

Functional programming is essentially based on termreduction:

Assume we introduce the natural numbers as analgebraic data type built from 0 and S (this is actualAgda code):

data N : Set where

Z : N

S : N→ N

(reductionSystems1.agda)We write here Z instead of 0, since the symbol 0 willbe reserved for the built-in integers.S n stands for n + 1.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-2

Notations in Agda

data N : Set where

Z : N

S : N→ N

data N : Set means that we have introduced a new set,which is given by the constructors which follow after thesymbol where.

What is in most programming languages called typeis in Agda for historic reasons called “Set”.The above code introduces a new set, namely theset of natural numbers.It has two constructors: the constant Z, and S whichtakes as argument an n : N and returns an elementof N.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-3

N as a Reduction SystemSo the elements of N are

Z S Z S (S Z) S (S (S Z)) · · ·

We can now define + and ∗ in N by induction over thedefinition of N.

For those with mathematical problems: “Inductionover the definition of N” means roughly casedistinction on N in a terminating way.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-4

Definition of +

_ + _ : N→ N→ N

n + Z = n

n + S m = S (n + m)

_ + _ means thatthe first argument (denoted by the first _) of + isplaced before +,the second argument (denoted by the second _) of +is placed after +,

which means here that + is used infix:we write s + t instead of _ + _ s t.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-5

Mixed Fixed SymbolsAgda allows arbitrary mixed fixed symbols:

For instance we can define _strange_symbol_ as asymbol which is used as

n strange m symbol k

for_strange_symbol_ n m k.

There are almost no restrictions.

If the parsing of an expression is ambiguous, a parseerror is given – then one needs to resolve the ambiguityby using parentheses.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-6

Definition of +

The definition of + above means that we have thefollowing reductions:

s + Z −→ s ,

s + S t −→ S (s + t) .

Note that S binds more than +. SoS r + s reads (S r) + s.r + S s reads r + (S s).

We have 2 + 2 −→ 4:

S (S Z) + S (S Z) −→ S (S (S Z) + S Z)

−→ S (S (S (S Z) + Z))

−→ S (S (S (S Z)))

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-7

Our first Agda ExampleWe are going to show how we can deal with thisexample in Agda.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-8

InstallationA substantially improved version of Agda called Agda2has been released recently.

In this module Agda2 will be used, which has acompletely different syntax from Agda1.

Currently the installation requires some work.Easy to compile versions (which exist for Agda1) arein preparation, but have not been released yet .

For Agda1 there exists a 1-click-Windows-installer.We hope that this problem will soon be solved.

Anton Setzer has installed Agda2 under Linux .

He is working on getting it installed under Windows.

Agda can be installed under Macintosh as well.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-9

Installation of AgdaInstructions on how to install Agda under Linux andhopefully as well under Windows will soon be createdand can then soon be found underhttp://www.cs.swan.ac.uk/∼csetzer/

othersoftware/agda2/agda2installation.html

The installation will provide an Emacs mode for Agdafiles.

If a file with extension .agda is loaded into Emacs, thenthis mode is invoked.

The source code for the examples given in this lecturewill be available from the course home page (the namesof the files are added in the notes, e.g. in the formreductionSystems1.agda .

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-10

Agda in the Linux LabAgda will be installed in the Linux lab.

When this is ready, follow the item “Getting startedwith Agda” on the home page of this module.Please check whether the installation works.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-11

Help System of EmacsNote that Emacs has an excellent and well-written helpsystem.

Includes a hypertext version of most features of theEmacs, search facilities, descriptions of all variables.

The help system is activated using Control-h plus anadditional key stroke.

Emacs notation: C-h.

A quick tutorial, which introduces the help system startswhen typing, after Emacs has been started, C-h t

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-12

Working in AgdaOnce, Agda is installed, the above can be defined asfollows:

One opens in Emacs a file with extension “.agda ”,e.g. “reductionSystems1.agda”.Emacs will switch into Agda mode .Code written needs to be part of a “module ”(we will not discuss details of the module system inthis lecture course.)We will create a module by typing in:

module reductionSystems1 where

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-13

Working in Agdamodule reductionSystems1 where

Now we add the definition of N:

module reductionSystems1 where

data N : Set where

Z : N

S : N→ N

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-14

Blanks around “ :”Please note that there needs to be a blank around all“:”.

Z: without a blank in between is considered by Agdaas an identifier Z:.:N without a blank in between is considered by Agdaas an identifier :N.Only brackets “(”, “”, “)”, “” and blanks (andpossibly some other symbols not discovered yet byA. Setzer) break identifiers.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-14a

Typing in Special SymbolsThe standard installation of Agda activates a specialmode of Emacs which allows to type in special symbols.

Special symbols are typed in by using commandsequences inspired by LATEX.

For instanceN is written by typing in \BbbN,α by typing in \alpha.

On the homepage for this course under “Other CourseMaterial” a file leimListOfSymbols.tut (which wasextracted from the source code of this mode) will bemade available, which contains the key bindings for thismode.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-15

Loading the BufferAgda doesn’t realise any changes in the buffer, unlesswe load it.

For this we can use the main menu , which one obtainsby right-clicking on the word “Agda2” in the Panel.

By choosing from the main menu “Load ”, we load thebuffer.

An additional buffer called ∗All Goals ∗ appears, whichwill be explained later.

If there was an error loading the buffer, then instead of∗All Goals ∗ a buffer ∗Error ∗ is displayed showing anerror message.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-16

Keyboard Short CutsIn the main menu, for each command a keyboard shortcut is presented.It is advisable to learn the most frequently used ones.

In order to type check the buffer, we can use thekeyboard commandC-c C-x C-l .

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-17

GoalsWhen defining code, one can leave some code open forbeing filled in a later step.

These holes are called goal , which stands for a termnot yet defined.

Syntax in Agda: ! !, written in “green” in theEmacs mode.

One can type in as well “?” for a goal, which will then beconverted, when loading the buffer, into the symbol! !.

So let’s type in the beginning of the definition of +:

_ + _ : N→ N→ N

n + m = ?

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-18

GoalsWhen the buffer is loaded, the goal will be shown in adifferent colour, and one can only edit inside or outsidethe goal.

Each goal gets a number.

When right-clicking on the goal, the goal-menu isopened.

when using Emacs, is activated and becomes thegoal-menu. (Outside a goal this menu doesn’t exist).

If one wants to edit the buffer in a way which isimpossible because of the restrictions of editing goals,one can do so by first deactivating agda using MenuDeactivate Agda (C-c C-x C-d)

When loading the buffer again one gets back to thestate in which goals have special status.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-19

GoalsGoals are numbered by the order in which they werecreated.Goals are displayed together with their type inseparate buffer called “∗ Goals ∗”.This can be activated as well by using menu“Show Goals (C-c C-?) ”.In Emacs mode, goals have a special status.

When typing in text into a goal, the goal expands.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-20

GoalsUsing the goal-menu, we can find out

what type is expected:Goal Type (C-c C-t) .Agda shows: ?0 : N

There is as well a variantGoal Type (normalised) .It evaluates the type of the goal using reduction rules(see later).

Using the main menu we can always show the types ofall goals (Show Goals (C-c C-?) ).

We can as well jump to the next and previous goalusing menu items Next Goal (C-c C-f) andPrevious Goal (C-c C-p) .

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-21

ContextInside a goal we can as well find out the current context:

Using menu Context (environment) (C-c C-e) .In our example Agda shows (apart from some libraryfunctions):n : N

m : N

So when defining n + m we can make use of n : N,m : N and the function + we are defining at present.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-22

Case Distinction_ + _ : N→ N→ N

n + m = ! !

In order to define n + m, we have to make a casedistinction on whether m = Z or m = S m′.

This can be achieved by replacing the line n + m = ! !by two lines for the two cases.

In order to achieve this we deactive Agda by using mainmenu command Deactivate Agda (C-c C-x C-d) .

Then we can replace the line n + m = ! ! by two linesas follows:

_ + _ : N→ N→ N

n + Z = ! !

n + S m = ! !

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-23

Coverage CheckerAgda has built in a coverage checker , which makessure that if one makes a case distinction as above, thenall cases are covered .

If we omit one of the cases, e.g. the S-case, and loadthe buffer:

_ + _ : N→ N→ N

n + Z = ! !

then we get an error message (see next slide)

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-24

Error Message/home/csetzer/lectures/07/intertheo/agdalectureexamples/reductionSystems1.agda:9,1-17 Incompletepattern matching for + .Missing cases:+ (S )when checking the definition of +

So in the definition of _ + _, the case where the firstargument is arbitrary, and the second argument is of theform S applied to something is missing.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-25

Give and Refine_ + _ : N→ N→ N

n + Z = ! !

n + S m = ! !

We can solve the first goal by typing in the value n.

Then we can right-click on the goal and use from thegoal-menu either Give (C-c C-SPC) orRefine (C-d C-r) .

Give works when one has an exact solution as in thesituation above.Refine works not always, but allows as well partialsolutions, which need refinement. See the case S n

on the next slide.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-26

RefineWe obtain in both cases

_ + _ : N→ N→ N

n + Z = n

n + S m = ! !

We can use the refine mechanism in case of n + S m asfollows:

We know that the solution will be of the form S ! !.We can now type into the goal S and then use thecommand refine.Agda knows that if we apply S to one argument(which is a natural number), then we get somethingwhich solves the goal.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-27

Refine_ + _ : N→ N→ N

n + Z = n

n + S m = ! !

If we type into the goal S and refine it, we obtain

_ + _ : N→ N→ N

n + Z = n

n + S m = S ! !

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-28

Refine_ + _ : N→ N→ N

n + Z = n

n + S m = S ! !

We want to solve the goal by typing in something ofthe form ! !+ ! !, which is _ + _ ! ! ! !.We can type into the goal _ + _ and use refineAgda realises that _ + _ applied this time to 2arguments solves the goal, and rearranges the resultin infix form.

We obtain

_ + _ : N→ N→ N

n + Z = n

n + S m = S (! ! + ! !)CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-29

Termination Checker

_ + _ : N→ N→ N

n + Z = n

n + S m = S (! ! + ! !)

The + and the defining symbol _ + _ are now marked inred.

This is because of the termination checker.

Agda disallows non-terminating programs, like

f : N→ N

f n = f n

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-30

Termination CheckerThe termination check is necessary, since otherwise thelogic of Agda is inconsistent.

This is no problem for Agda used as a dependentlytyped programming language.

But then the validity of any proved parts of it(e.g. that a list returned is sorted) will no longer beguaranteed.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-31

Termination CheckerIn the code

_ + _ : N→ N→ N

n + Z = n

n + S m = S (! ! + ! !)

we don’t know yet whether this will pass the terminationchecker when the goals are solved or not.

If we solve it by

n + S m = S (n + S m)

then we obtain a non-terminating program.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-32

Termination CheckerIf we solve

_ + _ : N→ N→ N

n + Z = n

n + S m = S (! ! + ! !)

in the correct way

n + S m = S (n + m)

it will pass the termination checker.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-33

Termination CheckerNote that only a warning (in the form of the symbolscoloured) is issued, but no error is issued.

This is since this warning only indicates that there ispotential problem.

It might be solved once all goals are solved.There are as well limitations to any terminationchecker:It is in principal not possible to write a terminationchecker which accepts all terminating Agdaprograms.· So a program could be terminating and therefore

okay, but still not pass the termination checker.· This will be discussed later.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-34

Termination CheckerIn order to make sure that there are no terminationcheck problems left, one can use from a shell thecommand“agda file”e.g.agda ∼csetzer/r/reductionSystems1.agda

This command will check the file and report type errors,problems of the coverage checker and problems of thetermination checker.

Code submitted as coursework should be checkedthis way, in order to guarantee that there are no hiddenerrors.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-35

Finishing Definition of +

_ + _ : N→ N→ N

n + Z = n

n + S m = S (! ! + ! !)

We can solve now the two goals by using n and m andgoal-menu refine or give and obtain

_ + _ : N→ N→ N

n + Z = n

n + S m = S (n + m)

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-36

Indentation SensitivityAgda is indentation sensitive.

So often instead of having parentheses “〈Code〉”, asin other languages, all lines belonging to 〈Code〉 have tobe intended more then the surrounding code, andusually in the same way.

Therefore top level definitions have to start in column1.Otherwise they are considered as being an extension ofa previous definition.

All code belonging to such a definition in later columnshas to be intended at least once.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-37

Indentation SensitivityExample: The following causes an error:

data N : Set where

Z : N

S : N→ N

Agda assumes that S is not a constructor of N, but afunction, which is not defined yet.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-38

Indentation SensitivityExample: The following causes an error:

data N : Set where

Z : N

S : N→ N

_ + _ : N→ N→ N

n + Z = n

n + S m = S (n + m)

Now _ + _ is considered as a constructor of N, and theequation n + Z = n doesn’t make sense for aconstructor and causes a parse error.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-39

Definition of MultiplicationDefinition of _ ∗ _:

_ ∗ _ : N→ N→ N

n ∗ Z = Z

n ∗ S m = (n ∗m) + n

This means that we have the following reductions:

s ∗ Z −→ Z ,

s ∗ S t −→ s ∗ t + s .

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-40

Binding of SymbolsWe can add the following two lines:

infixl 60 _ + _infixl 80 _ ∗ _

This means that_ + _ and _ ∗ _ are infix left-associative:

n + m + k is interpreted as (n + m) + k, similarly for∗.

If we had used infixr instead, we obtann + m + k is interpreted as n + (m + k), similarly for∗.

If we use infix, then n + m + k is considered asambiguous and causes a parse error.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-41

Binding of Symbols

infixl 60 _ + _infixl 80 _ ∗ _

Furthermore that _ ∗ _ has a higher number than _ + _means that ∗ binds more than +:

n + m ∗ k is interpreted as n + (m ∗ k).

Without stating the statements above n + m ∗ k isconsidered as ambiguous and causes a parse error.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-42

Complete Definition of + and ∗We obtain as definition of + and ∗:

infixl 60 _ + _infixl 80 _ ∗ _

_ + _ : N→ N→ N

n + Z = n

n + S m = S (n + m)

_ ∗ _ : N→ N→ N

n ∗ Z = Z

n ∗ S m = n ∗m + n

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-43

Testing the above in AgdaIn order to test the above we can make use of themain-menu (or goal-menu)Compute normal form (C-c C-n) .

It will ask in the mini-buffer for an expression.

If we type inS (S Z) + S (S Z)

(for 2 + 2), Agda shows in another buffer the result

S (S (S (S Z))) ,

i.e. 4.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-44

Using the Builtin Natural NumbersWe can use as well the builtin natural numbers:

If we add the following code

−# BUILTIN NATURAL N #−

−# BUILTIN ZERO Z #−

−# BUILTIN SUC S #−

−# BUILTIN NATPLUS _ + _ #−

−# BUILTIN NATTIMES _ ∗ _ #−

then N is identified with the builtin type of naturalnumbers, and Z, S, _ + _, _ ∗ _ with the correspondingbuiltin operations.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-45

Using the Builtin Natural NumbersThen we can define for instance

a : N

a = 5

and if we compute the normalform of 7 + 9 we obtain 16.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-46

Agda and Non-TerminationIf one makes a mistake and defines _ + _ so that itdoesn’t terminate (e.g. defining in case of S m

S (n + Sm) instead of S (n + m)), then Agda will crash,and not display anything.

This can be observed by checking the buffer ∗ghci ∗.All Emacs activities will result in Haskellcommands been issued to this buffer, and theresult is then used in order to modify the emacsbuffer.Since the interactive Glasgow Haskell Compilerghci is used, the buffer for communicating is called∗ghci∗.If Agda crashes, one sees that a command wasissued there, but no response was returned yet.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-47

Agda and Non-TerminationIf one has non-terminating recursion, Agda might crashduring type checking and at other places as well.

Can be observed as well by switching to buffer∗ghci∗.

The ghci buffer is a buffer to Haskell with Agda loaded.It can be used for carrying out haskell computations,e.g. for computing 3 + 3 in Haskell.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-48

Reduction Systems and AgdaWe want 2 + 2 and 4 to be the same.

In Agda, referring to our self-defined natural numbers,this means that S (S Z) + S (S Z) and S (S (S (S Z)))should be the same.

We have just seen that S (S Z) + S (S Z) reduces toS (S (S (S Z))).

The underlying principle behind this is:If a term reduces to another term,then these two terms are the same.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-49

Towards General Reduction SystemsSince we have dependent types, equality plays a rôle intype checking

If A r is a type depending on r and a : A r, thena : A s provided that r and s are equal.

In order to understand Agda better, we will study in thefollowing general reduction relations.

Given by a set of Terms T and a reduction relations −→ t between terms s and t.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (a) 2-50

(b) Reduction Systems

A::::::::::::

reduction::::::::::

system is a pair (T,−→) consisting of aset T (of terms) and a binary relation −→ on T.

We write s −→ t for “s, t are in relation −→” and sayusually “s reduces to t”.

Example 1:Let T be the set of terms formed from 0, S, + and ∗ inthe usual way.

So for instance 0, S 0 and S 0+0 are elements of T.Let −→ be the reduction relation defined as before.

So we have for instanceS(S 0) + 0 −→ S(S 0)S(S 0) + S 0 −→ S(S(S 0))

Then (T,−→) forms a reduction system.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (b) 2-51

Example 2 (Reduction System)A simple reduction system is T = N with reductionsn + 1 −→ n for n ∈ N:

2 30 1 4 . . .

So we have reductions of the form:

5 −→ 4 −→ 3 −→ 2 −→ 1 −→ 0 .

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (b) 2-52

Example 3 (Reduction System)Another simple example is T = N with reductionsn −→ m for n,m ∈ N s.t. n > m:

2 30 1 4

So we have reductions of the form:

23 −→ 11 −→ 3 −→ 1 −→ 0 .

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (b) 2-53

Example 4 (Reduction System)A further simple example is T = N ∪ •, with reductionsn + 1 −→ n for n ∈ N,and • −→ n for n ∈ N:

2 30 1 4

So we have reductions of the form:

• −→ 5 −→ 4 −→ 3 −→ 2 −→ 1 −→ 0 .

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (b) 2-54

−→,←→

If (T,−→) is a reduction system, we define for s, t ∈ T

s←− t :⇔ t −→ s

s←→ t :⇔ s −→ t ∨ s←− t

Note that we are using “∨”, not “∧”.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (b) 2-55

→∗ and ↔∗

In Agda we said we identify two terms which reduce toeach other in possible multiple steps.

Therefore we study two concepts:One is s −→∗ t, which means that s reduces to t inpossibly multiple steps.

When Agda reduces a term s, it returns a term t

s.t. s −→∗ t, and t cannot reduce any further.One is s←→∗ t, which is the equality induced by −→.

So Agda identifies terms s and t s.t. s←→∗ t.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (b) 2-56

→∗

If (T,−→) is a reduction system, we define

s−→∗:::::

t iff there exists a (possibly empty) sequence

s ≡ s0 −→ s1 −→ s2 −→ · · · −→ sn ≡ t

By empty sequence we mean: n = 0 is allowed, inwhich case we have s ≡ t.

(We write ≡::

for syntactic equality between terms in

order to avoid confusion with equality moduloreductions introduced later and denoted by =).

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (b) 2-57

ExampleIf we take N with reductionsn + 1 −→ n for n ∈ N:

2 30 1 4 . . .

Then 5 −→ 4 −→ 3 −→ 2, therefore 5 −→∗ 2.In general n −→∗ m⇔ n ≥ m.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (b) 2-58

→∗

In order to express the above shorter, one says that−→∗ is the reflexive and transitive closure of −→, i.e.the least reflexive and transitive relation containing −→.

This means the following:r −→ s implies r −→∗ s.−→∗ is reflexive, i.e. for all r ∈ T we have r −→∗ r.−→∗ is transitive, i.e. r −→∗ s −→∗ t implies r −→∗ t.If there is any other relation −→′, which is reflexive,transitive and contains −→, then r −→∗ s impliesr −→′ s.

The next slides contain a proof that −→∗ is indeed thereflexive transitive closure of −→. We jump over it.Jump over proof.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (b) 2-59

ProofWe show that −→∗, as defined originally, is in fact thereflexive and transitive closure of −→:

If r −→ s, then clearly r −→∗ s (take n = 1).−→∗ is reflexive: We have r −→ r for r ∈ T, byhaving n = 0 in the definition of −→∗.−→∗ is transitive: Assume r −→∗ s −→∗ t. Thenthere exist n,m, ri, si s.t.

r ≡ r0 −→ r1 −→ · · · −→ rn ≡ s

s ≡ s0 −→ s1 −→ · · · −→ sm ≡ t

But this implies r −→∗ t.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (b) 2-60

Proof (Cont.)Furthermore, if −→′ is another relation, which contains−→, and which is reflexive and transitive, then itcontains −→∗:

Assume −→′ having these properties.Assume r −→∗ s.Then there exist n ∈ N, ri s.t.

r ≡ r0 −→ r1 −→ · · · −→ rn ≡ s

In case n = 0 we have r ≡ s, therefore r −→′ s.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (b) 2-61

Proof (Cont.)r ≡ r0 −→ r1 −→ · · · −→ rn ≡ s

In case n > 0 we have by the fact that t −→ t′ impliest −→′ t′ that

r ≡ r0 −→′ r1 −→

′ · · · −→′ rn ≡ s

But since −→′ is transitive, it follows

r −→′ s

and we are done.

Note how easy it is to overlook that the case n = 0 hasto be treated separately.

If one does this more formally, or using an interactivetheorem proving, one would notice this missing case.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (b) 2-62

←→∗

If (T,−→) is a reduction system, we define

s←→∗:::::

t iff there exists a (possibly empty) sequence

s ≡ s0 ←→ s1 ←→ s2 ←→ · · · ←→ sn ≡ t

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (b) 2-63

←→∗

r ←→∗ t iffs ≡ s0 ←→ s1 ←→ s2 ←→ · · · ←→ sn ≡ t

Note that if we want to identify two elements r, s, ifr −→ s, we have to identify r, s if r ←→∗ s.

If we want to identify elements r, s s.t. r −→ s, wehave to identify as well elements r, s s.t. r ←− s.Then we have to identify elements r, s s.t. r ←→ s.Therefore we have, if n, si are as in the definition ofs←→∗ t, to identify s0 and s1; s1 and s2; etc.; sn−1

and sn.Therefore we have to identify s0 and sn.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (b) 2-64

ExampleAssume the following reduction system:

0 1 2

3 4

Then 0←− 3 −→ 1←− 4 −→ 2, therefore 0←→∗ 2.

Jump over next slide.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (b) 2-65

Illustration of ←→∗

r

s

In the reduction system above we have r ←→∗ s.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (b) 2-66

←→∗

In order to express the above shorter, one says that←→∗ is the reflexive, symmetric and transitive closure of−→, i.e. the least reflexive, symmetric and transitiverelation containing −→.

This means the following:s −→ t implies s←→∗ t.←→∗ is reflexive, and transitive.←→∗ is symmetric, i.e. s←→∗ t implies t←→∗ s.If there is any relation←→′ with the aboveproperties, then s←→∗ t implies s←→′ t.

The next slides contain a proof that←→∗ is indeed thereflexive, symmetric and transitive closure of←→. Wejump over it. Jump over proof.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (b) 2-67

ProofWe show that←→∗, as defined originally, is in fact thereflexive, symmetric, and transitive closure of −→:

That it contains −→ follows since r −→ s impliesr ←→ s by an argument similar to that for −→∗.That it is reflexive and transitive follows as for −→∗.←→∗ is symmetric: Assume r ←→∗ s. Then thereexist n, ri, s.t.

r ≡ r0 ←→ r1 ←→ · · · ←→ rn ≡ s

Now ri ←→ ri+1 implies ri+1 ←→ ri, therefore

s ≡ rn ←→ rn−1 ←→ · · · ←→ r0 ≡ r

which implies s←→∗ r.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (b) 2-68

Proof (Cont.)Furthermore, if←→′ is another relation which contains←→ and which is reflexive, symmetric and transitivethen it contains←→∗:

Assume←→′ having these properties.Assume r ←→∗ s.Then there exist n ∈ N, ri s.t.

r ≡ r0 ←→ r1 ←→ · · · ←→ rn ≡ s

In case n = 0 we have r ≡ s, therefore r ←→′ s.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (b) 2-69

Proof (Cont.)r ≡ r0 ←→ r1 ←→ · · · ←→ rn ≡ s

In case n > 0 we first note that, since←→′ contains−→ and is symmetric, we have that t −→ t′ impliest←→′ t′ and t′ ←− t implies t←→′ t′.Therefore t←→ t′ implies t←→′ t′, and we obtain bythe above

r ≡ r0 ←→′ r1 ←→

′ · · · ←→′ rn ≡ s

But since←→′ is transitive, it follows

r ←→′ s

and we are done.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (b) 2-70

Identification of Elements

If we have a reduction system (T,−→), one writes

s =→ t

or sometimess = t

for s←→∗ t.

In order to avoid confusion, we write

s ≡ t

for s and t are the same element of T without using anyreductions.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (b) 2-71

Determination of ←→∗

In general it is infeasible to determine whether s←→∗ t

holds.One has to check all possible ways of getting from s

to t, by both using −→ and←−.

In many cases this can be determined by:Reducing s to some term s′ s.t. s −→∗ s′ and s′ hasno further reductions,

i.e. by “evaluating s”.Doing the same with t to some term t′.Checking whether s′ is identical to t′.

This way of determining, whether s −→∗ t holds, iscorrect, if −→ is confluent and strongly normalising (seenext subsection).

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (b) 2-72

(c) Termination, Confluence, Normalisation

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-73

Strong NormalisationA reduction system (T,−→) is

:::::::::::::::

terminating or

:::::::::::

strongly::::::::::::::::

normalising , iff there is no infinite sequence

s0 −→ s1 −→ s2 −→ s3 −→ · · ·

of elements in T.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-74

ExamplesThe following reduction system is terminating:

2 30 1 4 . . .

Any reduction sequence will end in 0 and terminate.

The following reduction system is non terminating:

2 30 1 4 . . .

(Take 0 −→ 1 −→ 2 −→ 3 −→ · · · ).

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-75

ExamplesThe following reduction system is terminating, but thereare arbitrarily long reduction sequences starting with •:

2 30 1 4 . ..

We have • −→ n −→ (n− 1) −→ (n− 2) −→ · · · −→ 0.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-76

ExamplesThe untyped λ-calculus (See next Sect.) is nonterminating, since we have for Ω := (λx.x x) (λx.x x)

Ω −→ Ω −→ Ω −→ · · ·

The typed λ-calculus (see later Subsection) isterminating.

In fact, the typed λ-calculus was introduced in orderto obtain a terminate subtheory of the λ-calculus.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-77

Normal Form and IrreducibilityLet (T,−→) be a reduction system.

s ∈ T is::::::::::::::

irreducible , if there exists no t ∈ T s.t. s −→ t.

t is a:::::::::

normal:::::::

form:::

of::

s iff s −→∗ t and t is irreducible.

(T,−→) is:::::::::

weakly::::::::::::::::

normalising or:::::::::::::::

normalising , ifevery s ∈ T has a normal form.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-78

ExampleThe following system is weakly normalising, but not stronglynormalising:

0 1 2 3 . . .4

Every r has a normal form, namely •.

But there exists an infinite sequence

0 −→ 1 −→ 2 −→ 3 −→ · · ·

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-79

Example 2The following system is both weakly normalising andstrongly normalising:

0 1 2 3 . . .4

If one reduces any element as long as possible, onefinally ends up with •, which doesn’t reduce any further.

So every element has the same normal form namely •.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-80

Example 3In the following system 0 has two normal forms, namely 1and 2:

1

0

2

This system is both strongly and weakly normalising, but is

not confluent. (“Confluent” will be defined later).

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-81

LemmaLet (T,−→) be a strongly normalising reduction system.Then (T,−→) is weakly normalising.

Proof:A normal form of s ∈ T can be obtained by simply reducings as long as possible:Since (T,−→) is strongly normalising, the reductionsequence terminates in some t ∈ T.

t is a normal form of s.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-82

Church-RosserWe say a reduction system (T,−→) is

::::::::::::

confluent or hasthe

:::::::::::::::::::

Church-Rosser::::::::::::

property iff for all r, s, t ∈ T wehave

if r −→∗ s and r −→∗ t,then there exists an t′ s.t. s −→∗ t′ and t −→∗ t′.

**

* *

r

s t

∃t′

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-83

Diamond PropertyBecause of the shape of the picture on the previousslide, the Church-Rosser property is sometimes calledas well the

::::::::::::

Diamond:::::::::::

property or

::::::::::

Triangle::::::::::::

property .

So Church-Rosser means:Every triangle (or better fork) can be closed to adiamond .

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-84

Weakly Church-RosserOne might think that a weaker version ofChurch-Rosser suffices:

If r −→ s and r −→ t then there exists an r′ s.t.s −→∗ r′ and t −→∗ r′.So we demand only that r reduces in one step to s

and in one step to t.

r

s t

∗ ∗∃r′

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-85

Weak Church-RosserBut that condition is weaker than full Church-Rosser.

The following term rewriting system is weaklyChurch-Rosser, but doesn’t fulfil the full Church-RosserProperty:

r −→∗ r′, r −→∗ s′ but there is no t s.t. r′ −→∗ t ands′ −→∗ t.

r s

r′

s′

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-86

TheoremIf (T,−→) is confluent, then we have for r, s ∈ T:r ←→∗ s

iff there exists a t ∈ T s.t.r −→∗ t ∧ s −→∗ t

r s

∃t

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-87

Idea of Proof

∗ ∗

∗ ∗

∗ ∗

∗ ∗

r s

Common Reduct of r and s

Jump over rest of proof

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-88

Proof of the TheoremWe define for r, s ∈ T

r ↓ s :⇔ ∃t ∈ T.(r −→∗ t ∧ s −→∗ t) .

So r ↓ s means that r and s have a common reduct:

∗ ∗

r s

∃t

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-89

Proof of the TheoremSo we have to show

r ←→∗ s⇔ r ↓ s

“⇐” is easy. If r −→∗ t, s −→∗ t, then we get

r −→∗ t ∗←− s

(where t ∗←− s :⇔ s −→∗ t) and therefore r ←→∗ s

∗ ∗

r s

∃t

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-90

Proof of the TheoremFor the more difficult direction “⇒” we give two proofs:

One more concrete and intuitive one.Will be presented during the lecture.

One more abstract one.Will not be presented during the lecture.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-91

First Proof of “ ⇒”Assume r ←→∗ s.

This means that we have a chain

r ≡ r0 ←→ r1 ←→ r2 ←→ · · · ←→ rn ≡ s

We are going to show successively:r0 ↓ r0,r0 ↓ r1,r0 ↓ r2,· · ·

r0 ↓ rn ≡ s (the assertion).

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-92

First Proof of “ ⇒”r ↓ s :⇔ ∃t.(r −→∗ t ∧ s −→∗ t) .

In order to show this we have to show the following:For the first step, we need to show r0 ↓ r0, i.e. ingeneral we need to show

(1) r ↓ r

For the step from r0 ↓ ri to r0 ↓ ri+1 we need to show:If r0 ↓ ri and ri ↔ ri+1 then r0 ↓ ri+1.In general we have to show:If r ↓ s and s←→ t, then r ↓ t,in short:r ↓ s ←→ t implies r ↓ t.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-93

DiagramThat r ↓ s←→ t implies r ↓ t can be visualised asfollows:

∗ ∗

r s t

r′

∃ s’

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-94

First Proof of “ ⇒”• (1) r ↓ r.

• r ↓ s←→ t implies r ↓ t.

Since s←→ t means s −→ t or s←− t, we need toshow

(2) r ↓ s −→ t ⇒ r ↓ t ,

(3) r ↓ s←− t ⇒ r ↓ t ,

So in total we have to show (1), (2), (3) above.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-95

(1) r ↓ r

We show r ↓ r.

∗ ∗

r r

r

Formally: We have r −→∗ r and r −→∗ r, therefore r ↓ r.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-96

(2) r ↓ s→ t implies r ↓ t

Assume r ↓ s, s −→ t. Show r ↓ t.

r −→∗ r′, s −→∗ r′ for some r′.

By Church-Rosser, s −→∗ r′ and s −→∗ t implies thatthere exists a s′ s.t. r′ −→∗ s′, t −→∗ s′.

But thenr −→∗ r′ −→∗ s′ therefore r −→∗ s′,t −→∗ s′,therefore r ↓ t.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-97

(2) r ↓ s→ t implies r ↓ t

∗ ∗

∗ ∗

r s

tr′

s′

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-98

(3) r ↓ s← t implies r ↓ t

Assume r ↓ s, s←− t. Show r ↓ t.

r −→∗ r′, s −→∗ r′ for some r′.

But thenr −→∗ r′

t −→∗ s −→∗ r′,therefore r ↓ t.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-99

r ↓ s← t implies r ↓ t

∗ ∗

r s

t

r′

This completes the first proof of the Theorem.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-100

Second Proof of “ ⇒”We show that ↓ contains −→ and is reflexive, symmetricand transitive.

By definition,←→ is the least reflexive, symmetric andtransitive relation which contains −→.

Therefore←→ is contained in ↓ and we obtain

r ←→∗ s⇒ r ↓ s

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-101

Second Proof of “ ⇒”↓ contains −→:r −→ s implies r ↓ s.

r s

s

Formally: If r −→ s then we have with t := s that r −→∗ t

and s −→∗ t.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-102

Second Proof of “ ⇒”↓ is reflexive. (As in the previous proof).

r r

r

Formally: We have r −→∗ r and r −→∗ r, therefore r ↓ r.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-103

Second Proof of “ ⇒”↓ is symmetric:

* * * *=>

r s

t

s r

t

Formally:Assume r ↓ s.Then r −→∗ t and s −→∗ t for some t.Then s −→∗ t and r −→∗ t.Therefore s ↓ r.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-104

Second Proof of “ ⇒”↓ is transitive:

* *

=>

* * * *

* * * *r

r’

s t

s’

s t

r’ s’

t’

r

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-105

Second Proof of “ ⇒”(↓ is transitive:)Formally:

Assume r ↓ s and s ↓ t.Then there exists r′, s′ s.t. r −→∗ r′, s −→∗ r′,s −→∗ s′, t −→∗ s′.Then by confluence there exists an t′ s.t. r′ −→∗ t′,s′ −→∗ t′.Then r −→∗ t′ and t −→∗ t′.Therefore r ↓ t.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-106

Unique Normal FormsLemma :Let (T,−→) be a confluent reduction system.If r ∈ T has a normal form s, then it is unique:

If t is another normal form, then s ≡ t.

Proof:

We have r −→∗ s and r −→∗ t.

By confluence, there exists a r′ s.t. s −→∗ r′ andt −→∗ r′.

But since s and t are normal forms, it follows s ≡ r′ andt ≡ r′.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-107

Picture

* *

* *

r

s t

∃ r’

Since s, t are in normal form, s ≡ r′ ≡ t

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-108

LemmaLet (T,−→) be a weakly normalising and confluentreduction system. Then

r ←→∗ s iff the normal forms of r and s coincide.

Proof:

“⇒”: By Church Rosser r ←→∗ s implies the existenceof a t s.t. r −→∗ t and s −→∗ t.Reduce t further to a normal form r′.Then r′ is a normal form of both r and s as well.Since by the above lemma, normal forms are unique, r′

is the normal form of r and s.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-109

Picture (Proof of “ ⇒”)

rs

∃t (by confluence)∗ ∗

r′ := NF(t) (by weak normalisation)

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-110

Lemma“⇐”: If the normal forms t coincide, then we haver −→∗ t←−∗ s, therefore r ←→∗ s.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-111

Remark on AgdaThe underlying reduction system of Agda is stronglynormalising and confluent, provided the code has beentermination checked.

The equality derived from this reduction system is usedin order to typecheck terms.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (c) 2-112

(d) Term Rewriting Systems

Term rewriting systems are special cases of reductionsystems.

They are reduction systems, which are generated by a(in many cases finite) set of rules (i.e. basic reductions).

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (d) 2-113

Example of a Term Rewriting SystemTake T = set of arithmetic expressions formed fromvariables, 0 by using the successor operation S (whereS n stands n + 1), +, ∗ and brackets.

So the following are elements of T :x + S 0,S 0 + z ∗ (S (S x) + 0),S y ∗ S 0 + S x ∗ 0.

Take as rules the following:

x + 0 −→Rule x ,

x + S y −→Rule S (x + y) ,

x ∗ 0 −→Rule 0 ,

x ∗ S y −→Rule x ∗ y + x .

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (d) 2-114

Example Reductions

(0 + (0+0))+0 ((0+0)+0)+0 (0+0)+(0+0)

((0+0) + (0+0)) + 0

0+(0+0)(0+0)+0

0+0

3 reductions

0

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (d) 2-115

Example of a Term Rewriting System(The system will be in fact strongly normalising andconfluent).

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (d) 2-116

Term Rewriting Systems

x + 0 −→Rule x ,

x + S y −→Rule S (x + y) ,

x ∗ 0 −→Rule 0 ,

x ∗ S y −→Rule x ∗ y + x .

The reduction relation generated by these rules allowsto replace in a term

any subterm of the form s + 0 by s,any subterm of the form s + S t by S (s + t),any subterm of the form s ∗ 0 by 0,any subterm of the form s ∗ S t by s ∗ t + s.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (d) 2-117

Term Rewriting Systems

x + 0 −→Rule x ,

x + S y −→Rule S (x + y) ,

x ∗ 0 −→Rule 0 ,

x ∗ S y −→Rule x ∗ y + x .

So we have for instance the following reductions:0 + S (S 0) −→ S (0 + S 0),

Reduce 0 + S s to S (0 + s) using s ≡ S 0.S (0 + S 0 ) −→ S (S (0 + 0 )),

Reduce s + S t to S (s + t), using s ≡ t ≡ 0,S (S (0 + 0 )) −→ S (S 0 ) .

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (d) 2-118

Definition of Term Rewriting SystemsA term rewriting system consists of

a set of terms T built from variables, constants andsome function symbols,a relation −→Rule between terms

(if r −→Rule s we say hat r −→Rule s is a:::::

rule ),s.t., if s −→Rule t, then

s is not a variable, andall variables in t occur in s.

The variable conditions is needed so that the theory ofterm rewriting systems goes through smoothly.

This is not important for this lecture, and thereforethe explanation will be omitted.Jump over explanation.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (d) 2-119

Condition on VariablesIn the previous definition we demanded two variableconditions for s −→Rule t:

s is not a variable.If we allowed s to be a variable say x, then the rulewould have the form x −→ t.That would mean that any term r has a reduction,namely to t[x := r].

All variables in t occur in s.Assume y were a variable in t but not in s.If we substitute in s and t all variables from s byclosed terms, and obtain s′ and t′ then we wouldhave that s′ would have potentially infinitely manyreductions, namely for any substitution of the othervariables of t′ by closed terms.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (d) 2-120

Condition on VariablesThe second variable condition has something to do withdeterminism:

Assume we have chosen a rule r −→ s and chosen asubstitution of variables in r, which matches a term t.Then the reduct with respect to this rule is uniquelydetermined.

There are no other free variables in s which allowadditional choices for substitutions.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (d) 2-121

Condition on VariablesBoth these case would cause problems in the theory ofterm-rewriting systems (we won’t touch thoseproblems).

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (d) 2-122

Reduction generated by −→Rule

If we have a term rewriting system (T,−→Rule) weobtain a reduction relation −→ on T as follows:

First we construct a relation −→′ obtained fromreductions rules r −→Rule r′ by substituting thevariables in both r and r′ by some terms.

So the same substitutions are carried out in both r

and r′.If s −→′ s′ is obtained by carrying out such asubstitution in r −→Rule r′, then s −→′ s′ is called:::

an::::::::::::

instance:::

of::::::

rule::::::::::::::

r −→Rule r′.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (d) 2-123

Example (Instance of a Rule)

x + 0 −→Rule x ,

x + S y −→Rule S (x + y) ,

x ∗ 0 −→Rule 0 ,

x ∗ S y −→Rule x ∗ y + x .

0 + 0 −→′ 0 is an instance, obtained by substituting inx + 0 −→Rule x the variable x by 0.

S 0 ∗ S 0 −→′ S 0 ∗ 0 + S 0is an instance, obtained by substituting inx ∗ S y −→Rule x ∗ y + x the variable x by S 0 and thevariable y by 0.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (d) 2-124

Reduction generated by −→Rule

Then s −→ s′, if there exists an instance t −→′ t′ of arule s.t. s contains subterm t, and s′ is the result ofsubstituting in s the term t by t′.

The subterm s is called a:::::::

redex w.r.t. the termrewriting system used.· “Redex” is short for

::::::::::::

reducible:::::::::::::::

expression .· Plural of redex is

::::::::::

redexes .The reductions s −→ s′ obtained this way are thereductions

:::::::::::::

generated by the term rewriting system.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (d) 2-125

Example 1

x + 0 −→Rule x ,

x + S y −→Rule S (x + y) ,

x ∗ 0 −→Rule 0 ,

x ∗ S y −→Rule x ∗ y + x .

0 + S (S 0) −→ S (0 + S 0) is obtained as follows:The rule used is

x + S y −→Rule S (x + y) .

By substituting x by 0 and y by S 0 we obtain theinstance

0 + S (S 0) −→′ S (0 + S 0) .

In this example, the redex is the full term 0 + S (S 0)which is then reduced.

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (d) 2-126

Example 2

x + 0 −→Rule x ,

x + S y −→Rule S (x + y) ,

x ∗ 0 −→Rule 0 ,

x ∗ S y −→Rule x ∗ y + x .

S (0 + S 0 ) −→ S (S (0 + 0 )) is obtained as follows:The rule used is x + S y −→Rule S (x + y) .

By substituting x and y by 0 we obtain the instance

0 + S 0 −→′ S (0 + 0 ) .

The left hand side of our reduction S (0 + S 0 )contains now the redex 0 + S 0 .

By substituting it by S (0 + 0 ) we obtain the righthand side of the reduction, S (S (0 + 0 )) .

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (d) 2-127

Example 3

x + 0 −→Rule x ,

x + S y −→Rule S (x + y) ,

x ∗ 0 −→Rule 0 ,

x ∗ S y −→Rule x ∗ y + x .

S (S (0 + 0 )) −→ S (S 0 ).The rule used is x + 0 −→Rule x .

By substituting 0 for x, we obtain the instance

0 + 0 −→′0 .

The left hand side of the reduction S (S (0 + 0 ))contains the redex 0 + 0 .By substituting it by 0 we obtain the right hand sideof the reduction S (S 0 ).

CS 336/CS M36 (part 2)/CS M46 Interactive Theorem Proving, Lent Term 2008, Sec. 2 (d) 2-128