Syntax and Semantics

The abstract functions of section~#secsimilarities#27610> violate Scheme's basic grammar in two ways. First, the names of functions and primitive operations are used as arguments in applications. An argument, though, is an expression, and the class of expressions does not contain primitive operations and function names. It does contain variables, but we agreed that they are only those variables mentioned in variable definitions and as function parameters. Second, parameters are used as if they were functions, that is, the first position of applications. But the grammar of section~#secsynsem#27611> allows only the names of functions and primitive operations in this place.

#tabular27613#

<#27686#>Figure: Scheme with functions as values<#27686#>


Spelling out the problem suggests the necessary changes. First, we should include the names of functions and primitive operations in the definition of <#71299#><#65208#>;SPMlt;<#27688#>exp<#27688#>;SPMgt;<#65208#><#71299#>. Second, the first position in an application should allow other things than function names and primitive operations; at a minimum, it must allow variables that play the role of function parameters. In anticipation of other uses of functions, we agree on allowing expressions in that position. Here is a summary of the three changes:

#tabular27690#

Figure~#figgrammarprog#27701> displays the entire Scheme grammar, with all the extensions we have encountered so far. It shows that the accommodation of abstract functions does not lengthen the grammar, but makes it simpler. The same is true of the evaluation rules. Indeed, they don't change at all. What changes is the set of values. To accommodate functions as arguments of functions, the simplest change is to say that the set of values includes the names of functions and primitive operations:

#tabular27704#

Put differently, if we now wish to decide whether we can apply the substitution rule for functions, we must still ensure that all arguments are values, but we must recognize that function names and primitive operations count as values, too.
<#27725#>Exercise 20.1.1<#27725#> Assume the <#27727#>Definitions<#27727#> window in DrScheme contains <#65226#><#27728#>(define<#27728#><#27729#> <#27729#><#27730#>(f<#27730#>\ <#27731#>x)<#27731#>\ <#27732#>x)<#27732#><#65226#>. Identify the values among the following expressions:

  1. <#65227#><#27734#>(cons<#27734#>\ <#27735#>f<#27735#>\ <#27736#>empty)<#27736#><#65227#>
  2. <#65228#><#27737#>(f<#27737#>\ <#27738#>f)<#27738#><#65228#>
  3. <#65229#><#27739#>(cons<#27739#>\ <#27740#>f<#27740#>\ <#27741#>(cons<#27741#>\ <#27742#>10<#27742#>\ <#27743#>(cons<#27743#>\ <#27744#>(f<#27744#>\ <#27745#>10)<#27745#>\ <#27746#>empty)))<#27746#><#65229#>
Explain why they are values and why the remaining expressions are not values.~ external Solution<#65230#><#65230#> <#27753#>Exercise 20.1.2<#27753#> Argue why the following sentences are legal definitions:
  1. <#65231#><#27756#>(define<#27756#>\ <#27757#>(f<#27757#>\ <#27758#>x)<#27758#>\ <#27759#>(x<#27759#>\ <#27760#>10))<#27760#><#65231#>
  2. <#65232#><#27761#>(define<#27761#>\ <#27762#>(f<#27762#>\ <#27763#>x)<#27763#>\ <#27764#>f)<#27764#><#65232#>
  3. <#65233#><#27765#>(define<#27765#>\ <#27766#>(f<#27766#>\ <#27767#>x<#27767#>\ <#27768#>y)<#27768#>\ <#27769#>(x<#27769#>\ <#27770#>'<#27770#><#27771#>a<#27771#>\ <#27772#>y<#27772#>\ <#27773#>'<#27773#><#27774#>b))<#27774#><#65233#>
external Solution<#65234#><#65234#> <#27781#>Exercise 20.1.3<#27781#> Develop <#65235#><#27783#>a-function=?<#27783#><#65235#>. The function determines whether two functions from numbers to numbers produce the same results for <#65236#><#27784#>1.2<#27784#><#65236#>, <#65237#><#27785#>3<#27785#><#65237#>, and <#65238#><#27786#>-5.7<#27786#><#65238#>. Can we hope to produce <#65239#><#27787#>function=?<#27787#><#65239#>, which determines whether two functions from numbers are equal?~ external Solution<#65240#><#65240#>