# lectureNote2

lectureNote2

lectureNote2

### Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

I217: Functional Programming<br />

2. Basics of functional programming<br />

in CafeOBJ (2)<br />

Kazuhiro Ogata<br />

Roadmap<br />

• Some more basics of CafeOBJ<br />

– Mixfix operators, Od Order sorts, Modules<br />

– Substitutions, Pattern matching, Rewriting<br />

– Evaluation of terms in CafeOBJ<br />

– Exhaustive & non‐overlapping definition of functions<br />

• Lists of natural numbers<br />

• Examples that use lists of natural numbers<br />

– Quicksort<br />

– Sieve of Eratosthenes<br />

2

Mixfix Operators<br />

• Infix operators can be declared:<br />

op _ @ _ : Nat Nat ‐> Nat .<br />

• Prefix operators can be declared:<br />

op ~ _ : Nat ‐> Nat .<br />

• Postfix operators can be declared:<br />

op _ ! : Nat ‐> Nat .<br />

• Mixfix operators can be declared:<br />

op if _ { _ } else { _ } : Bool Nat Nat -> Nat .<br />

An underscore _ indicates a place where an argument<br />

is put, and the number of underscores must be the<br />

same as the number of arguments.<br />

3<br />

Order Sorts (1/2)<br />

• Sorts can be ordered.<br />

– The sort of 0 is Zero.<br />

– The sorts of 1, 2, … are NzNat.<br />

– Nat denotes the set {0, 1, 2, …}.<br />

– Zero is a subsort of Nat, denoting the set {0}.<br />

– NzNat is also a subsort of Nat, denoting the set<br />

{1, 2, …}.<br />

Nat<br />

• Ordering is declared as follows:<br />

[Zero NzNat < Nat]<br />

Zero<br />

NzNat<br />

4

Order Sorts (2/2)<br />

o Operator (function) quo is declared<br />

as follows:<br />

?Nat<br />

op _ quo _ : Nat NzNat -> Nat<br />

o 3 quo 2 is a proper term of Nat.<br />

Nat<br />

parse 3 quo 2 .<br />

o But, 3 quo 0 is not.<br />

Zero<br />

parse 3 quo 0 .<br />

o 3 quo 0 is a term of sort ?Nat.<br />

– ?Nat is the error sort of Nat; it is added by CafeOBJ.<br />

– Terms of ?Nat may represent errors or exceptions.<br />

red 3 quo 0 .<br />

parse 3 quo (p 3) .<br />

red 3 quo (p 3) .<br />

NzNat<br />

5<br />

Modules (1/6)<br />

• Functions defined in open‐close sections cannot<br />

be (re)used effectively.<br />

• Basic units of CafeOBJ programs (specifications)<br />

are modules.<br />

CafeOBJ has several built‐in modules such as<br />

NAT and BOOL.<br />

Users are also allowed to declare modules.<br />

• Modules can be (re)used effectively.<br />

• Functions should be defined in modules so as to<br />

(re)use the functions effectively.<br />

6

Modules (2/6)<br />

• A module named M is declared as follows:<br />

mod! M { … }<br />

Sorts, operators (functions), variables and equations<br />

are declared in the place … enclosed with { and }.<br />

• Modules can be reused by importing them.<br />

mod! M { pr(NAT) … }<br />

Module M imports NAT, which can be (re)used in<br />

M.<br />

7<br />

Modules (3/6)<br />

• A module named GCD is declared, in which function<br />

gcd is defined.<br />

mod! GCD {<br />

pr(NAT)<br />

op gcd : Nat Nat -> Nat<br />

vars X Y : Nat<br />

eq gcd(X,0) = X .<br />

ceq gcd(X,Y) = gcd(Y,X rem Y) if Y > 0 .<br />

}<br />

• In modules, operator (function) and variable<br />

declarations do not have to end with periods.<br />

Equation declarations must end with periods.<br />

8

Modules (4/6)<br />

• A module named LCM is declared, in which<br />

functions lcm is defined.<br />

mod! LCM {<br />

pr(GCD)<br />

op lcm : Nat Nat -> Nat<br />

vars X Y : Nat<br />

ceq lcm(X,Y) = 0 if X == 0 or Y == 0 .<br />

ceq lcm(X,Y) = (X quo gcd(X,Y)) * Y<br />

if X > 0 and Y > 0 .<br />

}<br />

9<br />

Modules (5/6)<br />

• Command show: It takes a module and shows<br />

