Mercurial > hg > plml
comparison pldoc/plml.tex @ 2:546bfd3988b0
Added unpolished pldoc documentation.
author | samer |
---|---|
date | Fri, 13 Jan 2012 15:53:04 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1:4d183f2855c2 | 2:546bfd3988b0 |
---|---|
1 \documentclass[10pt]{article} | |
2 \usepackage{pldoc} | |
3 \sloppy | |
4 \makeindex | |
5 | |
6 \begin{document} | |
7 % This LaTeX document was generated using the LaTeX backend of PlDoc, | |
8 % The SWI-Prolog documentation system | |
9 | |
10 | |
11 | |
12 \section{plml: Prolog-Matlab interface} | |
13 | |
14 \label{sec:plml} | |
15 | |
16 \begin{tags} | |
17 \tag{To be done} | |
18 Use mat(I) and tmp(I) as types to include engine Id. | |
19 | |
20 Clarify relationship between return values and valid Matlab denotation. | |
21 | |
22 Reshape/2 array representation: reshape([ ... ],Size) | |
23 Expression language: arr(Vals,Shape,InnerFunctor) - allows efficient | |
24 representation of arrays of arbitrary things. Will require more strict | |
25 nested list form. | |
26 | |
27 Deprecate old array(Vals::Type) and cell(Vals::Type) left-value syntax. | |
28 | |
29 Remove I from \dcgref{ml_expr}{2} and add to mx type? | |
30 \end{tags} | |
31 | |
32 \paragraph{Types} | |
33 | |
34 \textbf{ml_eng} - Any atom identifying a Matlab engine. | |
35 | |
36 \textbf{ml_stmt} - A Matlab statement | |
37 | |
38 \begin{code} | |
39 X;Y :: ml_stmt :- X:ml_stmt, Y:ml_stmt. | |
40 X,Y :: ml_stmt :- X:ml_stmt, Y:ml_stmt. | |
41 X=Y :: ml_stmt :- X:ml_lval, Y:ml_expr. | |
42 hide(X) :: ml_stmt :- X:ml_stmt. | |
43 \end{code} | |
44 | |
45 \begin{code} | |
46 ml_expr(A) % A Matlab expression, possibly with multiple return values | |
47 ml_loc ---> mat(atom,atom). % Matbase locator | |
48 \end{code} | |
49 | |
50 \paragraph{Matlab expression syntax} | |
51 | |
52 The Matlab expression syntax adopted by this module allows Prolog terms to represent | |
53 or denote Matlab expressions. Let T be the domain of recognised Prolog terms (corresponding to | |
54 the type ml_expr), and M be the domain of Matlab expressions written in Matlab syntax. | |
55 Then V : T\Sifthen{}M is the valuation function which maps Prolog term X to Matlab expression V[X]. | |
56 These are some of the constructs it recognises: | |
57 | |
58 Constructs valid only in top level statements, not subexpressions: | |
59 | |
60 \begin{code} | |
61 X;Y % |--> V[X]; V[Y] (sequential evaluation hiding first result) | |
62 X,Y % |--> V[X], V[Y] (sequential evaluation displaying first result) | |
63 X=Y % |--> V[X]=V[Y] (assignment, X must denote a valid left-value) | |
64 hide(X) % |--> V[X]; (execute X but hide return value) | |
65 \end{code} | |
66 | |
67 Things that look and work like Matlab syntax (more or less): | |
68 | |
69 \begin{code} | |
70 +X % |--> uplus(V[X]) | |
71 -X % |--> uminus(V[X]) | |
72 X+Y % |--> plus(V[X],V[Y]) | |
73 X-Y % |--> minus(V[X],V[Y]) | |
74 X^Y % |--> mpower(V[X],V[Y]) | |
75 X*Y % |--> mtimes(V[X],V[Y]) | |
76 X/Y % |--> mrdivide(V[X],V[Y]) | |
77 X\Y % |--> mldivide(V[X],V[Y]) | |
78 X.^Y % |--> power(V[X],V[Y]) | |
79 X.*Y % |--> times(V[X],V[Y]) | |
80 X./Y % |--> rdivide(V[X],V[Y]) | |
81 X.\Y % |--> ldivide(V[X],V[Y]) | |
82 X:Y:Z % |--> colon(V[X],V[Y],V[Z]) | |
83 X:Z % |--> colon(V[X],V[Z]) | |
84 X>Z % |--> gt(V[X],V[Y]) | |
85 X>=Z % |--> ge(V[X],V[Y]) | |
86 X<Z % |--> lt(V[X],V[Y]) | |
87 X=<Z % |--> le(V[X],V[Y]) | |
88 X==Z % |--> eq(V[X],V[Y]) | |
89 [X1,X2,...] % |--> [ V[X1], V[X2], ... ] | |
90 [X1;X2;...] % |--> [ V[X1]; V[X2]; ... ] | |
91 {X1,X2,...} % |--> { V[X1], V[X2], ... } | |
92 {X1;X2;...} % |--> { V[X1]; V[X2]; ... } | |
93 @X % |--> @V[X] (function handle) | |
94 \end{code} | |
95 | |
96 Things that do not look like Matlab syntax but provide standard Matlab features: | |
97 | |
98 \begin{code} | |
99 'Infinity' % |--> inf (positive infinity) | |
100 'Nan' % |--> nan (not a number) | |
101 X`` % |--> ctranpose(V[X]) (conjugate transpose, V[X]') | |
102 X#Y % |--> getfield(V[X],V[q(Y)]) | |
103 X\\Y % |--> @(V[X])V[Y] (same as lambda(X,Y)) | |
104 \\Y % |--> @()V[Y] (same as thunk(Y)) | |
105 lambda(X,Y) % |--> @(V[X])V[Y] (anonymous function with arguments X) | |
106 thunk(Y) % |--> @()V[Y] (anonymous function with no arguments) | |
107 vector(X) % |--> horzcat(V[X1],V[X2], ...) | |
108 atvector(X) % as vector but assumes elements of X are assumed all atomic | |
109 cell(X) % construct 1xN cell array from elements of X | |
110 `X % same as q(X) | |
111 q(X) % wrap V[X] in single quotes (escaping internal quotes) | |
112 qq(X) % wrap V[X] in double quotes (escaping internal double quotes) | |
113 tq(X) % wrap TeX expression in single quotes (escape internal quotes) | |
114 \end{code} | |
115 | |
116 Referencing different value representations. | |
117 | |
118 \begin{code} | |
119 mat(X,Y) % denotes a value in the Matbase using a dbload expression | |
120 mx(X:mx_blob) % denotes an MX Matlab array in SWI memory | |
121 ws(X:ws_blob) % denotes a variable in a Matlab workspace | |
122 wsseq(X:ws_blob) % workspace variable containing list as cell array. | |
123 \end{code} | |
124 | |
125 Tricky bits. | |
126 | |
127 \begin{code} | |
128 apply(X,AX) % X must denote a function or array, applied to list of arguments AX. | |
129 cref(X,Y) % cell dereference, |--> V[X]{ V[Y1], V[Y2], ... } | |
130 arr(Lists) % multidimensional array from nested lists. | |
131 arr(Lists,Dims) % multidimensional array from nested lists. | |
132 \end{code} | |
133 | |
134 Things to bypass default formatting | |
135 | |
136 \begin{code} | |
137 noeval(_) % triggers a failure when processed | |
138 atom(X) % write atom X as write/1 | |
139 term(X) % write term X as write/1 | |
140 \(P) % escape and call phrase P directly to generate Matlab string | |
141 $(X) % calls pl2ml_hook/2, denotes V[Y] where plml_hook(X,Y). | |
142 '$VAR'(N) % gets formatted as p_N where N is assumed to be atomic. | |
143 \end{code} | |
144 | |
145 All other Prolog atoms are written using \predref{write}{1}, while other Prolog terms | |
146 are assumed to be calls to Matlab functions named according to the head functor. | |
147 Thus V[ $<$head$>$( $<$arg1$>$, $<$arg2$>$, ...) ] = $<$head$>$(V[$<$arg1$>$, V[$<$arg2$>$], ...). | |
148 | |
149 There are some incompatibilities between Matlab syntax and Prolog syntax, | |
150 that is, syntactic structures that Prolog cannot parse correctly: | |
151 | |
152 \begin{itemize} | |
153 \item 'Command line' syntax, ie where a function of string arguments: | |
154 "save('x','Y')" can be written as "save x Y" in Matlab, | |
155 but in Prolog, you must use function call syntax with quoted arguments: | |
156 save(`x,`'Y'). | |
157 \item Matlab's postfix transpose operator "x'" must be written using a different | |
158 posfix operator "x``" or function call syntax "ctranspose(x)". | |
159 \item Matlab cell referencing using braces, as in x\{1,2\} must be written | |
160 as "cref(x,1,2)". | |
161 \item Field referencing using dot (.), eg x.thing - currently resolved | |
162 by using hash (\#) operator, eg x\#thing. | |
163 \item Using variables as arrays and indexing them. The problem is that | |
164 Prolog doesn't let you write a term with a variable as the head | |
165 functor. | |
166 \end{itemize} | |
167 | |
168 \vspace{0.7cm} | |
169 | |
170 \begin{description} | |
171 \predicate[nondet,multifile]{matlab_init}{2}{-Key, -Cmd:ml_expr} | |
172 Each user-defined clause of \predref{matlab_init}{2} causes \arg{Cmd} to be executed | |
173 whenever a new Matlab session is started. | |
174 | |
175 \predicate[nondet,multifile]{matlab_path}{2}{-Key, -Path:list(atom)} | |
176 Each user-defined clause of \predref{matlab_path}{2} causes the directories in \arg{Path} | |
177 to be added to the Matlab path of every new Matlab session. Directories | |
178 are relative to the root directory as returned by Matlab function proot. | |
179 | |
180 \predicate[nondet,multifile]{pl2ml_hook}{2}{+X:term, -Y:ml_expr} | |
181 Clauses of \predref{pl2ml_hook}{2} allow for extensions to the Matlab expression | |
182 language such that \verb|V[$X] = V[Y]| if \verb$pl2ml_hook(X,Y)$. | |
183 | |
184 \predicate[det]{ml_open}{3}{+Id:ml_eng, +Host:atom, +Options:list(_)} | |
185 \nodescription | |
186 \predicate[det]{ml_open}{2}{+Id:ml_eng, +Host:atom} | |
187 \nodescription | |
188 \predicate[det]{ml_open}{1}{+Id:ml_eng} | |
189 Start a Matlab session on the given host. If \arg{Host}=localhost | |
190 or the name of the current current host as returned by \predref{hostname}{1}, | |
191 then a Matlab process is started directly. Otherwise, it is | |
192 started remotely via SSH. \arg{Options} defaults to []. \arg{Host} defaults to | |
193 localhost. | |
194 | |
195 Start a Matlab session on the specified host using default options. | |
196 If \arg{Host} is not given, it defaults to localhost. Session will be | |
197 associated with the given \arg{Id}, which should be an atom. See \predref{ml_open}{3}. | |
198 | |
199 Valid options are | |
200 | |
201 \begin{description} | |
202 \termitem{noinit}{} | |
203 If present, do not run initialisation commands specified by | |
204 \predref{matlab_path}{2} and \predref{matlab_init}{2} clauses. Otherwise, do run them. | |
205 \end{description} | |
206 | |
207 \begin{description} | |
208 \termitem{debug}{In, Out} | |
209 if present, Matlab is started in a script which captures standard | |
210 input and output to files In and Out respectively. | |
211 | |
212 [What if session is already open and attached to \arg{Id}?] | |
213 \end{description} | |
214 | |
215 \predicate[det]{ml_close}{1}{+Id:ml_eng} | |
216 Close Matlab session associated with \arg{Id}. | |
217 | |
218 \predicate[det]{ml_exec}{2}{+Id:ml_eng, +Expr:ml_expr} | |
219 Execute Matlab expression without returning any values. | |
220 | |
221 \predicate[det]{ml_eval}{4}{+Id:ml_eng, +Expr:ml_expr, +Types:list(type), -Res:list(ml_val)} | |
222 Evaluate Matlab expression binding return values to results list \arg{Res}. This new | |
223 form uses an explicit output types list, so \arg{Res} can be completely unbound on entry | |
224 even when multiple values are required. | |
225 | |
226 \predicate[semidet]{ml_test}{2}{+Id:ml_eng, +X:ml_expr(bool)} | |
227 Succeeds if \arg{X} evaluates to true in Matlab session \arg{Id}. | |
228 | |
229 \predicate[det]{===}{2}{Y:ml_vals(A), X:ml_expr(A)} | |
230 Evaluate Matlab expression \arg{X} as in \predref{ml_eval}{4}, binding one or more return values | |
231 to \arg{Y}. If \arg{Y} is unbound or a single ml_val(_), only the first return value is bound. | |
232 If \arg{Y} is a list, multiple return values are processed. | |
233 | |
234 \predicate[det]{??}{1}{X:ml_expr(_)} | |
235 Execute Matlab expression \arg{X} as with \predref{ml_exec}{2}, without returning any values. | |
236 | |
237 \predicate[semidet]{???}{1}{X:ml_expr(bool)} | |
238 Evaluate Matlab boolean expression \arg{X} as with \predref{ml_test}{2}. | |
239 | |
240 \predicate[det]{ml_debug}{1}{+Flag:boolean} | |
241 Set or reset debug state. \verb$ml_debug(true)$ causes formatted Matlab | |
242 statements to be printed before being sent to Matlab engine. | |
243 | |
244 \predicate[det]{term_mlstring}{3}{+Id:ml_eng, +X:ml_expr, -Y:list(code)} | |
245 Convert term representing Matlab expression to a list of character codes. | |
246 | |
247 \predicate[det]{term_texatom}{2}{+X:tex_expr, -Y:atom} | |
248 Convert term representing TeX expression to a string in atom form. | |
249 | |
250 \predicate[semidet]{wsvar}{3}{+X:ws_blob(A), -Nm:atom, -Id:ml_eng} | |
251 True if \arg{X} is a workspace variable in Matlab session \arg{Id}. | |
252 Unifies \arg{Nm} with the name of the Matlab variable. | |
253 | |
254 \predicate[det]{dropmat}{2}{+Id:ml_id, +Mat:ml_loc} | |
255 Deleting MAT file from matbase. | |
256 | |
257 \predicate[det]{exportmat}{3}{+Id:ml_id, +Mat:ml_loc, +Dir:atom} | |
258 Export specified MAT file from matbase to given directory. | |
259 | |
260 \predicate[nondet]{matbase_mat}{2}{+Id:ml_eng, -X:ml_loc} | |
261 Listing mat files actually in matbase at given root directory. | |
262 | |
263 \predicate[det]{persist_item}{2}{+X:ml_expr(A), -Y:ml_expr(A)} | |
264 Convert Matlab expression to persistent form not dependent on | |
265 current Matlab workspace or MX arrays in Prolog memory space. | |
266 Large values like arrays and structures are saved in the matbase | |
267 replaced with matbase locators. Scalar values are converted to | |
268 literal numeric values. Character strings are converted to Prolog atoms. | |
269 Cell arrays wrapped in the \predref{wsseq}{1} functor are converted to literal | |
270 form. | |
271 | |
272 NB. any side effects are undone on backtracking -- in particular, any | |
273 files created in the matbase are deleted. | |
274 | |
275 \predicate[det]{mhelp}{1}{+Name:atom} | |
276 Lookup Matlab help on the given name. Equivalent to executing help(`X). | |
277 | |
278 \predicate[det]{compileoptions}{2}{+Opts:list(ml_options), -Prefs:ml_expr(options)} | |
279 Convert list of option specifiers into a Matlab expression representing | |
280 options (ie a struct). Each specifier can be a Name:Value pair, a name | |
281 to be looked up in the \predref{optionset}{2} predicate, a nested list of ml_options | |
282 compileoptions :: list (optionset \Sbar{} atom:value \Sbar{} struct) \Sifthen{} struct. | |
283 NB. option types are as follows: | |
284 | |
285 \begin{code} | |
286 X :: ml_options :- optionset(X,_). | |
287 X :: ml_options :- X :: ml_option(_). | |
288 X :: ml_options :- X :: list(ml_options). | |
289 X :: ml_options :- X :: ml_expr(struct(_)). | |
290 | |
291 ml_option(A) ---> atom:ml_expr(A). | |
292 \end{code} | |
293 | |
294 \predicate[det]{multiplot}{2}{+Type:ml_plot, +Cmds:list(ml_expr(_))} | |
295 \nodescription | |
296 \predicate[det]{multiplot}{3}{+Type:ml_plot, +Cmds:list(ml_expr(_)), -Axes:list(ml_val(handle))} | |
297 Executes plotting commands in \arg{Cmds} in multiple figures or axes as determined | |
298 by \arg{Type}. Valid types are: | |
299 | |
300 \begin{description} | |
301 \termitem{figs}{Range} | |
302 Executes each plot in a separate figure, Range must be P..Q where P | |
303 and Q are figure numbers. | |
304 \termitem{vertical}{} | |
305 Executes each plot in a subplot; | |
306 subplots are arranged vertically top to bottom in the current figure. | |
307 \termitem{horizontal}{} | |
308 Executes each plot in a subplot; | |
309 subplots are arranged horizontally left to right in the current figure. | |
310 \termitem{\Sdot}{Type, [link(Axis)]} | |
311 As for multplot type \arg{Type}, but link X or Y axis scales as determined by Axis, | |
312 which can be `x, `y, or `xy. | |
313 \end{description} | |
314 | |
315 Three argument form returns a list containing the Matlab handles to axes objects, | |
316 one for each plot. | |
317 | |
318 \predicate[semidet,multifile]{optionset}{2}{+Key:term, -Opts:list(ml_options)} | |
319 Extensible predicate for mapping arbitrary terms to a list of options | |
320 to be processed by \predref{compileoptions}{2}. | |
321 \end{description} | |
322 | |
323 | |
324 \printindex | |
325 \end{document} |