1

Programming in Haskell by Hutton says

When evaluating an expression, in what order should the reductions be performed? One common strategy, known as innermost evaluation, is to always choose a redex that is innermost, in the sense that it contains no other redex. If there is more than one innermost redex, by convention we choose the one that begins at the leftmost position in the expression.

Another common strategy for evaluating an expression, dual to innermost evaluation, is to always choose a redex that is outermost, in the sense that it is contained in no other redex. If there is more than one such redex then as previously we choose that which begins at the leftmost position. Not surprisingly, this evaluation strategy is known as outermost evaluation.

In partial application of a function, for example, mult(3)(4), where mult is defined as

mult    ::  (Int,Int)   ->  Int
mult    (x,y)   =   x   *   y

innermost evaluation will first evaluate mult(3) as \y->3*y, and then evaluate (\y->3*y)4. How will outermost evaluation evaluate mult(3)(4)?

In application of a curried function, for example, mult'(3)(4), where

mult'   ::  Int ->  Int ->  Int
mult'   x   =   \y  ->  x   *   y

innermost evaluation will first evaluate mult'(3) as \y->3*y, and then evaluate (\y->3*y)4. How will outermost evaluation evaluate mult'(3)(4)?

Will Ness
  • 70,110
  • 9
  • 98
  • 181
Tim
  • 1
  • 141
  • 372
  • 590
  • 2
    Innermost and outermost evaluation agree on your terms, because they are so simple that they only ever have one redex. But consider `mult (mult 3 4) 5` instead, which now must choose between reducing `mult 3 4` first or reducing `mult (...) 5` first. – Daniel Wagner Jul 14 '19 at 02:00
  • @DanielWagner Thanks. Is a function application an reduciable expression i.e. redex, if and only if the function application is not the result of another function application, i.e. if and only if the function application must be either a function name or a lambda expression – Tim Jul 14 '19 at 11:38
  • https://stackoverflow.com/questions/57027138/is-my-understanding-of-an-reduciable-expression-i-e-redex-correct – Tim Jul 14 '19 at 11:48

1 Answers1

6

The only sensible way of interpreting:

mult :: (Int, Int) -> Int
mult (x,y) = x * y

in the context of your larger question is as an unary function that takes a single argument of tuple type (Int, Int). So, mult cannot be partially applied. In particular, mult(3) doesn't make any sense, because 3 is not a tuple of type (Int, Int).

As a result, the reduction of the expression mult (3,4) in the sense meant by Hutton is the same whether you use outermost or innermost reduction. There's only one redex/application here, the application of mult to (3,4), and both outermost and innermost reduction would give the reductions:

mult (3,4)
=>  3 * 4
=>  12

For the function:

mult' :: Int -> Int -> Int
mult' x y = x * y

or equivalently:

mult' = \x -> (\y -> x * y)

the expression mult' 3 4 or equivalently (mult' 3) 4 undergoes innermost reduction as:

(mult' 3) 4
= ((\x -> (\y -> x * y)) 3) 4
=> (\y -> 3 * y) 4
=> 3 * 4
=> 12

Curiously, outermost reduction proceeds in exactly the same manner:

(mult' 3) 4
= ((\x -> (\y -> x * y)) 3) 4     -- (1)
=> (\y -> 3 * y) 4
=> 3 * 4
=> 12

That's because the application of ((\x -> \y -> x * y) 3) to 4 in line (1), while it's the outermost application, is not a redex. It can't be reduced, because the thing being applied ((\x -> \y -> x * y) 3) isn't a lambda expression. (It's an application of a lambda expression to an argument.)

Therefore, contrary to first appearances, there's only one redex in line (1), and the innermost and outermost reduction strategies choose the same redex.

K. A. Buhr
  • 45,621
  • 3
  • 45
  • 71
  • Thanks. Is a function application an reduciable expression i.e. redex, if and only if the function application is not the result of another function application, i.e. if and only if the function application must be either a function name or a lambda expression – Tim Jul 14 '19 at 11:38
  • https://stackoverflow.com/questions/57027138/is-my-understanding-of-an-reduciable-expression-i-e-redex-correct – Tim Jul 14 '19 at 11:48
  • Another question. "mult(3) doesn't make any sense, because 3 is not a tuple of type (Int, Int)". Isn't `mult(3)` a partial lapplication, so it makes sense? – Tim Jul 14 '19 at 12:18
  • Will evaluation of partial applications of a multi-argument function by using innermost evaluation or outermost evaluation be similar to evaluation of application of a curried function? – Tim Jul 14 '19 at 12:48