its information such as sorts, operator<br />

(function) declarations and equations.<br />

CafeOBJ> show GCD<br />

10

• Built‐in modules:<br />

Modules (6/6)<br />

– BOOL: Boolean values true & false<br />

– NAT: natural numbers 0, 1, 2, …<br />

– STRING: strings "CafeOBJ", "module", …<br />

– QID: quoted identifiers 'ID, 'P, 'Q, …<br />

11<br />

Substitutions<br />

• A substitution σ is a function that takes a variable<br />

and returns a term, where the sort of the term<br />

should be the same as that of the variable.<br />

Note that an application of σ to a variable X is<br />

expressed as σX.<br />

• Substitutions can be naturally extended to terms.<br />

σf(t 1 , …,t n ) = f(σt 1 , …, σt n )<br />

o Let σ be a substitution such that σX = 34and σY = 128, and<br />

then<br />

σgcd(X,Y) = gcd(σX,σY)<br />

= gcd(34,128)<br />

12

Pattern Matching & Rewriting (1/2)<br />

• Given a term p (called a pattern) and a ground term t,<br />

p is matched with t (t is an instance of p) if there exists<br />

σ such that σp = t.<br />

• A rewrite rule is a pair (denoted as l → r) of<br />

terms l,r such that l is not a single variable, each<br />

variable in r also occurs in l, and the sort of l is the<br />

same as that of r.<br />

A rewrite rule may have a condition c whose sort is<br />

Bool such that each variable in c also occurs in l. Such<br />

a rule is called a conditional rewrite rule and denoted<br />

as l → r if c.<br />

o gcd(X,Y) is matched with gcd(34,128) because there exists σ<br />

such that σX = 34and σY = 128, and σgcd(X,Y) = gcd(34,128).<br />

13<br />

Pattern Matching & Rewriting (2/2)<br />

• Given a ground term t and a set R of rewrite rules,<br />

– if there exists l → r in R, a sub‐term s in t and σ such<br />

that σl = s, t[σl] can be rewritten to t[σr];<br />

this one step rewriting is expressed as t[σl] → R t[σr]<br />

(R may be omitted if it is clear from the context);<br />

t 0 reduces to t n if and only if (iff) t 0 → R t 1 → R … → R t n .<br />

– if there exists l → r if c in R, a sub‐term s in t and σ<br />

such that σl = s and σc reduces to true, t[σl] can be<br />

rewritten to t[σr].<br />

Evaluation of terms in CafeOBJ is based on rewriting by<br />

regarding equations as rewrite rules.<br />

14

Evaluation of Terms in CafeOBJ (1/2)<br />

o gcd(4,6) is evaluated as follows:<br />

gcd(4,6) → gcd(6,4 rem 6) by 2 (b/c 6 > 0 → true by >)<br />

→ gcd(6,4) by rem<br />

→ gcd(4,6 rem 4) by 2 (b/c 4 > 0 → true by >)<br />

→ gcd(4,2) by rem<br />

→ gcd(2,4 rem 2) by 2 (b/c 2 > 0 → true by >)<br />

→ gcd(2,0) by 1<br />

→ 2<br />

eq gcd(X,0) = X . … 1<br />

ceq gcd(X,Y) = gcd(Y,X rem Y) if Y > 0 . … 2<br />

Note that b/c is an abbreviation of because.<br />

15<br />

Evaluation of Terms in CafeOBJ (2/2)<br />

• CafeOBJ can show information (traces) on<br />

evaluation of terms.<br />

set trace whole on<br />

open GCD<br />

red gcd(4,6) .<br />

close<br />

set trace whole off<br />

16

Exhaustive & Non‐overlapping<br />

Definition of Functions (1/3)<br />

• The equations defining a function is exhaustive if they<br />

take into account every possible combination of<br />

arguments.<br />

Let us consider some definitions of "op fib : Nat -> Nat".<br />

• Examples of exhaustive definitions<br />

eq fib(X) = if X == 0 then 0<br />

else if X == 1 then 1 else fib(p X) + fib(p p X) fi fi .<br />

eq fib(0) = 0 .<br />

eq fib(1) = 1 .<br />

ceq fib(X) = fib(p X) + fib(p p X) if X > 1 .<br />

• Examples of non‐exhaustive definitions<br />

eq fib(1) = 1 .<br />

ceq fib(X) = fib(p X) + fib(p p X) if X > 1 .<br />

17<br />

Exhaustive & Non‐overlapping<br />

