1% BEGIN LICENSE BLOCK
2% Version: CMPL 1.1
3%
4% The contents of this file are subject to the Cisco-style Mozilla Public
5% License Version 1.1 (the "License"); you may not use this file except
6% in compliance with the License.  You may obtain a copy of the License
7% at www.eclipse-clp.org/license.
8%
9% Software distributed under the License is distributed on an "AS IS"
10% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
11% the License for the specific language governing rights and limitations
12% under the License.
13%
14% The Original Code is  The ECLiPSe Constraint Logic Programming System.
15% The Initial Developer of the Original Code is  Cisco Systems, Inc.
16% Portions created by the Initial Developer are
17% Copyright (C) 1994 - 2006 Cisco Systems, Inc.  All Rights Reserved.
18%
19% Contributor(s):
20%
21% END LICENSE BLOCK
22%
23% @(#)umsporting.tex	1.1 94/10/07
24%
25
26\chapter{Language Dialects, ISO Prolog and Porting Prolog Applications}
27%HEVEA\cutdef[1]{section}
28\label{chapporting}
29
30The {\eclipse} system has evolved from the Edinburgh family of
31Prolog systems, and thus shares many properties with other systems
32in the same tradition.  It also supports the
33\index{ISO Prolog}
34ISO Prolog Standard from 1995 and its 2005 and 2012 corrigenda.
35
36However, the default programming language dialect used with {\eclipse}
37(known as \notationidx{eclipse_language}) is a separate and unique dialect,
38which is the result of design decisions taken for conceptual, practical
39and occasionally historical reasons.
40
41To run an application written in another Prolog dialect on {\eclipse},
42one has basically two choices:
43Using a compatibility package, or modifying the program.
44
45
46\section{Using compatibility language dialects}
47The {\eclipse} compatibility language dialects are the fastest way to get a
48program running that was originally written for a different system.
49The dialects are implemented as libraries.
50The module system makes it possible for different application
51modules to use different language dialects.
52
53To use a particular language dialect, prefix your program with a
54\bipref{module/3}{../bips/kernel/modules/module-3.html}
55directive that specifies the desired language dialect, for example
56\begin{verbatim}
57    :- module(mymodule, [], iso).
58\end{verbatim}
59Here, the last argument of the module/3 directive indicates the language
60(the default being eclipse_language).
61It is not advisable to use \verb.:-lib(iso). or \verb.:-ensure_loaded(library(iso)).
62within an eclipse_language module, because this would lead to import
63conflicts between the different versions of built-in predicates.
64
65Examples of supported language dialects are
66\begin{itemize}
67\item ISO Standard Prolog
68    (\bipref{iso_strict}{../bips/lib/iso_strict/index.html},
69    \bipref{iso}{../bips/lib/iso/index.html} and
70    \bipref{iso_light}{../bips/lib/iso_light/index.html}).
71\item C-Prolog (\bipref{cprolog}{../bips/lib/cprolog/index.html}), one
72    of the oldest and most influential Prolog implementations.
73\item Quintus Prolog (\bipref{quintus}{../bips/lib/quintus/index.html}),
74    an early influential commercial system.
75\item SICStus Prolog (\bipref{sicstus}{../bips/lib/sicstus/index.html}),
76    an academic and commercial system based on Quintus.
77\item SWI Prolog (\bipref{swi}{../bips/lib/swi/index.html}),
78    a popular Prolog with large user base. 
79\end{itemize}
80See the Reference Manual for details on the compatibility provided by the
81language dialects.
82The language dialects are just modules which provide the necessary code
83and exports  to emulate a particular Prolog dialect. This module is imported
84instead of the default \notation{eclipse_language} dialect which provides the
85{\eclipse} language.
86The source code of the language dialect module is provided in the
87{\eclipse} library directory.
88Using this as a guideline, it should be easy to write similar packages for
89other systems, as long as their syntax does not deviate too much
90from the Edinburgh tradition.
91
92For quick experiments with a language dialect, {\eclipse} can be started
93with a different \notationidx{default_language} option
94(see section \ref{cmdlineopts}), e.g.
95\begin{verbatim}
96    % eclipse -L <dialect>
97\end{verbatim}
98This will give you a toplevel prompt in the given language dialect.
99The same effect can be achieved by setting the ECLIPSEDEFAULTLANGUAGE
100\index{ECLIPSEDEFAULTLANGUAGE}
101environment variable to the name of the chosen dialect.
102
103
104\subsection{ISO Prolog}
105\index{ISO Prolog}
106The ISO Prolog standard \cite{isoprolog95} is supported in three variants:
107\begin{itemize}
108\item The \bipref{iso_strict}{../bips/lib/iso_strict/index.html} dialect
109    provides an implementation of ISO Standard Prolog and complies
110    strictly with ISO/IEC 13211-1 (Information Technology, Programming
111    Languages, Prolog, Part 1, General Core, 1995) and the technical
112    corrigenda ISO/IEC 13211-1 TC1 (2007) and TC2 (2012).
113\item The \bipref{iso}{../bips/lib/iso/index.html} dialect provides
114    a blend of ISO and ECLiPSe functionality.  All ISO features
115    are available, plus such ECLiPSe features that do not significantly
116    conflict with ISO.  As some of these extensions go beyond what the
117    letter of the standard allows, and because error checking may be
118    less strict than required by ISO, this is not a fully compliant mode.
119\item The \bipref{iso_light}{../bips/lib/iso_light/index.html} dialect
120    is the same as 'iso', with the exception of error handling.
121    This is sufficient for code that does not rely on a particular
122    form of error terms being thrown by built-in predicates.
123\end{itemize}
124The specification of implementation-defined features stipulated
125by the standard can be found in the reference manual for
126\bipref{iso_strict}{../bips/lib/iso_strict/index.html}.
127
128
129\subsection{Compiler versus interpreter}
130The following problem can occur despite the use of compatibility packages:
131If your program was written for an interpreter, e.g., C-Prolog,
132you have to be aware that {\eclipse} is a compiling system.
133There is a distinction between \emph{static} and \emph{dynamic} predicates.
134By default, a predicate is static. This means that its clauses have to be
135be compiled as a whole (they must not be spread over multiple files),
136its source code is not stored in the system,
137and it can not be modified (only recompiled as a whole).
138In contrast, a dynamic predicate may be modified by compiling or
139asserting new clauses and by retracting clauses.
140Its source code can be accessed using
141\biprefni{clause/1,2}{../bips/kernel/dynamic/clause-1.html}%
142\indextt{clause/1}\indextt{clause/2}
143or
144\biprefni{listing/0,1}{../bips/kernel/dynamic/listing-0.html}.%
145\indextt{listing/0}\indextt{listing/1}
146A predicate is dynamic when it is explicitly declared as such or when
147it was created using \bipref{assert/1}{../bips/kernel/dynamic/assert-1.html}.
148Porting programs from an interpreter usually requires the addition of
149some \notation{dynamic} declarations.
150In the worst case, when (almost) all procedures have to be dynamic,
151the flag \notation{all_dynamic} can be set instead.
152
153
154\section{Porting programs to plain {\eclipse}}
155If you want to use {\eclipse} to do further development of your application,
156it is probably advantageous to modify it such that it runs under plain
157{\eclipse}.
158In the following we summarise the main aspects that have to be considered
159when doing so.
160
161\begin{itemize}
162\item
163In general, it is almost always possible to add to your program
164a small routine that fixes the problem, rather than to modify
165the source of the application in many places.
166For example, name clashes are fixed more easily
167by using the \bipref{local/1}{../bips/kernel/modules/local-1.html} declaration
168rather than by renaming
169the clashing predicate in the whole application program.
170
171\item
172Due to lack of standardisation, some subtle differences in the
173syntax exist between Prolog systems. See \ref{syntaxdiff}
174for details. {\eclipse} has a number of options that make it possible
175to configure its behaviour as desired.
176
177\item
178{\eclipse} has the \notation{string} data type which is not present in Prolog
179of the Edinburgh family.
180Double-quoted items are parsed as strings in {\eclipse}, while they are
181lists of integers in other systems and when the compatibility
182packages are used (cf.\ chapter \ref{chapstring}).
183
184\item
185I/O predicates of the \predspec{see} and \predspec{tell} group are not built-ins
186in {\eclipse}, but they are provided in the \libspec{cio} library.
187Call \notation{lib(cio)} in order to have them available (cf.\ appendix A).
188Similarly for \bipref{numbervars/3}{../bips/lib/numbervars/index.html}.
189
190\item
191In {\eclipse}, some built-ins raise events in cases where they just fail
192in other systems, e.g., \notation{arg(1,~2,~X)} fails in C-Prolog, but
193raises a type error in {\eclipse}.
194If some code relies on such behaviour, it is best to modify it by
195adding an explicit check like
196\begin{verbatim}
197        ..., compound(T), arg(N, T, X), ...
198\end{verbatim}
199
200Another alternative is to redefine the \predspec{arg/3} built-in, using
201\predspec{:/2} to access the original version:
202\begin{verbatim}
203:- local arg/3.
204arg(N, T, X) :-
205        compound(X),
206	eclipse_language:arg(N, T, X).
207\end{verbatim}
208
209A third alternative
210is to define an error handler which will fail the predicate
211whenever the event is raised. In this case:
212\begin{verbatim}
213my_type_error(_, arg(_, _, _)) :- !, fail.
214my_type_error(E, Goal) :- error(default(E), Goal).
215:- set_error_handler(5, my_type_error/2).
216\end{verbatim}
217
218\item As the {\eclipse} compiler does not accept procedures whose clauses
219are not consecutive in a file, it may be necessary to add
220\bipref{discontiguous/1}{../bips/kernel/compiler/discontiguous-1.html}
221directives if you want to compile such procedures.
222
223\end{itemize}
224
225
226\section{Exploiting the features of {\eclipse}}
227When rewriting existing applications as well as when writing new programs,
228it is useful to bear in mind important {\eclipse} features which can make
229programs easier to write and/or faster:
230\begin{itemize}
231\item Compiler features relevant for performance can be found in section
232\ref{secefficientcode}.
233
234\item Use {\eclipse}'s nonlogical
235\bipref{storage}{../bips/kernel/storage/index.html} facilities
236(section \ref{secarrays}),
237which are usually more suitable to store permanent data than
238\bipref{assert/1}{../bips/kernel/dynamic/assert-1.html} is, and are usually
239faster.
240
241\item {\eclipse} has a number of language extensions which make programming
242easier, see chapter \ref{chaplanguage}.
243
244\item The predicates \predspec{get_flag/2}, \predspec{get_flag/3},
245\predspec{get_file_info/3}, \predspec{get_stream_info/3} and\\
246\predspec{get_var_info/3}
247give a lot of useful information about the system and the data.
248
249\item The {\eclipse} macros often help to solve syntactic problems
250(see chapter \ref{chapmacros}).
251
252\item The {\tkeclipse} GUI provides many features that should make
253developing programs easier than with the traditional tty interface.
254
255\item It is worth familiarising oneself with the debugger's features,
256see chapter \ref{chapdebug}.
257
258\item {\eclipse} is highly customizable, even problems which seemingly
259require modification of the {\eclipse} sources
260can very often be solved at the Prolog level.
261\end{itemize}
262
263%HEVEA\cutend
264