Main program ---------------------------------------------------------------------- > module Main ( main ) > where ---------------------------------------------------------------------- ---------------------------------------------------------------------- > import Support > import IOSupport > > import Text.ParserCombinators.Parsec > import Data.Char > import CLI > import Pretty > import CPSParser > > import Prim > import Expr > import Limes > import Print > import Parse > import Function hiding ( zero ) > import Derive > import Convert > import Simplify hiding ( summ, prod ) > import Replace > import Table > import Integrate ---------------------------------------------------------------------- Command-line interpreter ---------------------------------------------------------------------- > type MyState = (Expr, Table) > g # f = f . g > main = readTable "integrals" >>= \t -> > putStrLn "for help type /help" >> > commandLineInterpreter > "> " commands evaluate (Var "x", t) ---------------------------------------------------------------------- ---------------------------------------------------------------------- > evaluate :: MyState -> String -> IO MyState > evaluate s str = case runParser (expression s) () "" str of > Left _ -> putStrLn "syntax error" >> return s > Right Nothing -> sorry s > Right (Just (t, s')) -> putStrLn (pretty 78 t) >> return s' > sorry s = putStrLn "sorry, does not compute" >> return s ---------------------------------------------------------------------- Abarbeitung von Ausdr"ucken Execution for printing or printer ---------------------------------------------------------------------- > opt m a = m <|> return a > expression :: (Expr, Table) -> Text.ParserCombinators.Parsec.Parser (Maybe (Txt, MyState)) > expression (e, t) = ( (lit '\'' >> var >>= \x -> opt expr e >>= \e -> > return (wrap x (Just . derive) e)) > <|> (lit '|' >> var >>= \x -> opt expr e >>= \e -> > return (wrap x (integrate t) e)) > <|> (lit '[' >> expr >>= \e1 -> lit '/' >> expr >>= \e2 -> > lit ']' >> opt expr e >>= \e -> > return (wrap "" (Just . replace (abstract "" e1, > abstract "" e2)) e)) > <|> (lits "lim" >> var >>= \x -> lits "->" >> > ext expr >>= \a -> opt expr e >>= \e -> > return (wrap' x (limes (mapE (abstract "") a)) e)) > <|> (lits "st2" >> > return (wrap "" (Just . standard2) e)) > <|> (lits "st3" >> number >>= \n -> > return (wrap "" (Just . standard3 (read n)) e)) > <|> (expr >>= \e -> > return (wrap "" Just e)) ) > >>= \r -> eof >> return r > where > wrap x f = abstract x # standard1 # f > # mapMaybe (standard1 # rapply x # \e' -> (pp e', (e', t))) > wrap' x f = abstract x # standard1 # f > # mapMaybe (mapE (standard1 # rapply "") # \v -> (pp v, (e, t))) ---------------------------------------------------------------------- ---------------------------------------------------------------------- > rapply x f = apply f (Var x) ---------------------------------------------------------------------- ---------------------------------------------------------------------- > mapMaybe f Nothing = Nothing > mapMaybe f (Just a) = Just (f a) ---------------------------------------------------------------------- Abarbeitung von Kommandos Standardformen zu `expression'!!! Execution for commands standardforms for 'expression' ---------------------------------------------------------------------- > commands = [ > (["/exit", "/quit"], \_ _ -> > exit), > (["?", "/help"], \s _ -> > help >> return s)] ---------------------------------------------------------------------- ---------------------------------------------------------------------- > help = sequence (map putStr [ > " enter expression\n", > "' differentiate (or current expression)\n", > "| integrate (or current expression)\n", > "[/] replace by in (or current)\n", > "lim ... \n", > "st2 standard form 2\n", > "st3 standard form 3 [n]\n", > "? display this command summary\n", > "/exit quit the system\n", > "/help display this command summary\n", > "/quit quit the system\n"]) ---------------------------------------------------------------------- Hilfsfunktionen Helper functions ---------------------------------------------------------------------- > singleton a = [a] ---------------------------------------------------------------------- ---------------------------------------------------------------------- > getNatArg :: [String] -> IO (Integer, [String]) > getNatArg [] = error "argument expected" > getNatArg (arg:args) > | all isDigit arg = return (read arg, args) > | otherwise = error "number expected" ----------------------------------------------------------------------