Definition of Functions (2/3)<br />

• The equations defining a function is non‐<br />

overlapping if one combination of arguments is<br />

treated by at most one of the equations<br />

Let us consider some definitions of "op fib : Nat -> Nat".<br />

• Examples of non‐overlapping definitions<br />

eq fib(X) = if X == 0 then 0<br />

else if X == 1 then 1 else fib(p X) + fib(p p X) fi fi .<br />

eq fib(0) = 0 . eq fib(1) = 1 .<br />

ceq fib(X) = fib(p X) + fib(p p X) if X > 1 .<br />

• Examples of overlapping definitions<br />

eq fib(0) = 0 . eq fib(1) = 1 .<br />

eq fib(X) = fib(p X) + fib(p p X) .<br />

18

Exhaustive & Non‐overlapping<br />

Definition of Functions (3/3)<br />

• The equations defining a function should be<br />

exhaustive.<br />

Otherwise, some combinations of arguments are<br />

not treated.<br />

• The equations defining a function should be nonoverlapping.<br />

Otherwise, functions may not be well defined,<br />

namely that there may be multiple different<br />

values for one combination of arguments.<br />

There are some cases in which we want to use overlapping definition;<br />

we must make sure that functions are well defined in the case.<br />

19<br />

Lists of Natural Numbers (1/4)<br />

• Lists of natural numbers are inductively<br />

defined as follows:<br />

– nil is a list of natural numbers.<br />

– If n is a natural number and l is a list of natural<br />

numbers, then n | l is a list of natural numbers.<br />

o Examples<br />

– nil is the empty list of natural numbers.<br />

– 0 | (1 | (2 | nil)) is the list of natural numbers<br />

that consists of 0, 1 and 2 in this order.<br />

20

Lists of Natural Numbers (2/4)<br />

• The module in which lists of natural numbers are<br />

defined is as follows:<br />

mod! NATLIST {<br />

pr(NAT)<br />

[NatList]<br />

op nil : -> NatList {constr}<br />

op _|_ : Nat NatList -> NatList {constr}<br />

}<br />

where constr is an operator attribute, saying that<br />

the operator concerned is used to represent data<br />

structure, namely that it is a data constructor.<br />

21<br />

Lists of Natural Numbers (3/4)<br />

• The function that makes the list<br />

m | m+1 | … | n | nil<br />

for two natural numbers m,n:<br />

op mkl : Nat Nat -> NatList<br />

eq mkl(X,Y)<br />

= if Y < X then nil else X | mkl(X + 1,Y) fi .<br />

If n < m, then it returns the empty list.<br />

• The function that concatenates two lists:<br />

op _@_ : NatList NatList -> NatList<br />

eq nil @ L2 = L2 .<br />

eq (X | L1) @ L2 = X | (L1 @ L2) .<br />

22

Lists of Natural Numbers (4/4)<br />

• (0 | 1 | nil) @ (2 | 3 | nil) is evaluated as<br />

follows:<br />

(0 | 1 | nil) @ (2 | 3 | nil)<br />

→ 0 | ((1 | nil) @ (2 | 3 | nil)) by 2<br />

→ 0 | 1 | (nil @ (2 | 3 | nil)) by 2<br />

→ 0 | 1 | 2 | 3 | nil by 1<br />

eq nil @ L2 = L2 . ... 1<br />

eq (X | L1) @ L2 = X | (L1 @ L2) . ... 2<br />

23<br />

Quicksort (1/3)<br />

• Quicksort is a sorting algorithm that puts<br />

l t f li t i ti d<br />

elements of a list in a certain order.<br />

a 1<br />

a 1 c 1<br />

b 1<br />

24<br />

b i < a 1<br />

not(c j < a 1 )<br />

d 1 b 1 e 1 a 1 f 1 c 1 g 1<br />

d i < b 1 not(e j < b 1 ) f i < c 1 not(g j < c 1 )

Quicksort (2/3)<br />

• Quicksort is implemented as follows:<br />

op qs : NatList -> NatList<br />

op sqs : Nat NatList NatList NatList -> NatList<br />

eq qs(nil) = nil .<br />

eq qs(X | L) = sqs(X,L,nil,nil) .<br />

eq sqs(X,nil,L1,L2) = qs(L1) @ (X | qs(L2)) .<br />

eq sqs(X,Y | L,L1,L2)<br />

= if Y < X then sqs(X,L,Y | L1,L2)<br />

else sqs(X,L,L1,Y | L2) fi .<br />

