CSC 6575: Internet Security Fall...

30
Mohammad Ashiqur Rahman Department of Computer Science College of Engineering Tennessee Tech University Prolog Introduction to First Order Logic CSC 6575: Internet Security Fall 2017

Transcript of CSC 6575: Internet Security Fall...

Mohammad Ashiqur Rahman

Department of Computer ScienceCollege of Engineering

Tennessee Tech University

Prolog

Introduction to First Order Logic

CSC 6575: Internet SecurityFall 2017

Logical constants: true, false Propositional symbols: P, Q, S, ... (atomic sentences) Wrapping parentheses: ( … ) Sentences are combined by connectives:

∧ ...and [conjunction]∨ ...or [disjunction]⇒...implies [implication/conditional]⇔..is equivalent [biconditional]¬ ...not [negation]

Literal: atomic sentence or negated atomic sentence

Propositional Logic

P means “It is hot.”

Q means “It is humid.”

R means “It is raining.”

(P ∧ Q) → R “If it is hot and humid, then it is raining”

Q → P “If it is humid, then it is hot”

A better way:Hot = “It is hot”Humid = “It is humid”Raining = “It is raining”

Examples of PL Sentences

First-order logic (FOL) models the world in terms of Objects, which are things with individual identities Properties of objects that distinguish them from other objects Relations that hold among sets of objects Functions, which are a subset of relations where there is only one

“value” for any given “input”

Examples: Objects: students, lectures, companies, cars ... Relations: brother-of, father-of, bigger-than, outside, part-of, has-color,

occurs-after, owns, visits, precedes, ... Properties: blue, oval, even, large, ... Functions: grandparent-of, best-friend, second-half, one-more-than ...

A relations can be identified as a straight-forward function.

First-order logic

Constant symbols, which represent individuals in the world Mary 3 Green

Function symbols, which map individuals to individuals father-of(Mary) = John color-of(Sky) = Blue

Predicate symbols, which map individuals to truth values greater(5,3) green(Grass) color(Grass, Green)

User Provides

Variable symbols E.g., x, y, foo

Connectives Same as in PL: not (¬), and (∧), or (∨), implies (→), if and only if

(biconditional ↔)

Quantifiers Universal ∀x or (Ax) Existential ∃x or (Ex)

FOL Provides

Puzzle!!!

Farmer and his wolf, goat and cabbage A farmer has to cross a river with a wolf, a goat and a

cabbage. He has a boat, but in the boat he can take just one thing. He cannot let the goat alone with the wolf or the goat with the cabbage Why?

What is the solution? We won’t solve this today using Prolog.

Prolog

Programming in Logic Declarative programming

What to do?- Procedural: How to do?

Predicates Rules & facts Facts are always true, while rules are derived relations

Syntax

Syntax: head :- body. ‘:-’ is interpreted as reverse ‘implies’ Head is true if body is true Head consists of a single predicate (or goal) Body contains one or more predicates (or subgoals)

Predicates are joined together using logical conjunction (,) or disjunction (;) operators

It includes variables, constants and operators Variable must start with Capital letter while constant must

start with small letter

Example

elephant(george).elephant(mary).elephant(X) :- grey(X), mammal(X), hasTrunk(X).

Procedure for elephant

Predicate

Clauses

Rule

Facts

Prolog Compiler

Strawberry Prolog http://www.dobrev.com/download.html Light version is free Easy for the beginners

SWI-Prolog http://www.swi-prolog.org/download/stable Unix-like More mature

Extension: *.pl or *.pro

Family RelationJohn is the father of Jim.Jane is the mother of Jim.Jack is the father of John.

Example questions:Who is Jim's father?Is Jane the parent of Fred?Does Jack have a grandchild?

Person 1 is a parent of Person 2 ifPerson 1 is the father of Person 2 orPerson 1 is the mother of Person 2.

Person 1 is a grandparent of Person 2 ifsome Person 3 is a parent of Person 2 andPerson 1 is a parent of Person 3.

- What can be the fact predicates?

- What are the queries ?

- What will be the rules?

Family Relation… Solution

father( john, jim ).mother( jane, jim ).father( jack, john ).

parent( Person1, Person2 ) :- father( Person1, Person2 ).parent( Person1, Person2 ) :- mother( Person1, Person2 ).grandparent( Person1, Person2 ) :-

parent( Person1, Person3) , parent(Person3, Person2 ).

?- father( Who, jim).?- parent( jane, jim).?- grandparent( jack, _).

Facts

Rules

Queries

Prolog first looks in the program what it can find about ancestor. Try the goal parent(john,tom) -- Fail. Try the second clause of ancestor. The new query is parent(john, Z). Find Z=paul and try ancestor(paul,tom)

Check parent(paul,tom). -- Successful. As a result the goal ancestor(paul,tom) succeeds. Then ancestor(john,tom) can

succeed.

Recursion in Prolog

parent(john,paul). /* paul is john's parent */ parent(paul,tom). /* tom is paul's parent */ parent(tom,mary). /* mary is tom's parent */ ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y).

?- ancestor(john,tom).

CALL ancestor(john,tom). CALL parent(john,tom). FAIL parent(john,tom). CALL parent(john,Z).

TRY Z=paulCALL ancestor(paul,tom).

CALL parent(paul,tom). SUCCEEDS parent(paul,tom).

SUCCEEDS ancestor(paul,tom). SUCCEEDS with Z=paul

SUCCEEDS ancestor(john,tom).

Unification with Multiple Variables

You always hurt the ones you love.Politicians love themselves.Therefore, politicians hurt themselves.

