Functional Programming Flashcards

1
Q

Untyped lambda-calculus

A

N ::= x | λx.N | NN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Variable capture

A

When a term with free variable x is substituted inside λx.N, by which the variable x becomes bound.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Simple type syntax

A

τ ::= o | τ → τ

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Outermost reduction

A
  • Given a redex (λx.N)M, reduces the redex before any reductions in N and M
  • Guarantees to reach normal form but duplicates work as much as possible
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Innermost reduction

A
  • Given a redex (λx.N)M, reduces the redex after any reductions in N and M
  • Does not duplicate work but does not necessarily reach normal form
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Zip

A

zip :: [a] -> [b] -> [(a,b)]
zip _ [] = []
zip [] _ = []
zip (x:xs) (y:ys) = (x,y) : zip xs ys

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Weakly normalising term

A
  • A term that has reduction to normal form
  • For example, (λy.x)((λx.xx)(λx.xx))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Strongly normalising term

A
  • A term that is weakly normalising and has no infinite reduction sequence
  • For example, (λy.x)(λx.xx)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Example term that is neither weakly nor strongly normalizing

A

(λx.xx)(λx.xx)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Fixed-point combinator

A
  • A fixed-point combinator is a λ-term F that satisfies M =β F M for any M, where β is beta-equivalence
  • For example: λf.(λx.f(xx))(λx.f(xx))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Fixed-point combinator proof

A

For any lambda-term M, Y M = β M(Y M)

Y M
→ (λx.M(xx))(λx.M(xx))
→ M((λx.M(xx))(λx.M(xx)))
← M(Y M)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Capture-avoiding substitution

A

M[y/x] = M with x renamed to y

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Examples of capture-avoiding substitution

A

1) Example: (λx.xy)[λz.z/y]
This means change y to λz.z
Therefore = λx.x(λz.z)

2) Example: (λx.xy)[λz.zx/y]
Therefore = λw.w(λz.zx)
The x changes to w to avoid capturing the free x in λz.zx

3) Example: (f(λx.yx)yx)[fy/x]
Therefore = f(λx.yx)y(fy)
The bound x in λx.yx is not substituted

4) Example: (λf.f(λx.yx)yx)[fy/x]
Therefore = λg.g(λx.yx)y(fy)
The bound f is renamed to g avoiding capturing the free f

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Church-Rosser Property

A
  • M =β N if and only if there is a term P s.t. M → ∗β P and N → ∗β P
  • This proves that a lambda term can’t have more than one normal form
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Confluence

A

Whenever M →∗ N1 and M →∗ N2, there is some P such that N1 →∗ P and N2 →∗ P

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Local Confluence

A

Whenever M → N1 and M → N2, there is some P such that N1 →∗ P and N2 →∗ P

17
Q

Free and bound variables

A

If the first occurrence of a variable appears when defining a lambda, then it is bound to that lambda.

For example, in λx.x, variable x is bound as it first occurs in λx

Otherwise, it is a free variable. For example, in (λx.x)(λy.yx):
- the variable x in λx.x is bound to
the first lambda
- the variable y in λy.yx is bound to
the second lambda
- the variable x in λy.yx is free

Example 3 - (λx.xy)(λy.y):

  • the variable y in λx.xy is free
  • the variable y in λy.y is bound to the second lambda
18
Q

Lazy evaluation

A
  • A reduction strategy that uses graphs to ensure a term is evaluated at most once, if its result is needed for the overall computation, and otherwise not evaluated at all.
  • When a redex (λx.M)N is reduced, instead of substituting N for every free variable x in M, each variable x functions as a pointer to N.
  • Then N is evaluated at most once, and when its output is needed after that, the stored result is used
  • It always reaches normal form if one exists, and is efficient since it doesn’t evaluate terms more than once
19
Q

Fixed point in the λ-calculus

A

  • A λ-term M such that M = βF M
  • Every term in the lambda calculus has a fixed point
20
Q

Simply typed lambda-calculus

A

M ::= x | λx^τ.M | MM

21
Q

Typing rules

22
Q

Referential transparency

A
  • Means that the output of a function depends only on its inputs and not on other, less visible factors
  • A function that always returns the same values given the same inputs
  • Lazy evaluation needs referential transparency otherwise the evaluation steps do not preserve the meaning of a program
23
Q

Computational effects

A
  • May give different outputs on different occasions, even with the same input
  • They destroy referential transparency
24
Q

Functions that use computational effects

A
  • Example 1: mutable state
  • This interferes with referential transparency because the stored values in the state may have changed
  • Example 2: random values
  • This interferes with referential transparency because the random number generator may produce different numbers each time
  • Example 3: user inputs
  • This interferes with referential transparency because a user may give different input on
    each occasion
25
List comprehension syntax
function xs = [check x | x \<- xs, condition]
26
Recursion syntax
``` function [] = 0 function (x:xs) | condition x = change x + function xs | otherwise = function xs ```
27
Redex
An application in which the left-hand side is an abstraction
28
Beta reduction
- (λx.N)M → N[M/x] - Capture-avoiding substitution rules apply (renames bound variables)
29
Alpha conversion
- Renaming of variables - This fixes beta-reduction, to avoid variable capture
30
Alpha-equivalence
Two terms differ only in the names of their bound variables
31
Beta equivalence
The reflexive-transitive-symmetric closure of →β
32
Lambda terms for booleans
true: λx.λy.x false: λx.λy.y ifthen: λb.λx.λy.bxy and: λb1.λb2.ifthen b1 b2 false or: λb1.λb2.ifthen b1 true b2 add: F = λa.λm.λn.ifthen (iszero n) m (succ (a m (pred n))) add as a fixed point = YF succ: +1 = λn.λf.λx.f(n f x) pred: -1 = λm.λn.λf .λx.m f (n f x)
33
Lambda terms for Church numerals
add: λm.λn.λf .λx.m f (n f x) mul: λm.λn.λf .λx.m (n f) x The numerals themselves: 0: λf.λx.x 1: λf.λx.fx 2: λf.λx.f(fx) 3: λf.λx.f(f(fx))
34
Zero-testing
- Applying λx.false any non-zero number of times to any argument gives false - Applying it zero times to true gives true - Lambda term: λn.n (λx.false) true
35
Properties of the typed lambda-calculus
Uniqueness of types: For any context Γ and term M , there is at most one type τ such that Γ |- M : τ Substitution preserves types: If Γ , x : τ |- M : σ and Γ |- N : τ then Γ |- M[N/x] : σ Reduction preserves types: If Γ , x : τ |- M : σ and Γ |- N : τ then Γ |- M[N/x] : σ
36
Trees
Defined as data Tree a = Leaf | Node a (Tree a) (Tree a) - Tree a on LHS = initial definition - Tree a on RHS = recursive call with child of node - a = type, can represent anything - Leaf = constructor to define an empty tree - Node takes three arguments: the data to store at the node, and the two children of it
37
Encoding rules for addition
- If it's a recursive function, then it is weakly normalising as it can reach a normal form but it is also possible to unfold the fixpoint forever - If it's not recursive then it is strongly normalising and thus also weakly normalising - If add is encoded using the Y combinator, it is neither because the fixed point can never be discarded