Releases: oplS15projects/Lambda
Final Code Turn-in
Lambda
ShuddaWuddaCdr
Authors
- Brian Carlson
- Joshua Caravetta
- Norman Mutunga
Overview
The goal was to create a core mathematical engine that is simple, powerful and easy to build upon. Simple in terms that it allowed the user to enter their equation to evaluate in infix notation, along with English keywords to further define operations on the equation. The power of Lambda will comes from the ease at which the mathematical engine backend can be expanded on to add new functionality.
After demoing the project, we discovered that we effectively created our own language on top of Racket, where any user can add in their own keyword procedures with our backend abstraction. These procedures aren't limited to mathematical operations and can do anything the user wants. The argument of the keyword is simply a string and can be manipulated in almost anyway desirable.
Concepts Demonstrated
- Abstraction - The backend uses a method of KeyPairs to create a database of keywords with a paired procedure. An abstraction barrier is made between the database and the user, so one can simply add a key and paired procedure to the database with
add-keyword. - Lexical Parsing -
exp-lexeruses lexical parsing tools, along with regex to tag input data strings.read-arithuses regex to re-organize infix notation equations into prefix notation equations. Finally, themain-parseruses the power of theexp-lexerto extract keywords and format equations for evaluation in the backend. - Key Procedures - These procedures were added and constructed with ease to read in mind. Each procedure is placed in its own racket source file, and included into the
Keyword Definitions. The calls to these procedures in their KeyPair definitions are simple and abstracted from the low level source code. - GUI Strategies - The windowing toolbox is used to provide the basic building block for the
framewhich is a top level window ,textfields,editor-canvasandpasteboardare all defined in theframewhich is the parent. On this platform theInput textfieldtakes in expressions and hands them over to the abstractedmain-parserin the backend. The solution is sent to theOutput textfieldif its just a text result, while graphical outputs are also sent to the canvas.
External Technology and Libraries
- Plot - This library was used to create plots in Lambda's GUI canvas with use of the
plotkeyword. - GUI - This library was used to create Lambda's GUI.
- Parser - This library was used to create the
exp-lexer, that extracts and tags all data types from the user input string.
How to Download and Run
- Download the latest release .zip.
- Open and run
Lambda.rkt. - Input expression is typed into the
Input, using syntax:keyword equation. - Output is seen in the
Outputor lower canvas depending on keywords used.
Working keywords: eval, plot, deriv, simplify. All procedures only work with variable x.
eval- evaluation of basic equations with any combination of the following operators: -,+,/,*,^.plot- makes a plot of basic equations with any combination of the following operators: -,+,/,*,^.deriv- produces the derivative of a given equation. currently only works with operators: +,*,^.simplify- simplifies the given equation. currently only works with operators: +,*.
As a heads up, there are many cases that simplify and deriv can be broken
Example Inputs
Example 1: eval
evalis the default keyword if no keyword is entered.eval 1+2*3^4or1+2*3^4will both evaluate to: 163.- If
evalis used on an equation with a variable, such asx+2, it will returncannot evaluate: x+2since there is a variable.
Example 2: plot
plot x^2+x+3- This will plot
x+2*x+3in the lower canvas field
Example 3: deriv
deriv 6*x + 30*xwill output 36 .deriv x + 2will output 1 .
Example 4: simplify
simplify x+x+4will output2x+4.
Adding Your Own Keyword
Here is where Lambda becomes its own language, where we can add our own keywords and procedures. To add your own keyword to Lambda simply:
- Open
KeywordDefinitions.rktinProcedures/. - Scroll to the bottom of the file and insert
(add-keyword 'your-keyword (lambda(x) _your-procedure-body_)) - The argument
xto the lambda is in the form of a string. (It contains anything after thekeywordfrom theInputfield) - The return type of the keyword procedure must be a string to be printed to the
Outputfield of Lambda.
For example, during our demo, Roy asked how he could add his own keyword to Lambda. He wanted to implement increment as a keyword, so we quickly instantiated it in Lambda by adding the following to KeywordDefinitions.rkt.
(add-keyword `inc (lambda(x)
(number->string (+ (string->number x) 1)))
)
Now if when you run Lambda and input inc 10, you will get 11 in the output box. It is that simple!
Demo Release
Lambda Demo Release
How to Download and Run
- Download and unpack the .zip file
- Open and run Lambda.rkt
- Input expression is typed into the
Input, using syntax:keyword equation - Output is seen in the
Outputor lower canvas depending on keywords used.
Example 1: eval
evalis the default keyword if no keyword is entered.eval 1+2*3^4or1+2*3^4will both evaluate to: 163.- If
evalis used on an equation with a variable, such asx+2, it will returncannot evaluate: x+2since there is a variable.
Example 2: plot
plot x^2+x+3- support for only one variable x is currently available.- This will plot
x+2*x+3in the lower canvas field
Example 3: deriv
deriv 6*x + 30*xwill output 36 .derivofx + 2will output 1 .
Milestone 2
Current Project Status:
- Working frontend GUI with input and output fields.
- Working general expression parser and infix->prefix parser that handles precedence.
- High and Low level abstracted backend that forms a dynamic database of keys & pairs for additional procedures.
- Evaluation of basic equations with any combination of the following operators: -,+,/,*.
In Progress/ Problems:
(Josh)
Current state of simplify:
I am running into issues of matching patterns. My current thought is to break out each postfix list into the larger list before performing any simplifications. This would allow for larger scale simplification. I am not sure if this method is going to work. We have also decided to limit the number of variables to one.
(Brian)
Current state of plot:
The syntax of (plot) requires a function as its first argument. Currently, the user's input is in the form of a string and there seems to be no way of forming it into a workable function for plot to take in. We have tried numerous 'hacks' or workarounds but they haven't worked thus far. There is no file/function currently implemented yet, but it has been attempted.
(norman)
Doing research on a suitable derivative function.
How to use Lambda:
- Run Lambda.rkt
- Type in any numeric equation into the Input field (only integers).
- Hit enter, and the evaluated result of the equation will be presented in the Output field.
(If a variable is seen in the input, Lambda will return it cannot currently evaluate the input.)
Example input:
Input: 1+2*4+5
Output: evaluated to: 14
Milestone 1
We have the parser linked to the key pair.
Test this by using (main-parser exp)
example:
(main-parser "simplify x+2x+3")