License | GPL-3.0-or-later (see the LICENSE file) |
---|---|
Maintainer | michaellan202@gmail.com |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
This module exports everything you need to work with significant figures, including parsing and evaluation.
Synopsis
- data Term
- = Measured {
- numSigFigs :: Integer
- value :: BigDecimal
- | Constant Rational
- = Measured {
- data Op
- data Expr
- data Function
- parseEval :: Text -> Either Text Term
- parse :: Text -> Either Text Expr
- parse' :: Text -> Expr
- evaluate :: Expr -> Either Text Term
- evaluate' :: Expr -> Term
- display :: Term -> Text
- displayInformational :: Term -> (Text, Text)
- displayFull :: Term -> Text
- processExpression :: Text -> Text
- measured :: Integer -> Rational -> Term
- constant :: Rational -> Term
- l :: Term -> Expr
- lMeasured :: Integer -> Rational -> Expr
- lConstant :: Rational -> Expr
- add :: [Expr] -> Expr
- sub :: [Expr] -> Expr
- mul :: [Expr] -> Expr
- div :: [Expr] -> Expr
- exp :: Expr -> Expr -> Expr
- apply :: Function -> Expr -> Expr
Types
The basic datatype to represent measurements, constant terms, and evaluation results
Measured | A measured value with a finite number of significant figures and an associated value |
| |
Constant Rational | A constant value with infinite significant figures |
The types of (infix) operators
A datatype to represent (not-yet-evaluated) expressions. Use parse
to create such an expression from text.
A datatype representing the supported functions.
Instances
Bounded Function Source # | |
Enum Function Source # | |
Show Function Source # | |
Eq Function Source # | |
High-Level Functions
parseEval :: Text -> Either Text Term Source #
Takes an expression in text and returns either an error message or an evaluated term.
parse' :: Text -> Expr Source #
Like parse
, but assume the result is a valid expression and crash otherwise.
evaluate :: Expr -> Either Text Term Source #
Given an expression tree, evaluate it and return either an error or result.
evaluate' :: Expr -> Term Source #
Like evaluate
, but assume the result is a valid term and crash otherwise.
display :: Term -> Text Source #
Given a term, display it in the most convenient way possible. This means, if the normal representation of the number accurately represents how many significant figures it has, then display it normally. Adds trailing zeroes if necessary to floats and opts for scientific notation if necessary.
Examples
>>>
display $ measured 3 200
"200."
>>>
display $ measured 3 4
"4.00"
>>>
display $ measured 2 400
"4.0 x 10^2"
>>>
display $ measured 2 430
"430"
>>>
display $ measured 1 1
"1"
>>>
display $ constant (3 % 8)
"0.375"
>>>
display $ constant (4 % 9)
"4/9"
>>>
display $ measured 2 4.3
"4.3"
displayInformational :: Term -> (Text, Text) Source #
Given a term, return a tuple where the first element is the output of display and the second is an annotation of the type of value. Used in the API.
Examples
>>>
displayInformational $ constant 3
("3","constant value")
>>>
displayInformational $ measured 2 3.4
("3.4","2 significant figures")
>>>
displayInformational $ measured 3 3400
("3.40 x 10^3","3 significant figures")
displayFull :: Term -> Text Source #
Used in the CLI. Not super pretty but gets the job done in terms of displaying enough information.
Examples
>>>
displayFull (constant 3.45)
"3.45 (const)"
>>>
displayFull (measured 3 8500)
"8.50 x 10^3 (3 s.f.)"
processExpression :: Text -> Text Source #
A convenience function for use in REPLs (used in the CLI). Returns text that can either signify a result or error.
Creating and Manipulating Terms and Expressions
lMeasured :: Integer -> Rational -> Expr Source #
Create a literal node and construct the Measured
value argument at the same time. Convenience function.
lConstant :: Rational -> Expr Source #
Create a literal node and construct the Constant
value argument at the same time. Convenience function.