lecture 10: clarify WHNF

This commit is contained in:
Joel Kaasinen
2023-01-21 17:33:49 +02:00
parent 9d04ae8465
commit 3d0c1b1250
+1 -1
View File
@@ -1033,7 +1033,7 @@ check digit is 7, 33+7=40 is divisible by 10, valid</code></pre>
<li>is a function, for example: <code>(\x -&gt; 1+x)</code></li>
</ul>
<p>The most notable class of expressions that is <em>not</em> in WHNF is function applications. If an expression consists of a function (that is not a constructor), applied to some arguments, it is not in WHNF. We must evaluate it in order to get something pattern matchable.</p>
<p>In the previous example we couldn’t evaluate <code>length (map not (False:[]))</code> at the top level because the argument to <code>length</code> wasn’t in WHNF. When we apply the second equation of <code>map</code>, we get <code>length (not False : map not [])</code>, and now the argument to length is in WHNF since there is a constructor, <code>(:)</code>, at the top level. This is a bit more evident if we switch from infix to prefix notation and write the argument to <code>length</code> as <code>(:) (not False) (map not [])</code>.</p>
<p>In the previous example we couldn’t pick an equation for <code>length</code> in <code>length (map not (False:[]))</code>. The argument <code>(map not ...)</code> is not in WHNF, so it can’t be pattern matched on. Thus we need to evaluate it. When we apply the second equation of <code>map</code>, we get <code>length (not False : map not [])</code>, and now the argument to length is in WHNF since there is a constructor, <code>(:)</code>, at the top level. This is a bit more evident if we switch from infix to prefix notation and write the argument to <code>length</code> as <code>(:) (not False) (map not [])</code>.</p>
<p>In practice, pattern matching is not the only thing that forces evaluation. Primitives like <code>(+)</code> also force their arguments.</p>
<p>Instead of forcing, some sources talk about <em>strictness</em>, we can say for instance that <code>(+)</code> is <em>strict in both arguments</em>.</p>
</section>