25<br />

Quicksort (3/3)<br />

• qs(3 | 5 | 0 | 2 | 1 | 4 | nil) is evaluated as follows:<br />

qs(3 | 5 | 0 | 2 | 1 | 4 | nil)<br />

→ sqs(3, 5 | 0 | 2 | 1 | 4 | nil, nil, nil)<br />

→ * sqs(3, nil, 1 | 2 | 0 | nil, 4 | 5 | nil)<br />

→ qs(1 | 2 | 0 | nil) @ (3 | qs(4 | 5 | nil))<br />

→ * sqs(1, 2 | 0 | nil, nil nil) @ (3 | sqs(4, 5 | nil, nil, nil))<br />

→ * sqs(1, nil, 0 | nil, 2 | nil) @ (3 | sqs(4, nil, nil, 5 | nil))<br />

→ * qs (0 | nil) @ (1 | qs (2 | nil) @ (3 | (qs (nil) @ (4 | qs (5 | nil)))<br />

→ * sqs(0, nil, nil, nil) @ (1 | sqs(2, nil, nil, nil))<br />

@ (3 | (nil @ (4 | sqs(5, nil, nil, nil))))<br />

→ * qs (nil) @ (0 | qs (nil)) @ (1 | (qs (nil) @ (2 | qs (nil))))<br />

@ (3 | (nil @ (4 | (qs (nil) @ (5 | qs (nil))))))<br />

→ * nil @ (0 | nil) @ (1 | (nil @ 2 | nil))<br />

@ (3 | (nil @ (4 | (nil @ (5 | nil)))))<br />

→ * 0 | 1 | 2 | 3 | 4 | 5 | nill<br />

26

Sieve of Eratosthenes (1/3)<br />

• It is an algorithm generating prime numbers.<br />

2 3 4 5 6 7 8 9 10 11 12 13 14 15<br />

Delete the numbers that can be divided by 2.<br />

2 3 4 5 6 7 8 9 10 11 12 13 14 15<br />

Delete the numbers that can be divided by 3.<br />

2 3 4 5 6 7 8 9 10 11 12 13 14 15<br />

2 3 5 7 11 13<br />

27<br />

Sieve of Eratosthenes (2/3)<br />

• It is implemented as follows:<br />

op primes : Nat -> NatList<br />

op sieve : NatList -> NatList<br />

op check : Nat NatList -> NatList<br />

eq primes(X) = sieve(mkl(2,X)) .<br />

eq sieve(nil) = nil .<br />

eq sieve(X | L) = X | sieve(check(X,L)) .<br />

eq check(X,nil) = nil .<br />

eq check(X,Y | L)<br />

= if X divides Y then check(X,L)<br />

else Y | check(X,L) fi .<br />

28

Sieve of Eratosthenes (3/3)<br />

• primes(10) is evaluated as follows:<br />

primes(10)<br />

→ * sieve(2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | nil)<br />

→ 2 | check(2, 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | nil)<br />

→ * 2 | sieve(3 | 5 | 7 | 9 | nil)<br />

→ 2 | 3 | check(3, 5 | 7 | 9 | nil)<br />

→ * 2 | 3 | sieve(5 | 7 | nil)<br />

→ 2 | 3 | 5 | check(5, 7 | nil)<br />

→ * 2 | 3 | 5 | sieve(7 | nil)<br />

→ 2 | 3 | 5 | 7 | check(7, nil)<br />

→ 2 | 3 | 5 | 7 | sieve(nil)<br />

→ 2 | 3 | 5 | 7 | nil<br />

29<br />

Exercises (1/2)<br />

1. Declare a module in which a function that<br />

computes the (positive) integral part of the<br />

square root of a natural number is defined.<br />

2. Define four functions that compute the<br />

length of a list, reverse a list, add all numbers<br />

in a list (if the list is empty, then 0 is<br />

returned), and multiply all numbers in a list<br />

(if the list is empty, then 1 is returned),<br />

respectively.<br />

30

Exercises (2/2)<br />

3. Declare a module in which mergesort for lists of<br />

natural numbers (in increasing order) is<br />

implemented.<br />

Mergesort for a list l works as follows:<br />

1 If the length of l is 0 or 1, then l is already sorted.<br />

Otherwise, go next.<br />

2 Partition l into two sub‐lists of half the size.<br />

3 Sort each of the sub‐lists by recursively applying<br />

mergesort.<br />

4 Merge the two sub‐lists into one sorted list.<br />

31