Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Prolog Commenting, Study notes of Programming Languages

Prolog Commenting. Basics. Prolog supports both multi‑line and single‑line comments. Multi‑line comments use C‑style comment delimiters:.

Typology: Study notes

2021/2022

Uploaded on 09/27/2022

manager33
manager33 🇬🇧

4.4

(34)

241 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
You are logged in as STEPHEN EDWARDS ( LOGOUT )
Home CS 5314 F14 General Prolog commenting
CS 5314 Programming Languages
Prolog Commenting
Basics
Prolog supports both multi‑line and single‑line comments.
Multi‑line comments use C‑style comment delimiters:
Note that SWI‑Prolog allows
/*...*/
delimiters to be nested inside each other, something
that C (and the Prolog ISO standard) do not. You're welcome to write nested multi‑line
comments in class, however, since we are using SWI‑Prolog.
For single‑line comments, Prolog uses the percent sign (
%
) as the comment delimiter:
Documenting Predicates
For documenting predicates, we will use a Prolog adaptation of Javadoc‑style comments.
Place the comment for a predicate above the first fact or rule for that predicate:
First, notice that we cannot determine the argument names, argument types, or parameter
modes just by looking at the facts and rules defining the predicate, since these are not
explicitly declared in Prolog. As a result, the predicate documentation must begin with one
a
declaration header
that defines the argument names, types, and modes. This declaration
header includes one or more lines that show alternative
instantiation patterns
for a
predicate‑‑that is, indicating different ways the predicate may be called with variables or
with bound values in different parameter positions. Each line should use the same formal
parameter names for the arguments appearing in the predicate. Arguments can also
include a colon (:) and type specifier to indicate the expected type of that parameter.
Typically, arguments are usually one of the following:
+ArgName
A
bound
value, which is often considered an
incoming
value ("in" mode) provided by the
caller.
-ArgName
/* This is
a multiline
comment. */
% This is a single line comment.
/**
* concat(+List1 : list, ?List2 : list, ?List3 : list).
* concat(?List1 : list, ?List2 : list, +List3 : list).
*
* Succeeds if the third list is the concatenation of the first two.
*
* @param List1 The first (left) list to join.
* @param List2 The second (right) list to join.
* @param List3 The list containing all the elements of List1
* followed by all the elements of List2.
*/
concat([], List, List).
concat([Head | Tail], List, [Head | Rest]) :-
concat(Tail, List, Rest).
NAVIGATION
Home
My profile
Current course
CS 5314 F14
Participants
Reports
General
My courses
SETTINGS
Page module
administration
Course administration
Switch role to...
My profile settings
Site administration
S
ea
r
c
h
My home
Piazza course
discussion area
CS 5314
Syllabus
Grades
News forum
Prolog
resources
Prolog
commenting
Program
grading criteria
Login to Web‑
CAT
1
2
3
1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
pf3
pf4

Partial preview of the text

Download Prolog Commenting and more Study notes Programming Languages in PDF only on Docsity!

You are logged in as STEPHEN EDWARDS ( LOGOUT )

Home ⟩ CS 5314 F14 ⟩ General ⟩ Prolog commenting

CS 5314 Programming Languages

Prolog Commenting

Basics

Prolog supports both multi‑line and single‑line comments.

Multi‑line comments use C‑style comment delimiters:

Note that SWI‑Prolog allows /.../ delimiters to be nested inside each other, something

that C (and the Prolog ISO standard) do not. You're welcome to write nested multi‑line

comments in class, however, since we are using SWI‑Prolog.

For single‑line comments, Prolog uses the percent sign (%) as the comment delimiter:

Documenting Predicates

For documenting predicates, we will use a Prolog adaptation of Javadoc‑style comments.

Place the comment for a predicate above the first fact or rule for that predicate:

First, notice that we cannot determine the argument names, argument types, or parameter

modes just by looking at the facts and rules defining the predicate, since these are not

explicitly declared in Prolog. As a result, the predicate documentation must begin with one

adeclaration header that defines the argument names, types, and modes. This declaration

