]> matita.cs.unibo.it Git - helm.git/blob - helm/style/params.xsl
- the mathql interpreter is not helm-dependent any more
[helm.git] / helm / style / params.xsl
1 <?xml version="1.0"?>
2
3 <!-- Copyright (C) 2000, HELM Team                                     -->
4 <!--                                                                   -->
5 <!-- This file is part of HELM, an Hypertextual, Electronic            -->
6 <!-- Library of Mathematics, developed at the Computer Science         -->
7 <!-- Department, University of Bologna, Italy.                         -->
8 <!--                                                                   -->
9 <!-- HELM is free software; you can redistribute it and/or             -->
10 <!-- modify it under the terms of the GNU General Public License       -->
11 <!-- as published by the Free Software Foundation; either version 2    -->
12 <!-- of the License, or (at your option) any later version.            -->
13 <!--                                                                   -->
14 <!-- HELM is distributed in the hope that it will be useful,           -->
15 <!-- but WITHOUT ANY WARRANTY; without even the implied warranty of    -->
16 <!-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     -->
17 <!-- GNU General Public License for more details.                      -->
18 <!--                                                                   -->
19 <!-- You should have received a copy of the GNU General Public License -->
20 <!-- along with HELM; if not, write to the Free Software               -->
21 <!-- Foundation, Inc., 59 Temple Place - Suite 330, Boston,            -->
22 <!-- MA  02111-1307, USA.                                              -->
23 <!--                                                                   -->
24 <!-- For details, see the HELM World-Wide-Web page,                    -->
25 <!-- http://cs.unibo.it/helm/.                                         -->
26
27 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
28                               xmlns:m="http://www.w3.org/1998/Math/MathML">
29
30 <!--***********************************************************************--> 
31 <!-- auxiliary functions                                                   -->
32 <!-- HELM Group: Asperti, Padovani, Sacerdoti, Schena                      -->
33 <!--***********************************************************************--> 
34
35 <!--***********************************************************************-->
36 <!-- get the name from a URI                                               -->
37 <!--***********************************************************************-->
38
39 <!-- CSC: PROBLEMA: URI CHE NON CONTENGONO / ED INIZIANO CON cic: -->
40 <xsl:template name="name_of_uri">
41  <xsl:param name="uri" select="&quot;&quot;"/>
42  <xsl:variable name="suffix" select="substring-after($uri, &quot;/&quot;)"/>
43  <xsl:choose>
44   <xsl:when test="$suffix = &quot;&quot;">
45    <!-- CSC: PROBLEMA: .con PUO' APPARIRE ALL'INTERNO DELLE URI ===>
46      SCRIVERE UNA FUNZIONE RICORSIVA CHE RISOLVA -->
47    <xsl:value-of select="substring-before($uri,&quot;.con&quot;)"/>
48   </xsl:when>
49   <xsl:otherwise>
50    <xsl:call-template name="name_of_uri">
51     <xsl:with-param name="uri" select="$suffix"/>
52    </xsl:call-template>
53   </xsl:otherwise>
54  </xsl:choose>
55 </xsl:template>
56
57 <!--***********************************************************************-->
58 <!-- erase common prefix from two uris                                     -->
59 <!--***********************************************************************-->
60
61 <xsl:template name="common_prefix">
62  <xsl:param name="first_uri" select="&quot;&quot;"/>
63  <xsl:param name="second_uri" select="&quot;&quot;"/>
64  <xsl:choose>
65   <xsl:when test="(substring-before($first_uri,&quot;/&quot;) = 
66                 substring-before($second_uri,&quot;/&quot;) and 
67                 substring-after($second_uri,&quot;/&quot;) != &quot;&quot;)">
68    <xsl:call-template name="common_prefix">
69     <xsl:with-param 
70         name="first_uri" select="substring-after($first_uri,&quot;/&quot;)"/>
71     <xsl:with-param 
72         name="second_uri" select="substring-after($second_uri,&quot;/&quot;)"/>    </xsl:call-template>
73   </xsl:when>
74   <xsl:otherwise>
75    <xsl:call-template name="slash_counting">
76     <xsl:with-param name="uri" select="$second_uri"/>
77     <xsl:with-param name="counter" select="0"/>
78    </xsl:call-template>
79   </xsl:otherwise>   
80  </xsl:choose>
81 </xsl:template>
82
83 <xsl:template name="slash_counting">
84  <xsl:param name="uri" select="&quot;&quot;"/>
85  <xsl:param name="counter" select="0"/>
86  <xsl:choose>
87   <xsl:when test="(substring-after($uri,&quot;/&quot;) != &quot;&quot;)">
88    <xsl:call-template name="slash_counting">
89     <xsl:with-param 
90         name="uri" select="substring-after($uri,&quot;/&quot;)"/>
91     <xsl:with-param
92         name="counter" select="$counter +1"/> 
93    </xsl:call-template>
94   </xsl:when>
95   <xsl:otherwise>
96    <xsl:value-of select="$counter"/>
97   </xsl:otherwise>
98  </xsl:choose>   
99 </xsl:template>
100
101 <xsl:template name="blank_counting">
102  <xsl:param name="string" select="&quot;&quot;"/>
103  <xsl:param name="counter" select="0"/>
104  <xsl:choose>
105   <xsl:when test="(substring-after($string,&quot; &quot;) != &quot;&quot;)">
106    <xsl:call-template name="blank_counting">
107     <xsl:with-param 
108         name="string" select="substring-after($string,&quot; &quot;)"/>
109     <xsl:with-param 
110         name="counter" select="$counter +1"/> 
111    </xsl:call-template>
112   </xsl:when>
113   <xsl:otherwise>
114    <xsl:value-of select="$counter + 1"/>
115   </xsl:otherwise> 
116  </xsl:choose>  
117 </xsl:template>
118
119 <xsl:template name="double_point_counting">
120  <xsl:param name="string" select="&quot;&quot;"/>
121  <xsl:param name="counter" select="0"/>
122  <xsl:choose>
123   <xsl:when test="(substring-after($string,&quot;:&quot;) != &quot;&quot;)">
124    <xsl:call-template name="double_point_counting">
125     <xsl:with-param 
126         name="string" select="substring-after($string,&quot;:&quot;)"/>
127     <xsl:with-param 
128         name="counter" select="$counter +1"/> 
129    </xsl:call-template>
130   </xsl:when>
131   <xsl:otherwise>
132    <xsl:value-of select="$counter"/>
133   </xsl:otherwise> 
134  </xsl:choose>  
135 </xsl:template>
136
137 <xsl:template name="min">
138  <xsl:param name="string" select="&quot;&quot;"/>
139  <xsl:param name="counter" select="0"/>
140  <xsl:choose>
141   <xsl:when test="contains($string,concat($counter,&quot;:&quot;))
142          or (0 > $counter)">
143   <xsl:value-of select="$counter"/>
144   </xsl:when>
145   <xsl:otherwise>
146    <xsl:call-template name="min">
147     <xsl:with-param 
148         name="string" select="$string"/>
149     <xsl:with-param 
150         name="counter" select="$counter -1"/> 
151    </xsl:call-template>
152   </xsl:otherwise>
153  </xsl:choose>  
154 </xsl:template>
155
156 <xsl:template name="get_no_params">
157     <xsl:param name="first_uri" select="&quot;&quot;"/>
158     <xsl:param name="second_uri" select="&quot;&quot;"/>
159      <xsl:variable name="offset">
160       <xsl:call-template name="common_prefix">
161        <xsl:with-param name="first_uri" select="$first_uri"/>
162        <xsl:with-param name="second_uri" select="$second_uri"/>
163       </xsl:call-template>
164      </xsl:variable>
165      <xsl:choose>
166           <xsl:when test="$offset > 0">
167            <xsl:variable name="params"> 
168         <xsl:variable name="second_url">
169                         <xsl:call-template name="URLofURI4getter">
170                                 <xsl:with-param name="uri" select="$second_uri"/>
171                         </xsl:call-template>
172                 </xsl:variable>
173         <xsl:value-of select="document($second_url)/*/@params"/>
174        </xsl:variable>
175        <xsl:variable name="minimum">
176         <xsl:call-template name="min">
177          <xsl:with-param name="string" select="$params"/>
178          <xsl:with-param name="counter" select="$offset - 1"/>
179         </xsl:call-template>
180        </xsl:variable>
181        <xsl:choose>
182         <xsl:when test="0 > $minimum">0</xsl:when>
183         <xsl:otherwise>
184          <xsl:variable name="relevant_params">
185           <!-- the blank after : in the next line is essential -->
186           <xsl:value-of 
187             select="substring-after($params,concat($minimum,&quot;: &quot;))"/>
188          </xsl:variable>
189          <xsl:variable name="tokens">
190           <xsl:call-template name="blank_counting">
191            <xsl:with-param name="string" select="$relevant_params"/>
192            <xsl:with-param name="counter" select="0"/>
193           </xsl:call-template>
194          </xsl:variable>
195          <xsl:variable name="separators">
196           <xsl:call-template name="double_point_counting">
197            <xsl:with-param name="string" select="$relevant_params"/>
198            <xsl:with-param name="counter" select="0"/>
199           </xsl:call-template>
200          </xsl:variable>
201          <xsl:value-of select="number($tokens - $separators)"/>
202         </xsl:otherwise>
203        </xsl:choose>
204       </xsl:when>
205       <xsl:otherwise>0</xsl:otherwise>
206      </xsl:choose>
207 </xsl:template>
208
209
210 <!--***********************************************************************-->
211 <!--  Insert a subscript if there is a number at the end of a ci element   -->
212 <!--***********************************************************************-->
213
214 <xsl:template name="insert_subscript">
215 <xsl:param name="node_value" select="&quot;&quot;"/>
216 <xsl:param name="current_pos" select="1"/>
217 <xsl:param name="start_pos" select="0"/>
218     <xsl:choose>
219     <xsl:when test="$current_pos &lt;= string-length(string($node_value))">
220     <xsl:variable name="current_char"><xsl:value-of select="substring(string($node_value),$current_pos,1)"/></xsl:variable>
221      <xsl:choose>
222      <xsl:when test="(string($current_char) != &quot;0&quot;) and (string($current_char) != &quot;1&quot;) and (string($current_char) != &quot;2&quot;) and (string($current_char) != &quot;3&quot;) and (string($current_char) != &quot;4&quot;) and (string($current_char) != &quot;5&quot;) and (string($current_char) != &quot;6&quot;) and (string($current_char) !=  &quot;7&quot;) and (string($current_char) != &quot;8&quot;) and (string($current_char) != &quot;9&quot;)">
223       <xsl:choose> 
224       <xsl:when test="$start_pos != 0">
225        <xsl:call-template name="insert_subscript"><xsl:with-param name="node_value" select="$node_value"/><xsl:with-param name="current_pos" select="$current_pos + 1"/><xsl:with-param name="start_pos" select="0"/></xsl:call-template>
226       </xsl:when> 
227       <xsl:otherwise>
228        <xsl:call-template name="insert_subscript"><xsl:with-param name="node_value" select="$node_value"/><xsl:with-param name="current_pos" select="$current_pos + 1"/><xsl:with-param name="start_pos" select="$start_pos"/></xsl:call-template>
229       </xsl:otherwise>
230       </xsl:choose>
231      </xsl:when>
232      <xsl:otherwise>  
233       <xsl:choose>
234       <xsl:when test="$start_pos = 0">
235        <xsl:call-template name="insert_subscript"><xsl:with-param name="node_value" select="$node_value"/><xsl:with-param name="current_pos" select="$current_pos + 1"/><xsl:with-param name="start_pos" select="$current_pos"/></xsl:call-template>
236       </xsl:when>
237       <xsl:otherwise>
238        <xsl:call-template name="insert_subscript"><xsl:with-param name="node_value" select="$node_value"/><xsl:with-param name="current_pos" select="$current_pos + 1"/><xsl:with-param name="start_pos" select="$start_pos"/></xsl:call-template>
239       </xsl:otherwise>
240       </xsl:choose>      
241      </xsl:otherwise>
242      </xsl:choose>
243     </xsl:when>
244     <xsl:otherwise>
245      <xsl:choose>
246      <xsl:when test="$start_pos != 0">
247       <m:msub>
248        <m:mi><xsl:value-of select="substring(string($node_value),1,$start_pos -1)"/></m:mi>
249        <m:mn><xsl:value-of select="substring(string($node_value),$start_pos)"/></m:mn>
250        </m:msub>
251      </xsl:when>
252      <xsl:otherwise>
253       <xsl:value-of select="$node_value"/>
254      </xsl:otherwise>
255      </xsl:choose>
256     </xsl:otherwise>    
257     </xsl:choose>
258 </xsl:template>
259
260
261 <!--*******************************************-->
262 <!--    ABSTRACTING PARAMETERS AND COUNTING    -->
263 <!--*******************************************-->
264 <!-- Si dimentica i CAST dei termini che astrae. Nel caso dell'astrazione -->
265 <!-- dei lambda dei pattern del CASE, qualora i lambda non si trovino     -->
266 <!-- nella forma weak-head, astrae solo i lambda che trova e restituisce  -->
267 <!-- un corpo depurato da tutti i primi cast che precedono il termine     -->
268 <!-- restituito.                                                          -->
269
270 <xsl:template match="*" mode="abstparams">
271 <xsl:param name="noparams" select="0"/>
272 <xsl:param name="target" select="0"/>
273 <xsl:param name="binder">PROD</xsl:param>
274     <xsl:choose>
275     <xsl:when test="($noparams != 0) and ((name(.)=string($binder)) or (name(.)=&quot;CAST&quot;))">
276      <xsl:choose>
277      <xsl:when test="name(.) = string($binder)">
278       <xsl:if test="$target = 0">
279        <xsl:choose>
280        <xsl:when test="string($binder) = &quot;LAMBDA&quot;">
281         <m:ci>
282          <xsl:call-template name="insert_subscript"><xsl:with-param name="node_value"><xsl:value-of select="target/@binder"/></xsl:with-param></xsl:call-template>
283         </m:ci>
284        </xsl:when>
285        <xsl:otherwise> 
286         <Param name="{target/@binder}">
287          <xsl:apply-templates select="source"/>
288         </Param>
289        </xsl:otherwise>
290        </xsl:choose>
291       </xsl:if>
292       <xsl:apply-templates select="target/*[1]" mode="abstparams">
293        <xsl:with-param name="noparams" select="$noparams - 1"/>
294        <xsl:with-param name="target" select="$target"/>
295        <xsl:with-param name="binder" select="$binder"/>
296       </xsl:apply-templates>
297      </xsl:when>
298      <xsl:otherwise>
299       <xsl:apply-templates select="term/*[1]" mode="abstparams">
300        <xsl:with-param name="noparams" select="$noparams"/>
301        <xsl:with-param name="target" select="$target"/>
302        <xsl:with-param name="binder" select="$binder"/>
303       </xsl:apply-templates>
304      </xsl:otherwise>
305      </xsl:choose>
306     </xsl:when>
307     <xsl:otherwise> 
308      <xsl:choose>
309      <xsl:when test="($target = 1) and ($noparams != 0)">
310       <m:apply>
311       <m:csymbol>app</m:csymbol>
312 <!-- Mancava modalita': sono all'interno di un termine -->
313       <xsl:apply-templates select="." mode="pure"/>
314       <xsl:call-template name="printparam"><xsl:with-param name="noleft" select="$noparams"/></xsl:call-template>
315       </m:apply>
316      </xsl:when>
317      <xsl:otherwise>
318       <xsl:choose>
319       <xsl:when test="$noparams != 0">
320       <xsl:call-template name="printparam"><xsl:with-param name="noleft" select="$noparams"/></xsl:call-template>
321       </xsl:when>
322       <xsl:otherwise>
323        <xsl:if test="$target = 1">
324 <!-- Mancava modalita': con target=1 posso provenire sia da un oggetto che da un termine -->
325         <xsl:choose>
326         <xsl:when test="string($binder) = &quot;LAMBDA&quot;">
327          <!-- CSC: era pure, ma deve essere noannot. Giusto, Irene? -->
328          <xsl:apply-templates select="." mode="noannot"/>
329         </xsl:when>
330         <xsl:otherwise>
331          <xsl:apply-templates select="."/>
332         </xsl:otherwise>
333         </xsl:choose>
334        </xsl:if>
335       </xsl:otherwise>
336       </xsl:choose>
337      </xsl:otherwise>
338      </xsl:choose>
339     </xsl:otherwise>
340     </xsl:choose>
341 </xsl:template>
342
343 <xsl:template name="printparam">
344 <xsl:param name="noleft" select="0"/>
345 <xsl:param name="number" select="1"/>
346     <xsl:if test="$noleft != 0">
347      <m:ci><xsl:call-template name="insert_subscript"><xsl:with-param name="node_value">$<xsl:value-of select="$number"/></xsl:with-param></xsl:call-template></m:ci>
348      <xsl:call-template name="printparam"><xsl:with-param name="noleft" select="$noleft - 1"/><xsl:with-param name="number" select="$number + 1"/></xsl:call-template>  
349     </xsl:if>
350 </xsl:template>
351
352 <xsl:template match="*" mode="counting">
353 <xsl:param name="noparams" select="0"/>
354 <xsl:param name="count" select="0"/>
355  <xsl:choose>
356  <xsl:when test="name(.) = &quot;PROD&quot;">
357   <xsl:apply-templates select="target/*[1]" mode="counting">
358    <xsl:with-param name="noparams" select="$noparams"/>
359    <xsl:with-param name="count" select="$count + 1"/>
360   </xsl:apply-templates>
361  </xsl:when>
362  <xsl:when test="name(.) = &quot;CAST&quot;">
363   <xsl:apply-templates select="term/*[1]" mode="counting">
364    <xsl:with-param name="noparams" select="$noparams"/>
365    <xsl:with-param name="count" select="$count"/> 
366   </xsl:apply-templates>
367  </xsl:when>
368  <xsl:otherwise>
369   <xsl:value-of select="$count - $noparams"/>
370  </xsl:otherwise>
371  </xsl:choose>
372 </xsl:template>
373
374 </xsl:stylesheet>