1
0
forked from GitHub/gf-core
Files
gf-core/lib/resource/doc/gfdoc/Precedence.html
2007-12-12 20:30:11 +00:00

181 lines
5.4 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META NAME="generator" CONTENT="http://txt2tags.sf.net">
</HEAD><BODY BGCOLOR="white" TEXT="black">
<FONT SIZE="4">
</FONT></CENTER>
<P></P>
<HR NOSHADE SIZE=1>
<P></P>
<P></P>
<HR NOSHADE SIZE=1>
<P></P>
<P>
Last update: 2005-11-23 09:16:18 CET
</P>
<P>
Produced by
gfdoc - a rudimentary GF document generator.
(c) Aarne Ranta (<A HREF="mailto:aarne@cs.chalmers.se">aarne@cs.chalmers.se</A>) 2002 under GNU GPL.
</P>
<P>
operations for precedence-dependent strings.
five levels:
p4 (constants), p3 (applications), p2 (products), p1 (sums), p0 (arrows)
</P>
<PRE>
resource Precedence = open Prelude in {
param
Prec = p4 | p3 | p2 | p1 | p0 ;
lintype
PrecTerm = Prec =&gt; Str ;
oper
pss : PrecTerm -&gt; {s : PrecTerm} = \s -&gt; {s = s} ;
</PRE>
<P></P>
<P>
change this if you want some other type of parentheses
</P>
<PRE>
mkParenth : Str -&gt; Str = \str -&gt; "(" ++ str ++ ")" ;
</PRE>
<P></P>
<P>
define ordering of precedences
</P>
<PRE>
nextPrec : Prec =&gt; Prec =
table {p0 =&gt; p1 ; p1 =&gt; p2 ; p2 =&gt; p3 ; _ =&gt; p4} ;
prevPrec : Prec =&gt; Prec =
table {p4 =&gt; p3 ; p3 =&gt; p2 ; p2 =&gt; p1 ; _ =&gt; p0} ;
mkPrec : Str -&gt; Prec =&gt; Prec =&gt; Str = \str -&gt;
table {
p4 =&gt; table { -- use the term of precedence p4...
_ =&gt; str} ; -- ...always without parentheses
p3 =&gt; table { -- use the term of precedence p3...
p4 =&gt; mkParenth str ; -- ...in parentheses if p4 is required...
_ =&gt; str} ; -- ...otherwise without parentheses
p2 =&gt; table {
p4 =&gt; mkParenth str ;
p3 =&gt; mkParenth str ;
_ =&gt; str} ;
p1 =&gt; table {
p1 =&gt; str ;
p0 =&gt; str ;
_ =&gt; mkParenth str} ;
p0 =&gt; table {
p0 =&gt; str ;
_ =&gt; mkParenth str}
} ;
</PRE>
<P></P>
<P>
make a string into a constant, of precedence p4
</P>
<PRE>
mkConst : Str -&gt; PrecTerm =
\f -&gt;
mkPrec f ! p4 ;
</PRE>
<P></P>
<P>
make a string into a 1/2/3 -place prefix operator, of precedence p3
</P>
<PRE>
mkFun1 : Str -&gt; PrecTerm -&gt; PrecTerm =
\f -&gt; \x -&gt;
table {k =&gt; mkPrec (f ++ x ! p4) ! p3 ! k} ;
mkFun2 : Str -&gt; PrecTerm -&gt; PrecTerm -&gt; PrecTerm =
\f -&gt; \x -&gt; \y -&gt;
table {k =&gt; mkPrec (f ++ x ! p4 ++ y ! p4) ! p3 ! k} ;
mkFun3 : Str -&gt; PrecTerm -&gt; PrecTerm -&gt; PrecTerm -&gt; PrecTerm =
\f -&gt; \x -&gt; \y -&gt; \z -&gt;
table {k =&gt; mkPrec (f ++ x ! p4 ++ y ! p4 ++ z ! p4) ! p3 ! k} ;
</PRE>
<P></P>
<P>
make a string into a non/left/right -associative infix operator, of precedence p
</P>
<PRE>
mkInfix : Str -&gt; Prec -&gt; PrecTerm -&gt; PrecTerm -&gt; PrecTerm =
\f -&gt; \p -&gt; \x -&gt; \y -&gt;
table {k =&gt; mkPrec (x ! (nextPrec ! p) ++ f ++ y ! (nextPrec ! p)) ! p ! k} ;
mkInfixL : Str -&gt; Prec -&gt; PrecTerm -&gt; PrecTerm -&gt; PrecTerm =
\f -&gt; \p -&gt; \x -&gt; \y -&gt;
table {k =&gt; mkPrec (x ! p ++ f ++ y ! (nextPrec ! p)) ! p ! k} ;
mkInfixR : Str -&gt; Prec -&gt; PrecTerm -&gt; PrecTerm -&gt; PrecTerm =
\f -&gt; \p -&gt; \x -&gt; \y -&gt;
table {k =&gt; mkPrec (x ! (nextPrec ! p) ++ f ++ y ! p) ! p ! k} ;
</PRE>
<P></P>
<HR NOSHADE SIZE=1>
<P>
alternative:
precedence as inherent feature
</P>
<PRE>
lintype TermWithPrec = {s : Str ; p : Prec} ;
oper
mkpPrec : Str -&gt; Prec -&gt; TermWithPrec =
\f -&gt; \p -&gt;
{s = f ; p = p} ;
usePrec : TermWithPrec -&gt; Prec -&gt; Str =
\x -&gt; \p -&gt;
mkPrec x.s ! x.p ! p ;
</PRE>
<P></P>
<P>
make a string into a constant, of precedence p4
</P>
<PRE>
mkpConst : Str -&gt; TermWithPrec =
\f -&gt;
mkpPrec f p4 ;
</PRE>
<P></P>
<P>
make a string into a 1/2/3 -place prefix operator, of precedence p3
</P>
<PRE>
mkpFun1 : Str -&gt; TermWithPrec -&gt; TermWithPrec =
\f -&gt; \x -&gt;
mkpPrec (f ++ usePrec x p4) p3 ;
mkpFun2 : Str -&gt; TermWithPrec -&gt; TermWithPrec -&gt; TermWithPrec =
\f -&gt; \x -&gt; \y -&gt;
mkpPrec (f ++ usePrec x p4 ++ usePrec y p4) p3 ;
mkpFun3 : Str -&gt; TermWithPrec -&gt; TermWithPrec -&gt; TermWithPrec -&gt; TermWithPrec =
\f -&gt; \x -&gt; \y -&gt; \z -&gt;
mkpPrec (f ++ usePrec x p4 ++ usePrec y p4 ++ usePrec z p4) p3 ;
</PRE>
<P></P>
<P>
make a string a into non/left/right -associative infix operator, of precedence p
</P>
<PRE>
mkpInfix : Str -&gt; Prec -&gt; TermWithPrec -&gt; TermWithPrec -&gt; TermWithPrec =
\f -&gt; \p -&gt; \x -&gt; \y -&gt;
mkpPrec (usePrec x (nextPrec ! p) ++ f ++ usePrec y (nextPrec ! p)) p ;
mkpInfixL : Str -&gt; Prec -&gt; TermWithPrec -&gt; TermWithPrec -&gt; TermWithPrec =
\f -&gt; \p -&gt; \x -&gt; \y -&gt;
mkpPrec (usePrec x p ++ f ++ usePrec y (nextPrec ! p)) p ;
mkpInfixR : Str -&gt; Prec -&gt; TermWithPrec -&gt; TermWithPrec -&gt; TermWithPrec =
\f -&gt; \p -&gt; \x -&gt; \y -&gt;
mkpPrec (usePrec x (nextPrec ! p) ++ f ++ usePrec y p) p ;
} ;
</PRE>
<P></P>
<!-- html code generated by txt2tags 2.3 (http://txt2tags.sf.net) -->
<!-- cmdline: txt2tags -thtml -\-toc ../prelude/Precedence.txt -->
</BODY></HTML>