header includes one or more lines that show alternativeinstantiation patterns for a

predicate‑‑that is, indicating different ways the predicate may be called with variables or

with bound values in different parameter positions. Each line should use the same formal

parameter names for the arguments appearing in the predicate. Arguments can also

include a colon (:) and type specifier to indicate the expected type of that parameter.

Typically, arguments are usually one of the following:

+ArgName

Abound value, which is often considered anincoming value ("in" mode) provided by the

caller.

-ArgName

/* This is a multiline comment. */

% This is a single line comment.

  • concat(+List1 : list, ?List2 : list, ?List3 : list).
  • concat(?List1 : list, ?List2 : list, +List3 : list).
  • Succeeds if the third list is the concatenation of the first two.
  • @param List1 The first (left) list to join.
  • @param List2 The second (right) list to join.
  • @param List3 The list containing all the elements of List 1
  • followed by all the elements of List 2. */ concat([], List, List). concat([Head | Tail], List, [Head | Rest]) :- concat(Tail, List, Rest).

NAVIGATION

Home

My profile

Current course

CS 5314 F

Participants

Reports

General

My courses

SETTINGS

Page module administration

Course administration

Switch role to...

My profile settings

Site administration

Search

My home

Piazza course discussion area

CS 5314 Syllabus

Grades

News forum

Prolog resources

Prolog commenting

Program grading criteria

Login to Web‑ CAT

Edit settings Locally assigned roles Permissions Check permissions Filters Logs Backup Restore

Anunbound value, which means an unbound variable would be supplied by the caller to

receive anoutgoing value ("out" mode).

?ArgName

A value that may be either bound or unbound (or even partially bound), which means

the caller may supply either a known value or an unbound variable. The argument can

be treated as either incoming or outgoing, depending on what the caller has supplied.

Following the declaration header (and separated by a blank line), a normal Javadoc‑style

description of what the predicate does should be provided in one or more paragraphs. As

with Javadoc, be sure the first sentence in the predicate's description is a good single‑

sentence summary. Additional sentences can provide more detail.

Following the description, use Javadoc @param tags to define the meaning/interpretation of

each argument. Other Javadoc tags can be used as well where needed. Note that the

@return tag is typically not used, since all predicates either succeed or fail, and the

description should indicate when this is so.

Adding Test Cases

Test cases can be added to predicate headers as "examples":

In the examples above, you can see that each test case (or example ) starts with a goal

introduced by ?-. Goals may be written across multiple lines, as long as continuation lines

are indented further than the ?‑ (use spaces instead of tabs!).

The expected results for each goal are written immediately following the goal, and

continue up to the next blank line or the next goal. Expected results take one of three

forms:

A comma‑separated list of one or more solutions

When variables are used in a goal, the expected results appearing after the goal should

contain a list of one or more solutions separated by commas. Each solution is a list of

Variable = Value pairs giving the value for each unbound variable listed in the Prolog

  • concat(+List1 : list, ?List2 : list, ?List3 : list).
  • concat(?List1 : list, ?List2 : list, +List3 : list).
  • Succeeds if the third list is the concatenation of the first two.
  • @param List1 The first (left) list to join.
  • @param List2 The second (right) list to join.
  • @param List3 The list containing all the elements of List 1
  • followed by all the elements of List 2.
  • Examples:
  • ?- concat([a], [a], [a, b, c]).
  • fail
  • ?- concat([a], [b, c], [a, b, c]).
  • true
  • ?- concat([a], [b, c], C).
  • [ C = [a, b, c] ]
  • ?- concat(A, [b, c], [a, b, c]).
  • [ A = [a] ]
  • ?- concat([a], B, [a, b, c]).
  • [ B = [b, c] ]
  • ?- concat(A, B, [a, b, c]).
  • [ A = [], B = [a, b, c] ],
  • [ A = [a], B = [b, c] ],
  • [ A = [a, b], B = [c] ],
  • [ A = [a, b, c], B = [] ] */ concat([], List, List). concat([Head | Tail], List, [Head | Rest]) :- concat(Tail, List, Rest).
  • CS 5314 F