WeBWorK Main Forum

Inline Lua: proof of concept

Inline Lua: proof of concept

by Andrew Dabrowski -
Number of replies: 2
Here's a working example of using Inline::Lua to define webwork problems. The Lua files are based on code I wrote for use with lualatex in a Finite Math class with a module on voting. (My complete lualatex repository is here.) After two weeks of work and much help from the kind people of this forum I have got three problems working.

(0 pts) Problems/Voting/v01.pg
Three candidates, Abner, Barbara, and Chris, are running for mayor, and the voters have the following profile. \displaystyle{ \newcommand{\voteProfileABC}[6]{ \begin{tabular}{rcccccc} \toprule Votes: & #1 & #2 & #3 & #4 & #5 & #6 \\ \hline Preferences: & A & A & B & B & C & C \\ & B & C & A & C & A & B \\ & C & B & C & A & B & A \\ \bottomrule \end{tabular} } \newcommand{\agendaC}[5]{ \begin{picture}(60,60)(30,50) \put(0,80){\line(1,0){30}} \put(0,60){\line(1,0){30}} \put(30,70){\line(1,0){40}} \put(40,50){\line(1,0){30}} \put(70,60){\line(1,0){40}} \put(30,80){\line(0,-1){20}} \put(70,70){\line(0,-1){20}} \put(11,84){\cand{#1}} \put(11,64){\cand{#2}} \put(51,54){\cand{#3}} \put(51,74){\cand{#4}} \put(91,64){\cand{#5}} \end{picture} } \voteProfileABC{18}{3}{28}{10}{10}{31} }
Determine the social choice ordering under the Plurality system.
Three candidates, Althea, Barney, and Caspar, are running for D.A., and the voters have the following profile. \displaystyle{ \newcommand{\voteProfileABC}[6]{ \begin{tabular}{rcccccc} \toprule Votes: & #1 & #2 & #3 & #4 & #5 & #6 \\ \hline Preferences: & A & A & B & B & C & C \\ & B & C & A & C & A & B \\ & C & B & C & A & B & A \\ \bottomrule \end{tabular} } \newcommand{\agendaC}[5]{ \begin{picture}(60,60)(30,50) \put(0,80){\line(1,0){30}} \put(0,60){\line(1,0){30}} \put(30,70){\line(1,0){40}} \put(40,50){\line(1,0){30}} \put(70,60){\line(1,0){40}} \put(30,80){\line(0,-1){20}} \put(70,70){\line(0,-1){20}} \put(11,84){\cand{#1}} \put(11,64){\cand{#2}} \put(51,54){\cand{#3}} \put(51,74){\cand{#4}} \put(91,64){\cand{#5}} \end{picture} } \voteProfileABC{25}{7}{11}{17}{24}{16} } Determine the social choice ordering under the Agenda system, with the specific agenda given below. \displaystyle{ \newcommand{\voteProfileABC}[6]{ \begin{tabular}{rcccccc} \toprule Votes: & #1 & #2 & #3 & #4 & #5 & #6 \\ \hline Preferences: & A & A & B & B & C & C \\ & B & C & A & C & A & B \\ & C & B & C & A & B & A \\ \bottomrule \end{tabular} } \newcommand{\agendaC}[5]{ \begin{picture}(60,60)(30,50) \put(0,80){\line(1,0){30}} \put(0,60){\line(1,0){30}} \put(30,70){\line(1,0){40}} \put(40,50){\line(1,0){30}} \put(70,60){\line(1,0){40}} \put(30,80){\line(0,-1){20}} \put(70,70){\line(0,-1){20}} \put(11,84){\cand{#1}} \put(11,64){\cand{#2}} \put(51,54){\cand{#3}} \put(51,74){\cand{#4}} \put(91,64){\cand{#5}} \end{picture} } \agendaC{A}{C}{B}{}{} }
Three candidates A, B, and C are running for office, and there is one block of voters, the Werdna block, who all favor the ranking ABC. If the election were held today under the Iksworbad Voting System the result would be the social ranking B C A. Now suppose that some of the Werdna block of voters are thinking about changing their preferred ranking to B A C.
If the vote were held under the Iksworbad system with only those changes in the voters' profile, then the resulting social ranking would be A B C. Based on only these two examples, does the Iksworbad system necessarily violate Monotonicity, Independence of Irrelevant Alternatives, both, or neither? Give your answer as “Mono”, “IIA”, “Both”, or “Neither”.
The attached tgz file has all the necessary files (I think).

Part of the Lua code is structured something like MathObjects but less extensive and sophisticated: matrix, vector, and fraction objects are defined in the Lua folder in pg/lib. The file voting.lua contains the problem definitions and associated auxiliary functions. Note that the first argument is a template of the text, and the second argument is a function that generates replacement values and the correct answer. The file PGMLproblem.lua does the interpretation of voting.lua to produce problem generating objects.

Things I'm still unhappy about:

1. Lua is at version 5.3, lualatex requires 5.2, and Inline::Lua requires 5.1. This necessitated some rewriting and means the goal of "write once (Lua), run twice (webwork and lualatex)" (w1r2) is not yet quite attainable.

2. Another problem with Inline::Lua is that is does not now and may never support metatables, so I had to remove the nice overloading of arithmetic operators that metatables allow.

3. Another obstacle to w1r2 is that I couldn't find a good way to run latex text mode code inside PGML: [` `] calls math mode, so I save that for things like tabular that require the latex interpreter. The result is that I have to modify the problem definition functions in order to produce PGML. Maybe I could just put all my latex code inside an \mbox?
In reply to Andrew Dabrowski

Re: Inline Lua: proof of concept

by Andrew Dabrowski -
Two things I forgot to mention:

1. Of course you must add Lua_Voting.pm to your overrides.

2. Change the file paths in Lua_Voting.pmto reflect your installation.
In reply to Andrew Dabrowski

Re: Inline Lua: proof of concept

by Michael Gage -
This looks very interesting Andrew.  Thanks very much.  It will take a while for me to digest it.
-- MIke