GithubHelp home page GithubHelp logo

posd2017's Introduction

Pattern Oriented Software Design

Fall, 2017

Prof Y C Cheng

Dept of Computer Science and Information Engineering

Taipei Tech

Introduction

We will build a Prolog term matching program in this course. Functionally, the program is simple but non-trivial. Thus, we will have plenty of opportunities to encounter design problems. After analyzing the design problems, we will make use of appropriate design patterns to solve them. The patterns include Composite, Interpreter, Builder, Iterator, Proxy, Visitor, and so on. Along the way we will also pick up some useful domain knowledge of computing: symbolic matching, lexical analysis, and parsing.

Thus the course requires you to get familiar with matching, the fundamental operation of executing a Prolog program. Due to time limitation, we will focus only on defining terms and performing matches. We build our simplified term matching program after SWI Prolog.

I will be using the following simple way to write C++ programs. My programs (and your programs will, too) come with a makefile that builds it by typing make in bash. We will use the g++ stack. While you are free to code on any OS platform (e.g., Ubuntu, MacOS, and bash on Ubuntu on Windows), your program assignment will be graded on Ubuntu.

When coding in class, I will use the editor Atom, which comes with syntax highlighting, code completion to make coding easy. I will also use the plugin PlatformIO IDE Terminal so that we can access a terminal to build programs without leaving Atom.

Prolog basics - goal, relational goals, Conjunction of goals, disjunction of goals,

A query consists of one or more goal:

?- X=1.
X = 1.

is a query "is X matchable to 1?" consisting of the relational goal "X=1". The goal "X=1" succeeded (or is satisfiable) because variable X matches any legitimate term.

?- X=1, Y=X.
X = Y, Y = 1.

is a query "is X matchable to 1 and is Y matchable to X?" that a conjunction of two goals "X=1" and "Y = X". The goal succeeded.

?- X=1, X=2.
false.

is a query with a negative answer because the conjunction of goals failed or is unsatisfiable.

?- X=1; X=2.
X = 1 ;
X = 2.

is a query with a positive answer because the disjunctions succeeded in succession. Note that the ";" after "X=1" is typed by the user to query for more answers; the query terminates if the return key is hit.

Data objects in Prolog

Prolog comes four types of data objects: atom, number (collectively known as constant), variable (forming simple object with constant), and structure. For a simple description of syntax, see Data objects in Prolog.

Atom and number are self-identifying: thus wherever you see it, the atom 'tom' will always be tom, and the number 1 will always be 1. The value of an atom is exactly the symbol of the atom.

Variable brings universal quantification to Prolog. Variable X becomes instantiated when it is matched with a constant. This

X = 1

gives the variable with symbol X the value of 1.

A structure is defined by a name and its arguments. The name has a syntax of an atom, and an argument can be any data object including structure. Structure makes it possible to describe arbitrary complex objects through composition of other objects.

point(1,1)
triangle(point(1,1), point(0,0), point(0,1))

where point(1, 1) is a structure with name point and arguments 1 and 1, etc.

Rules of terms matching (Bratko p.41)

Let S and T be two terms.

  • If S and T are constants then S and T match only if they are the same object.

  • If S is a variable and T is anything, then they match, and S is instantiated to T. Conversely, if T is a variable then T is instantiated to S.

  • If S and T are structures then they match only if

    • S and T have the same principal functor, and
    • all their corresponding components match.

    The result of the instantiation is determined by the matching of the components.

List

?- X = [1, 2, 3].
X = [1, 2, 3].

?- X = .(1,[]).
X = [1]

?- [1, 2] = .(1, .(2, [])).
true.

?- [1,2,3] = [H|L].
H = 1,
L = [2, 3]

?- [1,2,3] = .(H, L).
H = 1,
L = [2, 3]

Lexical analysis and parsing

lexical analyzer, or token scanner (Dragon book, p71, Fig 2.37)

lexeme token attribute value
sequence of digits Number numeric value of sequence
small letter followed by alphanumeric Atom index into symbol
cap letter or '_' followed by alphanumeric Var index into symbol

term -> atom | number | var | struct | list

struct -> atom '(' terms ')'

list -> '[' terms ']'

terms -> term, terms | e

atom -> Atom

number -> Number

var -> Var

rewritten to

terms -> term rest| e rest -> ',' term rest | e

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.