prolog - university of calgary in...

1
02/23/2010 CPSC 449 Computer Science Unless otherwise noted, all artwork and illustrations by either Rob Kremer or Jörg Denzinger (course instructors) Prolog Structuring Programs 306 2015/01/05 CPSC 449 Structuring programs While in theory the rules (clauses) with a particular predicate as head can be distributed over several files that are consulted by the interpreter to load everything required by a program, naturally the sequence in which these rules end up in the data base will have a big impact on the program Therefore it is a good practice to cluster the rules with a particular head in one file and also within this file • GNU Prolog forces clustering of rules (except if you use the directives discontiguous/1 or multifile/1) 307 2015/01/05 CPSC 449 PROLOG program structure There really is not much of a structure required in PROLOG programs, most of the structure we see in books or real programs is voluntary by the programmer In most PROLOG versions there are -No modules -No types and type declarations -No particular layout The only real conventions are that every rule ends with a ".", that atoms are separated by "," and that ";" indicates an "or" in the body of a rule 308 02/23/2010 CPSC 449 Computer Science Unless otherwise noted, all artwork and illustrations by either Rob Kremer or Jörg Denzinger (course instructors) Prolog Executing Programs 309 2015/01/05 CPSC 449 Executing programs PROLOG is usually an interpreted language, although there are compilers that compile in bytecode for the Warren Abstract Machine (Java got the idea of the Java Virtual Machine from there) GNU Prolog is compiled for WAM. A PROLOG program is usually loaded into the interpreter's data base using the consult/1 predicate consult/1 expects as argument a file name and the exact format depends on the machine the interpreter runs on (on our machines you need consult('myfile').) 310 2015/01/05 CPSC 449 Executing programs GNU Prolog can be “scripted” by adding the line: #!/opt/local/bin/gprolog —consult-file as the first line of the Prolog file. • “#” is treated as a comment if it’s the first line of a file. • UNIX interprets the file with the “shebang” protocol. 311

Upload: others

Post on 15-Feb-2021

3 views

Category:

Documents


0 download

TRANSCRIPT

  • 02/23/2010 CPSC 449

    Computer Science

    Unless otherwise noted, all artwork and illustrations by either Rob Kremer or Jörg Denzinger (course instructors)

    PrologStructuring Programs

    306

    2015/01/05 CPSC 449

    Structuring programs

    ■While in theory the rules (clauses) with a particular predicate as head can be distributed over several files that are consulted by the interpreter to load everything required by a program, naturally the sequence in which these rules end up in the data base will have a big impact on the program ■Therefore it is a good practice to cluster the rules

    with a particular head in one file and also within this file • GNU Prolog forces clustering of rules (except if you use

    the directives discontiguous/1 or multifile/1)

    307

    2015/01/05 CPSC 449

    PROLOG program structure

    ■There really is not much of a structure required in PROLOG programs, most of the structure we see in books or real programs is voluntary by the programmer ■ In most PROLOG versions there are

    -No modules -No types and type declarations -No particular layout

    ■The only real conventions are that every rule ends with a ".", that atoms are separated by "," and that ";" indicates an "or" in the body of a rule

    308

    02/23/2010 CPSC 449

    Computer Science

    Unless otherwise noted, all artwork and illustrations by either Rob Kremer or Jörg Denzinger (course instructors)

    PrologExecuting Programs

    309

    2015/01/05 CPSC 449

    Executing programs

    ■PROLOG is usually an interpreted language, although there are compilers that compile in bytecode for the Warren Abstract Machine (Java got the idea of the Java Virtual Machine from there)

    ■GNU Prolog is compiled for WAM.

    ■A PROLOG program is usually loaded into the interpreter's data base using the consult/1 predicate ■consult/1 expects as argument a file name and the

    exact format depends on the machine the interpreter runs on (on our machines you need consult('myfile').)

    310 2015/01/05 CPSC 449

    Executing programs

    ■GNU Prolog can be “scripted” by adding the line:
#!/opt/local/bin/gprolog —consult-file
as the first line of the Prolog file. • “#” is treated as a comment if it’s the first line of a file. • UNIX interprets the file with the “shebang” protocol.

    311