Computer Science Programming Languages Semantics Blueprint

 24 June 14:10   

    The semantics of a programming accent are harder to specify than the syntax. For example, to accurate the syntax of abacus 5 with 4, we can say:

    :Put a + assurance in amid the 5 and 4, acquiescent 5 + 4.

    But cogent the semantics of abacus 5 with 4 is abundant added difficult: we charge to ascertain what it agency to be abacus in the first abode and what it agency to accept a amount of 5, 4, and 5 + 4.

    Semantics accept added than just abstract value: Some backdrop of our programming languages can alone be bent through accurate analysis. For example, wed like to create statements such as this programming accent is secure. But to authenticate that a programming accent is defended requires added than just a altercation of the languages syntax. After a academic affidavit of security, systems can be accessible to the hasty after-effects of some separate interacting concerns. One way to create such proofs is by using algebraic models.

    Given the semantics of one programming accent we accept the advantage of anecdotic additional programming languages in agreement of that abject language. So, one way to specify the semantics of a programming accent is to chronicle it to addition language. However, this creates a bootstrapping problem: what to use for the aboriginal abject language. programming accent advisers accept apparent the problem by using a algebraic archetypal that was aboriginal invented for logic, but has affecting computational applications: Lambda Calculus.

    Lambda Calculus, invented in the 1920s and 30s by Church, Kleene, and others, is one of the simplest to call Turing Complete languages. Its a accent that alone has a individual value: the function; and alone two operations: defining a action and calling a function. To create things even simpler, functions are alone accustomed to accept one parameter.

    In the afterward discussion, accessory your apperception to cerebration about functions that yield functions as arguments and acknowledgment additional functions as values. It is a actual abstruse concept, but, as able-bodied find, anon we can body up the accent through able definitions into something that looks added like the programming languages we are acclimated to. For example, one of the first tricks able-bodied analyze is how to accomplish the aforementioned aftereffect of acceptance assorted ambit even admitting anniversary action can alone yield a individual parameter.

    Before we alpha alive with lambda calculus, lets activate with a addle in a added accepted programming language. Accept that Java alone had the bifold < operator, and we bare to ascertain functions to accord us >, >=, and <= operators. We aswell dont accept unary operators either, but we still accept if-statements.

    First, lets attending at what were given:

     // < - Allotment true if a is beneath than b

     accessible changeless boolean lessThan(int a, int b)

    Out assignment is to ascertain greaterThan, greaterThanOrEqualTo, and lessThanOrEqualTo. Remember, for this puzzle, we can alone create calls to lessThan, use if-statements, and acknowledgment boolean values. Anticipate of how you ability do this afore account on.

    The anchor action is not too complicated:

     // > - Allotment true if a is greater than b

     accessible changeless boolean greaterThan(int a, int b)

    This works because whenever we accept b < a we already understand that a > b. We can use agnate acumen to address implementations for greaterThanOrEqualTo and lessThanOrEqualTo:

     // >= - Allotment true if a is greater than or according to b

     accessible changeless boolean greaterThanOrEqualTo(int a, int b)

     // <= - Allotment true if a is beneath than or according to b

     accessible changeless boolean lessThanOrEqualTo(int a, int b)

     // ! - Allotment the antithesis of b

     accessible changeless boolean not(boolean b)

    In the analogue of greaterThanOrEqualTo we are in aftereffect adverse the aftereffect of lessThan. Thus, if a
ot < b we acknowledgment true, and if a < b we acknowledgment false. But if a
ot < b we understand that age b; this is a case area we haveto acknowledgment true, and we do. Similarly, if a < b we understand is cannot be the case that age b, so we accurately acknowledgment false.

    Given these definitions, we can ascertain equalTo and notEqualTo:

     // , able-bodied aswell charge to ad-lib numbers, booleans, all operations on numbers, all relational operations, linked-lists, if expressions, looping constructs and even a archetypal for achievement and additional ancillary effects.

    Here is the grammar of the complete lambda calculus language:

     expr ::= λ id . expr abstraction, bearding action definition

     expr ::= expr expr application, calling a function

     expr ::= id capricious use

     expr ::= ( expr ) grouping

    Well aswell acquaint addition characters to acquiesce us to make short-hands:

     analogue ::= let id = expr

    After such a let is made, the identifier should be broadcast to be the announcement on the right-hand side. Able-bodied aswell cover addition anatomy of parenthesis, which are no altered than the arced departure already defined:

     expr ::=

    Because the accent doesnt accept abundant punctuation lots of departure are traveling to be used, so by acceptance two altered types of departure readability is bigger (because you can bigger visually bout area a alignment starts and ends).

    Finally, identifiers can be any arrangement of characters provided they arent already tokens that beggarly something else:

     id ::= langleany printable, non-whitespace characters except ()=.;, let, and λ
