Why guards are called 'guards'?

306 Views Asked by At

Does anyone know what is the origin of the name for the construct called guards?

func x
    | cond1 -> expr1  
    ...
    | condN -> exprN

Wikipedia article Guard_(computer_science) gives some historical perspective, but lacks explanation for where name came from (it just mentions that SASL was one of the first to use the name).

So far I found references to Edsger Dijkstra and his Guarded Command Language. Was he first to use the term 'guard'?

2

There are 2 best solutions below

0
On BEST ANSWER

Yes, the term is from Dijkstra. Guarded commands, non-determinacy and formal derivation of programs (CACM 1975)

It's ironically hilarious that the Wikipedia article Guard (computer science) says "Guard code provides an early exit from a subroutine, and is a commonly used deviation from structured programming", lacking a fundamental appreciation of guards & structured programming, since the term became important because Dijkstra's guards were a fundamental & influential contribution to structured programming.

3
On

I think it's pointless asking where terminology comes from. (For example why does Haskell have 'types' and 'kinds', whereas math has 'sorts'?) The wikipedia article is good.

The style of writing guards to the right does mirror maths (the wikipedia article has an example). That's probably what influenced Dijkstra. And there's the same mathematical style in Strachey 1967 'Fundamental Concepts'. (He doesn't use "guard".)

@Will Ness guards are not much different from plain conditionals is not right. So it's good to have a different word than "condition". Guards come after pattern-matching:

case x of
  (Maybe x') | x' > 0 -> ...
  Nothing {- x' not in scope here -} -> ...

In a case branch: first match the pattern, and that binds variables; then apply guards using the variables.

And you can extend the idea to the type level https://github.com/AntC2/ghc-proposals/blob/instance-apartness-guards/proposals/0000-instance-apartness-guards.rst