200 likes | 356 Views
Declarative Languages. Declarative languages. In an imperative programming language, a program specifies how to solve a problem In a declarative language, a program specifies what needs to be solved The language itself needs to provide a way to find the solution
E N D
Declarative languages • In an imperative programming language, a program specifies how to solve a problem • In a declarative language, a program specifies what needs to be solved • The language itself needs to provide a way to find the solution • Sometimes called rule-based languages • Examples • Prolog (logic programming) • MYCIN (Expert systems) • SQL (databases)
Logic Programming • Non-procedural – program specifies the desired result, not how to get it • Program consists of a set of data and a desired outcome • Logical inference is used to get the result • Prolog is the primary example of a logical language
Prolog • program consists of a collection of statements • each statement is constructed from terms • a term can be a constant, a variable or a structure • a constant is an atom (a specific object) or a number • a variable can refer to different objects at different times • a structure is an atomic proposition used to specify a facts (relations between objects) • functor( parameter list)
Prolog Statements • Several forms of statements occur in a Prolog program. • Fact – a headless Horn clause such as male( joe) • Rule – consequent := antecedent (The antecedent must be either a single term or a conjunction which is a sequence of terms separated by commas in Prolog.) Rules may be specific to particular objects or general like female(X ) ⇐ mother(X ) • Goal (query) – the desired outcome, what needs to be proved by the program. Programs may have compound goals; each part is a subgoal. Goals look similar to facts and rules; they are entered in a different mode.
Example Facts: father( john) father( bob) Rules: man( x) := father( x) Query: man( bob)
Rule-Based Languages • Rule-based languages are used for Expert Systems (Artificial Intelligence) • OPS5 • ART • CLIPS • Mathematica and Maple are rule-based • The make utility uses a rule-based approach
What is make • make is a utility that comes with the Unix (and Linux) operating system • make is a command generator • make is designed to help you compile large projects • make can be useful whenever you have long sequences of commands which are needed to accomplish a particular task or set of related tasks • make is non-procedural • you tell make what you want (e.g. a particular class file or executable • you provide a set of rules showing dependencies between files • make uses the rules to get the job done
make • make uses a file called makefile to determine what needs to be recompiled. • The makefile contains a set of rules for executing the jobs it can be asked to do. • When you run make, it uses the rules in the makefile to determine what has to be done. • make does the minimum amount of work needed to get the job done.
The make utility is rule-based • A makefile contains a set of rules explaining what needs to be done to compile the program. • When you type make or make target, the make utility will look at the rule for the desired target. make without a target name will assume you mean to make the first real target in the makefile. • If the current version of the target file happens to be up-to-date, make will do nothing. • If the target file is older than any of the files that it depends on, or any of those files is older than a file it depends on, make will invoke whatever rules are needed to get everything up-to-date.
Rules in a makefile • A typical rule has the form target: dependency list command list • target can be the name of a file that needs to be created • the dependency list is a space separated list of files needed to create the target • the command list is one or more linux commands needed to accomplish the task of creating the target file • The target need not be a real file clean: rm --force *.class
How does make work? • When you type make or make target, the make utility will look at the rule for the desired target. • make will resursively search through the rules for all the dependencies to determine what has been modified • If the current version of all the dependency files happen to be up-to-date, make will do nothing. • If a target file is older than any of the files that it depends on, the command following the rule will be executed • make without a target name will assume you mean to make the first real target in the makefile.
Example: the Interpreter • The interpreter you are working on has (at least) the following classes or modules. • Interpreter – the main program or driver class • Evaluator – a module to do the evaluation • Environment – a data structure for storing a program’s state • Parser – a module for recognizing and parse-tree generation • Lexer – input module • Lexeme – data structure for storing the lexical units of the program • Token • Every time you change one of the source files, you need to recompile at least that file. But how many of the others need to be recompiled? Do you do all of them every time you make a change?
Rules for the Interpreter Interpreter.class: Interpreter.java Environment.class \ Parser.class Lexer.class Lexeme.class Token.class javac Interpreter.java Environment.class: Environment.java Lexeme.class \ Token.class javac Environment.java Parser.class: Parser.class Lexer.class Lexeme.class \ Token.class javac Parser.java Lexer.class: Lexer.java Lexeme.class Token.class javac Lexer.java Lexeme.class: Lexeme.java Token.class javac Lexeme.java
Macros • Sometimes, you find yourself using the same sequence of command line options in lots of commands. • We’ll use a C example • You can define a macro with an assignment CC=gcc -O -Wall -g • then you use the macro by typing $(macroname) $(CC) -c ipl.c
Internal macros • % - wildcard • $@ – the name of the current target • $< – the list of dependencies for the current target
Substitution Rules • Often, you will find that your makefile has many similar commands • You can use patterns to define rules and commands for such cases • For example, in the java version of the makefile, we could use the rule %.class: %.java javac $< which says that every .class file depends on the corresponding .java file and can be created from it with the javac command
It is common to put some dummy targets into a make file doc: html javadoc –author -d html/ *.java clean: rm --force *.class
Information about make • Managing Projects with make by Andrew Oram and Steve Talbot (an O’Reilly book) • http://www.delorie.com/gnu/docs/make/make_toc.html • The man page man make