1\documentclass[11pt]{article}
2\usepackage{times}
3\usepackage{pl}
4\usepackage{html}
5\usepackage{plpage}
6\sloppy
7\makeindex
8
9\onefile
10\htmloutput{.}					% Output directory
11\htmlmainfile{pldoc}				% Main document file
12\bodycolor{white}				% Page colour
13
14\renewcommand{\runningtitle}{SWI-Prolog documentation package}
15
16\begin{document}
17
18\title{SWI-Prolog Source Documentation \\
19       Version 2}
20\author{Jan Wielemaker \\
21	VU University, Amsterdam \\
22	The Netherlands \\
23	E-mail: \email{J.Wielemaker@vu.nl}}
24
25\maketitle
26
27\begin{abstract}
28This document presents PlDoc, the SWI-Prolog source-code documentation
29infrastructure.  PlDoc is loosely based on JavaDoc, using structured
30comments to mix documentation with source-code.  SWI-Prolog's PlDoc
31is entirely written in Prolog and well integrated into the environment.
32It can create HTML+CSS and \LaTeX{} documentation files as well as act
33as a web-server for the loaded project during program development.
34
35The SWI-Prolog website (\url{http:www.swi-prolog.org}) is written in
36Prolog and integrates PlDoc to provide a comprehensive searchable
37\href{http://www.swi-prolog.org/pldoc/index.html}{online manual}.
38
39Version~2 of PlDoc extends the syntax with
40\href{http://en.wikipedia.org/wiki/Markdown}{Markdown} markup as
41specified by \href{http://www.stack.nl/~dimitri/doxygen/}{Docygen}.
42Based on experience with version~1, PlDoc~2 both tightens some rules
43to avoid misinterpretations and relaxes others that were considered
44too conservative.
45\end{abstract}
46
47\pagebreak
48\tableofcontents
49
50\vfill
51\vfill
52
53\newpage
54
55\section{Introduction}
56\label{sec:pldoc-intro}
57
58When developing Prolog source that has to be maintained for a longer
59period or is developed by a ---possibly distributed--- team some basic
60quality mechanisms need to be adopted. A shared and well designed
61codingstyle \cite{DBLP:dblpjournals/tplp/CovingtonBOWP12} is one of
62them. In addition, documentation of source-files and their primary
63interfaces as well as a testing framework must be established.
64
65In our view, hitherto existing documentation and testing frameworks
66fell short realising the basic needs in a lightweight and easy to
67adopt system. To encourage consistent style, well commented code and
68test-assisted development, we make sure that
69
70\begin{shortlist}
71    \item The documentation and testing framework requires a
72	  minimum of work and learning.
73    \item The framework is immediately rewarding to the individual
74          programmer as well as the team,
75\end{shortlist}
76
77First, we describe the documentation system we developed for SWI-Prolog.
78In \secref{motivation} we motivate our main choices.
79
80
81%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82\section{Overview}
83\label{sec:pldoc-overview}
84
85Like JavaDoc, the PlDoc infrastructure is based on \emph{structured
86comments}.  Using comments, no changes have to be made to Prolog to
87load the documented source.  If the \pllib{pldoc} library is loaded,
88Prolog will not only load the source, but also parse all structured
89comments.  It processes the mode-declarations inside the comments and
90stores these as annotations in the Prolog database to support the test
91framework and other runtime and compiletime analysis tools that may be
92developed in the future.
93
94Documentation for all or some of the loaded files can be written to file
95in either HTML+CSS or \LaTeX{} (see \secref{doclatex}) format. Each
96source file is documented in a single file. In addition, the
97documentation generator will generate an index file that can be used as
98an index for a browser or input file for \LaTeX{} for producing nicely
99typeset document.
100
101To support the developer, the documentation system can be asked to start
102a web-server that can be used to browse the documentation.
103
104
105%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106\section{Structured comments}
107\label{sec:pldoc-comments}
108
109Structured comments come in two flavours, the line-comment (\%) based
110one, seen mostly in the Prolog community and the block-comment
111(\verb$/*$\ldots\verb$*/$) based one, commonly seen in the Java and C
112domains.  As we cannot determine the argument names, type and modes
113from following (predicate) source itself, we must supply this in the
114comment.\footnote{See \secref{motivation:modes}.}  The overall
115structure of the comment therefore is:
116
117\begin{shortlist}
118    \item Semi-formal type- and mode-description, see \secref{modes}
119    \item Wiki-style documentation body, see \secref{wiki}
120    \item JavaDoc style tags (\chr{@}keyword value, see \secref{tags})
121\end{shortlist}
122
123The \verb$/*$\ldots\verb$*/$ style comment starts with
124\verb$/**$<white>. The type and mode declarations start at the first
125non-blank line and are ended by a blank line.
126
127The \chr{\%}-style line comments start with \verb$%!$<white> or, for
128compatibility reasons, with \verb$%%$<white>.\footnote{The \texttt{\%\%}
129leader was considered to give too many false positives on arbitrary
130source code. It is still accepted, but invalid comments are silently
131ignored, while invalid comments that start with \texttt{\%!} result in a
132warning.} The type and mode declaration is ended by the first line that
133starts with a single \%. E.g., the following two fragments are identical
134wrt. PlDoc. Skipping blank-lines in \verb$/**$ comments allows to start
135the comment on the second line.
136
137\begin{code}
138%!	predicate(-Arg:type) is nondet
139%	Predicate ...
140\end{code}
141
142\begin{code}
143/**
144 * predicate(-Arg:type) is nondet
145 *
146 * Predicate ...
147 */
148\end{code}
149
150
151The JavaDoc style keyword list starts at the first line starting
152with @<word>.
153
154
155%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156\section{File (module) comments}
157\label{sec:sectioncomments}
158
159An important aspect is documentation of the file or module as a whole,
160explaining its design, purpose and relation to other modules. In JavaDoc
161this is the comment that preceeds the class definition. The Prolog
162equivalent would be to put the module comment in front of the module
163declaration. The module declaration itself however is an important index
164to the content of the file and is therefore best kept first.
165
166The general comment-structure for module comments is to use a type
167identifier between angled brackets, followed by the title of the
168section.  Currently the only type provided is \const{module}.  Other
169types may be added later.
170
171\subsection*{Example}
172
173\begin{code}
174/** <module> Prolog documentation processor
175
176This module processes structured comments and generates both formal
177mode declarations from them as well as documentation in the form of
178HTML or LaTeX.
179
180@author	Jan Wielemaker
181@license GPL
182*/
183\end{code}
184
185
186%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187\section{Type, mode and determinism declaration headers}
188\label{sec:modes}
189
190Many predicates can sensibly be called in different ways, e.g. with a
191specific argument as input or as output. The header of the documentation
192of a predicate consists of one or more \jargon{templates}, each
193representing a specific way of calling the predicate.
194
195A template can contain information about types, argument instantiation
196patterns, determinism and more. The syntax is informally described below:
197
198\begin{center}
199\begin{tabular}{lrl}
200\hline
201<template>	\isa <head>['//'] 'is' <determinism> \\
202		\ora <head>['//'] \\
203<determinism>	\isa 'det' \\
204		\ora 'semidet' \\
205		\ora 'failure' \\
206		\ora 'nondet' \\
207		\ora 'multi' \\
208		\ora 'undefined' \\
209<head>		\isa <functor>'('<argspec> {',' <argspec>}')' \\
210		\ora <functor> \\
211<argspec>	\isa [<instantiation>]<argname>[':'<type>] \\
212<instantiation> \isa '++' $\mid$ '+' $\mid$ '-' $\mid$ '--' $\mid$ '?'
213		     $\mid$ ':' $\mid$ '@' $\mid$ '!' \\
214<type>		\isa <term> \\
215\hline
216\end{tabular}
217\end{center}
218
219The \jargon{determinism} values originate from Mercury. Their meaning is
220explained in the table below. Informally, \const{det} is used for
221deterministic transformations (e.g.\ arithmetic), \const{semidet} for
222tests, \const{nondet} and \const{multi} for \jargon{generators}.  The
223\const{failure} indicator is rarely used.  It mostly appears in hooks
224or the recovery goal of catch/3.
225
226\begin{center}
227\begin{tabular}{l|p{0.7\linewidth}}
228\hline
229\bf Determinism & \bf Predicate behaviour \\
230\hline
231\const{det}     & Succeeds exactly once without a choice point \\
232\const{semidet} & Fails or Succeeds exactly once without a choice-point \\
233\const{failure} & Always fails \\
234\const{nondet}  & No constraints on the number of times the predicate
235		  succeeds and whether or not it leaves choice-points
236		  on the last success. \\
237\const{multi}   & As \const{nondet}, but succeeds at least one time. \\
238\const{undefined} & Well founded semantics third value.
239		    See undefined/0. \\
240\hline
241\end{tabular}
242\end{center}
243
244The meanings of the \jargon{instantiation patterns} for individual
245arguments are:
246
247\begin{center}
248\begin{tabular}{l|p{0.7\linewidth}}
249\hline
250++&	Argument is ground at call-time, i.e., the argument does not contain a
251	variable anywhere. \\
252+ &	Argument is fully instantiated at call-time, to a term that satisfies
253	the type.  This is not necessarily \jargon{ground}, e.g., the term
254	\exam{[_]} is a \jargon{list}, although its only member is
255	unbound. \\
256- &	Argument is an \emph{output} argument. It may be unbound at call-time,
257	or it may be bound to a term. In the latter case, the predicate behaves
258	as if the argument was unbound, and then unified with that term after
259	the goal succeeds.
260	For example, the goal \exam{findall(X, Goal, [T])} is good style and
261	equivalent to \exam{findall(X, Goal, Xs), Xs = [T]}\footnote{The
262	ISO standard dictates that \exam{findall(X, Goal, 1)} raises a
263	\const{type_error} exception, breaking this semantic relation.
264	SWI-Prolog does not follow the standard here.} Determinism
265	declarations assume that the argument is a free variable at call-time.
266	For the case where the argument is bound or involved in constraints,
267	\const{det} effectively	becomes \const{semidet}, and \const{multi}
268	effectively becomes \const{nondet}.\\
269--&	Argument is unbound at call-time.  Typically used by predicates that
270	create `something' and return a handle to the created object,
271	such as open/3 which creates a \jargon{stream}. \\
272? &	Argument is bound to a \emph{partial term} of the indicated
273	type at call-time.  Note that a variable is a partial term for any
274	type. \\
275: &	Argument is a meta-argument.  Implies \chr{+}. \\
276@ &	Argument will not be further instantiated than it is at call-time.
277	Typically used for type tests. \\
278! &	Argument contains a mutable structure that may be modified using
279	setarg/3 or nb_setarg/3. \\
280\hline
281\end{tabular}
282\end{center}
283
284Users should be aware that calling a predicate with arguments instantiated
285in a way other than specified by one of the templates may result in errors or
286unexpected behavior.
287
288Developers should ensure that predicates are \jargon{steadfast} with respect to
289output arguments (marked - in the template). This means that instantiation of
290output arguments at call-time does not change the semantics of the goal (it may
291be used for optimization, though). If this steadfast behavior cannot be
292guaranteed, -- should be used instead.
293
294In the current version, argument \jargon{types} are represented by an arbitrary term
295without formal semantics. In future versions we may adopt a formal type
296system that allows for runtime verification and static type analysis
297\cite{DBLP:conf/cl/Hermenegildo00,DBLP:journals/ai/MycroftO84,DBLP:conf/acsc/JefferyHS00}
298
299
300\subsection*{Examples}
301
302\begin{code}
303%!	length(+List:list, -Length:int) is det.
304%!	length(?List:list, -Length:int) is nondet.
305%!	length(?List:list, +Length:int) is det.
306%
307%	True if List is a list of length Length.
308%
309%	@compat iso
310\end{code}
311
312%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
313\section{Tags: \chr{@}see, etc.}
314\label{sec:tags}
315
316Optionally, the description may be followed by one or more \emph{tags}.
317Our tag convention is strongly based on the conventions used by javaDoc.
318It is adviced to place tags in the order they are described below.
319
320\begin{description}
321    \definition{\chr{@}arg {\it Name Description}}
322Defines the predicate arguments. Each argument has its own \const{@arg}
323tag. The first word is the name of the argument. The remainder of the
324tag is the description. Arguments declarations normally appear in order
325used by the predicate.
326
327    \definition{\chr{@}param {\it Name Description}}
328This is a synonym for \const{@arg}, using the JavaDoc tag name.
329
330    \definition{\chr{@}throws {\it Term Description}}
331Error condition. First Prolog term is the error term. Remainder is the
332description.
333
334    \definition{\chr{@}error  {\it Error Description}}
335As \chr{@}throws, but the exception is embedded in \term{error}{Error,
336Context}.
337
338    \definition{\chr{@}author {\it Name}}
339Author of the module or predicate. Multiple entries are used if there
340are multiple authors.
341
342    \definition{\chr{@}version {\it Version}}
343Version of the module.  There is no formal versioning system.
344
345    \definition{\chr{@}see {\it Text}}
346Point to related material.  Often contains links to predicates or
347files.
348
349    \definition{\chr{@}deprecated {\it Alternative}}
350The predicate or module is deprecated.  The description specifies what
351to use in new code.
352
353    \definition{\chr{@}compat {\it Standards and systems}}
354When implementing libraries or externally defined interfaces this tag
355describes to which standard the interface is compatible.
356
357    \definition{\chr{@}copyright {\it Copyright holder}}
358Copyright notice.
359
360    \definition{\chr{@}license {\it License conditions}}
361License conditions that apply to the source.
362
363    \definition{\chr{@}bug {\it Bug description}}
364Known problems with the interface or implementation.
365
366    \definition{\chr{@}tbd {\it Work to be done}}
367Not yet realised behaviour that is enticipated in future versions.
368\end{description}
369
370
371%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
372\section{Wiki notation}
373\label{sec:wiki}
374
375Structured comments that provide part of the documentation are written
376in Wiki notation, based on \href{http://www.twiki.org}{TWiki}, with some
377Prolog specific additions.
378
379\subsection{Structuring conventions}
380\label{sec:pldoc-comment-structure}
381
382\begin{description}
383    \item [Paragraphs]
384Paragraphs are separated by a blank line. Paragraphs that are indented
385in the source-code \emph{after} normalising the left-margin with at
386least 16 spaces are \emph{centered}. Paragraphs where all the lines
387start with ``\verb$> $'' (greater than followed by a blank) are rendered
388in the HTML backend using a \const{blockquote} element and in \LaTeX{}
389using the \const{quote} environment.
390
391    \item [General lists]
392The wiki knows three types of lists: \emph{bullet lists} (HTML
393\const{ul}), \emph{numbered lists} (HTML \const{ol}) and
394\emph{description lists} (HTML \const{dl}). Each list environment is
395headed by an empty line and each list-item has a special symbol at the
396start, followed by a space.  Each subsequent item must be indented at
397exactly the same column.  Lists may be nested by starting a new list at
398a higher level of indentation.  The list prefixes are:
399
400\begin{center}
401\begin{tabular}{lp{0.7\linewidth}}
402 \tt *	& Bulleted list item \\
403 \tt 1.	& Numbered list item.  Any number from 1..9 is allowed, which
404	  allows for proper numbering in the source.  Actual numbers
405	  in the HTML or \LaTeX{} however are re-generated, starting
406	  at 1. \\
407 \tt \$ Title : Item & Description list item.
408\end{tabular}
409\end{center}
410
411    \item [Term lists]
412Especially when describing option lists or different accepted types,
413it is common to describe the behaviour on different terms.  Such
414lists must be written as below.  <Term1>, etc. must be valid Prolog
415terms and end in the newline.  The Wiki adds \verb$' . '$ to the text
416and reads it using the operator definitions also used to read
417the mode terms.  See \secref{modes}.  Variable names encountered in
418the \arg{Term} are used for indentifying variables in the following
419\arg{Description}.  At least one \arg{Description} must be non-empty
420to avoid confusion with a simple item list.
421
422\begin{code}
423   * Term1
424     Description
425   * Term2
426     Description
427\end{code}
428
429    \item [Predicate description lists]
430Especially for processing Wiki files, the Wiki notation allows for
431including the description of a predicate `in-line', where the
432documentation is extracted from a loaded source file. For example:
433
434\begin{code}
435The following predicates are considered Prolog's prime list processing
436primitives:
437
438    * [[member/2]]
439    * [[append/3]]
440\end{code}
441
442    \item [Tables]
443The Wiki provides only for limited support for tables.   A table-row
444is started by a \chr{|} sign and the cells are separated by the same
445character.  The last cell must be ended with \chr{|}.  Multiple lines
446that parse into a table-row together form a table.  Example:
447
448\begin{code}
449	| Algorithm    | Time (sec) |
450	| Depth first  | 1.0        |
451	| Breath first | 0.7        |
452	| A*           | 0.3        |
453\end{code}
454
455    \item [Section Headers]
456Section headers are creates using one of the constructs below taken
457from TWiki.  Section headers are normally not used in the source-code,
458but can be useful inside README and TODO files.  See \secref{dirindex}.
459
460\begin{code}
461---+ Section level 1
462---++ Section level 2
463---+++ Section level 3
464---++++ Section level 4
465\end{code}
466
467In addition, PlDoc recognises the \jargon{markdown} syntax, including
468named sections as defined by \program{doxygen}. A section is named
469(labeled) using an optional sequence \verb${\#$\textit{name}\verb$}$.
470The three code sections below provide examples. Note that \texttt{\#}
471section headers should be positioned at the left margin and the
472\texttt{\#} must be followed by blank space. If the header is
473underlined, the underline is a line that only contains \const{=} or
474\const{-} characters. There must be a minimum of three\footnote{Markdown
475demands two, but this results in ambiguities with the \texttt{==} fence
476for code blocks.} of such characters.
477
478
479\begin{code}
480Section level 1
481===============
482
483Section level 2
484---------------
485\end{code}
486
487\begin{code}
488# Section level 1
489## Section level 2
490### Section level 3
491#### Section level 4
492\end{code}
493
494\begin{code}
495Section level 1		{#label}
496===============
497
498# Section level 1	{#label}
499\end{code}
500
501    \item [Code blocks]
502There are two ways to write a code block. The first one is
503\jargon{fenced}. Here, the block is preceeded and followed by a fence
504line.  The traditional PlDoc fence line is \verb$==$.  Doxygen fence
505lines are also accepted.  They contain at least three tilde (\chr{~})
506characters, where the opening fence line may be followed by a file
507extension between curly brackets.  In all cases, the code is indented
508relative to the indentation of the fence line.  Below are two examples,
509the first being the traditional PlDoc style.  The second is the Doxygen
510style, showing a code block that is indented (because it is a body
511fragment) and that is flagged as Prolog source. Note that the
512\verb${.pl}$ is optional.
513
514\begin{code}
515  ==
516  small(X) :-
517     X < 2.
518  ==
519\end{code}
520
521\begin{code}
522  ~~~{.pl}
523    ...,
524    format('Hello ~w~n', [World]),
525    ...,
526  ~~~
527\end{code}
528
529The second form of code blocks are \jargon{indented blocks}. Such a
530block must be indented between 4 and 8 characters, relative to the
531indentation of the last preceeding non-blank line. The block is opened
532with a blank line and closed by a blank line or a line that is indented
533less than the indentation of the initial line. It is allowed to have a
534single blank line in the middle of a code block, provided that the next
535line is again indented at least as much as the initial line. The initial
536line as well as a line that follows a blank line may not be a valid list
537opening line or a table row, i.e., it may not start with one of
538\verb$*-$ followed by a space or \verb$|$.
539
540    \item [Rulers]
541PlDoc accepts both the original PlDoc and markdown conventions for
542rulers. A PlDoc ruler is a line with at least two dashes (-) that starts
543at the left-most column.  A markdown ruler holds at least three ruler
544characters and any number of spaces.  The ruler characters are the
545dash (-), underscore (\verb$_$) or asterisk (\verb$*$).  Below are
546three examples, the last two of which are valid markdown.
547
548\begin{code}
549--
550***
551		- - -
552\end{code}
553
554    \item [Line breaks]
555A line break may be added by \emph{ending} the physical line with the
556HTML linebreak, \verb$<br>$ or \verb$<br/>$.\footnote{The markdown
557conventions are (original) two spaces at the of the physical line and
558(GitHub) a physical line break. Neither fit well with source code.
559Doxygen supports restricted HTML and allows for
560\texttt{\string<br\string>}.}
561\end{description}
562
563\subsection{Text markup: fonts and links}
564\label{sec:pldoc-markup}
565
566\subsubsection{Emphasizing text}
567\label{sec:emph}
568
569Text emphasis is a combination of old plaintext conventions in Usenet
570and E-mail and the doxygen version of markdown. \Tabref{fonts} shows the
571font-changing constructions.  The phrase \jargon{limited context} means
572that
573
574\begin{itemize}
575    \item The opening \chr{*} or \chr{_} must be preceeded by white space
576          or a character from the set \verb$<{([,:;$ and must be
577	  followed by an alphanumerical character.
578    \item The closing \chr{*} or \chr{_} may not be followed by an
579	  alphanumerical character and may not be preceeded by white
580	  space or a character from the set \verb$({[<=+-\@$.
581    \item The scope of these operations is always limited to the
582	  identified structure (paragraph, list item, etc.)
583\end{itemize}
584
585Note that \verb$=$<identifier>\verb$=$ is limited to a an
586\jargon{identifier}, such as a file name, XML name, etc. Identifiers
587must start and end with an alphanumerical character, while characters
588from the set \verb$.-/:$ may appear internally. Note that this set
589explicitly does not allow for white space in code spans delimited by a
590single \const{=}. This markup is specifically meant to deal with code
591that is either not Prolog code or invalid Prolog code. Valid Prolog code
592should use the backtick as described in \secref{inlinecode}.
593
594\begin{table}
595\begin{center}
596\begin{tabular}{|l|p{0.7\linewidth}|}
597\hline
598\tt *bold*	& Typeset text in \textbf{bold} for
599		  limited content (see running text). \\
600\tt *|bold|*	& Typeset text in \textbf{bold}.
601		  Content can be long. \\
602\tt _emphasize_ & Typeset text as \emph{emphasize} for
603		  limited content (see running text). \\
604\tt _|emphasize|_ & Typeset text as \emph{emphasize}.
605		  Content can be long. \\
606\tt =code=      & Typeset text \texttt{fixed} font for
607		  identifiers (see running text). \\
608\tt =|code|=    & Typeset text \texttt{fixed} font.
609		  Content can be long. \\
610\tt Word	& Capitalised words that appear as argument-name
611		  are written in \arg{Italic} \\
612\hline
613\end{tabular}
614\end{center}
615    \caption{Wiki constructs to change the font}
616    \label{tab:fonts}
617\end{table}
618
619\subsubsection{Inline code}
620\label{sec:inlinecode}
621
622Inline code can be realised using the \verb$=$ switch described in
623\secref{emph} or the markdown backtick. In addition, it can use the
624mardown/Doxygen \jargon{backtick} (\verb$`$) convention: a string
625that is delimited by backticks is considered code, provided:
626
627    \begin{itemize}
628    \item An internal double backtick is translated into a single
629          backtick.
630    \item Inline code is limited to the current structure (paragraph,
631          table cell, list item, etc.
632    \item The content of the code block is valid Prolog syntax.
633	  Note that in Doxygen, the syntax is not validated and
634	  a single quote cancels the recognition as code.  The
635	  latter is a problematic in Prolog because single
636	  quotes are often required.
637    \end{itemize}
638
639Currently, `Var` is typeset as a variable (italics) and other terms
640are typeset using a fixed-width code font.
641
642In addition, compound terms in canonical notation (i.e.,
643\mbox{\textit{functor}\texttt{(},\textit{...args...}\texttt{)}} that can
644be parsed are first verified as a file-specification for
645absolute_file_name/3 and otherwise rendered as \jargon{code}.
646
647
648\subsubsection{Links}
649\label{sec:pldoc-links}
650
651\Tabref{links} shows the constructs for creating links.
652
653\begin{table}
654\begin{center}
655\begin{tabular}{|l|p{0.7\linewidth}|}
656\hline
657\tt name/arity  & Create a link to a predicate \\
658\verb$`name/arity`$  & Create a link to a predicate \\
659\verb$``name/arity``$  & Predicate indicator that does not create a
660			 link \\
661\tt name//arity & Create a link to a DCG rule \\
662\verb$`name//arity`$ & Create a link to a DCG rule \\
663\verb$``name//arity``$ & DCG indicator that does not create a
664			 link \\
665flag \verb$`name`$ & Create a link to the Prolog flag \arg{name}
666		  if this flag exists. \\
667\tt name.ext	& If <name>.<ext> is the name of an existing file
668		  and <ext> is one of \fileext{pl}, \fileext{txt},
669		  \fileext{md}, \fileext{png}, \fileext{gif}, \fileext{jpeg},
670		  \fileext{jpg} or \fileext{svg}, create a link to the file. \\
671\tt prot\verb$://$url & If <prot> is one of \const{http}, \const{https}
672		  or \const{ftp}, create a link. \\
673\tt\chr{<}url\chr{>} & Create a hyperlink to URL.  This construct
674		  supports the expand_url_path/2 using the construct
675		  <alias>:<local>.  <local> can be empty. \\
676\tt\verb$[[$label\verb$][$link\verb$]]$ &
677		  Create a link using the given <label>.  Label
678		  can be text or a reference to an image file.
679		  Additional arguments can be supplied as
680		  \chr{;}<name>=\chr{"}<value>\chr{"}.  More
681		  arguments are separated by commas. <link> must be a
682		  filename as above or a url. \\
683\tt\verb$[$label\verb$]($link\verb$)$ &
684		  The markdown version of the above. \\
685\tt\verb$[$@cite\verb$]$ &
686		  Include \LaTeX{} citation.  Multiple citations can
687		  be saparated using \chr{;}. No white space is allowed.
688		  \\
689\hline
690\end{tabular}
691\end{center}
692    \caption{Wiki constructs that create links}
693    \label{tab:links}
694\end{table}
695
696
697
698\subsection{Images}
699\label{sec:pldoc-images}
700
701Images can be included in the documentation by referencing an image file
702using one of the extensions \fileext{gif}, \fileext{png},
703\fileext{jpeg}, \fileext{jpg} or \fileext{svg}.\footnote{SVG images are
704included using the \const{object} element. This is supported by many
705modern browsers. When using IE, one needs at least IE9.} By default this
706creates a link to the image file that must be visited to see the image.
707Inline images can be created by enclosing the filename in double square
708brackets. For example
709
710\begin{code}
711The [[open.png]] icon is used open an existing file.
712\end{code}
713
714The markdown alternative for images is also supported, and looks as
715below.  The current implementation only deals with image \emph{files},
716not external resources.
717
718\begin{code}
719    ![Caption](File)
720\end{code}
721
722
723%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
724\section{Directory indices}
725\label{sec:dirindex}
726
727A directory index consists of the contents of the file \file{README}
728(or \file{README.TXT}), followed by a table holding all currently loaded
729source-files that appear below the given directory (i.e.\ traversal is
730\emph{recursive}) and for each file a list of public predicates and their
731descriptive summary.  Finally, if a file \file{TODO} or \file{TODO.TXT}
732exists, its content is added at the end of the directory index.
733
734
735%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
736\section{Documentation files}
737\label{sec:wikifiles}
738
739Sometimes it is desirable to document aspects of a package outside the
740source-files.  For this reason the system creates a link to files using
741the extension \fileext{txt}. The referenced file is processed as Wiki
742source. The two fragments below illustrate the relation between an
743\fileext{pl} file and a \fileext{txt} file.
744
745\begin{code}
746%!	read_setup(+File, -Setup) is det.
747%
748%	Read application setup information from File.  The details
749%	on setup are described in setup.txt.
750\end{code}
751
752\begin{code}
753---+ Application setup data
754
755If a file =|.myapprc|= exists in the user's home directory the
756application will process this data using setup.pl. ...
757\end{code}
758
759
760%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
761\section{Running the documentation system}
762\label{sec:running}
763
764
765\subsection{During development}
766\label{sec:pldoc-development}
767
768To support the developer with an up-to-date version of the documentation
769of both the application under development and the system libraries the
770developer can start an HTTP documentation server using the command
771\term{doc_server}{?Port}.  A good way to deploy PlDoc for program
772development is to write a file called e.g., \file{debug.pl} that
773sets up the preferred development environment and loads your program.
774below is an example \file{debug.pl} that starts PlDoc and prints strings
775as text before loading the remainder of your program.
776
777\begin{code}
778:- doc_server(4000).	% Start PlDoc at port 4000
779:- portray_text(true).	% Enable portray of strings
780
781:- [load].		% load your program
782\end{code}
783
784\begin{description}
785    \predicate{doc_collect}{1}{+Bool}
786Enable/disable collecting structured comments into the Prolog database.
787See doc_server/1 or doc_browser/0 for the normal way to deploy the
788server in your application. All these predicates must be called
789\emph{before} loading your program.
790
791    \predicate{doc_server}{1}{?Port}
792Start documentation server at \arg{Port}.  Same as
793\term{doc_server}{Port, [allow(localhost), workers(1)]}.  This predicate
794must be called \emph{before} loading the program for which you consult
795the documentation. It calls doc_collect/1 to start collecting
796documentation while (re-)loading your program.
797
798    \predicate{doc_server}{2}{?Port, +Options}
799Start documentation server at \arg{Port} using \arg{Options}.  Provided
800options are:
801
802    \begin{description}
803	\termitem{root}{+Path}
804Defines the root of all locations served by the HTTP server.  Default
805is \const{/}.  \arg{Path} must be an absolute URL location, starting
806with \chr{/} and ending in \chr{/}.  Intented for public services behind
807a reverse proxy.  See documentation of the HTTP package for details on
808using reverse proxies.
809
810	\termitem{edit}{+Bool}
811If \const{false}, do not allow editing, even if the connection comes
812from localhost. Intended together with the \const{root} option to make
813pldoc available from behind a reverse proxy. See the HTTP package for
814configuring a Prolog server behind an
815\href{http://httpd.apache.org/docs/1.3/mod/mod_proxy.html}{Apache
816reverse proxy}.
817
818	\termitem{allow}{+HostOrIP}
819Allow connections from \arg{HostOrIP}.  If \arg{Host} is an atom
820starting with a '.', suffix matching is preformed.  I.e.\
821\verb$allow('.uva.nl')$ grants access to all machines in this domain.
822IP addresses are specified using the library(socket) ip/4 term.  I.e.
823to allow access from the 10.0.0.X domain, specify
824\verb$allow(ip(10,0,0,_))$.
825
826	\termitem{deny}{+HostOrIP}
827Deny access from the given location.  Matching is equal to the
828\const{allow} option.
829    \end{description}
830
831Access is granted iff
832
833\begin{shortlist}
834    \item Both \emph{deny} and \emph{allow} match
835    \item \emph{allow} exists and matches
836    \item \emph{allow} does not exist and \emph{deny} does not match.
837\end{shortlist}
838
839    \predicate{doc_browser}{0}{}
840Open the user's default browser on the running documentation server.
841Fails if no server is running.
842
843    \predicate{doc_browser}{1}{+Spec}
844Open the user's default browser on the specified page.  \arg{Spec} is
845handled similar to edit/1, resolving anything that relates somehow to
846the given specification and ask the user to select.%
847	\bug{This flexibility is not yet implemented}.
848\end{description}
849
850%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
851\subsection{As a manual server}
852\label{sec:manserver}
853
854The library \pllib{pldoc/doc_library} defines doc_load_library/0 to
855load the entire library.
856
857\begin{description}
858    \predicate{doc_load_library}{0}{}
859Load all library files.  This is intended to set up a local documentation
860server.  A typical scenario, making the server available at port 4000 of
861the hosting machine from all locations in a domain is given below.
862
863\begin{code}
864:- doc_server(4000,
865	      [ allow('.my.org')
866	      ]).
867:- use_module(library(pldoc/doc_library)).
868:- doc_load_library.
869\end{code}
870
871Example code can be found in \file{$PLBASE/doc/packages/examples/pldoc}.
872\end{description}
873
874
875%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
876\subsection{Using the browser interface}
877\label{sec:browser}
878
879The documentation system is normally accessed from a web-browser after
880starting the server using doc_server/1.  This section briefly explains
881the user-interface provided from the browser.
882
883\subsubsection{Searching}
884\label{sec:pldoc-search}
885
886The top-right of the screen provides a search-form. The search string
887typed is searched as a substring and case-insensitive. Multiple strings
888separated by spaces search for the intersection. Searching for objects
889that do not contain a string is written as \chr{-}<string>.  A search
890for adjacent strings is specified as \chr{"}<string>\chr{"}.  Here
891are some examples:
892
893\begin{center}
894\begin{tabular}{|l|p{0.6\linewidth}|}
895\hline
896\tt load file	& Searches for all objects with the strings
897		  \texttt{load} and \texttt{file}. \\
898\tt load -file  & Searches for objects with \texttt{load}, but
899		  \emph{without} \texttt{file}. \\
900\tt "load file" & Searches for the string \texttt{load file}.\\
901\hline
902\end{tabular}
903\end{center}
904
905The two radio-buttons below the search box can be used to limit the
906search. \textsf{All} searches both the application and manuals.
907Searching for \textsf{Summary} also implies \textsf{Name}.
908
909\subsubsection{Views}
910\label{sec:pldoc-views}
911
912The web-browser supports several views, which we briefly summarise
913here:
914
915\begin{itemlist}
916    \item [ Directory ]
917In directory-view mode, the contents of a directory holding Prolog
918source-files is shown file-by-file in a summary-table.   In addition,
919the contents of the \file{README} and \file{TODO} files is given.
920
921    \item [ Source File ]
922When showing a Prolog source-file it displays the module documentation
923from the \verb$/** <module ... */$ comment and the public predicates
924with their full documentation. Using the \textsf{zoom} button the user
925can select to view both public and documentated private predicates.
926Using the \textsf{source} button, the system shows the source
927with syntax highlighting as in PceEmacs and formatted structured
928comments.%
929	\footnote{This mode is still incomplete.  It would be nice to
930		  add line-numbers and links to documentation and
931		  definitions in the sources.}
932
933    \item [ Predicate ]
934When selecting a predicate link the system presents a page with the
935documentation of the predicate.  The navigation bar allows switching
936to the Source File if the documentation comes from source or the
937containing section if the documentation comes from a manual.
938
939    \item [ Section ]
940Section from the manual. The navigation bars allows viewing the
941enclosing section (\emph{Up}).
942\end{itemlist}
943
944
945\subsubsection{Editing}
946\label{sec:pldoc-editing}
947
948If the browser is accessed from \const{localhost}, each object that
949is related to a known source-location has an edit icon at the right
950side.  Clicking this calls edit/1 on the object, calling the user's
951default editor in the file.  To use the built-in PceEmacs editor,
952either set the Prolog flag \const{editor} to \const{pce_emacs} or
953run \exam{?- emacs.} before clicking an edit button.
954
955Prolog source-files have a \emph{reload} button attached. Clicking this
956reloads the source file if it was modified and refreshes the page. This
957supports a comfortable edit-view loop to maintain the source-code
958documentation.
959
960
961%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
962\input{docfiles.tex}
963
964
965%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
966% Including PlDoc in a LaTeX document
967% Eating our own docfood
968%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
969\input{latex.tex}
970
971
972%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
973\section{Motivation of choices}
974\label{sec:motivation}
975
976Literate programming is an established field.  The \TeX{} source is one
977of the first and best known examples of this approach, where input
978files are a mixture of \TeX{} and Pascal source. External tools are
979used to untangle the common source and process one branch to produce
980the documentation, while the other is compiled to produce the program.
981
982A program and its documentation consists of various different parts:
983
984\begin{shortlist}
985    \item The program text itself.  This is the minimum that must be
986          handed to the compiler to create an executable (module).
987    \item Meta information about the program: author, modifications,
988          license, etc.
989    \item Documentation about the overall structure and purpose of
990	  the source.
991    \item Description of the interface: public predicates, their
992	  types, modes and whether or not they are deterministic as
993	  wel as an informative text on each public predicate.
994    \item Description of key private predicates necessary to understand
995	  how the public interface is realised.
996\end{shortlist}
997
998\subsection*{Structured comments or directives}
999
1000Comments can be added through Prolog directives, a route taken by Ciao
1001Prolog with lpdoc \cite{DBLP:conf/cl/Hermenegildo00} and Logtalk
1002\cite{pmoura03}. We feel structured comments are a better alternative
1003for the following reasons:
1004
1005\begin{itemize}
1006    \item Prolog programmers are used to writing comments as Prolog
1007          comments.
1008    \item Using Prolog strings requires unnatural escape sequences for
1009	  string quotes and long literal values tend to result in hard
1010	  to find quote-mismatches. Python uses comments in long
1011	  strings, solving this problem by using three double quotes
1012	  to open and close long comments.
1013    \item Comments should not look like code, as that makes it more
1014	  difficult to find the actual code.
1015\end{itemize}
1016
1017We are aware that the above problems can be dealt with using
1018syntax-aware editors. Only a few editors are sufficiently powerful to
1019support this correctly, though, and we do not expect the required advanced
1020modes to be widely available.  If comments are used, we do not need to force
1021users into using a particular editor.
1022
1023\subsection*{Wiki or HTML}
1024
1025JavaDoc uses HTML as markup inside the structured comments. Although
1026HTML is more widely known than ---for example--- \LaTeX{} or TeXinfo, we
1027think the Wiki approach is sufficiently widely known today. Using text
1028with minimal layout conventions taken largely from plaintext newsnet and
1029E-mail, Wiki input is much easier to read in the source-file than HTML
1030without syntax support from an editor.
1031
1032\subsection*{Types and modes}
1033\label{sec:motivation:modes}
1034
1035Types and modes are not a formal part of the Prolog language.
1036Nevertheless, their role goes beyond pure documentation. The
1037test-system can use information about non-determinism to validate that
1038deterministic calls are indeed deterministic.  Type information can be
1039used to analyse coverage from the test-suite, to generate runtime type
1040verification or to perform static type-analysis.  We have chosen to
1041use a structured comment with formal syntax for the following reasons:
1042
1043\begin{itemize}
1044    \item As comments, they stay together with the comment block
1045          of a predicate. We feel it is best to keep documentation
1046	  as close as possible to the source.
1047    \item As we parse them separately, we can pick up argument names
1048	  and create a readable syntax without introducing possibly
1049	  conflicting operators.
1050    \item As comments, they do not introduce incompatibilities with
1051          other Prolog systems.
1052\end{itemize}
1053
1054
1055\subsection*{Few requirements}
1056
1057SWI-Prolog aims at platform independence. We want tools to rely as
1058much as possible on Prolog itself. Therefore, the entire infrastructure
1059is written in Prolog. Output as HTML is suitable for browsing and not
1060very high quality printing on virtually all platforms. Output to
1061\LaTeX{} requires more infrastructure for processing and allows for
1062producing high-quality PDF documents.
1063
1064%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1065\section{Compatibility and standards}
1066\label{sec:pldoc-compatibility}
1067
1068Initially, the PlDoc wiki language was based on
1069\href{http://www.twiki.org}{Twiki}. Currently,
1070\href{http://en.wikipedia.org/wiki/Markdown}{markdown} is a wiki syntax
1071that is widely accepted and not tied to a single system.  In PlDoc~2,
1072we have adopted markdown, including many of the limitations and
1073extensions introduced by
1074\href{http://www.stack.nl/~dimitri/doxygen/}{Docygen}.  Limitations are
1075needed to avoid ambiguities due to the common use of symbol charaters
1076in programming languages. Extensions are desirable to make use of
1077already existing conventions and to support requirements of program
1078documentation.
1079
1080Some of the changes in PlDoc~2 are to achieve compatibility with the
1081\href{http://prolog-commons.org/}{Prolog Commons} project. The library
1082documentation conventions of this project will be based on PlDoc and the
1083Ciao lpdoc standards.  It is likely that there will be more changes to
1084the PlDoc format to synchronise with Commons.  We do not anticipate
1085significant impact on existing documentation.
1086
1087
1088\bibliographystyle{name}
1089\bibliography{pl}
1090
1091\printindex
1092
1093\end{document}
1094
1095