Changeset ce06be2 in report
 Timestamp:
 Jan 12, 2007 12:59:30 AM (12 years ago)
 Branches:
 h2010, master
 Children:
 6d9baa1
 Parents:
 1083481
 Location:
 report
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

report/decls.verb
r1083481 rce06be2 1533 1533  gdrhs [@where@ decls] 1534 1534 1535 gdrhs > gd @=@ exp [gdrhs] 1536 1537 gd > @@ exp^0 1535 gdrhs > guards @=@ exp [gdrhs] 1536 1537 guards > @@ guard_1, ..., guard_n & (n>=1) 1538 1538 1539 @@@ 1539 1540 \indexsyn{decl}% 1540 1541 \indexsyn{rhs}% 1541 1542 \indexsyn{gdrhs}% 1542 \indexsyn{ gd}%1543 \indexsyn{qs}% 1543 1544 We distinguish two cases within this syntax: a {\em pattern binding} 1544 1545 occurs when the left hand side is a "pat^0"; 
report/exps.verb
r1083481 rce06be2 664 664 types are in @Enum@ and their semantics. 665 665 666 \subsection{Qualifiers in Patterns}667 \index{qualifier}668 \label{qualifiersinpatterns}669 @@@670 qual > pat @<@ exp & (\tr{generator}, or \tr{pattern guard})671  @let@ decls & (\tr{local declaration})672  exp & (\tr{guard})673 @@@674 \indexsyn{qual}675 676 A {\em qualifier}\index{qualifier} has one of the following forms:677 \begin{itemize}678 \item {\em generators}\index{generator} (also known as {\em pattern guards}\index{pattern guard} of the form "p @<@ e", where679 "p" is a680 pattern (see Section~\ref{patternmatching}) of type "t" and "e" is an681 expression of type "@[@t@]@"682 \item {\em guards},\index{guard} which are arbitrary expressions of683 type @Bool@684 \item {\em local bindings} that provide new definitions for use in685 the generated expression "e" or subsequent guards and generators.686 \end{itemize}687 688 The first qualifier has the same environment as the righthandside of689 the caseexpression alternative, function definition, or pattern690 binding to which it is attached. A qualifier creates successive691 environments for "e" created by the nested, depthfirst evaluation of692 the generators and let bindings in the qualifier list. Binding of693 variables within generators occurs according to the normal pattern694 matching rules (see Section~\ref{patternmatching}), and may fail. A695 qualifier matches if all of the guards in that qualifier evaluate to696 @True@ and all of the generators match, otherwise it fails.697 698 666 \subsection{List Comprehensions} 699 667 \index{list comprehension} … … 703 671 @@@ 704 672 aexp > @[@ exp @@ qual_1 @,@ ... @,@ qual_n @]@ & (\tr{list comprehension}, n>=1) 673 qual > pat @<@ exp & (\tr{generator}) 674  @let@ decls & (\tr{local declaration}) 675  exp & (\tr{boolean guard}) 705 676 @@@ 706 677 \indexsyn{aexp} 678 \indexsyn{qual} 707 679 708 680 \noindent 709 A {\em list comprehension} has the form "@[@ e @@ q_1@,@ ...@,@ q_n 710 @]@, n>=1," where the "q_i" are qualifiers as in 711 Section~\ref{qualifiersinpatterns}. 712 713 Such a list comprehension returns the list of elements produced by 714 evaluating "e" in the environments created by the qualifiers. If a match 715 fails then that element of the list is simply skipped over. Thus:\nopagebreak[4] 681 A {\em list comprehension} has the form "@[@ e @@ q_1@,@ ...@,@ q_n @]@, 682 n>=1," where the "q_i" qualifiers\index{qualifier} are either 683 \begin{itemize} 684 \item {\em generators}\index{generator} of the form "p @<@ e", where 685 "p" is a 686 pattern (see Section~\ref{patternmatching}) of type "t" and "e" is an 687 expression of type "@[@t@]@" 688 \item {\em boolean guards},\index{boolean guard} which are arbitrary expressions of 689 type @Bool@ 690 \item {\em local bindings} that provide new definitions for use in 691 the generated expression "e" or subsequent boolean guards and generators. 692 \end{itemize} 693 694 Such a list comprehension returns the list of elements 695 produced by evaluating "e" in the successive environments 696 created by the nested, depthfirst evaluation of the generators in the 697 qualifier list. Binding of variables occurs according to the normal 698 pattern matching rules (see Section~\ref{patternmatching}), and if a 699 match fails then that element of the list is simply skipped over. Thus:\nopagebreak[4] 716 700 \bprog 717 701 @ … … 720 704 @ 721 705 \eprog 722 yields the list @[4,2]@. If a qualifier is a guard, it must evaluate706 yields the list @[4,2]@. If a qualifier is a boolen guard, it must evaluate 723 707 to @True@ for the previous pattern match to succeed. 724 708 As usual, bindings in list comprehensions can shadow those in outer … … 833 817  & (empty alternative) 834 818 835 gdpat > qs @>@ exp [ gdpat ] 836 qs > @@ qual_1, ..., qual_n 819 gdpat > guards @>@ exp [ gdpat ] 820 guards > @@ guard_1, ..., guard_n & (n>=1) 821 guard > pat @<@ exp^0 & (\tr{pattern guard}) 822  @let@ decls & (\tr{local declaration}) 823  exp^0 & (\tr{boolean guard}) 837 824 @@@ 838 825 \indexsyn{exp}% … … 840 827 \indexsyn{alt}% 841 828 \indexsyn{gdpat}% 842 \indexsyn{gd}% 829 \indexsyn{guards}% 830 \indexsyn{guard}% 831 843 832 844 833 A {\em case expression}\index{case expression} has the general form … … 848 837 where each "match_i" is of the general form 849 838 \[\ba{lll} 850 & "@@ q_{i1}" & "@>@ e_{i1}" \\839 & "@@ gs_{i1}" & "@>@ e_{i1}" \\ 851 840 & "..." \\ 852 & "@@ q_{im_i}" & "@>@ e_{im_i}" \\841 & "@@ gs_{im_i}" & "@>@ e_{im_i}" \\ 853 842 & \multicolumn{2}{l}{"@where@ decls_i"} 854 843 \ea\] 855 (Notice that in the syntax rule for " qs", the ``@@'' is a844 (Notice that in the syntax rule for "guards", the ``@@'' is a 856 845 terminal symbol, not the syntactic metasymbol for alternation.) 857 846 Each alternative "p_i match_i" consists of a 858 847 pattern\index{pattern} "p_i" and its matches, "match_i". 859 848 Each match in turn 860 consists of a sequence of pairs of qualifiers\index{qualifier} 861 "q_{ij}" and bodies "e_{ij}" (expressions), followed by 862 optional bindings ("decls_i") that scope over all of the qualifiers and 863 expressions of the alternative. An alternative of the form 849 consists of a sequence of pairs of guards\index{guard} 850 "gs_{ij}" and bodies "e_{ij}" (expressions), followed by 851 optional bindings ("decls_i") that scope over all of the guards and 852 expressions of the alternative. 853 854 \index{Pattern Guards} 855 \index{guards} 856 A {\em guard}\index{guard} has one of the following forms: 857 \begin{itemize} 858 \item {\em pattern guards}\index{pattern guard} are of the form "p @<@ e", where 859 "p" is a 860 pattern (see Section~\ref{patternmatching}) of type "t" and "e" is an 861 expression type "t". They succeed if the expression "e" matches the pattern "p", and introduce the bindings of the pattern to the environment. 862 \item {\em boolean guards}\index{boolean guard} are arbitrary expressions of 863 type @Bool@. They succeed if the expression evaluates to @True@, and they do not introduce new names to the environment. A boolean guard, "g", is semantically equivalent to the pattern guard "@True < @g". 864 \item {\em local bindings} are of the form "@let @decls". They always succeed, and they introduce the names defined in "decls" to the environment. 865 \end{itemize} 866 867 868 An alternative of the form 864 869 \[ 865 870 "pat @>@ exp @where@ decls" … … 877 882 A case expression is evaluated by pattern matching the expression "e" 878 883 against the individual alternatives. The alternatives are tried 879 sequentially, from top to bottom. If "e" matches the pattern in the880 alternative, the qualifiers for that alternative are tried881 sequentially from top to bottom,in the environment of the case884 sequentially, from top to bottom. If "e" matches the pattern of an 885 alternative, then the guarded expressions for that alternative are 886 tried sequentially from top to bottom in the environment of the case 882 887 expression extended first by the bindings created during the matching 883 of the pattern, and then by the bindings of variables within the 884 qualifier list (either by using a let clause or a generator), and then 885 by the "decls_i" in the @where@ clause associated with that 886 alternative. 887 888 If one of the qualifiers matches (see 889 Section~\ref{qualifiersinpatterns}), the corresponding righthand 890 side is evaluated in the same environment as the guard. 891 892 If none of the qualifiers for a given alternative match, matching 893 continues with the next alternative. If no match succeeds, the result 894 is "\bot". Pattern matching is described in 888 of the pattern, and then by the "decls_i" in the @where@ clause 889 associated with that alternative. 890 891 For each guarded expression, the commaseparated guards are tried 892 sequentially from left to right. If all of them succeed, then the 893 corresponding expression is evaluated in the environment extended with 894 the bindings introduced by the guards. That is, the bindings that are 895 introduced by a guard (either by using a let clause or a pattern 896 guard) are in scope in the following guards and the corresponding 897 expression. If any of the guards fail, then this guarded expression 898 fails and the next guarded expression is tried. 899 900 If none of the guarded expressions for a given alternative succeed, 901 then matching continues with the next alternative. If no alternative 902 succeeds, then the result is "\bot". Pattern matching is described in 895 903 Section~\ref{patternmatching}, with the formal semantics of case 896 904 expressions in Section~\ref{casesemantics}. … … 911 919 parsers with limited lookahead may incorrectly commit to this choice, and hence 912 920 reject the program. Programmers are advised, therefore, to avoid guards that 913 end with a type signature  indeed that is why a "g d" contains921 end with a type signature  indeed that is why a "guard" contains 914 922 an "exp^0" not an "exp". 915 923
Note: See TracChangeset
for help on using the changeset viewer.