Carlos Varela Rennselaer Polytechnic Institute September 10, 2007

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

CSA2050: DCG I1 CSA2050 Introduction to Computational Linguistics Lecture 8 Definite Clause Grammars.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Prolog COMP 112 #
Lesson 4: Formatting Input Data for Arithmetic
TCP1211-Logic Programming Control and Side Effects Programming Faculty of Information Technology Multimedia University.
PROLOG I/O I/O and the relational model. PROLOG I/O operators. –Not really predicates. I/O with characters. I/O with terms. I/O with lists. I/O.
Context-Free Grammars Lecture 7
Review for midterm exam Dilshad M. Shahid Spring NYU.
1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
C. Varela1 Logic Programming (PLP 11.3) Prolog Imperative Control Flow: Backtracking Cut, Fail, Not Carlos Varela Rennselaer Polytechnic Institute September.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Grammars and Parsing. Sentence  Noun Verb Noun Noun  boys Noun  girls Noun  dogs Verb  like Verb  see Grammars Grammar: set of rules for generating.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
P-Phunck - Prolog1 Introduction to Prolog Brian Paden.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
1 Lecture Expert Systems &. 2 Operator Notation A programmer can define new operators by inserting into the program special kinds of clauses,
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Type Conversions Implicit Conversion Explicit Conversion.
Parsing. Language A set of strings from an alphabet which may be empty, finite or infinite. A language is defined by a grammar and we may also say that.
Introduction to Computer Programming
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 4: Computation 1 Based on slides created by Bjarne Stroustrup.
The Functions and Purposes of Translators Syntax (& Semantic) Analysis.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
1 Using Yacc. 2 Introduction Grammar –CFG –Recursive Rules Shift/Reduce Parsing –See Figure 3-2. –LALR(1) –What Yacc Cannot Parse It cannot deal with.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
C. Varela1 Logic Programming (PLP 11, CTM 9.2, 9.4, ) Constraint Satisfaction Problems, Natural Language Parsing (Definite Clause Grammars) Carlos.
C. Varela1 Logic Programming (PLP 11) Predicate Calculus, Horn Clauses, Clocksin-Mellish Procedure Carlos Varela Rennselaer Polytechnic Institute November.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
C. Varela1 Logic Programming Prolog: Arithmetic, Equalities, Operators, I/O. (PLP 11) Databases: assert, retract. (CTM 9.6) Carlos Varela Rennselaer Polytechnic.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Parsing and Code Generation Set 24. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program,
GRAMMARS & PARSING. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program, referred to as a.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
Chapter 2 Syntax and meaning of prolog programs Part 1.
More yacc. What is yacc – Tool to produce a parser given a grammar – YACC (Yet Another Compiler Compiler) is a program designed to compile a LALR(1) grammar.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Chapter Three: Operators, Arithmetic 1. Chapter three: 3.3Operator notation 3.4Arithmetic 2.
C. Varela1 Logic Programming (PLP 11, CTM 9.3) Prolog Imperative Control Flow: Backtracking, Cut, Fail, Not Lists, Append Carlos Varela Rennselaer Polytechnic.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
Artificial Intelligence Programming in Prolog
Carlos Varela Rennselaer Polytechnic Institute November 18, 2016
Grammars and Parsing.
Input and Output Upsorn Praphamontripong CS 1110
Introduction to Parsing (adapted from CS 164 at Berkeley)
Variables, Expressions, and IO
Carlos Varela Rensselaer Polytechnic Institute November 17, 2017
Carlos Varela Rensselaer Polytechnic Institute November 21, 2017
PROLOG.
Carlos Varela Rensselaer Polytechnic Institute November 28, 2017
Introduction to C++ Programming
Programming Funamental slides
Course websites CS201 page link at my website: Lecture slides
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Rocky K. C. Chang September 18, 2018 (Based on Zelle and Dierbach)
Python Primer 1: Types and Operators
CS150 Introduction to Computer Science 1
Carlos Varela Rennselaer Polytechnic Institute November 22, 2016
Chapter 2 Syntax and meaning of prolog programs
Carlos Varela Rennselaer Polytechnic Institute November 15, 2016
Carlos Varela Rennselaer Polytechnic Institute August 30, 2007
Carlos Varela Rennselaer Polytechnic Institute September 6, 2019
Presentation transcript:

Carlos Varela Rennselaer Polytechnic Institute September 10, 2007 Logic Programming (PLP 11.3) Prolog: Arithmetic, Equalities, Operators, I/O, Natural Language Parsing Carlos Varela Rennselaer Polytechnic Institute September 10, 2007 C. Varela