angle

    So, clashing alotof programming languages, in lambda calculus 0, +, % and 15 are all accurate identifiers, just as i, x, y1, butt and theReturnValue are all identifiers in added accepted programming languages.

    We will use ; for band comments.

    Well activate allegorical lambda calculus rules by allegory to a academic Java-like language. The absorption rule,

     expr ::= λ id . expr

    allows us to make new functions. The lambda λ attribute marks a new action is getting defined, and the identifier that follows it is the name of its parameter. The announcement afterwards the dot . is an announcement that may accredit to the constant and any additional variables in scope. Agenda that the functions do not accept names in lambda calculus. If we wish to name them able-bodied charge to use the let anatomy to just make a short-hand.

    It seems that we deceit ascertain abundant of annihilation at all so far. The first action that ability action to you is the character function:

     λx. x

    That is, for any x you accumulation this function, youll get x back. Instead of consistently autograph λx. x anniversary time we wish to use the character function, we can make a short-hand

     let character = λx. x

    and then accredit to identity instead, which would be the aforementioned as apropos to the syntactically added λx. x.

    In Java, the aforementioned analogue would attending added like this:

     accessible changeless Action identity(Function x)

    Because the alone ethics or types in lambda calculus are functions, both the altercation and the acknowledgment amount are of a academic Action type.

    The appliance rule

     expr ::= expr expr

    allows us to administer (or to call) functions. Accept that f were a action and a was its argument. To administer f to a, we artlessly analyze them together:

     f a

    Because departure just accommodate groupings, and because f and a are already terminal elements, all of the afterward are agnate to the above:

     f(a)

     (f a)

     ((f) a)

     ((f) (a))

     (f)a

    Supposing that f was an instance of Action in our academic Java-like language. We ability address action appliance like this:

     f.apply(a)

    which would beggarly administer f to a.

    The absolute semantics of appliance is the barter aphorism (also alleged the β-reduction rule). If f is a lambda action and a is some value, and they are jutaposed, an appliance occurs. Accept that fs constant were called x and some announcement expr acclimated x. Then

     f a

    would be the aforementioned as

     (λx. expr) a

    An appliance states to alter all occurences of x with a. We would address this barter as:

     expr

    Given our character action from before, we can administer it to elements. So,

     character a

    is the aforementioned as

     (λx. x) a

    which is the aforementioned as

     x

    which, afterwards the barter becomes

     a

    Also,

     character identity

    this is the aforementioned as saying:

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

    which is the aforementioned as

     x

    which, afterwards the barter becomes

     (λx. x)

    or, that is to say, the character action itself.

    Currently it doesnt assume like lambda calculus can do abundant added than make the character action and administer it to itself. In adjustment get added in the language, we charge to alpha cerebration of the absorption operation as a true operation in its own right. For example, instead of defining the character function, lets ascertain a action that creates the character function:

     λy. λx. x

    Which should be interpreted as accustomed any y, acknowledgment the character function.

    Previously in our lambda action bodies, weve alone alternate the parameter. The aloft archetype shows that we aswell accept addition choice: acknowledgment the constant of a action were authentic in. For example, what would the afterward mean?

     λy. λx. y

    This action should be interpreted as accustomed any y, allotment a action that, accustomed any x, consistently allotment y. In additional words, it creates a connected function, which we represent in mathematics as a accumbent band on a graph. (The character action on a blueprint would instead be a band at a 45 amount angle, active through the origin.) This area shows how this new advantage can acquiesce you to accurate affluent concepts. In particular, this area shows how you can simulate functions that yield assorted arguments.

    One way you ability anticipate of assuming functions that yield assorted arguments by starting from functions that alone yield a individual altercation is to amalgamate all of the arguments into a assemblage (just as a circuitous amount contains two floating-point numbers) and casual that unit. Such a address can be declared as a tuple technique: as continued as you can canyon an altercation that is infact a accumulating of assorted ethics its absolutely no altered than casual in those assorted ethics in the first place.

    You could use the tuple technique, but lambda calculus doesnt accept any tuple types in it: The alone blazon is a action that takes in a individual argument. But anamnesis the two functions above, that use x and y. Because the action λy action creates the λx action the λx action can use either of x and y.

    One way to anticipate of the problem is to focus on a accurate example. So, accept you wish to ascertain a new action that takes in arguments a, b, and c and does some ciphering involving those values. You can anticipate of this action as a machine. The limitation is that in lambda calculus these machines can alone yield in a individual value. But there arent any austere limitations on what can be returned. So, anticipate first about a apparatus that can alone yield in a: because it doesnt understand b or c it deceit do the adapted computation. But because it knows a, it can acknowledgment a new apparatus that aswell knows a. Because that apparatus aswell takes in a parameter, we can let that be b. Thus, you now accept a apparatus that knows a and b, but not c. So, this apparatus should in about-face make a apparatus that understand a and b and takes in a constant c. Already that apparatus is accustomed c it will be able to compute with all three values.

    To create this abstraction clearer, accept you wish to make a apparatus that can do addition. That is, some affectionate of action that ability apprehend as, accustomed any n and m, allotment the sum of n + m. We can accomplish such an aftereffect if we make a added archaic action first: create-adder. We wont yet apparatus create-adder, but able-bodied accept it exists with a blueprint like this:

     ; create-adder: accustomed n, allotment a action that adds n to its argument

     let create-adder = λn. (details larboard out for the moment)

    Assuming create-adder exists, its atomic to ascertain addition:

     let + = λn.λm. ((create-adder n) m)

    First, dont let the + cruise you up. Thats just a attribute like any additional identifier. We could accept just as able-bodied create the short-hand for this action be additional or add instead of +. So, what is the + action itself?

    The way to apprehend + is: accustomed any n, acknowledgment a action that, accustomed any m, allotment n + m. Bold that 5 and 4 are authentic to be ethics (well appearance how we can do this soon), + can be acclimated to add them:

     ((+ 5) 4)

    Here, first + is activated to 5, and that aftereffect is activated to 4. Because at a high-level + is anticipation of as demography two arguments, it can be easier to accept if we abolish some of the parenthesis:

     (+ 5 4)

    which still agency the aforementioned thing: administer + to 5, and then administer that aftereffect to 4 (and would still beggarly that in this case if we abolish all of the parenthesis). This way of calling the action is aswell the aforementioned as a prefix notation.

    But how absolutely does the + action acknowledgment n + m? The physique is this:

     ((create-adder n) m)

    Here, create-adder is anesthetized n. By definition, it will acknowledgment a action that, accustomed any number, will acknowledgment the sum of n additional that number. We then anon administer that action to m, consistent in the sum of n + m.

    This ambush is accepted as Currying and is how able-bodied achieve assorted arguments, even admitting the accent itself alone technically allows individual altercation functions.

    Now that we can see some way to add assorted arguments to a accent area functions alone yield a individual altercation we can analyze additional extensions. In adjustment to make added programming constructs able-bodied not alone charge to create control-flow statements, able-bodied aswell charge to create new values.

    The alone ethics weve absolutely complete accept been: (1) the character function; (2) a action that allotment the character function; and (3) a action that allotment a connected function.

    The first ethics able-bodied make are the booleans true and false. We could alpha by just defining true to be the character action and then absolution false be the action that allotment the character function, but then we ability acquisition it harder to create these definitions be useful.

    Instead, we should ask first, afore defining true and false, What do we wish to do with true and false admitting that we had them?

    One adorable acreage is that we could accomplish an if somehow. We could anticipate of if as a three-argument function, that takes a boolean value, a then value, and then an abroad value:

     if cond A B

    Note that if is not a keyword, it is just symbols were using. We wish to ascertain it such that if cond is true

     if cond A B

    reduces to

     A

    and if cond is false then

     if cond A B

    reduces to

     B

    To get to this point, we can just anticipate of true as a action that has two parameters, a t constant and an f parameter, and allotment t:

     let true = λt. λf. t

    And we can anticipate of false as a action that has two parameters, a t constant and an f parameter, and allotment f:

     let false = λt. λf. f

    Given that, its simple to ascertain if:

     let if = λcond. λA. λB. cond A B

    Here, the boolean itself (named cond) is accomplishing all of the abundant lifting: The boolean is anesthetized both A and B. If the boolean is true, it should acknowledgment A, and if the boolean is false, it should acknowledgment B. Accustomed our accurate definitions of true and false this is what will happen.

    Now that we accept acceptable definitions for true and false, and even an if construct, we can ascertain additional boolean operations absolutely simply. This recalls the antecedent exercise we did in Java with lessThan and the additional relational functions:

    not: analytic not operation of a boolean value

     let not = λb.

     if b

     false

     true

    You may accept noticed by now that the if is not infact needed, because we could accept cut it out and anesthetized the ethics on anon to the boolean:

     let not = λb. b false true

    It just a amount of appearance that we use if, as a way to advance readability.

    and: analytic and of two booleans if b is true, it allotment c, contrarily it allotment false, thus, it alone allotment true if b and c are both true

     let and = λb. λc.

     if b

     c

     false

    or: analytic or operation: allotment false alone if both b and c are false

     let or = λb. λc.

     if b

     true

     c

    xor: analytic exclusive-or operation: allotment true if b and c are different, false otherwise

     let xor = λb. λc.

     if b

     (not c)

     c

    bool-eq: boolean equality: allotment true if b has the aforementioned amount as c

     let bool-eq = λb. λc.

     if b

     c

     (not c)

    note that beq could aswell be accounting as not (xor b c)

    Church numbers -- For all n>=0: The nth Abbey amount takes in a zero

    value z and applies a accustomed almsman action s to that aught amount n

    times. In algebraic terms, 0 = z (i.e., no applications of s to z are made) and 1 = s(z), 2 = s(s(z)), 3 = s(s(s(z))), ... and so on

     let 0 = λs. λz. z

     let 1 = λs. λz. s z

    successor function: administer the almsman to the accustomed character one added time

     let succ = λn.

     λs. λz.

     s (n s z)

    is-zero?: acknowledgment true if the amount anesthetized in equals zero. This is able by accepting the aught action be true and the almsman action be a action that consistently allotment false. Thus, true will alone be the aftereffect if the almsman is activated aught times.

     let is-zero? = λn. n (λx. false) true

    a beautiful adaptation that uses Currying is: which ability be easier to understand

    addition: allotment the sum of n and m

     let + = λn. λm. n succ m

     let + = λs. λz. m s (n s z)

    multiplication: allotment the artefact of n and m, done by applying the add

    n to yourself operation m times to 0.

     let

    natural amount exponentiation: accession n to the m power

     let pow = λn. λm. m (

    subtraction is trickier, so we charge to ascertain pairs first

     let brace = λfirst. λsecond. λbool. bool first second

    extract first aspect of a pair

     let first = λpair. brace true

    extract additional aspect of a pair

     let additional = λpair. brace false

    cons, car, cdr: to anticipate of pairs as lists instead

     let cons = pair

     let car = first

     let cdr = second

    now, aback to subtraction: the key abstraction is to alpha with an operation that

    takes in a brace of numbers (a, b) and allotment (a+1, a)

     let next-pair = λp. brace (succ (first p)) (first p)

    now apprehension that if we alpha with (0, 0) as a zero, and next-pair as the

    successor function, applying the almsman action n-times yields the

    following:

     times activated amount of brace amount of brace in agreement of n

     ------------- ------------- ---------------------------

     n = 0 (0, 0) (n, n)

     n = 1 (1, 0) (n, n - 1)

     n = 2 (2, 1) (n, n - 1)

     n = 3 (3, 2) (n, n - 1)

     n = 4 (4, 3) (n, n - 1)

    Note, then, that (n next-pair (pair 0 0)) yields the brace (n, n - 1) for

    n >= 1, and (0, 0) for n = 0. If we wish the antecedent of n to be n - 1 for n >= 1, and 0 for n = 0, we just charge to yield the additional aspect of this pair! Thats absolutely how we apparatus predecessor:

     let pred = λn. additional (n next-pair (pair 0 0))

    subtraction: acknowledgment n - m; but if that amount would be negative, 0 is alternate instead

     let - = λn. λm. m pred n

    now that we accept subtraction, we can analyze numbers:

    is n greater than or according to m?

     let >= = λn. λm. is-zero? (- m n)

    is n beneath than or according to m?

     let <= = λn. λm. >= m n

    is n beneath than m?

     let < = λn. λm. not (>= n m)

    is n greater than m?

     let > = λn. λm. not (>= m n)

    equal: are n and m according numbers?

     let according = λn. λm. and (>= n m) (>= m n)

    not-equal: are n and m altered numbers?

     let not-equal = λn. λm. not (equal n m)

    div: accustomed a numerator n and a denominator d, allotment the quotient

     let / = λn. λd.

     (if (< n d)

     0 ; -- (n < d)

     (if (equal n d)

     1 ; -- (n == d)

     ; decrease d from n until the amount is < d -- (n > d)

     (n

     n)))

    mod: accustomed a numerator n and a denominator d, allotment the remainder

     let mod = λn. λd. (- n (

    the fixed-point Y-combinator (call-by-value), for implementing recursion

     let fix = λf.

    Y: alias for fix

     let Y = fix

    

 


Tags: computer, times, example, numbers, language, science, programming, languages, functions, value, greater, start, public, adding, second, result, terms

 &lambda, language, programming, function, equal, value, returns, languages, semantics, given, second, functions, successor, greater, times, lambda, numbers, calculus, subtraction, example, element, return, define, adding, secure, terms, result, syntax, start, , programming language, programming languages, pair pair, returns the, greater than, lambda calculus, pair let, successor function, public static boolean, &lambda pair pair, languages semantics specification, programming languages semantics, science programming languages, computer science programming,

Share Computer Science Programming Languages Semantics Blueprint:
Digg it!   Google Bookmarks   Del.icio.us   Yahoo! MyWeb   Furl  Binklist   Reddit!   Stumble Upon   Technorati   Windows Live   Bookmark

Text link code :
Hyper link code:

Also see ...

Permalink
Article In : Computers & Technology  -  Computer