Dana Vrajitoru
C311 Programming Languages
First Class Objects
Classes of Objects
- A classification of entities one can find in a program by what can
be done with them.
- First class: can be used without restriction. Example:
integers in C++ or any other language.
- Second class: cannot be assigned to a variable or returned
by functions. Example: Classes in Smalltalk.
- Third class: cannot be passed as function
parameters. Example: data types in C++.
First Class Objects
- Expressible as an anonymous literal value
- Storable in variables
- Storable in data structures
- Having an intrinsic identity (independent of any given name)
- Comparable for equality with other entities
- Passable as a parameter to a function
- Returnable as the result of a function call
- Constructable at runtime
First Class Functions
- A programming language supports first class functions if it allows
functions to be first class objects.
- Supported by all functional languages: Lisp, Scheme, ML, as well
as many of the interpreted languages: Python, Perl, JavaScript.
- Creating a function at runtime means your program must contain a
compiler.
- In C++ only function references (pointers) are first class
objects. Function Pointer
Tutorials.
Higher-Order Functions
- A higher-order function accepts functions as arguments and
is able to return a function as its result.
- A higher-order language supports higher-order functions and allows
functions to be constituents of data structures.
- Functions of order 0: both the domain and the range of the
function are non-function data (numbers, lists, arrays).
- Functions of order k: Both the domain and the range can be
functions of order k-1.
- Higher order: where the order is at least 2.
Lambda Expressions
- Also called anonymous functions or methods.
- They describe functions by directly describing their behavior.
- Inspired from lambda calculus, a branch of mathematics that
studies functions, recursion, computability. Invented by A. Church in
1930.
- Present in many languages: C#, Python, Perl.
- Usually they mean the language allows first class functions.
Lambda Calculus
-
- A mathematical tool to study functions, applications, recursion.
- It also provides a meta-language for formal definition of
functions.
- Some notations:
lambda x. 2*x-1 describes a function f(x) = 2x-1
(lambda x. 2*x-1) 7 means the previous function applied to the
constant 7 and is evaluated to 13.
lambda x. lambda y. x % y describes a function with two variables f(x,
y) = x%y
Arithmetic and Lambda Calculus
- Church numerals: defines the numerals as the n-th
composition of a function with itself.
0: lambda f x. x
1: lambda f x. f x
2: lambda f x. f ( f x)
- Arithmetic operations:
n+m: lambda f x. (n f) (m f x)
n*m: lambda f x. (n (mf x))
SUCC := lambda n f x. f (n f x)
PLUS := lambda m n f x. m f (n f x)
MULT := lambda m n f. m (n f)
Predicates in Lambda Calculus
- TRUE := lambda x y. x
- FALSE := lambda x y. y
- AND := lambda p q. p q FALSE
- OR := lambda p q. p TRUE q
- NOT := lambda p. p FALSE TRUE
- IFTHENELSE := lambda p x y. p x y
Computability
- A function is called computable if there exists an
algorithm to compute it.
- Church-Turing thesis - states that any computable function
can be computed by a Turing machine with unlimited time/storage and
the other way around.
- A function can be defined as computable if and only if there
exists a lambda expression f such that for every pair of x, y in N,
F(x) = y if and only if
f x == y, where x and y are the Church numerals corresponding to x and
y, respectively.
Incompleteness Theorem
- Gödel used the Church numerals to prove his Incompleteness Theorem
(1931).
- The first theorem states that no natural numbers system is capable
of proving all the statements that are true using only the axioms of
the system and logic deduction.
- The second one states that such a system cannot demonstrate its
own consistency.
- This theorem shows that logic alone is not enough to build a
self-contained theory.
- It is the opposite of theories such as formal systems initiated by
Hilbert.
- It also shows that to properly understand a system you need a
frame of reference bigger than it.
Lambda Expressions in Lisp
(lambda (arg-variables...)
[documentation-string]
[interactive-declaration]
body-forms...)
A lambda expression evaluates to itself.
(lambda (x y) (* x y))
((lambda (x y) (* x y)) 2 3)
6