[Aimmath-commit] AIM/WEB-INF/maple/aim SyntaxHints.mpl,1.6,1.7
Brought to you by:
gustav_delius,
npstrick
From: <mo...@us...> - 2004-01-12 06:11:43
|
Update of /cvsroot/aimmath/AIM/WEB-INF/maple/aim In directory sc8-pr-cvs1:/tmp/cvs-serv998/WEB-INF/maple/aim Modified Files: SyntaxHints.mpl Log Message: Added custom Maple types: OrderedPair, OrderedTriple, OrderedTuple, and SeqOrderedPair. Also added custom parsers `Parse/OrderedTuple` and `Parse/SeqOrderedTuple` to parse strings that match these data types into lists. This allows the student to enter ordered tuples such as (1,2) as any of (1,2), [1,2], or simply 1,2. Similarly a finite sequence can be entered using this notation (by considering it as an n-tuple). The question author can specify the Maple type of the components in the tuples via the String() wrapper at the c> flag. For example, c> String(OrderedPair(integer,integer)) tells AIM that the student's answer should be left as an unevaluated string, which is then checked against the data type OrderedPair(integer,integer) to see if it parses to an ordered pair of integers. To parse the student's answer string in the marking proc, the Parse routines are used. Index: SyntaxHints.mpl =================================================================== RCS file: /cvsroot/aimmath/AIM/WEB-INF/maple/aim/SyntaxHints.mpl,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** SyntaxHints.mpl 10 Oct 2003 09:48:30 -0000 1.6 --- SyntaxHints.mpl 12 Jan 2004 06:11:40 -0000 1.7 *************** *** 962,966 **** msg, sprintf(__("#Explain constant functions (%s,%s,%s,%A,%s)"), ! fstring0,xstring,red(fstring1,"*(",xstring,")"), fx,fstring0); else --- 962,966 ---- msg, sprintf(__("#Explain constant functions (%s,%s,%s,%A,%s)"), ! fstring0,xstring,red(fstring1,"*(",xstring,")"), fx,fstring0); else *************** *** 968,972 **** msg, sprintf(__("#Explain funny functions (%s,%s,%s)"), ! red(fstring0),red(xstring),red(fstring1,"*(",xstring,")")); fi; --- 968,972 ---- msg, sprintf(__("#Explain funny functions (%s,%s,%s)"), ! red(fstring0),red(xstring),red(fstring1,"*(",xstring,")")); fi; *************** *** 1092,1098 **** badm := `if`(member(lcm,vars),ucm,lcm); msg := cat(msg, ! sprintf( ! __("If so, you should note that this involves the variable <tt><font color='red'>%s</font></tt>, which is not relevant for this question. Perhaps you mean <tt><font color='green'>%s</font></tt> instead. (Remember that Maple is case-sensitive.)"), ! badm,goodm)); return(msg); fi; --- 1092,1098 ---- badm := `if`(member(lcm,vars),ucm,lcm); msg := cat(msg, ! sprintf( ! __("If so, you should note that this involves the variable <tt><font color='red'>%s</font></tt>, which is not relevant for this question. Perhaps you mean <tt><font color='green'>%s</font></tt> instead. (Remember that Maple is case-sensitive.)"), ! badm,goodm)); return(msg); fi; *************** *** 1176,1178 **** ###################################################################### ! EndPackage(): --- 1176,1381 ---- ###################################################################### ! ###################################################################### ! # Custom Student Input Types and Parsers ! ###################################################################### ! # TODO: Make these more general and robust ! ! `Package/Assign`( ! `type/OrderedPair`::boolean, ! "Returns @true@ if the argument is a Maple list of two elements or a string that has ! one of the three forms: @(a,b)@, @[a,b]@, @a,b@. This is intended for use with ! the c> flag and the @String@ keyword, so that students can enter an ordered pair for ! their answer. It is forgiving with brackets in that it will accept @(a,b)@, @[a,b]@, ! or simply @a,b@ as the student answer and treat each as the same ordered pair. ! The optional second and third arguments can be used to specify the Maple type of ! @a@ and @b@ respectively.<br><br> ! Example: <br><br> ! @c> String(OrderedPair(integer,rational))@ <br><br> ! will only match ordered pairs @(a,b)@ for which @a@ parses to an @integer@ and ! @b@ parses to a @rational@. Note that currently this type checking is not recursive, ! so that @a@ and @b@ are interpreted as parsed Maple expressions, not strings ! (unless the student encloses them in quotes). In particular, you cannot ! test for an @OrderedPair@ of @OrderedPair@'s (which would be ambiguous anyway since ! brackets/parentheses are optional). ! ", ! proc(s::anything) ! local S,t1,t2,T,n: ! t1:='anything'; t2:='anything'; ! if nargs>1 then t1:=args[2] fi: ! if nargs>2 then t2:=args[3] fi: ! if not type(s,string) then return type(s,[t1,t2]) fi; ! S:=`Util/RemoveWhiteSpace`(s): ! # convert () to [] ! if S[1]="(" and S[-1]=")" then S:=cat("[",S[2..-2],"]"); fi: ! # if they omitted brackets, supply them ! if S[1]<>"[" or S[-1]<>"]" then S:=cat("[",S,"]"): fi: ! n:=length(S); ! if n<5 then return false fi: ! T:=`aim/SafeParse`(S): ! return evalb(T['Status']="OK" and type(T['Value'],[t1,t2])) ! end ! ): ! ! `Package/Assign`( ! `type/OrderedTriple`::boolean, ! "Returns @true@ if the argument is a Maple list of three elements or a string that has ! one of the three forms: @(a,b,c)@, @[a,b,c]@, or @a,b,c@. This is intended for use with ! the c> flag and the @String@ keyword, so that students can enter an ordered triple for ! their answer. It is forgiving with brackets in that it will accept any of the three ! forms above as the student answer and treat each as the same ordered ! triple. The optional second, third, and fourth arguments can be used to specify the ! Maple type of @a@, @b@, and @c@ respectively.<br><br> ! Example: <br><br> ! @c> String(OrderedPair(integer,rational,algebraic))@ <br><br> ! will only match ordered triples @(a,b,c)@ for which @a@ parses to an @integer@, ! @b@ parses to a @rational@, and @c@ parses to a @algebraic@. ! Note that currently this type checking is not recursive, ! so that @a@, @b@, and @c@ are interpreted as parsed Maple expressions, not strings ! (unless the student encloses them in quotes). In particular, you cannot ! test for an @OrderedTriple@ of @OrderedPair@'s, for example (which would be ambiguous ! anyway since brackets/parentheses are optional). ! ", ! proc(s::anything) ! local S,t1,t2,t3,T,n: ! t1:='anything'; t2:='anything'; t3:='anything'; ! if nargs>1 then t1:=args[2] fi: ! if nargs>2 then t2:=args[3] fi: ! if nargs>3 then t3:=args[4] fi: ! if not type(s,string) then return type(s,[t1,t2,t3]) fi; ! S:=`Util/RemoveWhiteSpace`(s): ! # convert () to [] ! if S[1]="(" and S[-1]=")" then S:=cat("[",S[2..-2],"]"); fi: ! # if they omitted brackets, supply them ! if S[1]<>"[" or S[-1]<>"]" then S:=cat("[",S,"]"): fi: ! n:=length(S); ! if n<7 then return false fi: ! T:=`aim/SafeParse`(S): ! return evalb(T['Status']="OK" and type(T['Value'],[t1,t2,t3])) ! end ! ): ! ! `Package/Assign`( ! `type/OrderedTuple`::boolean, ! "Returns @true@ if the argument is any non-string Maple object or a string that has ! one of the three forms: @(a1,a2,...)@, @[a1,a2,...]@, or @a1,a2,...@. This is intended ! for use with the c> flag and the @String@ keyword, so that students can enter an ! ordered tuple or sequence for their answer. It is forgiving with brackets in that it ! will accept any of the three forms described above as the student answer and treat each ! as the same ordered tuple. ! The optional second argument can be used to specify the Maple type of all of the ! components of the tuple.<br><br> ! Example: <br><br> ! @c> String(OrderedTuple(integer))@ <br><br> ! will only match ordered tuples @(a1,a2,...)@ for which each @ai@ parses to an ! @integer@. Note that currently this type checking is not recursive, ! so that each @ai@ is interpreted as a parsed Maple expressions, not strings ! (unless the student encloses them in quotes). In particular, you cannot ! test for an @OrderedTuple@ of @OrderedPair@'s, for example, (which would be ambiguous ! anyway since brackets/parentheses are optional). ! ", ! proc(s::anything) ! local S,t1,T,n: ! t1:='anything'; ! if nargs>1 then t1:=args[2] fi: ! if not type(s,string) then return type(s,list(t1)) fi; ! S:=`Util/RemoveWhiteSpace`(s): ! # convert () to [] ! if S[1]="(" and S[-1]=")" then S:=cat("[",S[2..-2],"]"); fi: ! # if they omitted brackets, supply them ! if S[1]<>"[" or S[-1]<>"]" then S:=cat("[",S,"]"): fi: ! n:=length(S); ! if n<3 then return false fi: ! T:=`aim/SafeParse`(S): ! return evalb(T['Status']="OK" and type(T['Value'],list(t1))) ! end ! ): ! ! `Package/Assign`( ! `type/SeqOrderedPair`::boolean, ! "Returns @true@ if the argument is a Maple list of lists of two elements or a string ! that has the forms: @(a1,b1),(a2,b2),...,(an,bn)@ where any of the parentheses can ! be replaced by square brackets and the entire list can be surrounded by square ! brackets. This is intended for use with the c> flag and the @String@ keyword, so ! that students can enter a sequence of ordered pairs for their answer. ! The optional second and third arguments can be used to specify the Maple type of ! @ai@ and @bi@ respectively. All pairs must have the same types of components.<br><br> ! ! Example: <br><br> ! @c> String(SeqOrderedPair(integer,rational))@ <br><br> ! ! will only match sequences of ordered pairs @(a,b)@ for which @a@ parses to an ! @integer@ and @b@ parses to a @rational@. Note that currently this type checking ! is not recursive, so that @a@ and @b@ are interpreted as parsed Maple ! expressions, not strings (unless the student encloses them in quotes). ! ", ! proc(s::anything) ! local S,t1,t2,T,n,LPorB,RPorB,NotComma,OnePair,OnePPair, ! OneBPair,SeqPair,SeqPPair,SeqBPair,BSeqBPair: ! t1:='anything'; t2:='anything'; ! if nargs>1 then t1:=args[2] fi: ! if nargs>2 then t2:=args[3] fi: ! if not type(s,string) then return type(s,list([t1,t2])) fi; ! S:=`Util/RemoveWhiteSpace`(s): ! # build our regular expression ! NotComma:="[^,]"; ! OnePPair:=cat("\\((",NotComma,"+,",NotComma,"+)","\\)"); ! # convert all (a,b) to [a,b] ! S:=StringTools:-RegSubs(OnePPair = "[\\1]",S); ! # if they omitted surrounding brackets, supply them ! if S[1..2]<>"[[" or S[-2..-1]<>"]]" then S:=cat("[",S,"]"): fi: ! T:=`aim/SafeParse`(S): ! return evalb(T['Status']="OK" and type(T['Value'],list([t1,t2]))) ! end ! ): ! ! `Package/Assign`( ! `Parse/OrderedTuple`::list, ! "Parses a string of type @OrderedPair@, @OrderedTriple@, or @OrderedTuple@ and returns ! the Maple list of elements that the string represents. If the string cannot be ! parsed it returns @NULL@. ! ", ! proc(s::{string,list}) ! local S,T,n: ! if type([s],[list]) then return s fi; ! S:=`Util/RemoveWhiteSpace`(s): ! n:=length(S); ! # convert () to [] ! if S[1]="(" and S[-1]=")" and n>2 then S:=cat("[",S[2..-2],"]"); fi: ! # if they omitted brackets, supply them ! if S[1]<>"[" or S[-1]<>"]" and n>2 then S:=cat("[",S,"]"): fi: ! T:=`aim/SafeParse`(S): ! if T['Status']="OK" then ! return T['Value'] ! else ! return NULL; ! fi ! end ! ): ! ! `Package/Assign`( ! `Parse/SeqOrderedTuple`::list(list), ! "Parses a string of type @SeqOrderedPair@ and returns ! the Maple list of elements that the string represents. If the string cannot be ! parsed it returns @NULL@. ! ", ! proc(s::{string,list}) ! local S,T,n,NotComma,OnePPair: ! if type([s],[list(list)]) then return s fi; ! S:=`Util/RemoveWhiteSpace`(s): ! # build our regular expression ! NotComma:="[^,]"; ! OnePPair:=cat("\\((",NotComma,"+,",NotComma,"+)","\\)"); ! # convert all (a,b) to [a,b] ! S:=StringTools:-RegSubs(OnePPair = "[\\1]",S); ! # if they omitted surrounding brackets, supply them ! if S[1..2]<>"[[" or S[-2..-1]<>"]]" then S:=cat("[",S,"]"): fi: ! T:=`aim/SafeParse`(S): ! if T['Status']="OK" then ! return T['Value'] ! else ! return NULL; ! fi ! end ! ): ! ! EndPackage(): \ No newline at end of file |