Haskell Computer Algebra System Rob Tougher December 15 2007 Rob Tougher Haskell Computer Algebra System Outline I Tutorial I Implementation I Looking Back Rob Tougher Haskell Computer Algebra System Tutorial Language Summary HCAS is a subset of Haskell plus support for computer algebra I Purely functional language I Construction of mathematical expressions I Navigation of mathematical expressions Rob Tougher Haskell Computer Algebra System Tutorial Running HCAS echo main 7 hcasi 7 Rob Tougher Haskell Computer Algebra System Tutorial Hello World The HCAS Hello World program main Hello World Output Hello World Rob Tougher Haskell Computer Algebra System Tutorial Basic Data Types I Number integer and floating point types for numbers I Character single printable character I List contains zero or more elements I String list of characters Rob Tougher Haskell Computer Algebra System Tutorial Numbers Numbers represent integers or floating point types main 7 5 Output 7 5 Rob Tougher Haskell Computer Algebra System Tutorial Strings Strings represent a list of characters main Hello World Output Hello World Rob Tougher Haskell Computer Algebra System Tutorial Lists Lists represent zero or more items main 1 2 3 4 5 Output 1 2 3 4 5 Rob Tougher Haskell Computer Algebra System Tutorial Operators I Math operators addition subtraction multiplication etc For basic math I List operators the operator concatenates two lists Rob Tougher Haskell Computer Algebra System Tutorial Math Operators Math operators follow normal rules of associativity and precedence main 2 3 4 Output 14 Rob Tougher Haskell Computer Algebra System Tutorial List Operators The concatenation operator lets you concatenate two lists main 1 2 3 4 5 Output 1 2 3 4 5 Rob Tougher Haskell Computer Algebra System Tutorial Functions Functions represent callable HCAS expressions I Zero or more input arguments I Applicative order evaluation I Strict evaluation Rob Tougher Haskell Computer Algebra System Tutorial Calling a Function No Arguments Calling a function with zero arguments foo 7 main foo Output 7 Rob Tougher Haskell Computer Algebra System Tutorial Calling a Function w Arguments Calling a function with one or more arguments add x y x y main add 3 4 Output 7 Rob Tougher Haskell Computer Algebra System Tutorial Function List Patterns The colon operator in a function argument creates a list pattern reverse x xs reverse xs x reverse main reverse Hello World Output dlroW olleH Rob Tougher Haskell Computer Algebra System Tutorial Math Expression Data Type If an identifier does not match a function name it represents a mathematical expression main x y Output x y Rob Tougher Haskell Computer Algebra System Tutorial Math Expression Data Type A math expression is stored as a tree using the normal rules of precedence and associativity main a b c d a d c b Rob Tougher Haskell Computer Algebra System Tutorial Function Math Patterns You can put any math operators in a function argument These create math patterns printType x y addition printType x y subtraction main printType a b c Output addition In the call to printType x refers to a b and y refers to c c a b Rob Tougher Haskell Computer Algebra System Tutorial Let Expressions Let expressions create a new scope main let x 7 y 8 add a b a b in add x y Output 15 Rob Tougher Haskell Computer Algebra System Tutorial Derivative Example main derivative 3 x 2 2 x derivative a b derivative a b derivative c x e derivative c x derivative x 0 simplify x 1 simplify x 0 simplify x 0 simplify 0 x simplify x y simplify x y simplify x x derivative a derivative b derivative a derivative b c e simplify x e 1 c x 1 x x simplify x simplify y simplify x simplify y Output 6 x 2 Rob Tougher Haskell Computer Algebra System Tutorial Questions Any questions on the language Rob Tougher Haskell Computer Algebra System Implementation Technologies I Haskell the entire interpreter is written in Haskell using the Glasgow Haskell Compiler v 6 6 1 I HUnit a unit testing framework similar to JUnit and NUnit I Parsec a monadic parsing library for top down parsing Rob Tougher Haskell Computer Algebra System Implementation Haskell Modules I AST hs contains the abstract syntax tree I Parser hs contains the parsing code Takes an input string and returns an AST I Interpreter hs contains the interpreter code I MainInterpreter hs contains the main bootup code reading from stdin writing to stdout Rob Tougher Haskell Computer Algebra System Implementation AST hs data Block Block Statement data Statement Function String Expression Expression data Expression Strings and lists List Expression Concat Expression Expression ListPattern Expression CharValue Char Function related items Call String Expression Let Block Expression Rob Tougher Haskell Computer Algebra System Implementation Parser hs identifier Parser String identifier do c letter cs many identifierChar return c cs identifierChar do alphaNum char Rob Tougher Haskell Computer Algebra System Implementation Interpreter hs interpret Block Expression Expression interpret Number n Number n interpret blocks Let block expr interpret block blocks expr interpret blocks Addition left right addition left right where left interpret blocks left right interpret blocks right addition Number n1 Number n2 Number n1 n2 addition left right Addition left right Rob Tougher Haskell Computer Algebra System Implementation MainInterpreter hs main do script getContents case parse file script of Right parsed do interpreted return interpretFile parsed putStrLn showHCAS interpreted Left err do putStrLn show err Rob Tougher Haskell Computer Algebra System Implementation Unit Testing Unit testing used to verify functionality Three types of tests I Haskell unit tests I HCAS boolean unit tests I HCAS expected vs actual unit tests Rob Tougher Haskell Computer Algebra System Implementation Haskell Unit Tests Haskell unit tests are writing using Haskell testNum2 TestCase do expected return Number 1 3 Right actual return parse numberAtom 1 3 assertEqual testNum2 expected actual Rob Tougher Haskell Computer Algebra System Implementation HCAS Boolean Unit Tests HCAS boolean tests are HCAS scripts that must return a boolean true value main 7 1 2 4 Output True Rob Tougher Haskell Computer Algebra System Implementation HCAS Expected vs Actual Unit Tests HCAS expected vs actual tests have an HCAS script and expected output file for each test addition hcas addition expected txt subtraction hcas subtraction expected txt
View Full Document
Unlocking...