🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

lec1_Prolog Fundamentals.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

Prolog Fundamentals By Hiba Ali Nasir Prolog Fundamentals What is prolog Declarative and Procedural Programming Predicate logic Knowledge Representation Facts Rules From natural language to prolog syntax Basic prolog sections Variables Constan...

Prolog Fundamentals By Hiba Ali Nasir Prolog Fundamentals What is prolog Declarative and Procedural Programming Predicate logic Knowledge Representation Facts Rules From natural language to prolog syntax Basic prolog sections Variables Constants comment examples What is prolog Prolog is a declarative Prolog program isn't a sequence of actions it's a collection of facts together with rules for drawing conclusions from those facts. Programming in logic Prolog is based on clauses, which are a subset of a formal system called predicate logic. What is prolog Predicate logic ` Predicate logic was developed to easily convey logic-based ideas into a written form. Prolog takes advantage of this syntax to develop a programming language based on logic. In predicate logic, you first eliminate all unnecessary words from your sentences. You then transform the sentence, placing the relationship first and grouping the objects after the relationship. The objects then become arguments that the relationship acts upon What is prolog Prolog uses a simplified variation of predicate logic syntax because it provides an easy-to-understand syntax very similar to natural language, Prolog includes an inference engine (pattern matcher) In Prolog, you arrive at solutions by logically inferring one thing from something already known. What is prolog In the predicate logic that Prolog uses, a relation is summarized in a simple phrase- -a fact--that consists of the relation name followed by the object or objects (enclosed in parentheses). As with a sentence, the fact ends with a period (.) Knowledge Representation Facts Facts are relations or properties that must to be true a fact represents one single instance of either a property of an object or relation between objects. Facts have the general form: Property(object1) Or Relation(object1,object2). From Natural Language to Prolog Syntax example: the following sentences are transformed into predicate logic syntax: Natural Language Predicate Logic A car is fun fun(car) A rose is red. red(rose). Bill likes a car if the car is fun. likes(bill, Car) if fun(Car). From Natural Language to Prolog Syntax The symbolic name of a relation is called the predicate name. The objects that it relates are called its arguments; Prolog's Basic Program Sections: Predicates clauses goal predicates section The predicates section is where you declare your predicates and the domains (types) of the arguments to your predicates. Predicate names must begin with a lower case letter followed by a sequence of letters, digits, and underscores, up to 250 characters long. You can't use spaces, the minus sign, asterisks, or slashes in predicate names. Predicate declarations are of the form Predicate name(argtype1,argtype2,…,argtypeN) clauses section The clauses section is where you put the facts and rules that Visual Prolog will operate on when trying to satisfy the program's goal. Goal section The goal section is where you put your program's query; Goals type: 1-Simple goal 2-Compound goal Program's internal goal: allows the program to run independent of the development environment. Program's external goal : allows the program to run in the Dialog window at run time domains section The domains section is where you declare any non standard domains you're using for the arguments to your predicates. Domains in Prolog are like types in other languages. Prolog's basic standard domains are char, byte, short, ushort, word, integer, unsigned, long, ulong, dword, real, string, and symbol. The basic domain declarations are of the form: Argumenttype1= standard domain Basic Standard Domains char A character, implemented as an unsigned byte. Syntactically, it is written as a character surrounded by single quotation marks: 'a'. Real A floating-point number, implemented as 8 bytes string A sequence of characters, Two formats are permitted for strings: 1. a sequence of letters, numbers and underscores, provided the first character is lower-case; or 2. a character sequence surrounded by a pair of double quotation marks. Symbol A sequence of characters. The syntax is the same as for strings. The Constants Section You can declare and use symbolic constants in your Visual Prolog programs. A constant declaration section is indicated by the keyword constants, followed by the declarations themselves, using the following syntax: = Ex: CONSTANTS zero = 0 name = “John” x =2 pi = 3.141592653 The Fact Section Visual Prolog includes a special section for declaring the facts in the program that are to be a part of the dynamic (or changing) database. Variables Variables enable you to write general facts and rules and ask general questions. Variable names in Visual Prolog must begin with a capital letter or an underscore character (_), after which you can use any number of letters (upper-case or lower-case), digits, or underscores. Variables in Prolog get their values by being matched to constants in facts or rules. Until it gets a value, a variable is said to be free; when it gets a value, it becomes bound. Anonymous variables If you only need certain information from a query, you can use anonymous variables to ignore the values you don't need. In Prolog, the anonymous variable is represented by a lone underscore (_). constant Constant always small letters. Example x omer ,why ,”OMER” Comments Comments make your programs easier to read; you can enclose a comment with delimiters or precede a single-line comment with a percent sign, % like this.

Use Quizgecko on...
Browser
Browser