Guard (computing)

Guard (computing)

In computer programming, a guard is a boolean expression that must evaluate to true if the program execution is to continue in the branch in question. The term is used at least in Haskell, Clean, Erlang, and OCaml programming languages. In Mathematica, guards are called "constraints". Guards are the fundamental concept in Guarded commands, a language in Formal methods. Guards can be used to augment pattern matching with the possibility to skip a pattern even if the structure matches. Boolean expressions in conditional statements usually also fit this definition of a guard although they are called "conditions".

In the following Haskell example, the guards occur between each pair of "|" and "=":

f x
x > 0 = 1
otherwise = 0

This is similar to the respective mathematic notation:

f(x) = left{ egin{matrix} 1 & mbox{if } x>0 \ 0 & mbox{otherwise} end{matrix} ight.

In this case the guards are in the "if" and "otherwise" clauses.

If there are several parallel guards, such as in the example above, they are normally tried in a top to bottom order and the branch of the first to pass is chosen. Guards in a list of cases are typically parallel.

However, in Haskell list comprehensions the guards are in series, and if any of them fails, the list element is not produced. This would be the same as combining the separate guards with logical AND, except that there can be other list comprehension clauses among the guards.

Evolution

A simple conditional expression, already present in CPL in 1963, has a guard on first sub-expression, and another sub-expression to use in case the first one cannot be used. Some common ways to write this: (x>0) -> 1/x; 0 x>0 ? 1/x : 0

If the second sub-expression can be a further simple conditional expression, we can give more alternatives to try before the last "fall-through": (x>0) -> 1/x; (x<0) -> -1/x; 0

Already ISWIM in 1966 had a form of conditional expression without an obligatory fall-through case, thus separating guard from the concept of choosing either-or. In the case of ISWIM, if none of the alternatives could be used, the value was to be "undefined", which was defined to never compute into a value.

SASL (1976) was one of the first programming languages to use the term "guard". In the language, functions could have several definitions and the one to apply was chosen based on the guards that followed each definition: fac n = 1, n = 0 = n * fac (n-1), n > 0

Pattern guard

In addition to a guard attached to a pattern, pattern guard can refer to the use of pattern matching in context of a guard. In effect, a match of the pattern is taken to mean pass. This meaning was introduced by a proposal for Haskell by Simon Peyton Jones titled [http://research.microsoft.com/Users/simonpj/Haskell/guards.html A new view of guards] in April 1997 and has been further used about the implementation of the proposal. The feature provides the possibility to use patterns in the guards of a pattern.

An example in the extended Haskell: clunky env var1 var2
Just val1 <- lookup env var1 , Just val2 <- lookup env var2 = val1 + val2 ...other equations for clunky...

This would read: "Clunky for an environment and two variables, "in case that the lookups of the variables from the environment produce values", is the sum of the values. ..." Like in list comprehensions, the guards are in series, and if any of them fails the branch is not taken.

See also

* assertion (computing)
* logical conditional
* switch statement

References

* [http://ftp.sunet.se/foldoc/foldoc.cgi?guard Guard] in "The Free On-line Dictionary of Computing", Denis Howe (editor).
* "The Haskell 98 Report", chapter [http://haskell.org/onlinereport/exps.html 3 Expressions] .
* "The Mathematica Book," section [http://documents.wolfram.com/mathematica/book/section-2.3.5 2.3.5 Putting Constraints on Patterns]
* "The Glorious Glasgow Haskell Compilation System User's Guide", Version 6.4, section [http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#pattern-guards 7.3.2. Pattern guards]


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Guard — may refer to:Professional occupations* Bodyguard, who protects an individual from personal assault * Crossing guard, who stops traffic so pedestrians can cross the street * Prison guard, who supervises prisoners in a prison or jail * Security… …   Wikipedia

  • Trusted Computing — (TC) is a technology developed and promoted by the Trusted Computing Group. The term is taken from the field of trusted systems and has a specialized meaning. With Trusted Computing the computer will consistently behave in specific ways, and… …   Wikipedia

  • List of computing and IT abbreviations — This is a list of computing and IT acronyms and abbreviations. Contents: 0–9 A B C D E F G H I J K L M N O P Q R S T U V W X Y …   Wikipedia

  • Cluster Computing — NASA Computercluster Ein Computercluster, meist einfach Cluster (engl. „Schwarm“, „Gruppe“, „Haufen“), bezeichnet eine Anzahl von vernetzten Computern, die von außen in vielen Fällen als ein Computer gesehen werden können. In der Regel sind die… …   Deutsch Wikipedia

  • AVG — Average (Academic & Science » Meteorology) * AminoethoxyVinylGlycine (Academic & Science » Botany) * Anti Viral Guard (Computing » Software) * Anti Viral Guard (Computing » Security) …   Abbreviations dictionary

  • PGP — Pretty Good Privacy (Computing » Security) Pretty Good Privacy (Computing » Software) ** Gnu Privacy Guard (Computing » Security) * Pretty Good Protection (Community » Law) * Population Growth Rate (Governmental » US Government) * Pretty Good… …   Abbreviations dictionary

  • List comprehension — A list comprehension is a syntactic construct available in some programming languages for creating a list based on existing lists. It follows the form of the mathematical set builder notation (set comprehension) as distinct from the use of map… …   Wikipedia

  • PG — Proctor & Gamble Company (Business » NYSE Symbols) * Pregnant (Medical » Physiology) * Pretty Good (Internet » Chat) * Prostaglandin (Medical » Physiology) * Papua New Guinea (Regional » Countries) * Postgraduate (Medical » Physiology) * Parental …   Abbreviations dictionary

  • WG — Working Group (Governmental » Military) Working Group (Academic & Science » Meteorology) ** Wohngemeinschaft (International » German) * Weight Gain (Medical » Physiology) * Won t Go (Governmental » Transportation) * WillBros Group, Inc. (Business …   Abbreviations dictionary

  • Robert Byrd — Senator Byrd redirects here. For other U.S. Senators named Byrd, see Harry F. Byrd (Virginia, 1933–1965) and Harry F. Byrd, Jr. (Virginia, 1965–1983). For other people named Robert Byrd, see Robert Byrd (disambiguation). Robert Byrd President pro …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”