¬ love(x,y) ∨ hurt(x,y)love(x,y) -> hurt(x,y)

¬politician(z) ∨ love(z,z)politician(z) -> love(z,z)

politician(z) -> hurt(z,z)¬politician(z) ∨ hurt(z,z)

Unification with Multiple Variables

You always hurt the ones you love.Politicians love themselves.Therefore, politicians hurt themselves.

¬ love(x,y) ∨ hurt(x,y) ¬politician(z) ∨ love(z,z)

¬politician(w) ∨ hurt(w,w)

rename “z” as“w” so that noclauses havevariables withthe samename

More about Unification Can resolve clauses if can unify one pair of literals

Same predicate, one positive, one negative Match variable(s) to other variables, constants, or complex terms

(function symbols) Carry bindings on variables through to all the other literals in the result

(Mortal(HENRY))(¬Mortal(y)∨Fallible(y))(Mortal(y)->Fallible(y))

(Fallible(HENRY))

Loading a file:?- [<file name>].?- consult(<file name>) Example: ?- consult('F:\\Google Drive\\Teaching\\CSC 6575\\Assignment\\Assignment 3\\Test.pl').

Since the fact does not exist in the database. If this is the case, try asserting the fact first: ?- assert(like(x,y)).

SWI-Prolog has an emacs-like editor that you can bring up by typing ‘emacs.’ Then consult (in menu)

Debug and tracing: ‘debug’, ‘trace’, ‘notrace’, ‘nodebug’ Graphical debugger (in menu)

How to Load/Execute in SWI-Prolog?

Some Operatorsis = >= =<

Difference between ‘is’ and ‘=‘?- A is B + 1?- A = B + 1

http://www.swi-prolog.org/pldoc/man?predicate=is/2

Difference between ‘=’ and ‘==‘ The = "operator" in Prolog is actually a predicate (with infix

notation) =/2 that succeeds when the two terms are unified. X = 2 or 2 = X represent the same thing, a goal to unify X with 2.

The == "operator" differs in that it succeeds only if the two terms are already identical without further unification. X == 2 is true only if the variable X had previously been assigned the value 2.

check(A, B):- A = B + 1.

?- check(5, 4).

check(A, B):- A is B + 1.

?- check(5, 4).

Towers of Hanoi

Prolog solution:

move(1,A,_,C).move(N,A,B,C) :-

N>1, M is N-1, move(M,A,C,B), move(1,A,_,C), move(M,B,A,C).

Strategy: Move N disks from peg A to peg C, with peg B being the auxiliary peg

Reachability in Graph

What we have edge

When reachable? If any path!!!

a

b

d

e

c

path(X, Y):- edge(X, T), path(T, Y) path(X, Y):- edge(X, Y).

Reachability in Network A Simple Network

Nodes: host or router Links No firewall

Router should have route entries

Can traffic from a particular node (source) reach another particular node (destination)?

Reachability in Network…

host(h1).host(h2).

router(r1).router(r2).

link(h1, r1).link(r1, r2).link(r2, h2).

routeEntry(r1, h2, r2).routeEntry(r2, h1, r1).

slink(A, B):- link(A, B).slink(A, B):- link(B, A).

forward(N, S, D, T):- T = D, slink(N, D).forward(N, S, D, T):-

N = S, host(N), slink(N, T), router(T).forward(N, S, D, T):-

router(N), routeEntry(N, D, T), slink(N, T).

traceRoute(N, S, D):- N = D.traceRoute(N, S, D):-

forward(N, S, D, T), traceRoute(T, S, D).

reachable(S, D):- traceRoute(S, S, D).

r2r1 h2h1

Reachability in NetworkRules can be written shorter way:

slink(A, B):- link(A, B).slink(A, B):- link(B, A).

forward(N, _, D, D):- slink(N, D).forward(S, S, _, T):- host(S), slink(S, T), router(T).forward(N, _, D, T):- router(N), routeEntry(N, D, T), slink(N, T).

traceRoute(D, _, D).traceRoute(N, S, D):- forward(N, S, D, T), traceRoute(T, S, D).

reachable(S, D):- traceRoute(S, S, D).

If we execute as ‘?- reachable(h1, X). What will we get?

We can here see the use of cut (!)

List Data Structure

[a, b, c] [H|T] = [a, b, c]

H is a, while T is [b, c]

Member– A List Examplemember(H, [H|_]).member(H, [_|T]):- member(H,T).

?- member(c, [b, c, d]).?- member(X, [b, c, d]).

Other Examples

append([], L, L).append([H|T], L, [H|LT]):- append(T, L, LT).

delete(X, [X|T], T). delete(X, [Y|T], [Y|NT]):- delete(X, T, NT).

Assignment 04 Write a program that finds if traffic can reach from a

source host to a destination host, given A network topology that includes hosts, routers, and

firewalls, and IDS (10%) Routing/forwarding policy (20%) Reachability- the traffic path (20%) Firewall access control (40%) IDS access control (20%)

Find whether a traffic is payload-inspected. The traffic passes through an IDS.

Expected Output Example (Partial) Input

Example Output

h1

h4

h3f1r1 r2

h2

Firewall rules:h1 h3 denyh1 * allowh3 h1 allowh3 * denyh4 * allow* * deny

?- reachable(h1, h3, P).false.

?- reachable(h1, h4, P).P = [h1, r1, f1, r2, h4] .

THANKS

Sources:- http://www.cs.swarthmore.edu/~eeaton/teaching/cs63/slides/Logic.ppt- http://www.arc.uncc.edu/~ehab/Courses/ITIS6167/PDF/12-logic-programming.pdf