Haskell Computer Algebra System (HCAS) Final ReportRob Tougher ([email protected])December 15, 20071Contents1 Introduction 32 Language Tutorial 32.1 Command Line Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.2 Hello World! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Language Manual 63.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63.2 Top-Level Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63.3 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73.3.1 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73.3.2 Expression Atoms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83.3.3 Boolean Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103.3.4 Math Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113.3.5 List Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113.3.6 Miscellaneous Expressions . . . . . . . . . . . . . . . . . . . . . . . . 123.4 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123.4.1 Function Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . 133.4.2 Applicative-Order Argument Evaluation . . . . . . . . . . . . . . . . 143.5 Syntax Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Project Plan 1525 Architectural Design 166 Test Plan 176.1 Example Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176.1.1 Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176.1.2 Calculus Derivatives . . . . . . . . . . . . . . . . . . . . . . . . . . . 196.1.3 Simplification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196.2 Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Lessons Learned 218 Appendix - Code Listing 218.1 AST.hs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218.2 Parser.hs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268.3 Interpreter.hs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328.4 MainInterpreter.hs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388.5 AllTests.hs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3931 IntroductionFor the class project I implemented a simple computer algebra system, HCAS. HCAS isa purely functional programming language that provides a set of basic operations for con-structing and manipulating algebraic expressions. Simply put, you can think of HCAS as asubset of Haskell, plus support for computer algebra.HCAS has three main features:• Purely Functional Language. HCAS is a purely functional subset of Haskell. Thereare functions, recursion, lists, strings, pattern matches for function arguments, etc.No variables, sequencing of operations, or other items from imperative programminglanguages.• Construction of Mathematical Expressions. HCAS allows you to construct math-ematical expressions using an intuitive syntax. You are able to define mathematicalexpressions inline. That is, if you write the expression x + y − z, this is automaticallyconstructed as a math expression.• Navigation of Mathematical Expressions. HCAS uses the concept of patternmatching in function arguments to allow you to navigate mathematical expressions.Consider the following simple function:printType ( l e f t ∗ r i g h t ) =” M u l t i p l i c a t i o n ”printType ( l e f t+r i g h t ) =” Addition ”This function has two definitions, one for addition and one for multiplication. Theversion that gets executed at runtime is chosen based on the expression argument thatyou pass in. So if you call the function as “printType(x*y+z)”, the version for additionwill be called, because addition binds the loosest. In the body of the function “left”refers to “x*y”, and “right” refers to “z”.2 Language Tutorial2.1 Command Line UsageOne command line utility is shipped with this project: hcasi, the HCAS interpreter. Thisutility uses standard input for the HCAS input and standard output for the program output:4$ ./hcasi < myscript.hcas > out.txt2.2 Hello World!Every good language tutorial contains a few “Hello …
View Full Document