Programming Languages • Syllogisms and Proof by Contradiction • Midterm Review Dr. Philip Cannata 1 Notions of Truth Propositions: Statements that can be either True or False Truth: Are there well formed propositional formulas (i.e., Statements) that return True when their input is True truth1 :: (Bool -> Bool) -> Bool truth1 wff = (wff True) truth2 :: (Bool -> Bool -> Bool) -> Bool truth2 wff = (wff True True) ( \ p -> not p) ( \ p q -> (p && q) || (not p ==> q)) ( \ p q -> not p ==> q) ( \ p q -> (not p && q) && (not p ==> q) ) If it was never possible for it not to be True that something was going to exist, and it will never be possible for it not to be True that something existed in the past then it is impossible for Truth ever to have had a beginning or ever to have an end. That is, it was never possible that Truth cannot be conceived not to exist. If R is something that can be conceived not to exist and T is something that cannot be conceived not to exist and T is greater than R and God is that, than which nothing greater can be conceived, then God exists and is Truth. Dr. Philip Cannata 2 Proof by Contradiction Facts 1. A is an Animal. 2. 3. A Barks. A is a Dog :- A is an Animal, A Barks. 4. -(A is a Dog) Database (A is a Dog IF A is and Animal AND A Barks) :- , Rule Query (If you want to know if A is a Dog based upon the Facts and Rules in the Database try to see if A is not a Dog.) Contradiction Therefore A is a Dog Can this reasoning be automated? Dr. Philip Cannata 3 Syllogisms Note: this is Prolog notation. In “standard” notation, this would be P Q. P implies Q (i.e., Q is True if P is True or If P is True then Q is True) Syllogism – e.g., Modus Ponens – 1). P is True; 2). P Q is True, therefore Q is True. Green – assume 2 things; the implied Result is in Red; if the result is just True, then the syllogism is Valid, if the results are True and False, then the syllogism is Invalid. Dr. Philip Cannata 4 Proof by Contradiction Database P1 P2 1). Let P = It’s raining, I’m outside (comma means “&&”) 2). P1. (P1 is True, i.e., it’s raining) Facts Rule 3). P2. (P2 is True, i.e., I’m outside) 4). Q :- P = I’m wet :- It’s raining, I’m outside. (if it’s raining and I’m outside then I’m wet) (To answer the Query “Am I wet” against the Database, assume I’m not wet) 5). –Q 6). –(It’s raining, I’m outside) 7). –I’m outside 8). Contradiction – Therefore I’m wet ( From 4 and 5 and Pattern 1 ) ( From 2 and 6 and Pattern 2 ) ( From 3 and 7 and Pattern 3 ) Pattern 1 (Modus Tollens): Q :- (P1, P2). -Q -(P1, P2) P Pattern 2 (Affirming a Conjunct): R P1. -(P1, P2) S -P2 Pattern 3: P2. -P2 Contradiction Dr. Philip Cannata 5 Proof by Contradiction 1. A is an Animal. 2. A Barks. 3. A is a Dog :- A is an Animal, A Barks. 4. -(A is a Dog) Query (If you want to know if A is a 5. 3 and 4 and Pattern 1 -(A is an Animal, A Barks) Dog based upon the Facts and Rules in the Database try to see if A is not a Dog.) 6. 1 and 5 and Pattern 2 -A Barks 7. 2 and 6 and Pattern 3 Contradiction 8. Therefore A is a Dog Dr. Philip Cannata Database 6 Midterm Review What we cannot speak about we must pass over in silence. • The world consists of independent atomic facts—existing states of affairs—out of which larger facts are built. • Language consists of atomic, and then larger-scale, propositions that correspond to these facts by sharing the same "logical form". • Thought, expressed in language, "pictures" these facts. Tractatus Dr. Philip Cannata 7 Previous Midterm Exam Dr. Philip Cannata 8 Study All the Homework and Quizzes Dr. Philip Cannata 9 Functional Programming (Relation Programming) Universe (Concepts for building a lisp interpreter) T. Skolem World Relation Function Primitive Recursive Functions Primitive Recursive Arithmetic Primitive Recursive Relations Gödel's Incompleteness Theorem J. McCarthy and G. Sussman/G. Steel World Lisp / Scheme: First-Class Functions Function Definition (lambda) [e.g., (lambda (x) x) ] Function Application (f a) Substitution (let) [e.g., (let ((x 3)) x) ] car, cdr, cons, s-expressions foldr, foldl, map Dynamic / Static Scoping Dr. Philip Cannata A. Church and H. Curry World λ calculus: Alpha-conversion Beta Reduction Combinator Logic: <S>, <K>, <B>, <C>, <Y>, and cond S. Krishnamurthi World PLAI – AE, WAE, FAE: Function Definition (fun) [e.g., (fun (x) x) ] Function Application (app) [e.g., (app (id 'f) (id 'a)) ] Substitution (with) [e.g., (with (x 3) x) ] Converting “withs” to “(f a)” (I.e., the FAE parser converts concrete syntax to abstract syntax) Environment / Closures Dynamic / Static Scoping Lazy / Eager Evaluation (interp (parse ‘expr) Env.) 10 High Level Language Universe (Concepts for building a HLL interpreter like ReL) N. Chomsky Regular Grammar World Regular Grammar Regular Expressions Finite State Machines Token (Non-Terminal) Lexical Analyzer (Tokenizer) Parser.jj ./grammar/Python.g N. Chomsky Regular Context Free Grammar World Context-free Grammar BNF EBNF Terminal Syntactic Analyzer (Parser) Internal Parse Tree Ambiguous Grammar Parser.jj ./grammar/Python.g Abstract Syntax [Tree] World Nodes on an AST ./src/org/python/antlr/ast ./dist/bin/jython ast/astview.py file.py Tools javacc / JJTree / JTB / JJDoc o Antlr ant SQLDeveloper jDeveloper or Eclipse jSQLParser Dr. Philip Cannata Interpreter World Process an AST Visitor Pattern Symbol Table Type Checking Runtime Stack / Activation Record SQL, SIM RDF/RDFS/OWL/SPARQL ./src/org/python/antlr/ast/Tuple.java ./org/python/core/PyTuple.java 11 Dr. Philip Cannata 12 Relations and Functions Relations: A Relation is a subset of the cross-product of a set of domains. Functions: An n-ary relation R is a function if the first n-1 elements of R are the function’s arguments and the last element is the function’s results and whenever R is given the same set of arguments, it always returns the same results. [Notice, this is an unnamed function!]. Dr. Philip Cannata 13 Function Bodies With the same goals as Alfred Whitehead and Bertrand Russell in Principia Mathematica (i.e., to rid mathematics of the paradoxes of the infinite and to show that mathematics is consistent) but also to avoid the complexities of mathematical logic - “The foundations of elementary arithmetic established by means of the recursive mode of thought, without use of apparent variables ranging over Thoralf Skolem infinite domains” – Thoralf Skolem, 1923 In a small town there is This article can be found in “From only one barber. This man is defined to be the Frege to Gödel: A Source Book in one who shaves all the men who do not shave Mathematical Logic, 1879-1931 (Source themselves. The question Books in the History of the Sciences)” is then asked, 'Who shaves the barber?' If the barber doesn't shave himself, then -- by definition -- he does. And, if the barber does shave himself, then -- by definition -- he does not. or Consider the statement 'This statement is false.‘ If the statement is false, then it is true; and if the statement is true, then it is false. Dr. Philip Cannata A function is called primitive recursive if there is a finite sequence of functions ending with f such that each function is a successor, constant or identity function or is defined from preceding functions in the sequence by substitution or recursion. 14 Primitive Recursive Functions and Arithmetic (see “A Profile of Mathematical Logic” by Howard Delong – pages 152 – 160) • Thoralf Skolem “The foundations of elementary arithmetic by means of the recursive mode of thought, without the use of apparent variables ranging over infinite domains” (1919, 1923). • He assumed that the following notions are already understood: – natural number (0 is included in the book), – successor of x (i.e., x’), – substitution of equals (if x = y and y = z, then x = z), – and the recursive mode of thought (see next page). Dr. Philip Cannata 15 Primitive Recursive Functions and Arithmetic (see “A Profile of Mathematical Logic” by Howard Delong – pages 152 – 160) The addition function Book notation f(x, 0) = x f(x, y’) = (f(x, y))’ Relation notation Arithmetic notation (x 0 x) (x y’ ( x y b)’) x+0=x x + y’ = (x + y)’ Example 2 + 3 (The similar example of 7 + 5 on pages 153-154 is actually a bit flawed as a comparison with the following will show): (0’’ 0 0’’) 2+0=2 (0’’ 0’ (0’’ 0 b)’) => (0’’ 0’ (0’’)’) => (0’’ 0’ 0’’’) 2+1=3 (0’’ 0’’ (0’’ 0’ b)’) Dr. Philip Cannata => (0’’ 0’’ (0’’’)’) => (0’’ 0’’ 0’’’’) 2+2=4 16 Primitive Recursive Functions and Arithmetic (see “A Profile of Mathematical Logic” by Howard Delong – pages 152 – 160) “We may summarize the situation by saying that while the usual definition of a function defines it explicitly by giving an abbreviation of that expression, the recursive definition defines the function explicitly only for the first natural number, and then provides a rule whereby it can be defined for the second natural number, and then the third, and so on. The philosophical importance of a recursive function derives from its relation to what we mean by an effective finite procedure, and hence to what we mean by algorithm or decision procedure.” [DeLong, page 156] Dr. Philip Cannata 17 Primitive Recursive Functions and Arithmetic (see “A Profile of Mathematical Logic” by Howard Delong – pages 152 – 160) A Sequence of Functions from definitions on DeLong, page 157: Book notation Relation notation Arithmetic notation f1(x) = x’ (x (+ x 1)) f1 is the successor function f2(x) = x (x x) f2 is the identity function with i = 1 f3(y, z, x) = z (y z x z) f3 is the identity function with i = 2 f4(y, z, x) = f1(f3(y,z,x)) (y z x ((x (+ x 1)) (y z x z))) f4 is defined by substitution for f1 and f3 This is how you would do this in lisp (let ((f1 (lambda (x) (+ x 1))) (f3 (lambda (y z x) z))) (let ((f4 (lambda (y z x) (f1 (f3 y z x))))) (f4 2 4 6))) f5(0, x) = f2(x) (0 x ( x x)) f5(y’, x) = f4(y, f5(y,x), x) (not doable yet) f5 is defined by recursion and f2 and f4 f5 is primitive recursive addition (let ((f1 (lambda (x) (+ x 1))) (f2 (lambda (x) x)) (f3 (lambda (y z x) z))) (let ((f4 (lambda (y z x) (f1 (f3 y z x))))) (letrec ((f5 (lambda (a b) (if (= a 0) (f2 b) (f4 (- a 1) (f5 (- a 1) b) b))))) (f5 2 3)))) Dr. Philip Cannata 18 Primitive Recursive Functions and Arithmetic (see “A Profile of Mathematical Logic” by Howard Delong – pages 152 – 160) Primitive Recursive Relations on DeLong, pages 158-159: Example of eq primitive recursive relation: (letrec ((pd (lambda (x) (if (= x 0) 0 (- x 1)))) (dm (lambda (x y) (if (= y 0) x (pd (dm x (pd y)))))) (abs (lambda (x y) (+ (dm x y)(dm y x)))) (sg (lambda (x) (if (= x 0) 0 1))) (eq (lambda (x y) (sg (abs x y))))) (eq 1 1)) Dr. Philip Cannata 19 A little Bit of Lambda Calculus – Lambda Expressions The manipulation of meaningless symbols? ________________________________________________ A lambda expression is a particular way to define a function: LambdaExpression variable | (M N) | ( variable . M ) M LambdaExpression N LambdaExpression E.g., ( x . (sq x) ) represents applying the square function to x. Dr. Philip Cannata 20 A little Bit of Lambda Calculus – Properties of Lambda Expressions In ( x . M), x is bound. Other variables in M are free. A substitution of N for all occurrences of a variable x in M is written M[x N]. Examples: • An alpha-conversion allows bound variable names to be changed. For example, alpha-conversion of λx.x might yield λy.y. • A beta reduction (( x . M)N) of the lambda expression ( x . M) is a substitution of all bound occurrences of x in M by N. E.g., (( x . x2) 5) = 52 Dr. Philip Cannata 21 Lambda Calculus Lambda Calculus x.x s.(s s) func.arg.(func arg) def identity = x.x def self_apply = s.(s s) def apply = func.arg.(func arg) def select_first = first.second.first def select_second =first.second.second def cond= e1.e2.c.((c e1) e2) def true=select_first def false=select_second def not= x.(((cond false) true) x) Or def not= x.((x false) true) def and= x.y.(((cond y) false) x) Or def and= x.y.((x y) false) def or= x.y.(((cond true) y) x) Or def or= x.y.((x true) y) Dr. Philip Cannata 22 Lambda Calculus Substitution In lambda calculus, if cond is defined as def cond= e1.e2.c.((c e1) e2), def and= x.y.(((cond y) false) x) is equivalent to def and= x.y.((x y) false) because: (((cond y) false) x) (((e1.e2.c.((c e1) e2) y) false) x) ((e2.c.((c y) e2) false) x) (c.((c y) false) x) ((x y) false) In lambda calculus, if cond is defined as def cond= e1.e2.c.((c e1) e2), def or= x.y.(((cond true) y) x) is equivalent to def or= x. y.((x true) y) because: (((cond true) y) x) (((e1.e2.c.((c e1) e2) true) y) x) ((e2.c.((c true) e2) y) x) (c.((c true) y) x) ((x true) y) Dr. Philip Cannata 23 Simple Lisp in Scheme (letrec ( (first (lambda (List) (if (null? List) (list) (car List)))) (sum-list (lambda (List) (if (null? List) 0 (+ (car List) (sum-list (cdr List)))))) (nth (lambda (N List) (if (not (= N 0))(nth (- N 1) (cdr List))(car List)))) (head (lambda (N List) (if (= N 0) (list) (cons (car List) (head (- N 1) (cdr List)))))) ) (nth 1 (list 1 2 3))) 2 (letrec ( (List (list 1 2 3 4 5 6)) (first (lambda (List) (if (null? List) (list) (car List)))) (sum-list (lambda (List) (if (null? List) 0 (+ (car List) (sum-list (cdr List)))))) (nth (lambda (N List) (if (not (= N 0))(nth (- N 1) (cdr List))(car List)))) (head (lambda (N List) (if (= N 0) (list) (cons (car List) (head (- N 1) (cdr List)))))) ) (head (nth 1 List) List) ) (list 1 2) Code for Chaitin page 43 - 44 (letrec ( (map (lambda (Function List) (if (null? List) List (cons (Function (car List)) (map Function (cdr List))) )) ) (factorial (lambda (N) (if (= N 0) 1 (* N (factorial (- N 1)))))) ) (map factorial (list 4 1 2 3 5))) (list 24 1 2 6 120) Define statement: (define nth (lambda (N List) (if (not (= N 0))(nth (- N 1) (cdr List))(car List)))) (nth 2 (list 1 2 3 4 5)) 3 Dr. Philip Cannata 24 Develop Substitution for the Following Expressions Start with schema from Chapter 2 Get the following expressions to work: (subst (id 'x) 'x (id 'y)) (subst (num 2) 'x (num 1)) (subst (id 'x) 'x (num 1)) (subst (id 'y) 'x (num 1)) (subst (add (id 'x) (id 'x)) 'x (num 1)) (subst (with 'y (num 2) (id 'x)) 'x (num 1)) (subst (with 'y (num 2) (add (id 'x) (id 'y))) 'x (num 1)) (subst (with 'y (id 'x) (add (id 'x) (id 'y))) 'x (num 1)) (subst (with 'x (id 'x) (id 'x)) 'x (num 1)) (calc (subst (with 'y (add (num 2) (id 'x)) (add (id 'y)(id 'x))) 'x (num 1))) Dr. Philip Cannata 25 Scheme for Textbook Chapter 3 #lang plai (define-type WAE [num (n number?)] [add (lhs WAE?) (rhs WAE?)] [sub (lhs WAE?) (rhs WAE?)] [with (name symbol?) (named-expr WAE?) (body WAE?)] [id (name symbol?)]) ;; subst : WAE symbol WAE!WAE (define (subst expr sub-id val) (type-case WAE expr [num (n) expr] [add (l r) (add (subst l sub-id val) (subst r sub-id val))] [sub (l r) (sub (subst l sub-id val) (subst r sub-id val))] [with (bound-id named-expr bound-body) (if (symbol=? bound-id sub-id) (with bound-id (subst named-expr sub-id val) bound-body) (with bound-id (subst named-expr sub-id val) (subst bound-body sub-id val)))] [id (v) (if (symbol=? v sub-id) val expr)])) Dr. Philip Cannata ;; calc : WAE!number (define (calc expr) (type-case WAE expr [num (n) n] [add (l r) (+ (calc l) (calc r))] [sub (l r) (- (calc l) (calc r))] [with (bound-id named-expr bound-body) (calc (subst bound-body bound-id (num (calc named-expr))))] [id (v) (error 'calc "free identifier")])) 26 Functions and Recursion Dr. Philip Cannata 27 “let” transformation, differed substitution and closures, and interpretation in FAE “let” transformation: (let ((A B)) C) == ((lambda (A) C) B) A B ---------------- C ------------------A ---------- B -------- C (let (( x 3)) (let ((f (lambda (y) (+ x y)))) (f 4)) ((lambda (x) ((lambda (f) (f 4)) (lambda (y) (+ x y)))) 3) (app (fun 'x [app (fun 'f [app (id 'f) (num 4)]) (fun 'y (add (id 'x) (id 'y)))]) (num 3)) (app ------- arg1 ------------------ ------------arg2--------------(app --------------------------------------- arg1------------------------------------- --arg2— Differed substitution and closures: (aSub 'f (closureV 'y (add (id 'x) (id 'y)) (aSub 'x (numV 3) (mtSub))) (aSub 'x (numV 3) (mtSub))) Interpretation: (interp (app (id 'f) (num 4)) (aSub 'f (closureV 'y (add (id 'x) (id 'y)) (aSub 'x (numV 3) (mtSub))) (aSub 'x (numV 3) (mtSub)))) (numV 7) Dr. Philip Cannata 28 Static and Dynamic Scoping Static scoping: (interp (parse '{with {x 5} {f 4}}) (aSub 'f (closureV 'y (add (id 'x) (id 'y)) (aSub 'x (numV 3) (mtSub))) (aSub 'x (numV 3) (mtSub)))) (numV 7) Dynamic Scoping: (interp (parse '{with {x 5} {f 4}}) (aSub 'f (closureV 'y (add (id 'x) (id 'y)) (aSub 'x (numV 3) (mtSub))) (aSub 'x (numV 3) (mtSub)))) (numV 9) Think about this expression for both Static and Dynamic Scoping: (let ((z 3)) (let ((d 3) (f (lambda (x) x))) (let ((z 27)) (let ((z 3) (a 5) (x (lambda (x y) (+ x (+ y z))))) (let ((z 9) (a 7)) (x z a)))))) Dr. Philip Cannata 29 Recursive Functions Exemplified by foldr in lisp (letrec ((f (lambda (l) (if (null? l) (cons 1 || (cons 2 || (cons 3 || (cons 4 || (cons 5 || (cons 5 '())))))) '(1 2 3 4 5 6) (letrec ((f (lambda (v l) (if (null? l) v (cons (car l) (f v (cdr l))))))) (f '() '(1 2 3 4 5 6))) '(1 2 3 4 5 6) (letrec ((f (lambda (f1 v l) (if (null? l) v (f1 (car l) (f f1 v (cdr l))))))) (f cons '() '(1 2 3 4 5 6))) '(1 2 3 4 5 6) f == foldr If f1 == cons, foldr is the identity function for a list. It‘s the same as (cons 1 (cons 2 (cons 3( cons 4 (cons 5 (cons 6 '())))))) Here the stack is upside down Dr. Philip Cannata (cons (car l) (f (cdr l))))))) (f '(1 2 3 4 5 6))) (cons 1 || (cons 2 || (cons 3 || (cons 4 || (cons 5 || (cons 5 '())))))) This can be thought of as a stack with “cons”s on it. '() 30 Recursive Functions Exemplified by foldl in lisp (letrec ((f (lambda (f1 v l) (if (null? l) v (f f1 (car l) (cdr l)))))) (f cons '() '(1 2 3 4 5 6))) 6 Nothing goes on the stack in this case. (letrec ((f (lambda (f1 v l) (if (null? l) Dr. Philip Cannata v (f f1 (f1 (car l) v) (cdr l)))))) (f cons '() '(1 2 3 4 5 6))) '(6 5 4 3 2 1) f == foldl If f1 == cons, foldl reverses the list. foldl is tail-recursive because nothing goes on the stack. It‘s the same as (cons 6 (cons 5 (cons 4 ( cons 3 (cons 2 (cons 1 '())))))) 31 PLAI Chapters 4, 5 and 6 Chapter 6, Pages 41 & 42 – “first-order Functions are not values in the language. They can only be defined in a designated portion of the program, where they must be given names for use in the remainder Page 27 - "of the program. The functions in F1WAE are of this nature, which explains the 1 in the name of the language. higher-order Functions can return other functions as values. first-class Functions are values with all the rights of other values. In particular, they can be supplied as the value of arguments to functions, returned by functions as answers, and stored in data structures. Dr. Philip Cannata 32 A Different Road to High-level Languages Dr. Philip Cannata 33 We’ll be starting with javacc moving to ANTLR later Instance of a Programming Language: int main () { return 0 ; Internal Parse Tree } Program (abstract syntax): Function = main; Return type = int params = Block: Return: Variable: return#main, LOCAL addr=0 IntValue: 0 Abstract Syntax Dr. Philip Cannata 34 Syntax and Grammar – Parser.jj PARSER_BEGIN(Parser) import java.io.*; import java.util.*; public class Parser { public static void main(String args[]) throws ParseException { Parser parser = new Parser (System.in); parser.ae(); } } Parser PARSER_END(Parser ) SKIP : { "" | "\t" | "\n" | "\r" | <"//" (~["\n","\r"])* ("\n"|"\r")> } Grammar Production Rules TOKEN: { < LCURLY: "{" > | < RCURLY: "}" > | < MINUS: "-" > | < PLUS: "+" > } TOKEN: /* Literals */ { < INTEGER: (["0"-"9"])+ > } TOKEN: { <ERROR: ~[] > } Dr. Philip Cannata Tokens, Terminals void ae() : { Token n; } { n = <INTEGER> { System.out.print("(num " + n +")"); } | list() } void list() : {} { LOOKAHEAD(2) <LCURLY> <PLUS> { System.out.print(" (add ");} ( ae() )* <RCURLY> { System.out.print(") "); } | <LCURLY> <MINUS> { System.out.print(" (sub ");} ( ae() )* <RCURLY> { System.out.print(") "); } } 35 PARSER_BEGIN(Rel) import java.io.*; import java.util.*; public class Rel { public static void main(String args[]) throws ParseException { Rel parser = new Rel(System.in); parser.program(); } } PARSER_END(Rel) SKIP : { "" | "\t" | "\n" | "\r" | <"//" (~["\n","\r"])* ("\n"|"\r")> } TOKEN: { < RELATION: "Relation" > | < COMPOSE: "Compose" > | < WITH: "with" > } void program() : {} { ( rels() )+ ( comps() )* } Example Javacc Parser void rels() : { String id; } { <RELATION> ( id = identifier() { System.out.println(" Saw IDENTIFIER " + id); } )+ { System.out.println("Saw a RELATION"); } } String identifier() : { Token t; } { t = <IDENTIFIER> { return new String(t.image); } } void comps() : { String id1, id2; } { <COMPOSE> id1 = identifier() <WITH> id2 = identifier() { System.out.println("Saw Composiiton of " + id1 + " with " + id2); } } TOKEN: /* Literals */ { < IDENTIFIER: [ "A"-"Z", "a"-"z" ] ( [ "A"-"Z", "a"-"z", "0"-"9", "_"] )* > } TOKEN: { <ERROR: ~[] > } Dr. Philip Cannata 36 Chomsky Hierarchy • Regular grammar – used for tokenizing • Context-free grammar (BNF) – used for parsing • Context-sensitive grammar – not really used for programming languages Dr. Philip Cannata 37 Regular Grammar • Simplest; least powerful • Equivalent to: – Regular expression (think of perl) – Finite-state automaton • Right regular grammar: Terminal*, A and B Nonterminal A→B A→ • Example: Integer → 0 Integer | 1 Integer | ... | 9 Integer | 0 | 1 | ... | 9 Dr. Philip Cannata 38 Regular Grammar • Less powerful than context-free grammars • The following is not a regular language { aⁿ bⁿ | n ≥ 1 } i.e., cannot balance: ( ), { }, begin end Dr. Philip Cannata 39 Regular Expressions x \x { name } M|N MN M* M+ M? [aeiou] [0-9] . Dr. Philip Cannata a character x an escaped character, e.g., \n a reference to a name M or N M followed by N zero or more occurrences of M One or more occurrences of M Zero or one occurrence of M the set of vowels the set of digits any single character 40 Finite State Automaton for Identifiers (S, a2i$) ├ (I, 2i$) ├ (I, i$) ├ (I, $) ├ (F, ) Thus: (S, a2i$) ├* (F, ) Dr. Philip Cannata 41 Context-Free Grammar Production: α→β α Nonterminal β (Nonterminal Terminal)* ie, lefthand side is a single nonterminal, and righthand side is a string of nonterminals and/or terminals (possibly empty). Dr. Philip Cannata 42 Context-Sensitive Grammar Production: α→β |α| ≤ |β| α, β (Nonterminal Terminal)* ie, lefthand side can be composed of strings of terminals and nonterminals, however, the number of items on the left must be smaller than the number of items on the right. Dr. Philip Cannata 43 Syntax • The syntax of a programming language is a precise description of all its grammatically correct programs. • Precise syntax was first used with Algol 60, and has been used ever since. • Three levels: – Lexical syntax - all the basic symbols of the language (names, values, operators, etc.) – Concrete syntax - rules for writing expressions, statements and programs. – Abstract syntax - internal representation of the program, favoring content over form. Dr. Philip Cannata 44 Grammars Grammars: Metalanguages used to define the concrete syntax of a language. Backus Normal Form – Backus Naur Form (BNF) • Stylized version of a context-free grammar (cf. Chomsky hierarchy) • First used to define syntax of Algol 60 • Now used to define syntax of most major languages Production: α→β α Nonterminal β (Nonterminal Terminal)* ie, lefthand side is a single nonterminal, and β is a string of nonterminals and/or terminals (possibly empty). • Example Integer Digit | Integer Digit Digit 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Dr. Philip Cannata 45 Extended BNF (EBNF) Additional metacharacters { } a series of zero or more ( ) must pick one from a list [ ] pick none or one from a list Example Expression -> Term { ( + | - ) Term } IfStatement -> if ( Expression ) Statement [ else Statement ] EBNF is no more powerful than BNF, but its production rules are often simpler and clearer. Javacc EBNF ( … )* a series of zero or more ( … )+ a series of one or more [ … ] optional Dr. Philip Cannata 46 High Level Overview of Grammar Concepts { } a series of zero or more ( ) must pick one from a list [ ] pick none or one from a list expression -> term { ( + | - ) term } term -> factor { ( * | / ) factor } factor -> ( expression ) | number // the parenthesis are part of the grammar not the EBNF number -> { 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 } 6 * ( 11 – 7 ) / 3 + 100 Dr. Philip Cannata 47 Associativity and Precedence • A grammar can be used to define associativity and precedence among the operators in an expression. E.g., + and - are left-associative operators in mathematics; * and / have higher precedence than + and - . • Consider the following grammar: Expr -> Expr + Term | Expr – Term | Term Term -> Term * Factor | Term / Factor | Term % Factor | Factor Factor -> Primary ** Factor | Primary Primary -> 0 | ... | 9 | ( Expr ) Dr. Philip Cannata 48 Associativity and Precedence Parse of 4**2**3 + 5 * 6 + 7 Dr. Philip Cannata 49 Functions and Recursion Dr. Philip Cannata 50 Runtime Stack for Functions Program int h, i; void B(int w) { int j, k; i = 2*w; w = w+1; } void A(int x, int y) { bool i, j; B(h); } int main() { int a, b; h = 5; a = 3; b = 2; A(a, b); } Dr. Philip Cannata • parameters and local variables • Return address • Saved registers • Temporary variables • Return value 51 with Dr. Philip Cannata ReL 52 ReL Components CodeCompiler.java Python.g AST The Python grammar was changed to include SIM, SQL, Prolog, and ASP statements. jSIMParser There is no jSIMParser to parse SIM statements like jSQLParser for SQL but I hope one will be built as a project. SIMHelper.java SIMHelper produces appropriate SPARQL statements for the SIM statements which are sent to SPARQLDoer. Dr. Philip Cannata Visitor Interpreter Runtime Stack PyTuple.java ---------------parseSIM() parseSIM parses SIM statements from PyTuple and produces and passes AS information to SIMHelper. SPARQLDoer.java Oracle DBMS The Tuple entry in CodeCompiler was modified to deal with putting SIM, SQL, Prolog, and ASP information and expressions on the runtime stack and to assure that a new instance of PyTuple is created after the expressions are evaluated. jSQLParser jSQLParser parses SQL statements from PyTuple and produces an AST that can be visited. SQLVisitor.java SQLVisitor visits the jSQLParser AST and produces appropriate SPARQL statements for the SQL statements which are sent to SPARQLDoer. 53 What would you expect to find in Jython • A grammar file for the python language – ./grammar/python.g • Java classes that get instantiated as nodes on the Abstract Syntax Tree – ./src/org/python/antlr/ast • A way of printing the Abstract Syntax Tree – ./dist/bin/jython ast/astview.py file.py • Visitor classes for the interpreter – ./org/python/antlr/ast/VisitorBase.java ./org/python/antlr/ast/VisitorIF.java ./org/python/antlr/Visitor.java • A way to hook into the visitor’s process of setting up the runtime stack – ./org/python/compiler/Code.java ./org/python/compiler/CodeCompiler.java • A way to work with the results from the jython interpreter – an example that we’ll use a lot is ./org/python/core/PyTuple.java Dr. Philip Cannata 54 $ cat tests/demo.py ; dist/bin/jython ast/astview.py tests/demo.py MAKECONNECT URL jdbc:oracle:thin:@rising-sun.microlab.cs.utexas.edu:1521:orcl UNAME cs345_50 PWORD cs345_50p; DROP TABLE NEWTEST1; # CREATE TABLE NEWTEST1 (VAL1 NUMBER, VAL2 NUMBER, VAL3 NUMBER); x=3 for i in [2, 4, 7]: INSERT INTO NEWTEST1 (VAL1, VAL2, VAL3) VALUES ((lambda x:x+1) (4), i, x); print (SELECT * FROM NEWTEST1;) print (SELECT VAL2 FROM NEWTEST1;) print (SELECT VAL1, VAL2 FROM NEWTEST1 WHERE VAL3 = x;) ('Module', ('body', ('Expr (1,0)', ('value', ('Connection (1,0)', ('elts',), ('ctx', ('Load',))))), ('Expr (2,0)', ('value', ('Tuple (2,0)', ('elts',), ('ctx', ('Load',))))), ('Assign (4,0)', ('targets', ('Name (4,0)', ('id', 'x'), ('ctx', ('Store',)))), ('value', ('Num (4,2)', ('n', 3)))), ('For (5,0)', ('target', ('Name (5,4)', ('id', 'i'), ('ctx', ('Store',)))), ('iter', ('List (5,9)', ('elts', ('Num (5,10)', ('n', 2)), ('Num (5,13)', ('n', 4)), ('Num (5,16)', ('n', 7))), ('ctx', ('Load',)))), ('body', ('Expr (6,1)', ('value', ('Tuple (6,1)', ('elts', ('Call (6,49)', ('func', ('Lambda (6,50)', ('args', ('arguments', ('args', ('Name (6,57)', ('id', 'x'), ('ctx', ('Param',)))), ('vararg', None), Dr. Philip Cannata ('body', ('BinOp (6,59)', ('left', ('Name (6,59)', ('id', 'x'), ('ctx', ('Load',)))), ('op', ('Add',)), ('right', ('Num (6,61)', ('n', 1))))))), ('args', ('Num (6,65)', ('n', 4))), ('keywords',), ('starargs', None), ('kwargs', None)), ('Name (6,69)', ('id', 'i'), ('ctx', ('Load',))), ('Name (6,72)', ('id', 'x'), ('ctx', ('Load',)))), ('ctx', ('Load',)))))), ('orelse',)), ('Print (7,0)', ('dest', None), ('values', ('Tuple (7,7)', ('elts',), ('ctx', ('Load',)))), ('nl', True)), ('Print (8,0)', ('dest', None), ('values', ('Tuple (8,7)', ('elts',), ('ctx', ('Load',)))), ('nl', True)), ('Print (9,0)', ('dest', None), ('values', ('Tuple (9,7)', ('elts', ('Name (9,52)', ('id', 'x'), ('ctx', ('Load',)))), ('ctx', ('Load',)))), ('nl', True)))) AST 55 MAKECONNECT– Python.g Dr. Philip Cannata 56 OWL Inferencing: A short Primer rdfs:subClassOf owl:FunctionalProperty rdfs:subPropertyOf owl:InverseFunctionalProperty rdfs:domain owl:SymmetricProperty rdfs:range owl:TransitiveProperty owl:inverseOf owl:equivalentClass owl:equivalentProperty owl:disjointWith owl:complementOf owl:sameAs owl:differentFrom owl:someValuesFrom owl:allValuesFrom owl:hasValue Dr. Philip Cannata 57 Inference: Examples owl:FunctionalProperty owl:InverseFunctionalProperty owl:SymmetricProperty owl:TransitiveProperty owl:inverseOf :hasMother rdf:type owl:FunctionalProperty :John :John => :Mary :Maria Dr. Philip Cannata :hasMother :hasMother :Mary :Maria owl:sameAs :Maria owl:sameAs :Mary 58 Inference: Examples owl:FunctionalProperty owl:InverseFunctionalProperty owl:SymmetricProperty owl:TransitiveProperty owl:inverseOf :hasSSN rdf:type owl:InverseFunctionalProperty :John :hasSSN 123-45-6789 :Johny :hasSSN 123-45-6789 => :John owl:sameAs :Johny :Johny owl:sameAs :John Dr. Philip Cannata 59 Inference: Examples owl:FunctionalProperty owl:InverseFunctionalProperty owl:SymmetricProperty owl:TransitiveProperty owl:inverseOf :hasSibling rdf:type owl:SymmetricProperty :John => :Mary Dr. Philip Cannata :hasSibling :Mary :hasSibling :John 60 Inference: Examples owl:FunctionalProperty owl:InverseFunctionalProperty owl:SymmetricProperty owl:TransitiveProperty owl:inverseOf :hasAncestor rdf:type owl:TransitiveProperty :John :Mary => :John Dr. Philip Cannata :hasAncestor :hasAncestor :Mary :Tom :hasAncestor :Tom 61 Inference: Examples owl:FunctionalProperty owl:InverseFunctionalProperty owl:SymmetricProperty owl:TransitiveProperty owl:inverseOf :hasParent owl:inverseOf :hasChild :John => :Mary Dr. Philip Cannata :hasParent :Mary :hasChild :John 62 Inference: Examples owl:equivalentClass owl:equivalentProperty owl:disjointWith owl:complementOf :Male owl:disjointWith :Female :John :Mary => :John :Mary Dr. Philip Cannata rdf:type rdf:type :Male :Female owl:differentFrom owl:differentFrom :Mary :John 63 Inference: Examples owl:equivalentClass owl:equivalentProperty owl:disjointWith owl:complementOf :NonHuman owl:complementOf :Human :Fish => :Fish :Human rdfs:subClassOf :NonHuman owl:disjointWith owl:disjointWith :Human :Fish Dr. Philip Cannata 64 Semantic Web – RDF and RDFS Example Dr. Philip Cannata 65 Semantic Web – RDFS and OWL Example Dr. Philip Cannata 66 Semantic Web – SQL Type Query USA Dr. Philip Cannata 67 Semantic Web – SPARQL Dr. Philip Cannata 68 OWL Inferencing – don’t copy this demo from a PDF, it won’t work – use the PPT file instead -- If you don’t have an RDS_DATA_TABLE in your user account, uncomment the following two lines the first time you run this. -- EXECUTE IMMEDIATE 'CREATE TABLE RDF_DATA_TABLE( id NUMBER, triple SDO_RDF_TRIPLE_S)'; -- SEM_APIS.CREATE_RDF_MODEL('RDF_MODEL_CS345_PROF1', 'RDF_DATA_TABLE', 'triple'); TRUNCATE TABLE "RDF_DATA_TABLE" drop storage; DROP SEQUENCE RDF_DATA_TABLE_SQNC; CREATE SEQUENCE RDF_DATA_TABLE_SQNC START WITH 1 NOCACHE; INSERT INTO RDF_DATA_TABLE VALUES ( RDF_DATA_TABLE_SQNC.nextval, SDO_RDF_TRIPLE_S('RDF_MODEL_CS345_PROF1:<http://www.example.org/people.owl>', '<http://www.example.org/people.owl#OBJECT>', 'rdf:type', 'rdfs:Class')); INSERT INTO RDF_DATA_TABLE VALUES ( RDF_DATA_TABLE_SQNC.nextval, SDO_RDF_TRIPLE_S('RDF_MODEL_CS345_PROF1:<http://www.example.org/people.owl>', '<http://www.example.org/people.owl#ANIMAL>', 'rdf:type', 'rdfs:Class')); Dr. Philip Cannata 69 OWL Inferencing INSERT INTO RDF_DATA_TABLE VALUES ( RDF_DATA_TABLE_SQNC.nextval, SDO_RDF_TRIPLE_S('RDF_MODEL_CS345_PROF1:<http://www.example.org/people.owl>', '<http://www.example.org/people.owl#ANIMAL>', 'rdfs:subClassOf', '<http://www.example.org/people.owl#OBJECT>')); INSERT INTO RDF_DATA_TABLE VALUES ( RDF_DATA_TABLE_SQNC.nextval, SDO_RDF_TRIPLE_S('RDF_MODEL_CS345_PROF1:<http://www.example.org/people.owl>', '<http://www.example.org/people.owl#CAT>', 'rdf:type', 'rdfs:Class')); INSERT INTO RDF_DATA_TABLE VALUES ( RDF_DATA_TABLE_SQNC.nextval, SDO_RDF_TRIPLE_S('RDF_MODEL_CS345_PROF1:<http://www.example.org/people.owl>', '<http://www.example.org/people.owl#CAT>', 'rdfs:subClassOf', '<http://www.example.org/people.owl#ANIMAL>')); INSERT INTO RDF_DATA_TABLE VALUES ( RDF_DATA_TABLE_SQNC.nextval, SDO_RDF_TRIPLE_S('RDF_MODEL_CS345_PROF1:<http://www.example.org/people.owl>', '<http://www.example.org/people.owl#i5>', 'rdf:type', '<http://www.example.org/people.owl#CAT>')); INSERT INTO RDF_DATA_TABLE VALUES ( RDF_DATA_TABLE_SQNC.nextval, SDO_RDF_TRIPLE_S('RDF_MODEL_CS345_PROF1:<http://www.example.org/people.owl>', '<http://www.example.org/people.owl#i5>', '<http://www.example.org/people.owl#value>', '"101"^^xsd:integer')); Commit; Dr. Philip Cannata 70 OWL Inferencing -- named graph local inference (NGLI) BEGIN sem_apis.create_entailment( 'RDF_ENT_CS345_PROF1', models_in => sem_models('RDF_MODEL_CS345_PROF1'), rulebases_in => sem_rulebases('owl2rl'), passes => SEM_APIS.REACH_CLOSURE, inf_components_in => null, options => 'LOCAL_NG_INF=T' ); END; / Dr. Philip Cannata 71 OWL Inferencing select g, s, p, o from table(sem_match('{graph ?g {?s ?p ?o}}', sem_models('RDF_MODEL_CS345_PROF1'),sem_rulebases('owl2rl'),null,null)) MINUS select g, s, p, o from table(sem_match('{graph ?g {?s ?p ?o}}', sem_models('RDF_MODEL_CS345_PROF1'),null,null,null)) Returns: http://www.example.org/people.owl http://www.example.org/people.owl#CAT http://www.w3.org/2000/01/rdf-schema#subClassOf http://www.example.org/people.owl#OBJECT http://www.example.org/people.owl http://www.example.org/people.owl#i5 http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.example.org/people.owl#ANIMAL http://www.example.org/people.owl http://www.example.org/people.owl#i5 http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.example.org/people.owl#OBJECT Dr. Philip Cannata 72 OWL Inferencing SEM_MODELS('RDF_MODEL_CS345_prof1'), null, SEM_ALIASES( SEM_ALIAS('', 'http://www.example.org/people.owl#')), null) ) SELECT value from table( sem_match('select * where { ?indiv rdf:type :CAT. ?indiv :value ?value . }', SEM_MODELS('RDF_MODEL_CS345_prof1'), sem_rulebases('owl2rl'), SEM_ALIASES( SEM_ALIAS('', 'http://www.example.org/people.owl#')), null) ) Returns: Value 101 Dr. Philip Cannata 73 OWL Inferencing SEM_MODELS('RDF_MODEL_CS345_prof1'), null, SEM_ALIASES( SEM_ALIAS('', 'http://www.example.org/people.owl#')), null) ) SELECT value from table( sem_match('select * where { ?indiv rdf:type :ANIMAL. ?indiv :value ?value . }', SEM_MODELS('RDF_MODEL_CS345_prof1'), sem_rulebases('owl2rl'), SEM_ALIASES( SEM_ALIAS('', 'http://www.example.org/people.owl#')), null) ) Returns: Value 101 Dr. Philip Cannata 74 OWL Inferencing SEM_MODELS('RDF_MODEL_CS345_prof1'), null, SEM_ALIASES( SEM_ALIAS('', 'http://www.example.org/people.owl#')), null) ) SELECT value from table( sem_match('select * where { ?indiv rdf:type :OBJECT. ?indiv :value ?value . }', SEM_MODELS('RDF_MODEL_CS345_prof1'), sem_rulebases('owl2rl'), SEM_ALIASES( SEM_ALIAS('', 'http://www.example.org/people.owl#')), null) ) Returns: Value 101 Dr. Philip Cannata 75 Initial Concepts for Dr. Philip Cannata 76 Proof by Contradiction Database P1 P2 1). Let P = It’s raining, I’m outside (comma means “&&”) 2). P1. (P1 is True, i.e., it’s raining) Facts Rule 3). P2. (P2 is True, i.e., I’m outside) 4). Q :- P = I’m wet :- It’s raining, I’m outside. (if it’s raining and I’m outside then I’m wet) (To answer the Query “Am I wet” against the Database, assume I’m not wet) 5). –Q 6). –(It’s raining, I’m outside) 7). –I’m outside 8). Contradiction – Therefore I’m wet ( From 4 and 5 and Pattern 1 ) ( From 2 and 6 and Pattern 2 ) ( From 3 and 7 and Pattern 3 ) Pattern 1 (Modus Tollens): Q :- (P1, P2). -Q -(P1, P2) P Pattern 2 (Affirming a Conjunct): R P1. -(P1, P2) S -P2 Pattern 3: P2. -P2 Contradiction Dr. Philip Cannata 77 Proof by Contradiction 1. A is an Animal. 2. A Barks. 3. A is a Dog :- A is an Animal, A Barks. 4. -(A is a Dog) Query (If you want to know if A is a 5. 3 and 4 and Pattern 1 -(A is an Animal, A Barks) Dog based upon the Facts and Rules in the Database try to see if A is not a Dog.) 6. 1 and 5 and Pattern 2 -A Barks 7. 2 and 6 and Pattern 3 Contradiction 8. Therefore A is a Dog Dr. Philip Cannata Database 78