Transcript ppt
CS252: Systems Programming
Ninghui Li
Topic 4: Programming in a FIZ and FLIZ: Simple
Functional Programming Language
What is FIZ
FIZ
F is for functional programming
I is for integer
(we only use integer data type)
Z is for zero, denoting the simplicity of the language
In Functional Programming, one
defines functions
writes expressions
Syntax: instead of writing f(a, b, c); we write (f a b c)
slide 2
Basic Features of FIZ
non-negative integer
evaluates to its value
(inc exp) evaluates to value of exp + 1
(dec exp) evaluates to halt if value of exp is 0
otherwise evaluates to value of exp - 1
(ifz cexp texp fexp)
evaluates to value of texp when cexp evaluates to 0
and to value of fexp when cexp evaluates to none 0
slide 3
Examples
(inc 3)
(inc (dec (inc 1)))
(ifz (dec 1) 2 3)
4
2
2
slide 4
Defining New Functions
(define (name arguments) function-body )
Examples:
(define (add2 x) (inc (inc x)))
(define (add x y)
(ifz y x (add (inc x) (dec y))))
(name arguments)
E.g., (add 2 3); (add (inc 2) (dec 3))
slide 5
Dealing with Errors
(halt) stops the program
Whenever the evaluation leads to non-integer or
negative number, use (halt)
Note:
FIZ has no assignment, except in function
invocation.
FIZ has no loop, except in recursion
slide 6
An Example: Add
(define (add x y)
(ifz y x
(add (inc x) (dec y))))
; if y==0, x+y=x
; otherwise x+y = (x+1) + (y-1)
We assume lazy evaluation, i.e., in
(ifz cond t_exp e_exp)
We evaluate t_exp only when cond is 0, and
evaluate e_exp only when cond is not 0
Lazy versus Eager Evaluation
In Eager evaluation
In Lazy evaluation
(add 3 0)
(add 3 0)
becomes
becomes
(ifz 0 3 (add (inc 3) (dec 0)))
(ifz 0 3 (add (inc 3) (dec 0)))
We do not evaluate the last
We need to evaluate the last
argument in the above function argument yet, and figures out
call, i.e.,
that we do not need it
(add (inc 3) (dec 0)),
Which becomes
(add 4 -1)
This then becomes infinite loop
A similar issue in C: In “If (cond_1 && func(a,b))”, if
cond_1 is false, would func(a,b) be called?
More Examples: Multiplication
(define (mul x y)
(ifz y 0
; when y=0, answer is 0
(add x (mul x (dec y))))
; otherwise, answer is x + x*(y-1)
Key is to find a base case, and then figure out how to
reach the base case.
Multiplication base case: When y=0, x*y=0
Recursive step: x*y = x + x*(y-1)
More Examples: Substraction
; Evalutes x – y
What is the base case?
• When y is 0, result is x.
What is the recursive step?
• Computer (dec x) – (dec y)
(define (sub x y)
(ifz y x
(ifz x (halt)
; if y==0, x-y = x
; else result is negative
(sub (dec x) (dec y)))))
; else x-y = (x-1) - (y-1)
More Examples: Less Than
; (define (lt x y)
; Evaluates to 0 if x < y, and 1 otherwise
What are the base cases?
• One of x and y is 0.
What is the recursive step?
• Compare x-1 with y-1
(define (lt x y)
(ifz y 1
; if y==0, x<y cannot be true
(ifz x 0 ; else if x==0, x<y must be true
(lt (dec x) (dec y)))))
; else x<y if and only if x-1 < y-1
Last FIZ Example: Remainder
; Evaluates x mod y (i.e., x % y)
What are the base cases?
• When x < y, x mod y = x
What is the recursive step?
• Otherwise, x mod y = (x-y) mod y
(define (rem x y)
(ifz y (halt)
(ifz (lt x y) x
; Division by 0
; if x<y, x mod y = x
(rem (sub x y) y))))
; else x mod y = (x-y) mod y
Concept of Public Key
Encryption
In Traditional Cryptography, encryption key = decryption key,
and must be kept secret, and key distribution/agreement is a
difficult problem to solve
In public key encryption, each party has a pair (K, K-1) of keys:
K is the public key, and used for encryption
K-1 is the private key, and used for decryption
Satisfies DK-1[EK[M]] = M
Knowing K, it is computationally expensive to find K-1
The public key K may be made publicly available, e.g., in a
publicly available directory
Many can encrypt, only one can decrypt
Public-key systems aka asymmetric crypto systems
RSA Algorithm
Invented in 1978 by Ron Rivest, Adi Shamir and
Leonard Adleman
Published as R L Rivest, A Shamir, L Adleman, "On
Digital Signatures and Public Key Cryptosystems",
Communications of the ACM, vol 21 no 2, pp120126, Feb 1978
Security relies on the difficulty of factoring large
composite numbers
Essentially the same algorithm was discovered in
1973 by Clifford Cocks, who works for the
British intelligence
RSA Public Key Crypto System
Key generation:
1. Select 2 large prime numbers of about the same
size, p and q
Typically each p, q has between 512 and 2048 bits
2. Compute n = pq, and (n) = (q-1)(p-1)
3. Select e, 1<e< (n), s.t. gcd(e, (n)) = 1
Typically e=3 or e=65537
4. Compute d, 1< d< (n) s.t. ed 1 mod (n)
Knowing (n), d easy to compute.
Public key: (e, n)
Private key: d
RSA Description (cont.)
Encryption
Given a message M, 0 < M < n M Zn {0}
use public key (e, n)
compute C = Me mod n
C Zn {0}
Decryption
Given a ciphertext C, use private key (d)
Compute Cd mod n = (Me mod n)d mod n =
Med mod n = M
RSA Example
p = 41, q = 23, n = 943, (n) = 40*22= 880
Choosing e = 3
Then d = 587 because 587 * 3 % 880 = 1761 % 880=1
Let M = 15. Then C Me mod n
C 153 (mod 943) = 546
M Cd mod n
M 546587 (mod 943) = 15
Topic 6: Public Key Encrypption
and Digital Signatures
17
How to do modular
exponentiation in FIZ.
; How to compute xe mod y?
What is the base case?
• When e = 0, xe mod y = 1
What is the recursive step?
• Otherwise, xe mod y = (x * (xe-1 mod y)) mod y
(define (msexp x e y)
(ifz e 1
(rem (mul (msexp x (dec e) y) x) y)))
Topic 6: Public Key Encrypption
and Digital Signatures
18
A Faster Way to Do modular
exponentiation.
; How to compute x^e mod y?
How to improve the recursive step?
• When e is even, xe mod y = ((xe/2 mod y)2 mod y)
• When e is odd, xe mod y = (x*(xe-1 mod y) mod y)
(define (mexp x e y)
(ifz e 1 (ifz (rem e 2)
(rem (square (mexp x (div e 2) y)) y)
(rem (mul x (mexp x (dec e) y)) y))))
If e==0, then result is 1
Else if e is even, result is (x^(e/2) mod y) ^ 2 mod y
Else if e is odd, result is (x * (x^(e-1) mod y)) mod y
Topic 6: Public Key Encrypption
and Digital Signatures
19
The FLIZ Language
There are the following constant expressions
• 123
; a number
• []
; the empty list
• [c c … c]
; a list of one of more constant expressions
Examples:
123
; 123
[1 2 3]
; [ 1 2 3 ]
[]
; [ ]
[1 [2 3] [4 5]] ; [ 1 [ 2 3 ] [ 4 5] ]
[1 [] 2 [[3 4]]]; [ 1 [ ] 2 [ [ 3 4 ] ] ]
slide 20
The FLIZ Language: Builtin
Functions
(list exph expt) a list obtained by inserting the value of
exph as first element to the value of expt
(head exp)
first element of the list exp
(tail exp) a list obatined by removing the first element
(ifa cexp texp fexp) when cexp evaluates to a number,
evaluates to texp, otherwise evaluates to fexp
(ifn cexp texp fexp)
evaluates to value of texp when cexp evaluates to []
and to value of fexp when cexp evaluates not to []
slide 21
Programming in FLIZ
; append two lists (append x y)
• What is the base case?
• When x is the empty list, result is y
• What is the recursive step?
• Otherwise, result is a list with head being the head of x, and tail
being that from appending tail of x and y
(define (append x y)
(ifn x y
(list (head x) (append (tail x) y))))
Programming in FLIZ
; flatten a list (flatten x)
• What is the base case?
• When x is the empty list, result is []; or when x is an atomic value
(i.e., a number), result is [x]
• What is the recursive step?
• Flatten head and tail respectively, and then append them.
((define (flatten x)
(ifn x x (ifa x (list x [])
(append (flatten (head x))
(flatten (tail x))))))
Another FLIZ Example Program
; flatten a list
(define (flatten x)
(ifn x x
(ifa x (list x [])
(append (flatten (head x))
(flatten (tail x))))))
FIZ and FLIZ are based on
LISP/Scheme
LISP/Scheme is a functional programming
language
Uses the list data structure extensively
Program/Data equivalence
Heavy use of recursion
Garbage-collected, heap-allocated
Usually interpreted, but good compilers exist
25
History of Function
Programming
Lisp was created in 1958 by John McCarthy
at MIT
Stands for LISt Processing
Scheme developed in 1975
A dialect of Lisp
Racket
ML
OCaml
Haskell
26
Application Areas
Artificial Intelligence
expert systems
planning
Simulation, modeling
Rapid prototyping
27
Functional Languages
Imperative Languages
Ex. Fortran, Algol, Pascal, Ada
based on von Neumann computer model
Functional Languages
Ex. Scheme, Lisp, ML, Haskell
Based on mathematical model of computation
and lambda calculus
28
Review
Be able to write simple programs in FIZ and
FLIP.
Know the concept of lazy evaluation versus
eager evaluation.
How RSA work is not required.
29
Upcoming Attraction
How to write an interpreter/compiler
frontend?
Topic 5: Regular expressions & Lexical
Analyzer
Topic 6: Context-free grammar & Parser
30