150 likes | 267 Views
Lambda Representation. We have used different ways (e.g. f(x), x f y, and a set of tuples) to represent a function. In all of these kinds of representation, the function may not be clear.
E N D
Lambda Representation • We have used different ways (e.g. f(x), x f y, and a set of tuples) to represent a function. In all of these kinds of representation, the function may not be clear. • f(x) may mean the specific element, y, in the range that is mapped from x. e.g. f(x) = x + 3 can mean either: • (i) y= x+3 where f(x) represents the “range item” or specific y valueor • (ii) just the“function”, x+3, itself, with a bounded variable of x. • x f y does not show how the function relates the domain element, x, to the range element, y. • { (1.0), (2,3),(3,8),(4,15), -----} listing does explicitly relate the domain element, x, and the range element, y, but the relationship is not clear • Alonzo Church (1940’s) introduced Lambda Calculus and Lambda representation, ג .
Representation • In ג representation the variables, or parameters, of the function are made explicit. To denote the function , f, one would write as follows: • x . f(x) • For an expression, f, then x. findicates that f is considered a function. • e.g. let f = (x+9) • then x. f is x. (x+9), where i) (x+9) is the function and ii) x is the parameter of the function Remember “bound” variable? ---- λ x states that x is a “bound” variable
Examples of Lambda Expressions • גy. (y2 + y + 3) would mean a function g (y) = (y2 + y +3) where y is the bound variable • for y = 5, we would have g(5) = 52 + 5 + 3 • in expression, we would write it as follows • ( y. (y2 + y +3 ) ) 5 = (52 + 5 + 3 )
A little more general Lambda expression • The previous lambda expressions worked very well for algebraic expressions, but a more general way to express them is as follows: • signature I predicate term • the signature establishes the types of “variables” • predicate establishes the condition that thedomain of thefunction must satisfy • term gives the rangeof the function
Examples with general lambda expressions • m : N I m > 10 m – 2 • (i) m is of type N, (ii) m must satisfy m>10 predicate, (iii) range is (m-2) • this function = { (11,9), (12, 10), ------ } or • g (m) = m -2, for m > 10 • x : 1,2,3,4 (2x, 2x+1) • this function = { (1, (2,3)) , (2, (4,5)), (3, (6,7)), (4,(8,9)) }
Composite functions • Consider a function, f: file_name x address, composed with a shift function of 10 bytes represented by lambda representation • f = {(sys1, 200), (comp2, 5000), (misc, 10500)} • f ;{ z : N I z< 11,990 z+10 } • The composed functions shift the original file addresses by 10 bytes. • Since functions are special relations, they can also be composed as long as the range of the first-function is of the same type as the domain of the second-function
“Traditional” composite functions • Note that the way to show composition with the symbol “;”is a bit different than the traditional representation. • f ;g • takes the elements in the range of function f and use that as the domain of function g • results in a pair whose domain is the domain of f and the range is the range of g • f ( g ) • takes the range of function g and use that as the domain of function f. • results in a pair of whose domain is the domain of g and the range is the range of f
Example of composite representations • Let f be the function, “father of,” and m be the function, “mother of.” • Then f ( m (x) ) provides the person’s maternal grandfather. • And m ( f (x) ) provides the person’s paternal grandmother • Using the ; representation for composite functions • Then f ; m provides the person’s paternal grandmother • and m ; f provides the person’s maternal grandfather
Curried functions • Curried functions are attributed to H.B. Curry • The composite of lambda expression is represented as follows in Curry expression: • x: N y: N x - y • This notation allows us to substitute into the arguments as follows • ( x: N y: N x-y ) 5 9 • what is the answer? Is the sequence obvious? • what about ( x: N ( y: N (x-y) ) 5 ) 9 ? • The answer is 4 NO! Substitute outer most number, 9, into variable of the first ג , which Is ג x
Curried functions • A nice feature of Curried functions is that it allows us to represent a “family” of functions as follows: • ( x: N y: N (x – y) ) 4 to represent or “generate” the function y : N ( 4– y) • From the above , how would you “generate” the function y: N ( y - 4) ? Use parenthesis to clarify? Reverse the notations? What would you do? ( גx: N גy:N (y-x) ) 4
Curried function • In a sense with curried function, we have a composite of functions where the range of the first function is a function as follows • Let Z = F (x, y) = (x + y), then for N= nat numb • ( λx:N λy:N (x + y) ) 6 reduces to • λy : N (6 + y) • So --- the range of first function is a function: f(y) = (6 + y) • thus [λx:N λy:N (x + y)] is N -> (N ->N)
“Sequence” function • A “sequence” may be defined as a function, Seq: N1 x T, that maps positive integers to a set of elements, T. • more formally a sequence , Seq, may be “defined” as Seq: N1 x T such that • (dom Seq = 1, - - - , #Seq) /\ ( ran Seq T)
Examples of Sequences • Let set T be { Tom, Sue, Jose, Yang, Chaudry} • Then {(1, Sue), (2, Chaudry), (3,Tom), (4,Yang), (5, Jose)} is a sequence • Let the above sequence be called “Entry_Order,” then • Entry_Order (1) would be Sue • Entry_Order (3) would be Tom • We can define another sequence “Height_Order” over the same set T as: • Height_Order = {(1, Tom), (2, Jose), (3, Chaudry), (4, Yang), (5, Sue) } • Then Height_Order (1) would give us Tom • and Height_Order (3) would give us Chaudry
Operators over Sequence • One can define several operator over a set of sequences, SS. • First_operator • λs: SS I s is non-empty s (1) • Last_operator • λ s: SS I s is non-empty s( #s) • Front_operator of first z elements of a sequence s from SS, where z < #s • λ s: SS I s is non-empty (1, - - -, z) s
Concatenation Operator • Concatenation is an operator defined over two sequences. • Let s1 and s2 be two sequences, then the concatenation of s1 and s2 is a new sequence, s3, • that starts with s1 and • then places the second sequence s2 in the following order (#s1+1), - - - -, (#s1+#s2).