Grammar Makes Logic Predictable

 

James B. Wilson Professor of Mathematics

Grammar

  • We have selected a logic.
  • We have agreed on our implicit context
  • Now how do we tell people all our choices?

Grammar explains expressions

\[8(7+3)^2?\]

\(=8(10)^2\)

\(=8\times 100\)

\(=800\)

Logical Grammar is a Rooted Tree

This And That

This

That

And

\(3+(-7)\)

3

\(-7\)

7

+

-

\(3+(-7)\)

3

\(-7\)

7

+

-

-4

3+(-7)=-4

=

Rooted Trees:

  • Has a root node (vertex)
  • Each node can have edges to new nodes.
  • A node without children is a leaf.

root

leaf

Trees can be visualized as plants, roots, file folders..

illegal not a new node

Major Fact: Every leaf has a unique path to the root.  Hence, grammar as rooted trees has a unique interpretation.

Making a grammar

   <Subject> ::= Jack
   <Subject> ::= Jill
  <Modifier> ::= up
  <Modifier> ::= down
   <Objects> ::= hill
   <Objects> ::= river
<<Sentence>> ::= <Subject> went <Modifier> the <Objects>.

Accepted "well-formed" sentences

  • "Jack went up the hill."
  • "Jill went down the river."

Rejected "ill-formed" sentences

  • "hill went Jack the up."
  • "Jack and Jill went down the river."
  • \(x^2=9-x\) (totally different language!)

Jargon

  • Reading a sentence as a tree for a grammar is called Parsing.
  • The notation <subject> ::= Jack.  is called Backus-Naur Form or "BNF

Grammar short cuts

   <Subject> ::= Jack
   <Subject> ::= Jill
  <Modifier> ::= up
  <Modifier> ::= down
   <Objects> ::= hill
   <Objects> ::= river
<<Sentence>> ::= <Subject> went <Modifier> the <Objects>.
   <Subject> ::= Jack | Jill
  <Modifier> ::= up 
    		   | down
   <Objects> ::= hill
   <Objects> ::= river
<<Sentence>> ::= <Subject> went <Modifier> the <Objects>.

Use "|" to separate options

Grammar for Natural numbers \(\mathbb{N}\) / Nat

   <Digit> ::= 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
     <Nat> ::= 0 | <Digit> | <Digit><Nat>
  • Accepts "541", written 541:Nat (or \(541:\mathbb{N}\)) because
    • 1:Digit; so, also 1:Nat (or \(1:\mathbb{N}\));
    • 4:Digit, 1:Number, so 41:Nat;
    • 5:Digit, 41:Number; so, 541:Nat.

Grammar for Integers \(\mathbb{Z}\) / Int

   <Digit> ::= 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
     <Nat> ::= 0 | <Digit> | <Digit><Nat>
  • Accepts "541", written 541:Nat (or \(541:\mathbb{N}\)).
  • Rejects "-541" as there is minus sign.   Lets try to add negatives!
   <Digit> ::= 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
     <Nat> ::= 0 | <Digit> | <Digit><Number>
     <Int> ::= <Nat> | -<Nat>

This now accepts "-541". (-541:Int or \(-541:\mathbb{Z}\))

Warning about artifacts: Accepts "-0" and "0" but doesn't know 0=-0!

Notations

  • int 5 or 5:Int. -- Computation
  • \(5:\mathbb{Z}\)  -- Math and Computation
  • \(5\in \mathbb{Z}\) -- Math and Science, slightly different meaning.

Grammar for polynomials

         <Nat> ::= ...# already done
     <Decimal> ::= ... # can be done, more work.
    <Variable> ::= x | y | z
  <Polynomial> ::= <Decimal> 
                 | <Variable>
                 | (<Polynomial>+<Polynomial>)
                 | (<Polynomial>*<Polynomial>)
    			 | (<Polynomial>)^<Nat>
    

Accepts...\[(3.14+x\hat{\hspace{3mm}} 2)*(y+(2*z))\]

Rejects...\[(\pi+x^2)(y+2z)\]

 

While logical grammars are predictable;

humans like a bit of flexibility.

 

Natural language grammar is complicated

Debates still rage over whether English is a "context-free grammar" (see James Higginbotham)

Technical Jargon: Logical grammars are "Context Free".

Those make trees.

Summary

  • Logical Grammar lets our expression be translated into rooted trees.
  • Rooted trees have unique paths between the root and each leaf.
  • The point: logical grammar reads the same way every time!
  • Natural language can lead us in circles, literally.

Logical Grammar is a Rooted Tree, why?

This And This

This

This

And

We argue with operators,

which combine lists of data.

If we reuse data we just put it in as many times as used.

This And This

This

And

Logical Grammar is a Rooted Tree

\(3+7\)

3

7

+

\[\begin{array}{cc} & 3\\ + & 7\\ \hline & 10\end{array}\]

> 3 + 7
10

This And This

This

That

And

\[\begin{array}{cc} & \text{this}\\ \wedge & \text{that}\\ \hline & \text{this}\wedge \text{that}\end{array}\]

> this = true
> that = false
> this && that
false

\[\begin{array}{cc} & 3\\ & 7\\ + & 3\\ \hline & 13\end{array}\]

\(3+7+3\)

[3,7,3]

sum

> as = [3,7,3]
> sum(as)
13

Parse

To recover the tree from a formula

\frac{\sqrt{x-3}}{(x^2+4)}
\sqrt{x-3}

Parse this formula

x+4\neq 0

\(\div\)

u=x-3
\sqrt{u}
x
-3
x\neq -4
4

\(\circ\)

\(+\)

\(+\)

Logical Grammar

  • Operators are names attached to lists of given data.
  • Repeat data must be repeated!
    • Which keeps the grammar as a tree and 
    • lets us use operators to count resources
  • Recovering the tree from a formula is called parsing.

 

Sentence that parse are called "well-formed" and those are the only ones we bother to user in our logic.

Grammar's role in reasoning & programming

By James Wilson

Grammar's role in reasoning & programming

To make sure your logic is understood we go through language, and that means grammar. Fortunately most of logical grammar follows very basic rules.

  • 97