Haskell Computer Algebra SystemRob TougherDecember 15, 2007Rob Tougher Haskell Computer Algebra SystemOutlineITutorialIImplementationILooking BackRob Tougher Haskell Computer Algebra SystemTutorial: Language SummaryHCAS is a subset of Haskell, plus support for computer algebra.IPurely functional languageIConstruction of mathematical expressionsINavigation of mathematical expressionsRob Tougher Haskell Computer Algebra SystemTutorial: Running HCAS$ echo "main = 7" | ./hcasi7$Rob Tougher Haskell Computer Algebra SystemTutorial: Hello World!The HCAS Hello World program:main = "Hello World!"Output: “Hello World!”Rob Tougher Haskell Computer Algebra SystemTutorial: Basic Data TypesINumber – integer and floating point types for numbersICharacter – single printable characterIList – contains zero or more elementsIString – list of charactersRob Tougher Haskell Computer Algebra SystemTutorial: NumbersNumbers represent integers or floating point types:main = 7.5Output: 7.5Rob Tougher Haskell Computer Algebra SystemTutorial: StringsStrings represent a list of characters:main = "Hello World!"Output: “Hello World!”Rob Tougher Haskell Computer Algebra SystemTutorial: ListsLists represent zero or more items:main = [1,2,3,4,5]Output: [1,2,3,4,5]Rob Tougher Haskell Computer Algebra SystemTutorial: OperatorsIMath operators – addition, subtraction, multiplication, etc.For basic math.IList operators – the “++” operator concatenates two lists.Rob Tougher Haskell Computer Algebra SystemTutorial: Math OperatorsMath operators follow normal rules of associativity and precedence:main = 2 + 3 * 4Output: 14Rob Tougher Haskell Computer Algebra SystemTutorial: List OperatorsThe concatenation operator lets you concatenate two lists:main = [1,2,3] ++ [4,5]Output: [1,2,3,4,5]Rob Tougher Haskell Computer Algebra SystemTutorial: FunctionsFunctions represent callable HCAS expressions:IZero or more input arguments.IApplicative-order evaluation.IStrict evaluationRob Tougher Haskell Computer Algebra SystemTutorial: Calling a Function, No ArgumentsCalling a function with zero arguments:foo = 7main = fooOutput: 7Rob Tougher Haskell Computer Algebra SystemTutorial: Calling a Function, w/ ArgumentsCalling a function with one or more arguments:add(x, y) = x + ymain = add(3,4)Output: 7Rob Tougher Haskell Computer Algebra SystemTutorial: Function List PatternsThe 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 SystemTutorial: Math Expression Data TypeIf an identifier does not match a function name, it represents amathematical expression:main = x + yOutput: x + yRob Tougher Haskell Computer Algebra SystemTutorial: Math Expression Data TypeA math expression is stored as a tree, using the normal rules ofprecedence and associativity:main = a*b + c - d-+*a bcdRob Tougher Haskell Computer Algebra SystemTutorial: Function Math PatternsYou can put any math operators in a function argument. Thesecreate 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”.)+*a bcRob Tougher Haskell Computer Algebra SystemTutorial: Let ExpressionsLet expressions create a new scope:main =letx = 7y = 8add(a,b) = a+binadd(x,y)Output: 15Rob Tougher Haskell Computer Algebra SystemTutorial: Derivative Examplemain = derivative(3*x^2+2*x)derivative(a+b) = derivative(a) + derivative(b)derivative(a-b) = derivative(a) - derivative(b)derivative(c*x^e) = c*e*simplify(x^(e-1))derivative(c*x) = cderivative(x) = 0simplify(x^1) = xsimplify(x^0) = 1simplify(x+0) = xsimplify(0+x) = xsimplify(x+y) = simplify(x) + simplify(y)simplify(x-y) = simplify(x) - simplify(y)simplify(x) = xOutput: 6*x+2Rob Tougher Haskell Computer Algebra SystemTutorial: Questions?Any questions on the language?Rob Tougher Haskell Computer Algebra SystemImplementation: TechnologiesIHaskell – the entire interpreter is written in Haskell, using theGlasgow Haskell Compiler, v 6.6.1.IHUnit – a unit testing framework, similar to JUnit and NUnit.IParsec – a monadic parsing library for top-down parsing.Rob Tougher Haskell Computer Algebra SystemImplementation: Haskell ModulesIAST.hs – contains the abstract syntax tree.IParser.hs – contains the parsing code. Takes an input string,and returns an AST.IInterpreter.hs – contains the interpreter code.IMainInterpreter.hs – contains the main bootup code (readingfrom stdin, writing to stdout).Rob Tougher Haskell Computer Algebra SystemImplementation: AST.hsdata Block = Block [Statement]data Statement = Function String [Expression] Expressiondata 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 SystemImplementation: Parser.hsidentifier :: Parser Stringidentifier =do {c <- letter;cs <- many (identifierChar);return (c:cs);}identifierChar =do {(alphaNum <|> char ’_’);}Rob Tougher Haskell Computer Algebra SystemImplementation: Interpreter.hsinterpret :: [Block] -> Expression -> Expressioninterpret _ (Number n) = (Number n)interpret blocks (Let block expr) =(interpret ([block] ++ blocks) expr)interpret blocks (Addition left right) =(addition left’ right’)whereleft’ = (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 SystemImplementation: MainInterpreter.hsmain =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 SystemImplementation: Unit TestingUnit testing used to verify functionality. Three types of tests:IHaskell unit testsIHCAS boolean unit testsIHCAS expected vs. actual unit testsRob Tougher Haskell Computer Algebra SystemImplementation: Haskell Unit TestsHaskell 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 SystemImplementation: HCAS
View Full Document