-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.dtx
5466 lines (5465 loc) · 201 KB
/
export.dtx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%
% \iffalse driver
%<*driver>
\documentclass{mtmtcl}
\begin{document}
\tableofcontents
\DocInput{export.dtx}
\end{document}
%</driver>
% \fi
%
%
% ^^A \part{Conversion of exported data}
%
%
% The basic format for exporting structure elements is as a
% data-tree, so that further processing is easy in \Tcl. An important
% technique for further processing is data-is-code, meaning the
% \word{tag}s in the tree are defined as \Tcl\ commands in some
% namespace (each performing a particular operation on \emph{its}
% kind of node) and the data-tree is processed through |eval|uation
% of it in that particular namespace. Among the operations which can
% be implemented this way is conversion to XML, although if
% readability of the result is of interest then it is probably better
% to let the \textsf{tdom} package do this conversion.
%
% \changes{0}{2008/10/01}{Prioritising OpenMath over MathML.
% Introducing the idea of \emph{temporary} conversions from
% OpenMath node types to specialised node types. (LH)}
%
% The node types (\word{tag}s) used should primarily be those defined
% in the OpenMath standard, but there are exceptions (e.g.~|integer|)
% in which a more \Tcl-native encoding is preferable. Specific
% processing tasks may also benefit if specialised node types are
% used instead of the generic |OMA|~(OpenMath function application),
% since data-is-code can only do dispatching based on the \word{tag}
% of the node. This division is however overcome by providing
% operations that transform data-trees between the generic OpenMath
% format and more specialised formats; neither form is irreversible,
% and the OpenMath one should be considered normal.
%
% For processing to the end of rendering data, it is often
% interesting to incorporate presentation-oriented information in the
% data-tree. Here it is preferable to use MathML-elements to encode
% such information, when there is one which is suitable for the
% problem at hand (e.g.~|mfenced| for bracketing subexpressions).
% MathML does however seem less useful as a \emph{standard} for the
% data.
%
% Conversions to presentation formats should \emph{not} rely on having
% a big built-in table of OpenMath symbols and how they are
% rendered\Dash the converter should instead expect to find all the
% necessary information provided in the data-tree to convert
% (e.g.~as attributions attached to the \OMSref{altenc}{LaTeX_encoding}
% symbol). Tools should be provided for scanning trees for symbols
% without such information and for adding such information to
% selected symbols, but it may in many cases be better to have it
% supplied already by the original |export| method.
%
% \stringtypeheading{node type}{Data-tree node types:}
% \stringtypeheading{attribute}{Data-tree attributes:}
%
%
% \section{Export data details}
%
% The basic format of data-trees encoding \mtl\ structure elements
% is mostly OpenMath, i.e., there are node types:
% \begin{description}
% \item[{\describestring*+[node type]{OMA}}]
% for function applications, where the first child is the
% function being applied and subsequent children are its
% arguments;
% \item[{\describestring*+[node type]{OMS}}]
% for general symbols, precisely identified through
% \describestring+[attribute]{name} and
% \describestring+[attribute]{cd} (Content Dictionary) attributes;
% \item[{\describestring*+[node type]{OMV}}]
% for variables, distinguished through the
% \describestring+[attribute]{name} attribute;
% \item[{\describestring*+[node type]{OMF}}]
% for floating-point numbers (IEEE |double|s), with the value
% given by a \describestring+[attribute]{dec} or
% \describestring+[attribute]{hex} attribute;
% \end{description}
% and others (|OMBIND|, |OMSTR|, etc.), but one that should normally
% not be used is \describestring+[node type]{OMI} for integers, since
% its general structure
% \begin{displaysyntax}
% OMI |{} {{#text| \word{string}|}}|
% \end{displaysyntax}
% is unnecessarily awkward to interpret programmatically in
% \Tcl~(partly because the forms allowed in the \word{string} are not
% the same as \Tcl\ understands for integers). Instead the
% \describestring+[node type]{integer} node type should be used,
% which has the format
% \begin{displaysyntax}
% integer |{|value \word{integer}|} {}|
% \end{displaysyntax}
% i.e., the value is given as a \describestring+[attribute]{value}
% attribute, whose allowed forms are precisely those that \Tcl\
% understands.
%
% The |OMS|, |OMV|, |OMF|, and |OMR| node types cannot have children
% in OpenMath, so it is convenient to sometimes\Ldash particularly
% during rendering\Dash use the first child of these nodes as
% ``contents'' of the node even though they are conceptually atomic.
%
% What the above describes is thus a sort of ``near-OpenMath''.
% Basic operations related to it can be found in the
% |mtmtcl::openmath| package and namespace.
% \begin{tcl}
%<docstrip.tcl::catalogue>pkgIndex openmath
%<*openmath>
package require Tcl 8.5
package provide mtmtcl::openmath 1.1
% \end{tcl}
%
%
% \setnamespace{mtmtcl::openmath}
%
% \begin{proc}{tailcall}
% In some places below the |tailcall| command is used because it
% is the most efficient way of achieving what needs to be done, but
% this does not prevent supporting \Tcllogo\,8.5 because the
% necessary aspects of the core |tailcall| command can be faked
% there.
% \begin{tcl}
namespace eval mtmtcl::openmath {
if {[namespace which tailcall] eq ""} then {
proc tailcall {args} {
uplevel 2 [list ::catch $args ::mtmtcl::openmath::res\
::mtmtcl::openmath::opts]
dict incr ::mtmtcl::openmath::opts -level 2
return -options $::mtmtcl::openmath::opts\
$::mtmtcl::openmath::res
}
}
}
% \end{tcl}
% \end{proc}
%
%
% \subsection{Generating (near-)OpenMath}
%
% \begin{ensemble}{OM}
% While it is possible to construct (near-)OpenMath objects using
% nothing but |list|, it is more convenient to have a specialised
% constructor that takes care of the double nesting arising from
% having both the data-is-code list and the list-of-children list;
% forgetting one is a common typo, especially when there is exactly
% one child. The |OM| ensemble has subcommands for constructing the
% basic types of node, with names generally formed by dropping the
% |OM| prefix from the corresponding OpenMath tag. This ensemble is
% exported by the |mtmtcl::openmath| namespace.
% \begin{tcl}
namespace eval mtmtcl::openmath {
namespace eval OM {
namespace export *
namespace ensemble create
}
namespace export OM
}
% \end{tcl}
%
% \begin{ensproc}{S}
% The |OM S| command is thus for constructing |OMS| nodes. It has
% the syntax
% \begin{displaysyntax}
% OM S \word{cd} \word{name} \begin{regblock}[\regstar]
% \word{attribute} \word{value} \end{regblock}
% \end{displaysyntax}
% where the content dictionary \word{cd} and symbol name
% \word{name} are mandatory arguments. Additional attributes
% (e.g. |cdbase|) can be specified in additional key--value pairs
% of arguments.
% \begin{tcl}
proc mtmtcl::openmath::OM::S {cd name args} {
list OMS [dict create cd $cd name $name {*}$args] {}
}
% \end{tcl}
% \end{ensproc}
%
% \begin{ensproc}{mS}
% The |OM mS| command is again for constructing |OMS| nodes, but
% this time to conform to the \APIref+{export}{2.0} interface
% handling of the |mtmtcl:path| attribute. It has the syntax
% \begin{displaysyntax}
% OM mS \word{cd} \word{name} \word{common attributes}
% \word{suffix} \begin{regblock}[\regstar]
% \word{attribute} \word{value} \end{regblock}
% \end{displaysyntax}
% where the content dictionary \word{cd} and symbol name
% \word{name} are as for |OM S|. The \word{common attributes} is
% an inherited dictionary of attributes which is getting
% overridden, and the \word{suffix} is a list of elements to
% append to the |mtmtcl:path|. Further attributes (e.g. |cdbase|)
% can be specified in additional key--value pairs of arguments,
% and will override those in the \word{common attributes}.
% \begin{tcl}
proc mtmtcl::openmath::OM::mS {cd name attr suffix args} {
dict lappend attr mtmtcl:path {*}$suffix
list OMS [dict replace $attr cd $cd name $name {*}$args] {}
}
% \end{tcl}
% \end{ensproc}
%
% \begin{ensproc}{A}
% The |OM A| command is for constructing |OMA| nodes. It has
% the syntax
% \begin{displaysyntax}
% OM A \word{child}\regplus
% \end{displaysyntax}
% where each \word{child} should be a near-OpenMath data-tree,
% the first of which will be the head (``function'') of the
% application.
% \begin{tcl}
proc mtmtcl::openmath::OM::A {args} {
list OMA {} $args
}
% \end{tcl}
% \end{ensproc}
%
% \begin{ensproc}{AS}
% The |OM AS| command is for constructing |OMA| nodes whose first
% childs are |OMS| nodes. It has
% the syntax
% \begin{displaysyntax}
% OM AS \word{cd} \word{name} \word{child}\regstar
% \end{displaysyntax}
% where each \word{child} should be a near-OpenMath data-tree to
% be made an argument of the |OMS| node with the specified
% \word{cd} and \word{name}.
% \begin{tcl}
proc mtmtcl::openmath::OM::AS {cd name args} {
list OMA {} [linsert $args 0 [S $cd $name]]
}
% \end{tcl}
% This command does not cover the case where the |OMS| child has
% a |cdbase| attribute, but that could be covered with a separate
% |OM| subcommand.
% \end{ensproc}
%
% \begin{ensproc}{I}
% The |OM I| command is for constructing |integer| nodes (rather
% than |OMI| nodes). It has the syntax
% \begin{displaysyntax}
% OM I \word{value}
% \end{displaysyntax}
% where the \word{value} is the integer value to encode.
% \begin{tcl}
proc mtmtcl::openmath::OM::I {value} {
list integer [dict create value $value] {}
}
% \end{tcl}
% \end{ensproc}
%
% \begin{ensproc}{STR}
% The |OM STR| command is for constructing |OMSTR| nodes. It has
% the syntax
% \begin{displaysyntax}
% OM STR \word{value}
% \end{displaysyntax}
% where the \word{value} is the string to encode.
% \begin{tcl}
proc mtmtcl::openmath::OM::STR {value} {
list OMSTR {} [list [list \#text $value]]
}
% \end{tcl}
% \end{ensproc}
%
% \begin{ensproc}{V}
% The |OM V| command is for constructing |OMV| nodes. It has
% the syntax
% \begin{displaysyntax}
% OM V \word{name}
% \end{displaysyntax}
% where the \word{name} is the name of the OM variable.
% \begin{tcl}
proc mtmtcl::openmath::OM::V {name} {
list OMV [dict create name $name] {}
}
% \end{tcl}
% \end{ensproc}
%
% \begin{ensproc}{US}
% The |OM US| command constructs an |OMS| element from the
% URI for the symbol. The call syntax is
% \begin{displaysyntax}
% OM US \word{uri}
% \end{displaysyntax}
% where the \word{uri} is one of
% \begin{displaysyntax}
% \meta{cd}\#\meta{name}\par
% \meta{cdbase}/\meta{cd}\#\meta{name}
% \end{displaysyntax}
% depending on whether the |OMS| element should include a
% |cdbase| attribute.
% \begin{tcl}
proc mtmtcl::openmath::OM::US {uri} {
if {[
regexp -- {^(.*)/([^/#]*)#([^/#]*)$} $uri "" base cd name
]} then {
list OMS [dict create cdbase $base cd $cd name $name] {}
} elseif {[
regexp -- {^([^/#]*)#([^/#]*)$} $uri "" cd name
]} then {
list OMS [dict create cd $cd name $name] {}
} else {
error "Bad symbol URI: $uri"
}
}
% \end{tcl}
% \end{ensproc}
%
% \begin{ensproc}{UATTR}
% The |OM UATTR| command is for attaching attributions to a given
% object, while specifying the attribution symbols through URIs.
% The syntax is
% \begin{displaysyntax}
% OM UATTR \word{core} \begin{regblock}[\regstar] \word{URI}
% \word{object} \end{regblock}
% \end{displaysyntax}
% where the \word{core} is the object that will be put in an
% |OMATTR| node. Each \word{object} is the value of an OM
% attribution, and the corresponding \word{URI} is the URI of the
% annotation symbol.
% \begin{tcl}
proc mtmtcl::openmath::OM::UATTR {core args} {
set L {}
foreach {uri obj} $args {lappend L [US $uri] $obj}
list OMATTR {} [list [list OMATP {} $L] $core]
}
% \end{tcl}
% \end{ensproc}
% \end{ensemble}
%
%
% \subsection{Parsing (near-)OpenMath}
%
% The following commands perform simple parsing of nodes in OpenMath
% and near-OpenMath data-trees. Latter subsections contain commands
% that can be appropriate for more complex parsing problems.
%
% \begin{proc}{gettext}
% The |gettext| command walks through a data-tree and concatenates
% all the \describestring*+[node type]{#text} nodes found in it.
% This is typically used where a node is only supposed to contain
% text, and one doesn't want to mandate that all of it is in a
% single |#text| child. The call syntax is
% \begin{displaysyntax}
% gettext \word{tree} \word{join-string}\regopt
% \end{displaysyntax}
% where the \word{join-string} will be put between each |#text|
% leaf. It defaults to a space.
% \begin{tcl}
proc mtmtcl::openmath::gettext {tree {how " "}} {
set res {}
set stack [list {}]
while {[llength $stack]} {
set path [lindex $stack end]
set stack [lreplace $stack [set stack end] end]
set node [lindex $tree $path]
switch -- [lindex $node 0] "#text" {
lappend res [lindex $node 1]
} default {
lappend path 2
for {set n [llength [lindex $node 2]]} {$n>0} {} {
lappend stack [linsert $path end [incr n -1]]
}
}
}
return [join $res $how]
}
% \end{tcl}
% \end{proc}
%
% \begin{proc}{symbol_URI}
% This command returns the URI corresponding to an \texttt{OMS}
% node. It has the call syntax
% \begin{displaysyntax}
% |symbol_URI| \word{tree} \begin{regblock}[\regstar] \word{option}
% \word{value} \end{regblock}
% \end{displaysyntax}
% The implemented \word{option}s are
% \begin{ttdescription}
% \item[-cdbase]
% The |cdbase| value to use, if none is specified. Defaults to
% `|http://www.openmath.org/cd|'.
% \item[-skipattr]
% Boolean controlling whether |OMATTR| wrappers around the
% |OMS| should be silently ignored. Defaults to true.
% \item[-otherwise]
% If the \word{tree} (even after possibly skipping |OMATTR|s)
% is not an |OMS|, then return this \word{value}. Otherwise an
% error is thrown in this situation.
% \end{ttdescription}
% \begin{tcl}
proc mtmtcl::openmath::symbol_URI {node args} {
array set O {-cdbase http://www.openmath.org/cd -skipattr 1}
array set O $args
if {$O(-skipattr)} then {
while {[lindex $node 0] eq "OMATTR"} {
set node [lindex $node 2 1]
}
}
if {[lindex $node 0] eq "OMS"} then {
set res [dict get [lindex $node 1] cd]
append res \# [dict get [lindex $node 1] name]
if {[dict exists [lindex $node 1] cdbase]} then {
return [dict get [lindex $node 1] cdbase]/$res
} else {
return $O(-cdbase)/$res
}
} elseif {[info exists O(-otherwise)]} then {
return $O(-otherwise)
} else {
return -code error "Not an OM symbol: $node"
}
}
% \end{tcl}
% \end{proc}
%
% \begin{proc}{OMAS_switch}
% This procedure is a kind of |switch| for
% \texttt{OMA}--\texttt{OMS} combinations. The call syntax is
% \begin{displaysyntax}
% |OMAS_switch| \word{tree} \word{else}
% \begin{regblock}[\regplus] \word{pattern} \word{script}
% \end{regblock}
% \end{displaysyntax}
% where \word{tree} is a data-tree suspected to be of the form
% \begin{displaysyntax}
% <OMA><OMS \dots
% \end{displaysyntax}
% The \word{pattern}s are glob-style patterns which are matched
% against the URI for the symbol; the entire block of patterns and
% scripts are handed over to |switch|, so the |default| pattern
% and |-| bodies work as usual. \word{else} is a script which is
% evaluated if the \word{tree} does not have an |OMA| at the root.
%
% The \texttt{OMS} parsing is done by |symbol_URI| with
% |-otherwise ""| as only option, so \texttt{OMATTR}s are skipped
% and \texttt{cdbase} defaults to the OpenMath default. The case of
% an \texttt{OMA} node with a first child that is not a symbol
% would therefore be handled by a \word{script} with empty
% \word{pattern}.
% \begin{tcl}
proc mtmtcl::openmath::OMAS_switch {tree else args} {
if {[lindex $tree 0] ne "OMA"} then {
tailcall ::if 1 then $else
} else {
tailcall\
::switch -glob -- [symbol_URI $tree -otherwise ""] $args
}
}
% \end{tcl}
% \end{proc}
%
% \begin{proc}{integer_value}
% This procedure takes a data-tree encoding an integer value as
% argument. It parses and returns that value (or throws an error if
% it wasn't an integer).
% It understands both \describestring*+[node type]{integer} and
% \describestring*+[node type]{OMI} nodes, but the former is
% preferred. It also skips any |OMATTR| nodes that may surround
% the element.
%
% This procedure is deliberately designed such that it can be used
% to implement the \APIref+{import}{1.0} interface (for integer
% values). Therefore the call syntax is
% \begin{displaysyntax}
% |::mtmtcl::openmath::integer_value| \word{path} \word{data-tree}
% \end{displaysyntax}
% where the \word{path} is only used when throwing \texttt{API
% import EDOM} errors.
%
% \begin{tcl}
proc mtmtcl::openmath::integer_value {path tree} {
while {[lindex $tree 0] eq "OMATTR"} {
set tree [lindex $tree 2 1]
}
switch -- [lindex $tree 0] "integer" {
set value [dict get [lindex $tree 1] value]
if {[string is integer -strict $value]} then {return $value}
} "OMI" {
set str [gettext $tree]
regsub -all {\s} $str {} str
if {[regsub {[Xx]} $str {} str] ?\
[scan $str %llx value] : [scan $str %lld value]} then {
return $value
}
}
return -code error -errorcode [list API import EDOM $path $tree]\
"Not an integer"
}
% \end{tcl}
% \end{proc}
%
%
% \begin{proc}{float_value}
% This procedure takes a data-tree encoding an float value as
% argument. It parses and returns that value (or throws an error if
% it wasn't a float).
% It also skips any |OMATTR| nodes that may surround
% the element.
%
% This procedure is deliberately designed such that it can be used
% to implement the \APIref+{import}{1.0} interface (for float
% values). Therefore the call syntax is
% \begin{displaysyntax}
% |::mtmtcl::openmath::float_value| \word{integers?} \word{path}
% \word{data-tree}
% \end{displaysyntax}
% where the \word{path} is only used when throwing \texttt{API
% import EDOM} errors. The \word{integers?} (which could be supplied
% as part of a command prefix) controls the extent to which integer
% values are accepted as floats. If it is |0| then they are not
% accepted, if it is |1| then they are accepted but converted to
% doubles before returned, and if it is |2| then they are passed
% through as-is.
%
% \begin{tcl}
proc mtmtcl::openmath::float_value {intQ path tree} {
while {[lindex $tree 0] eq "OMATTR"} {
set tree [lindex $tree 2 1]
}
switch -- [lindex $tree 0] "OMF" {
if {[dict exists $attr dec]} then {
scan [dict get $attr dec] %g value
} else {
binary scan [binary format H* [dict get $attr hex]] Q value
}
return $value
} "integer" - "OMI" {
if {$intQ && ![catch {
integer_value $path $tree
} value]} then {
if {$intQ==2} then {
return $value
} else {
return [::tcl::mathfunc::double $value]
}
}
}
return -code error -errorcode [list API import EDOM $path $tree]\
"Not a float"
}
% \end{tcl}
% \end{proc}
%
%
% \subsection{Cleaning OpenMath}
%
% \setnamespace{mtmtcl::openmath::stricten::helper}
% \begin{ensemble}{stricten}
% \setnamespace{mtmtcl::openmath::stricten}
% The |stricten| command transforms a near-OpenMath data-tree to a
% strictly OpenMath data-tree, i.e., nonstandard node types are
% converted to their standard counterparts and nonstandard contents
% of nodes are dropped. The external call syntax is
% \begin{displaysyntax}
% mtmtcl::openmath::stricten \meta{tree} \word{clean}
% \end{displaysyntax}
% where the \word{clean} is a boolean which controls whether
% nonstandard attributes should be removed as well (true is yes).
% The return value is the strictened data-tree.
%
% Instead of dropping nonstandard attributes, one could encode
% them using |OMATTR| nodes, but this can easily be implemented as
% a separate operation. This operation is primarily about getting
% rid of nonstandard nodes (except as children of |OMFOREIGN|).
%
% The organisation of this namespace and ensemble is as follows:
% \begin{itemize}
% \item
% The |mtmtcl::openmath::stricten| namespace contains
% commands for handling specific node types. All commands in
% this namespace are exported and show up in the ensemble.
% \item
% The |helper| child of the ensemble namespace contains helpers
% for the various subcommands. One of these is the actual
% ensemble command, which is needed for processing children of
% a node.
% \end{itemize}
%
% \begin{tcl}
namespace eval mtmtcl::openmath::stricten {
namespace export *
namespace eval helper {
interp alias {} [namespace parent]\
{} [namespace current]::stricten
}
namespace ensemble create -prefix 0\
-command [namespace current]::helper::stricten
}
% \end{tcl}
% There is no unknown handler, since it is an error to apply this
% operation to an unknown node type.
%
% \begin{ensproc}{#text}
% Text is returned unchanged.
% \begin{tcl}
proc mtmtcl::openmath::stricten::#text {str clean} {list #text $str}
% \end{tcl}
% \end{ensproc}
%
% \begin{ensproc}{integer}
% Integers are converted to |OMI| nodes.
% \begin{tcl}
proc mtmtcl::openmath::stricten::integer {attr children clean} {
list OMI {} [list [
list \#text [format %lld [dict get $attr value]]
]]
}
% \end{tcl}
% \end{ensproc}
% \end{ensemble}
%
% For the next couple of definitions, it's actually easiest to be
% within the |mtmtcl::openmath::stricten::helper| namespace, since
% that is where the helper procedures will be created, and one
% anyway has to pretty much fully qualify the alias names.
% \begin{tcl}
namespace eval mtmtcl::openmath::stricten::helper {
% \end{tcl}
%
% \begin{proc}{truncate}
% This procedure can do what is needed for strictening
% several OpenMath node types; it is meant to be useful as an
% alias target. The call syntax is
% \begin{displaysyntax}
% |helper::truncate| \word{type} \word{attribute-list}
% \word{attributes} \word{children} \word{clean}
% \end{displaysyntax}
% and the return value is the cleaned-up node of type
% \word{type}, without children.
% \begin{tcl}
proc truncate {type attrL attr children clean} {
if {!$clean} then {return [list $type $attr {}]}
set D [dict create]
foreach key $attrL {
if {[dict exists $attr $key]} then {
dict set D $key [dict get $attr $key]
}
}
return [list $type $D {}]
}
% \end{tcl}
% \end{proc}
%
% \begin{ensemble}{stricten}
% \setnamespace{mtmtcl::openmath::stricten}
% \begin{ensproc}{OMS}
% \begin{ensproc}{OMV}
% \begin{ensproc}{OMF}
% \begin{ensproc}{OMR}
% As stated above, |OMS|, |OMV|, |OMF|, and |OMR| nodes should
% not have children in strict OpenMath, so they're the ones that
% |helper::truncate| are for.
%
% Symbols may carry |name|, |cd|, |cdbase|, and |id| attributes.
% \begin{tcl}
interp alias {} [namespace parent]::OMS {}\
[namespace which truncate] OMS {name cd cdbase id}
% \end{tcl}
% Floats can carry |id|, |dec|, and |hex| attributes.
% \begin{tcl}
interp alias {} [namespace parent]::OMF {}\
[namespace which truncate] OMF {dec hex id}
% \end{tcl}
% Variables may carry |name| and |id| attributes.
% \begin{tcl}
interp alias {} [namespace parent]::OMV {}\
[namespace which truncate] OMV {name id}
% \end{tcl}
% References can carry |id| and |href| attributes.
% \begin{tcl}
interp alias {} [namespace parent]::OMR {}\
[namespace which truncate] OMR {id href}
% \end{tcl}
% \end{ensproc}\end{ensproc}\end{ensproc}\end{ensproc}
% \end{ensemble}
%
% \begin{proc}{textchild}
% Another class of node types, from the strictening point of
% view, are those which can have |#text| children and an |id|
% attribute, so this is what this procedure checks for.
% The call syntax is
% \begin{displaysyntax}
% |helper::textchild| \word{type} \word{attributes}
% \word{children} \word{clean}
% \end{displaysyntax}
% and the return value is the cleaned-up node of type
% \word{type}.
% \begin{tcl}
proc textchild {type attr children clean} {
foreach child $children {
if {[lindex $child 0] ne "#text"} then {
error "Type \"[lindex $child 0]\" forbidden as child of\
\"$type\""
}
}
if {$clean} then {
if {[dict exists $attr id]} then {
set attr [dict filter $attr keys id]
} else {
set attr {}
}
}
return [list $type $attr $children]
}
% \end{tcl}
% \end{proc}
%
% \begin{ensemble}{stricten}
% \setnamespace{mtmtcl::openmath::stricten}
% \begin{ensproc}{OMI}
% \begin{ensproc}{OMB}
% \begin{ensproc}{OMSTR}
% Integers, bytearrays, and strings are node types that can be
% handled by |textchild|.
% \begin{tcl}
interp alias {} [namespace parent]::OMI {}\
[namespace which textchild] OMI
interp alias {} [namespace parent]::OMB {}\
[namespace which textchild] OMB
interp alias {} [namespace parent]::OMSTR {}\
[namespace which textchild] OMSTR
% \end{tcl}
% \end{ensproc}\end{ensproc}\end{ensproc}
% \end{ensemble}
%
% \begin{proc}{recurse}
% The final class of node types are those where we actually need
% to recurse over the children (and suspect they can be pretty
% much anything). The helper for this has the call syntax
% \begin{displaysyntax}
% |helper::recurse| \word{type} \word{attribute-list}
% \word{attributes} \word{children} \word{clean}
% \end{displaysyntax}
% and the return value is the cleaned-up node of type
% \word{type}.
% \begin{tcl}
proc recurse {type attrL attr children clean} {
set L {}
foreach child $children {
lappend L [stricten {*}$child $clean]
}
if {!$clean} then {return [list $type $attr $L]}
set D {}
foreach key $attrL {
if {[dict exists $attr $key]} then {
dict set D $key [dict get $attr $key]
}
}
return [list $type $D $L]
}
% \end{tcl}
% \end{proc}
%
% \begin{ensemble}{stricten}
% \setnamespace{mtmtcl::openmath::stricten}
% \begin{ensproc}{OMA}
% \begin{ensproc}{OMBIND}
% \begin{ensproc}{OMATTR}
% \begin{ensproc}{OMATP}
% Compound elements can carry |id| and |cdbase| attributes.
% \begin{tcl}
interp alias {} [namespace parent]::OMA {}\
[namespace which recurse] OMA {cdbase id}
interp alias {} [namespace parent]::OMBIND {}\
[namespace which recurse] OMBIND {cdbase id}
interp alias {} [namespace parent]::OMATTR {}\
[namespace which recurse] OMATTR {cdbase id}
interp alias {} [namespace parent]::OMATP {}\
[namespace which recurse] OMATP {cdbase id}
% \end{tcl}
% \end{ensproc}\end{ensproc}\end{ensproc}\end{ensproc}
%
% \begin{ensproc}{OMBVAR}
% \begin{ensproc}{OME}
% Bound variables and errors only carry |id| attributes.
% \begin{tcl}
interp alias {} [namespace parent]::OME {}\
[namespace which recurse] OME id
interp alias {} [namespace parent]::OMBVAR {}\
[namespace which recurse] OMBVAR id
% \end{tcl}
% \end{ensproc}\end{ensproc}
%
% OK, that's the end of the |mtmtcl::openmath::stricten::helper|
% material.
% \begin{tcl}
}
% \end{tcl}
%
% \begin{ensproc}{OMFOREIGN}
% Foreign elements can carry |id| and |encoding| attributes.
% Since the children are pretty arbitrary, there's no point in
% recursing over them.
% \begin{tcl}
proc mtmtcl::openmath::stricten::OMFOREIGN {attr children clean} {
if {!$clean} then {
return [list OMFOREIGN $attr $children]
}
set D [dict create]
foreach key {id encoding} {
if {[dict exists $attr $key]} then {
dict set D $key [dict get $attr $key]
}
}
return [list OMFOREIGN $D $children]
}
% \end{tcl}
% \end{ensproc}
% \end{ensemble}
%
%
%
% \subsection{Generic helpers}
%
% \setnamespace{mtmtcl::openmath}
%
% \begin{proc}{cdbase}
% This procedure revises the |cdbase| attributes of all nodes
% in a data-tree, to make sure an |OMS| node has a |cdbase|
% attribute if and only if the effective |cdbase| value at that
% point is not the default one. The call syntax is
% \begin{displaysyntax}
% cdbase \word{tree-var} \word{inherited cdbase}\regopt
% \end{displaysyntax}
% where \word{tree-var} is the name of a variable in the calling
% context in which the tree is stored, and \word{inherited cdbase}
% is the |cdbase| value (`|http://www.openmath.org/cd|') that would
% be inherited from the parent. There is no particular return value.
%
% In order to ensure this operation is invertible, any existing
% attribute with a name on the form |cdbase|$($|-orig|$)^*$ gets an
% extra |-orig| in the name.
%
% \begin{tcl}
proc mtmtcl::openmath::cdbase\
{treevar {cdbase http://www.openmath.org/cd}} {
upvar 1 $treevar tree
% \end{tcl}
% The main loop in this procedure is over the nodes in the tree, in
% string order but with list indexing. The |path| variable points
% to the current node, and the last element in |stack| is the
% inherited cdbase value for the current node. Whenever one element
% is popped of the |stack|, two elements are removed from the end
% of the |path|.
% \begin{tcl}
set stack [list $cdbase]
set path {}
while 1 {
% \end{tcl}
% This outer loop is over the nodes. It is terminated by
% |return|ing from the entire procedure.
% \begin{tcl}
set cdbase [lindex $stack end]
set type [lindex [lindex $tree $path] 0]
if {$type ne "#text"} then {
set apath [linsert $path end 1]
set attr [lindex $tree $apath]
if {[dict exists $attr cdbase]} then {
set cdbase [dict get $attr cdbase]
}
% \end{tcl}
% The following uses |dict filter| to drop |cdbase|$($|-orig|$)^*$
% elements from the attribute dictionary, while collecting modified
% forms of them in |kvL| that are then put back.
% \begin{tcl}
set kvL {}
set fattr [dict filter $attr script {k v} {
switch -regexp -- $k {^cdbase(-orig)*$} {
lappend kvL $k-orig $v
return -level 0 0
} default {
return -level 0 1
}
}]
set attr [dict replace $fattr {*}$kvL]
% \end{tcl}
% The attribute dictionary in the tree is only updated if it
% changes. The main reason for doing this is that the effective
% |cdbase| is not the default one, but a nonempty |kvL| also
% calls for an update.
% \begin{tcl}
if {$type eq "OMS" &&\
$cdbase ne "http://www.openmath.org/cd"} then {
dict set attr cdbase $cdbase
lset tree $apath $attr
} elseif {[llength $kvL]} then {
lset tree $apath $attr
}
% \end{tcl}
% This steps the |path| forward into the list of children, if there
% are any. The final path component |-1| will be incremented by the
% next code block before it is used as index into the tree.
% \begin{tcl}
if {[llength [lindex [lindex $tree $path] 2]]} then {
lappend stack $cdbase
lappend path 2 -1
}
}
% \end{tcl}
% The following loop handles incrementing the |path| to point to
% the next (in string representation order) node in the tree. There
% is one iteration of this loop for every tree level that needs to
% be popped of the |stack|.
% \begin{tcl}
while 1 {
if {![llength $path]} then {return}
% \end{tcl}
% If the |path| is empty then we're done with the root node and can
% |return|. Otherwise increment the final |path| element, to point
% it at the next sibling of the current node.
% \begin{tcl}
lset path end [expr {1 + [lindex $path end]}]
if {[llength [lindex $tree $path]]} then {break}
% \end{tcl}
% If we were at the final sibling, then the |path| now points to
% nothing, and the |lindex| returns an empty string. Any proper
% node would have positive list length, so if we don't then it's
% time to return to the parent and consider its next sibling.
% \begin{tcl}
set path [lreplace $path [set path end-1] end]
set stack [lreplace $stack [set stack end] end]
}
}
}
% \end{tcl}
% \end{proc}
%
% \begin{proc}{cdbase-orig}
% This procedure is the inverse of |cdbase|, primarily throwing
% away |cdbase| attributes and removing one |-orig| from the name of
% every |cdbase|$($|-orig|$)^+$ attribute.
% However, it also preserves the effective |cdbase|, so that trees
% which have been modified can be restored to a correct state. The
% call syntax is the same as for |cdbase|, i.e.,
% \begin{displaysyntax}
% cdbase-orig \word{tree-var} \word{inherited cdbase}\regopt
% \end{displaysyntax}
% with no particular return value, as the \word{tree-var} in the
% calling context gets modified in place.
%
% The overall loop structure is the same as in |cdbase|. The main
% difference here is that there are two |cdbase| values: |cdbase|
% which is the one encoded in the input, and |cdbase0| which is the
% one which would be effective for the node if merely an |-orig|
% was removed from all |cdbase|$($|-orig|$)^+$ attributes. When
% these are different for an |OMS| node, then the |cdbase| value
% is taken as value for the |cdbase| attribute.
% \begin{tcl}
proc mtmtcl::openmath::cdbase-orig\
{treevar {cdbase http://www.openmath.org/cd}} {
upvar 1 $treevar tree
set stack [list $cdbase]
set path {}
while 1 {
set cdbase0 [lindex $stack end]
set type [lindex [lindex $tree $path] 0]
if {$type ne "#text"} then {
set apath [linsert $path end 1]
set attr [lindex $tree $path]
if {[dict exists $attr cdbase-orig]} then {
set cdbase0 [dict get $attr cdbase-orig]
}
if {[dict exists $attr cdbase]} then {
set cdbase [dict get $attr cdbase]
} else {
set cdbase "http://www.openmath.org/cd"
}
set kvL {}
set fattr [dict filter $attr script {k v} {
switch -regexp -- $k {
{^cdbase(-orig)+$} {
lappend kvL [string range $k 0 end-5] $v
return -level 0 0
}
{^cdbase$} {return -level 0 0}
default {return -level 0 1}
}
}]
if {$type eq "OMS" && $cdbase0 ne $cdbase} then {
lappend kvL cdbase $cdbase
set cdbase0 $cdbase
}
if {[llength $kvL]} then {
lset tree $apath [dict replace $fattr {*}$kvL]
} elseif {[dict size $attr] != [dict size $fattr]} then {
lset tree $apath $fattr
}
if {[llength [lindex [lindex $tree $path] 2]]} then {
lappend stack $cdbase0
lappend path 2 -1
}
}
while 1 {