46 lines
980 B
Haskell
46 lines
980 B
Haskell
module Calculator where
|
|
|
|
import Utils
|
|
import Expr
|
|
import Parser
|
|
|
|
data Law = Law LawName Equation
|
|
|
|
type LawName = String
|
|
type Equation = (Expr,Expr)
|
|
|
|
instance Show Law where
|
|
showsPrec _ (Law name (e1, e2)) =
|
|
showString (name ++ ": ")
|
|
. shows e1
|
|
. showString " = "
|
|
. shows e2
|
|
|
|
law :: Parser Law
|
|
law = do name <- upto ':'
|
|
eqn <- equation
|
|
return (Law name eqn)
|
|
|
|
equation :: Parser Equation
|
|
equation = do lh <- expr
|
|
symbol "="
|
|
rh <- expr
|
|
return (lh, rh)
|
|
|
|
sortLaws :: [Law] -> [Law]
|
|
sortLaws laws = simple ++ others ++ defn
|
|
where (simple, nonsimple) = partition isSimple laws
|
|
(defn, others) = partition isDefn nonsimple
|
|
|
|
isSimple, isDefn :: Law -> Bool
|
|
|
|
isSimple (Law _ (Compose a, Compose b))
|
|
= length a > length b
|
|
|
|
isDefn d = case d of
|
|
(Law _ (Compose [Con f es], _)) -> all Calculator.isVar es
|
|
_ -> False
|
|
|
|
isVar v = case v of
|
|
(Compose [Var _]) -> True
|
|
_ -> False
|