Strict function

from Wikipedia, the free encyclopedia

In computer science , a single-digit function is called strict if the following applies: If its argument is undefined ( , bottom ), the result of the function is also undefined. So if: . A multi-digit function can be strict in individual arguments or in all arguments. If all arguments are strict, then the function is strict.

example

In Haskell , defined functions are by default non-strict, but there are strictness annotations that can be used to mark individual arguments as strict. For example, the following Haskell program delivers:

{-# OPTIONS -XBangPatterns #-}

bottom = undefined

f  a  b = a
f' a !b = a

main = do
  print $ f  [1,2,3] bottom
  print $ f' [1,2,3] bottom

following edition:

 [1,2,3]
 strict: Prelude.undefined

In the example, the function is strict and the function is non-strict.

Non-strict functions in strict programming languages

A programming language is said to be strict when defined functions are strict by default. In programming languages ​​with strict evaluation , too, individual functions are often predefined that are not evaluated strictly .

For example, imperative programming languages ​​such as Java or C contain the logical-or operator (i.e. a two-digit function in infix notation), which is not strictly evaluated:

byte a;
boolean b = (a == 0 || 1/a > 0);

If a is 0 here, the last part of the expression is no longer evaluated. If the or ( ||) were strict here, then b would be undefined if a were equal to 0. This type of evaluation is also called short-circuit evaluation .

In functional programming languages with strict evaluation, the if-then-else function does not have to be strictly defined so that a recursion (which contains an if expression) can be programmed at all. In pseudo-code that uses pattern matching :

-- if_then_else condition expr1 expr2

if_then_else True  expr1 expr2 = expr1
if_then_else False expr1 expr2 = expr2

Evaluation strategy

Depending on which evaluation strategy a functional programming language uses, defined functions are by default strict or non-strict. For example, the evaluation strategy left-most / innermost-first leads to strict functions. The evaluation relates to the selection of a reducible expression (redex) in a functional expression that is not yet in normal form . The normal form is present when the expression is redex-free and the execution of a functional program corresponds to the conversion of the program into the normal form. The innermost-first evaluation is also known as a strict evaluation. Intuitively, this corresponds to the procedure that the arguments of a function are evaluated before the function is called (and not only when they are needed).

literature