Arithmetic Goals N>M N<M N=<M N>=M N and M must be bound to numbers for these tests to succeed or fail. X is 1+2 is used to assign numeric value of right-hand-side to variable in left-hand-side. C. Varela

Loop Revisited natural(1). natural(N) :- natural(M), N is M+1. my_loop(N) :- N>0, natural(I), I=<N, write(I), nl, I=N, !, fail. Also called generate-and-test. C. Varela

= is not equal to == or =:= X=Y X\=Y test whether X and Y can be or cannot be unified. X==Y X\==Y test whether X and Y are currently co-bound, i.e., have been bound to, or share the same value. X=:=Y X=\=Y test arithmetic equality and inequality. C. Varela

More equalities X=@=Y X\=@=Y test whether X and Y are structurally identical. =@= is weaker than == but stronger than =. Examples: a=@=A false A=@=B true x(A,A)=@=x(B,C) false x(A,A)=@=x(B,B) true x(A,B)=@=x(C,D) true C. Varela

More on equalities X==Y  X=@=Y  X=Y but not the other way (). If two terms are currently co-bound, they are structurally identical, and therefore they can unify. Examples: a=@=A false A=@=B true x(A,A)=@=x(B,C) false x(A,A)=@=x(B,B) true x(A,B)=@=x(C,D) true C. Varela

Prolog Operators :- op(P,T,O) declares an operator symbol O with precedence P and type T. Example: :- op(500,xfx,’has_color’) a has_color red. b has_color blue. then: ?- b has_color C. C = blue. ?- What has_color red. What = a. C. Varela

Operator precedence/type Precendence P is an integer: the larger the number, the less the precedence (ability to group). Type T is one of: T Position Associativity Examples xfx Infix Non-associative is xfy Right-associative , ; yfx Left-associative + - * / fx Prefix ?- fy xf Postfix yf C. Varela

Testing types atom(X) tests whether X is an atom, e.g., ‘foo’, bar. integer(X) tests whether X is an integer; it does not test for complex terms, e.g., integer(4/2) fails. float(X) tests whether X is a float; it matches exact type. string(X) tests whether X is a string, enclosed in `` … ``. C. Varela

Prolog Input seeing(X) succeeds if X is (or can be) bound to current read port. X = user is keyboard (standard input.) see(X) opens port for input file bound to X, and makes it current. seen closes current port for input file, and makes user current. read(X) reads Prolog type expression from current port, storing value in X. end-of-file is returned by read at <end-of-file>. C. Varela

Prolog Output telling(X) succeeds if X is (or can be) bound to current output port. X = user is screen (standard output.) tell(X) opens port for output file bound to X, and makes it current. told closes current output port, and reverses to screen output (makes user current.) write(X) writes Prolog expression bound to X into current output port. nl new line (line feed). tab(N) writes N spaces to current output port. C. Varela

I/O Example browse(File) :- seeing(Old), /* save for later */ see(File), /* open this file */ repeat, read(Data), /* read from File */ process(Data), seen, /* close File */ see(Old), /* prev read source */ !. /* stop now */ process(end_of_file) :- !. process(Data) :- write(Data), nl, fail. C. Varela

Natural Language Parsing (Example from "Learn Prolog Now Natural Language Parsing (Example from "Learn Prolog Now!” Online Tutorial) word(article,a). word(article,every). word(noun,criminal). word(noun,'big kahuna burger'). word(verb,eats). word(verb,likes). sentence(Word1,Word2,Word3,Word4,Word5) :- word(article,Word1), word(noun,Word2), word(verb,Word3), word(article,Word4), word(noun,Word5). C. Varela

Parsing natural language Definite Clause Grammars (DCG) are useful for natural language parsing. Prolog can load DCG rules and convert them automatically to Prolog parsing rules. C. Varela

DCG Syntax --> DCG operator, e.g., sentence-->subject,verb,object. Each goal is assumed to refer to the head of a DCG rule. {prolog_code} Include Prolog code in generated parser, e.g., subject-->modifier,noun,{write(‘subject’)}. [terminal_symbol] Terminal symbols of the grammar, e.g., noun-->[cat]. C. Varela

Natural Language Parsing (example rewritten using DCG) sentence --> article, noun, verb, article, noun. article --> [a] | [every]. noun --> [criminal] | ['big kahuna burger']. verb --> [eats] | [likes]. C. Varela

Exercises How would you translate DCG rules into Prolog rules? PLP Exercise 11.26 (pg 655). *PLP Exercise 11.30 (pg 655). C. Varela