Negation#Programming

{{short description|Logical operation}}

{{for-multi|negation in linguistics|Affirmation and negation|other uses|Negation (disambiguation)}}

{{Use dmy dates|date=March 2020}}

{{Infobox logical connective

| title = Negation

| other titles = NOT

| wikifunction = Z10216

| Venn diagram = Venn10.svg

| definition = \lnot{x}

| truth table = (01)

| logic gate = NOT_ANSI.svg

| DNF = \lnot{x}

| CNF = \lnot{x}

| Zhegalkin = 1 \oplus x

| 0-preserving = no

| 1-preserving = no

| monotone = no

| affine = yes

| self-dual = yes

}}

{{Logical connectives sidebar}}

In logic, negation, also called the logical not or logical complement, is an operation that takes a proposition P to another proposition "not P", written \neg P, \mathord{\sim} P, P^\primeVirtually all Turkish high school math textbooks use p' for negation due to the books handed out by the Ministry of National Education representing it as p'. or \overline{P}.{{cn|reason=Provide a source for each of the notations, including P'.|date=December 2024}} It is interpreted intuitively as being true when P is false, and false when P is true.{{Cite web|last=Weisstein|first=Eric W.|title=Negation|url=https://mathworld.wolfram.com/Negation.html|access-date=2020-09-02|website=mathworld.wolfram.com|language=en}}{{Cite web|title=Logic and Mathematical Statements - Worked Examples|url=https://www.math.toronto.edu/preparing-for-calculus/3_logic/we_3_negation.html|access-date=2020-09-02|website=www.math.toronto.edu}} For example, if P is "Spot runs", then "not P" is "Spot does not run". An operand of a negation is called a negand or negatum.{{Cite book |last=Beall |first=Jeffrey C. |title=Logic: the basics |date=2010 |publisher=Routledge |isbn=978-0-203-85155-5 |edition=1. publ |location=London |pages=57 |language=en}}

Negation is a unary logical connective. It may furthermore be applied not only to propositions, but also to notions, truth values, or semantic values more generally. In classical logic, negation is normally identified with the truth function that takes truth to falsity (and vice versa). In intuitionistic logic, according to the Brouwer–Heyting–Kolmogorov interpretation, the negation of a proposition P is the proposition whose proofs are the refutations of P.

Definition

Classical negation is an operation on one logical value, typically the value of a proposition, that produces a value of true when its operand is false, and a value of false when its operand is true. Thus if statement P is true, then \neg P (pronounced "not P") would then be false; and conversely, if \neg P is true, then P would be false.

The truth table of \neg P is as follows:

:

class="wikitable" style="text-align:center; background-color: #ddffdd;"
bgcolor="#ddeeff"

| P

\neg P
{{yes2|True}}{{no2|False}}
{{no2|False}}{{yes2|True}}

Negation can be defined in terms of other logical operations. For example, \neg P can be defined as P \rightarrow \bot (where \rightarrow is logical consequence and \bot is absolute falsehood). Conversely, one can define \bot as Q \land \neg Q for any proposition {{mvar|Q}} (where \land is logical conjunction). The idea here is that any contradiction is false, and while these ideas work in both classical and intuitionistic logic, they do not work in paraconsistent logic, where contradictions are not necessarily false. As a further example, negation can be defined in terms of NAND and can also be defined in terms of NOR.

{{anchor|Algebra}} Algebraically, classical negation corresponds to complementation in a Boolean algebra, and intuitionistic negation to pseudocomplementation in a Heyting algebra. These algebras provide a semantics for classical and intuitionistic logic.

Notation

The negation of a proposition {{mvar|p}} is notated in different ways, in various contexts of discussion and fields of application. The following table documents some of these variants:

class="wikitable"
style="background:paleturquoise"

! Notation

! Plain text

! Vocalization

style="text-align:center" | \neg p

| style="text-align:center" | {{mono|¬p}} , {{mono|7p}}Used as makeshift in early typewriter publications, e.g. {{cite journal | doi=10.1145/990518.990519 | author=Richard E. Ladner | title=The circuit value problem is log space complete for P | journal=ACM SIGACT News | volume=7 | number=101 | pages=18–20 | date=Jan 1975 }}

| Not p

style="text-align:center" | \mathord{\sim} p

| style="text-align:center" | {{mono|~p}}

| Not p

style="text-align:center" | -p

| style="text-align:center" | {{mono

p}}

| Not p

style="text-align:center" | Np

|

| En p

style="text-align:center" | p'

| style="text-align:center" | {{mono|p'}}

| {{unbulleted list

| p prime,

| p complement

}}

style="text-align:center" | \overline{p}

| style="text-align:center" | {{mono| ̅p}}

| {{unbulleted list

| p bar,

| Bar p

}}

style="text-align:center" | !p

| style="text-align:center" | {{mono|!p}}

| {{unbulleted list

| Bang p

| Not p

}}

The notation Np is Polish notation.

In set theory, \setminus is also used to indicate 'not in the set of': U \setminus A is the set of all members of {{mvar|U}} that are not members of {{mvar|A}}.

Regardless how it is notated or symbolized, the negation \neg P can be read as "it is not the case that {{mvar|P}}", "not that {{mvar|P}}", or usually more simply as "not {{mvar|P}}".

=Precedence=

{{See also|Logical connective#Order of precedence}}

As a way of reducing the number of necessary parentheses, one may introduce precedence rules: ¬ has higher precedence than ∧, ∧ higher than ∨, and ∨ higher than →. So for example, P \vee Q \wedge{\neg R} \rightarrow S is short for (P \vee (Q \wedge (\neg R))) \rightarrow S.

Here is a table that shows a commonly used precedence of logical operators.{{citation|title=Discrete Mathematics Using a Computer|first1=John|last1=O'Donnell|first2=Cordelia|last2=Hall|first3=Rex|last3=Page| publisher=Springer| year=2007| isbn=9781846285981|page=120|url=https://books.google.com/books?id=KKxyQQWQam4C&pg=PA120}}.

class="wikitable" style="text-align: center;"

!Operator !!Precedence

\neg1
\land2
\lor3
\to4
\leftrightarrow5

Properties

=Double negation=

Within a system of classical logic, double negation, that is, the negation of the negation of a proposition P, is logically equivalent to P. Expressed in symbolic terms, \neg \neg P \equiv P. In intuitionistic logic, a proposition implies its double negation, but not conversely. This marks one important difference between classical and intuitionistic negation. Algebraically, classical negation is called an involution of period two.

However, in intuitionistic logic, the weaker equivalence \neg \neg \neg P \equiv \neg P does hold. This is because in intuitionistic logic, \neg P is just a shorthand for P \rightarrow \bot, and we also have P \rightarrow \neg \neg P . Composing that last implication with triple negation \neg \neg P \rightarrow \bot implies that P \rightarrow \bot .

As a result, in the propositional case, a sentence is classically provable if its double negation is intuitionistically provable. This result is known as Glivenko's theorem.

=Distributivity=

De Morgan's laws provide a way of distributing negation over disjunction and conjunction:

:\neg(P \lor Q) \equiv (\neg P \land \neg Q),  and

:\neg(P \land Q) \equiv (\neg P \lor \neg Q).

=Linearity=

Let \oplus denote the logical xor operation. In Boolean algebra, a linear function is one such that:

If there exists a_0, a_1, \dots, a_n \in \{0,1\},

f(b_1, b_2, \dots, b_n) = a_0 \oplus (a_1 \land b_1) \oplus \dots \oplus (a_n \land b_n),

for all b_1, b_2, \dots, b_n \in \{0,1\}.

Another way to express this is that each variable always makes a difference in the truth-value of the operation, or it never makes a difference. Negation is a linear logical operator.

=Self dual=

In Boolean algebra, a self dual function is a function such that:

f(a_1, \dots, a_n) = \neg f(\neg a_1, \dots, \neg a_n) for all

a_1, \dots, a_n \in \{0,1\}.

Negation is a self dual logical operator.

= Negations of quantifiers =

In first-order logic, there are two quantifiers, one is the universal quantifier \forall (means "for all") and the other is the existential quantifier \exists (means "there exists"). The negation of one quantifier is the other quantifier (\neg \forall xP(x)\equiv\exists x\neg P(x) and \neg \exists xP(x)\equiv\forall x\neg P(x)). For example, with the predicate P as "x is mortal" and the domain of x as the collection of all humans, \forall xP(x) means "a person x in all humans is mortal" or "all humans are mortal". The negation of it is \neg \forall xP(x)\equiv\exists x\neg P(x), meaning "there exists a person x in all humans who is not mortal", or "there exists someone who lives forever".

Rules of inference

{{See also|Double negation}}

There are a number of equivalent ways to formulate rules for negation. One usual way to formulate classical negation in a natural deduction setting is to take as primitive rules of inference negation introduction (from a derivation of P to both Q and \neg Q, infer \neg P; this rule also being called reductio ad absurdum), negation elimination (from P and \neg P infer Q; this rule also being called ex falso quodlibet), and double negation elimination (from \neg \neg P infer P). One obtains the rules for intuitionistic negation the same way but by excluding double negation elimination.

Negation introduction states that if an absurdity can be drawn as conclusion from P then P must not be the case (i.e. P is false (classically) or refutable (intuitionistically) or etc.). Negation elimination states that anything follows from an absurdity. Sometimes negation elimination is formulated using a primitive absurdity sign \bot. In this case the rule says that from P and \neg P follows an absurdity. Together with double negation elimination one may infer our originally formulated rule, namely that anything follows from an absurdity.

Typically the intuitionistic negation \neg P of P is defined as P \rightarrow \bot. Then negation introduction and elimination are just special cases of implication introduction (conditional proof) and elimination (modus ponens). In this case one must also add as a primitive rule ex falso quodlibet.

Programming language and ordinary language

{{redirect|!vote|use of !votes in Wikipedia discussions|Wikipedia:Polling is not a substitute for discussion#Not-votes|selfref=yes}}

As in mathematics, negation is used in computer science to construct logical statements.

if (!(r == t))

{

/*...statements executed when r does NOT equal t...*/

}

The exclamation mark "!" signifies logical NOT in B, C, and languages with a C-inspired syntax such as C++, Java, JavaScript, Perl, and PHP. "NOT" is the operator used in ALGOL 60, BASIC, and languages with an ALGOL- or BASIC-inspired syntax such as Pascal, Ada, Eiffel and Seed7. Some languages (C++, Perl, etc.) provide more than one operator for negation. A few languages like PL/I and Ratfor use ¬ for negation. Most modern languages allow the above statement to be shortened from if (!(r == t)) to if (r != t), which allows sometimes, when the compiler/interpreter is not able to optimize it, faster programs.

In computer science there is also bitwise negation. This takes the value given and switches all the binary 1s to 0s and 0s to 1s. This is often used to create ones' complement (or "~" in C or C++) and two's complement (just simplified to "-" or the negative sign, as this is equivalent to taking the arithmetic negation of the number).

To get the absolute (positive equivalent) value of a given integer the following would work as the "-" changes it from negative to positive (it is negative because "x < 0" yields true)

unsigned int abs(int x)

{

if (x < 0)

return -x;

else

return x;

}

To demonstrate logical negation:

unsigned int abs(int x)

{

if (!(x < 0))

return x;

else

return -x;

}

Inverting the condition and reversing the outcomes produces code that is logically equivalent to the original code, i.e. will have identical results for any input (depending on the compiler used, the actual instructions performed by the computer may differ).

In C (and some other languages descended from C), double negation (!!x) is used as an idiom to convert x to a canonical Boolean, ie. an integer with a value of either 0 or 1 and no other. Although any integer other than 0 is logically true in C and 1 is not special in this regard, it is sometimes important to ensure that a canonical value is used, for example for printing or if the number is subsequently used for arithmetic operations.{{cite web |last1=Egan |first1=David |title=Double Negation Operator Convert to Boolean in C |url=https://dev-notes.eu/2019/10/Double-Negation-Operator-Convert-to-Boolean-in-C/ |website=Dev Notes}}

The convention of using ! to signify negation occasionally surfaces in ordinary written speech, as computer-related slang for not. For example, the phrase !voting means "not voting". Another example is the phrase !clue which is used as a synonym for "no-clue" or "clueless".Raymond, Eric and Steele, Guy. [https://books.google.com/books?id=g80P_4v4QbIC&pg=PA18&lpg=PA18 The New Hacker's Dictionary], p. 18 (MIT Press 1996).Munat, Judith. [https://books.google.com/books?id=UOPXXYslemYC&pg=PA148&lpg=PA148 Lexical Creativity, Texts and Context], p. 148 (John Benjamins Publishing, 2007).

Kripke semantics

In Kripke semantics where the semantic values of formulae are sets of possible worlds, negation can be taken to mean set-theoretic complementation{{citation needed|date=August 2012}} (see also possible world semantics for more).

See also

References

{{reflist}}

Further reading

  • Gabbay, Dov, and Wansing, Heinrich, eds., 1999. What is Negation?, Kluwer.
  • Horn, L., 2001. A Natural History of Negation, University of Chicago Press.
  • G. H. von Wright, 1953–59, "On the Logic of Negation", Commentationes Physico-Mathematicae 22.
  • Wansing, Heinrich, 2001, "Negation", in Goble, Lou, ed., The Blackwell Guide to Philosophical Logic, Blackwell.
  • {{cite journal | last1 = Tettamanti | first1 = Marco | last2 = Manenti | first2 = Rosa | last3 = Della Rosa | first3 = Pasquale A. | last4 = Falini | first4 = Andrea | last5 = Perani | first5 = Daniela | last6 = Cappa | first6 = Stefano F. | last7 = Moro | first7 = Andrea | year = 2008 | title = Negation in the brain: Modulating action representation | doi = 10.1016/j.neuroimage.2008.08.004 | journal = NeuroImage | volume = 43 | issue = 2| pages = 358–367 | pmid=18771737| s2cid = 17658822 }}