180 likes | 513 Vues
First Order Logic. Logic is a mathematical attempt to formalize the way we think. First-order predicate calculus was created in an attempt to mechanize reason. First Order Logical Representations. All zombies eat brains.
E N D
First Order Logic • Logic is a mathematical attempt to formalize the way we think. • First-order predicate calculus was created in an attempt to mechanize reason.
First Order Logical Representations • All zombies eat brains. • This assumption can be represented in propositional logic by the propositional variable p. • In first order logic, the expression can be broken down • ∀X (zombie(X) →eatsbrains(X)) • ∀X (zombie(X) → eats(X, brains)) • ∀X ∃Y (zombie(X) →eats(X, Y) ^ brains(Y)) • Some people with no heartbeats are zombies. • ∃X (person(X) ^ zombie(X) ^ ¬ heartbeat(X))
Natural Deductive Rules • Natural deductive rules are logical rules accepted as being intuitively true. • Universal Instantiation: If a variable is assigned a value, it will always have that value. • If X = joe, ∀X (zombie(X) →eatsbrains(X)) is the same as (zombie(joe)→eatsbrains(joe)) • Modus Ponens: For some pair of expressions p and q, p ^ (p →q) ≡ q. • If zombie(X), (zombie(X) →eatsbrains(X)) becomes (eatsbrains(X))
Truth Tables • Truth tables can be constructed from first order logic by transforming expressions into atomic formulas (something with value true or false). • Atomic formulas can be constructed from first order logic by either quantifying a formula or mapping it to a value (analogous to valuation). • Interpretations are analogous to truth tables.
Truth Tables zombie(joe) zombie(X) eatsbrains(joe) eatsbrains(X) ... 0 ? 0 ? 1 ? 0 ? By mapping (valuation) of X to fred zombie(joe) zombie(fred) eatsbrains(joe) eatsbrains(fred) 0 0 0 0 1 0 0 1
Brief Intro: Herbrand Base • The Herbrand base is the set of ground atomic formulas. • In the previous example, this is the set {zombie(joe), zombie(fred), eatsbrains(joe), eatsbrains(fred)}. • A model is satisfiable if and only if it has a Herbrand Interpretation (a valid truth assignment given its Herbrand base).
Function Symbols • Function symbols map an individual in the domain to another in the domain. • Everyone loves their mother. • In the expression ∀X loves(X, mother(X)), mother(X) maps X to the element in the domain. • Peano's Axioms for Arithmetic • In the expression ∀X (number(X)→ number(S(X)), the occurance of number in number(X) is a function symbol. In the second occurrence, number is a predicate and S(X) is a function symbol.
Relational Databases • Assume there is a relational database with a patient table, visit table, and a prescription table. • The patient table has fields ID, Date of Birth, and Gender. • The visit table has fields ID, Visit Date, and Diagnosis Code. • The prescription table has fields ID, Date, and Drug.
Relational Databases Patient Visit Prescription
Relational Databases • In a relational database, the table can be represented by an atomic formula of a predicate objects composed of constants and/or function symbols. One such formula exists for each patient. • Patient table: patient(id, date of birth, gender) • Visit table: visit(id, date, diagnosis code) • Prescription table: prescription(id, date, drug)
Relational Databases • Any query can be represented as a formula. • To find all male patients with diagnosis code 111, use the query formula∀P (interest(P)←patient(P, B, male) ^ visit(P, D, 111) • The query result can define a new table containing the desired data. • Extensional: Write out entries in a table. • Intensional: Write out tables via formula
First Order Logical Inference-Datalog • Datalog is a syntactic variation on relational database queries. It forms an intensional representation to the query equivalent to those formed by relational algebra. • All quanitifiers are assumed universal and out front of the formula. All formulas are atomic or implications. No function symbols are used. (interest(P)←patient(P, B, male), visit(P, D, 111)
Queries in Datalog • Datalog queries can be called with a goal.male-flu(P, D)←(patient(P, B, male), visit(P, D, 111) • Goals can be satisfied by finding all bindings on the goal variables and using pattern matching. • The process of matching all atomic formulas by binding the variables to force matching is known as Unification.
Queries in Datalog • Unification is most frequently executed by running a backtracking search for proofs satisfying all subgoals. • Running unification on the male-flu example, if there exists patient(1, 1-1-01, male) and visit(1, 3-3-03, 111), there will be a new row binding in the resulting male-flu table with P=1, D=3-3-03.
Prolog • All formulas have the format: atomic formula ← atomic formula [, atomic formula(s)]written in Prolog as: atomic formula :-atomic formula [, atomic formula(s)]. • Prolog uses only universally quantified variables. • Some arithmetic functions are allowed, such as W is X*Y • Specification of correct output is allowed.
Prolog Lists • cons(H, T) constructs the list with H as the head and T as the tail. This is equivalent to [H|T].[1,2,3]=[1|[2, 3]]=[1|2|3|nil]]]=cons(1, cons(2, cons(3, nil))) where nil is the empty list. • All functions are evaluated based on unification. • Conventionally, arguments to a Prolog expression are given with inputs first, then outputs. • Prolog is Turing-complete so long as function symbols are allowed.
Defining Prolog Functions • Member function: member(H, [H|T]).member(H, [S|T]) ← member(H,T). • Insertion Sort:sort([ ], [ ]).sort([H|T], L) ← sort(T, T2), insert(H, T2, L).insert(H, [A|T], [H,A|T] ← H < A.insert(H, [A|T], [A|L]) ← H >= A, insert(H, T, L).
Running Prolog • On CSL machines, Prolog is run via the yap interpreter. • To load a file into the interpreter: ?- consult('filename'). • Once a file has been loaded, definitions contained in that file may be executed. The code will then be run top down until a solution is found and then bound to the output. ?- sort([5, 1, 10, 3, 9], X). %Bind sorted list to X