-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/
-- | Solve simple simultaneous equations
--
-- Solve a number of equations simultaneously. This is not Computer
-- Algebra, better think of a kind of type inference algorithm or logic
-- programming with only one allowed solution.
--
-- Only one solution is computed. Simultaneous equations with multiple
-- solutions are not allowed. However, variables may remain undefined.
-- The solver may optionally check for consistency. It does not do so by
-- default since with floating point numbers or symbolic expressions even
-- simple rules may not be consistent.
--
-- The modules ordered with respect to abstraction level are:
--
--
-- - UniqueLogic.ST.System: Construct and solve sets of
-- functional dependencies. Example: assignment3 (+) a b c
-- meaning dependency a+b -> c.
-- - UniqueLogic.ST.Rule: Combine functional dependencies to
-- rules that can apply in multiple directions. Example: add a b
-- c means relation a+b = c which resolves to dependencies
-- a+b -> c, c-a -> b, c-b -> a. For an executable
-- example see UniqueLogic.ST.Example.Rule.
-- - UniqueLogic.ST.Expression: Allows to write rules using
-- arithmetic operators. It creates temporary variables automatically.
-- Example: (a+b)*c =:= d resolves to a+b = x, x*c = d.
-- For an executable example see
-- UniqueLogic.ST.Example.Expression.
-- - UniqueLogic.ST.System.Simple: Provides specialised
-- functions from UniqueLogic.ST.System for the case of a system
-- without labels and consistency checks.
-- - UniqueLogic.ST.System.Label: Provides a custom constructor
-- for variables. When creating a variable you decide whether and how an
-- assignment to this variable shall be logged. There is an example that
-- shows how to solve a logic system using symbolic expressions. The
-- naming and logging allows us to observe shared intermediate results.
-- For an executable example see
-- UniqueLogic.ST.Example.Label.
-- - By using more sophisticated monad transformers, we can check the
-- equations for consistency, report inconsistencies and how they arised.
-- We demonstrate that in UniqueLogic.ST.Example.Verify.
--
--
-- The package is purely Haskell 98.
@package unique-logic
@version 0.4
-- | This module provides several ways to cope with over-determined values.
module UniqueLogic.ST.Duplicate
class C a
accept :: C a => a -> a -> Bool
accept :: C a => a -> a -> Bool
-- | Ignore duplicate ways to determine a variable. The chosen value
-- depends on the particular algorithm.
newtype Ignore a
Ignore :: a -> Ignore a
-- | Duplicate ways to determine a variable value are always considered an
-- error. If you use Rules or Expressions this is not a
-- good idea, since every rule is over-determined.
newtype Forbid a
Forbid :: a -> Forbid a
-- | Duplicate ways to determine a variable value are allowed as long as
-- every way yields the same result. "Same" is meant with respect to the
-- Eq class.
newtype Verify a
Verify :: a -> Verify a
instance GHC.Show.Show a => GHC.Show.Show (UniqueLogic.ST.Duplicate.Verify a)
instance GHC.Classes.Ord a => GHC.Classes.Ord (UniqueLogic.ST.Duplicate.Verify a)
instance GHC.Classes.Eq a => GHC.Classes.Eq (UniqueLogic.ST.Duplicate.Verify a)
instance GHC.Show.Show a => GHC.Show.Show (UniqueLogic.ST.Duplicate.Forbid a)
instance GHC.Classes.Ord a => GHC.Classes.Ord (UniqueLogic.ST.Duplicate.Forbid a)
instance GHC.Classes.Eq a => GHC.Classes.Eq (UniqueLogic.ST.Duplicate.Forbid a)
instance GHC.Show.Show a => GHC.Show.Show (UniqueLogic.ST.Duplicate.Ignore a)
instance GHC.Classes.Ord a => GHC.Classes.Ord (UniqueLogic.ST.Duplicate.Ignore a)
instance GHC.Classes.Eq a => GHC.Classes.Eq (UniqueLogic.ST.Duplicate.Ignore a)
instance GHC.Classes.Eq a => UniqueLogic.ST.Duplicate.C (UniqueLogic.ST.Duplicate.Verify a)
instance GHC.Num.Num a => GHC.Num.Num (UniqueLogic.ST.Duplicate.Verify a)
instance GHC.Real.Fractional a => GHC.Real.Fractional (UniqueLogic.ST.Duplicate.Verify a)
instance GHC.Float.Floating a => GHC.Float.Floating (UniqueLogic.ST.Duplicate.Verify a)
instance UniqueLogic.ST.Duplicate.C (UniqueLogic.ST.Duplicate.Forbid a)
instance GHC.Num.Num a => GHC.Num.Num (UniqueLogic.ST.Duplicate.Forbid a)
instance GHC.Real.Fractional a => GHC.Real.Fractional (UniqueLogic.ST.Duplicate.Forbid a)
instance GHC.Float.Floating a => GHC.Float.Floating (UniqueLogic.ST.Duplicate.Forbid a)
instance UniqueLogic.ST.Duplicate.C (UniqueLogic.ST.Duplicate.Ignore a)
instance GHC.Num.Num a => GHC.Num.Num (UniqueLogic.ST.Duplicate.Ignore a)
instance GHC.Real.Fractional a => GHC.Real.Fractional (UniqueLogic.ST.Duplicate.Ignore a)
instance GHC.Float.Floating a => GHC.Float.Floating (UniqueLogic.ST.Duplicate.Ignore a)
instance (UniqueLogic.ST.Duplicate.C a, UniqueLogic.ST.Duplicate.C b) => UniqueLogic.ST.Duplicate.C (a, b)
instance (UniqueLogic.ST.Duplicate.C a, UniqueLogic.ST.Duplicate.C b, UniqueLogic.ST.Duplicate.C c) => UniqueLogic.ST.Duplicate.C (a, b, c)
-- | This module is intended for documentation purposes. Do not import it!
module UniqueLogic.ST.Example.Term
data T
Const :: Rational -> T
Var :: Name -> T
Max :: T -> T -> T
Add :: T -> T -> T
Sub :: T -> T -> T
Mul :: T -> T -> T
Div :: T -> T -> T
Abs :: T -> T
Signum :: T -> T
type Name = String
instance GHC.Show.Show UniqueLogic.ST.Example.Term.T
instance GHC.Num.Num UniqueLogic.ST.Example.Term.T
instance GHC.Real.Fractional UniqueLogic.ST.Example.Term.T
module UniqueLogic.ST.MonadTrans
-- | Provide the methods that make a transformed monad a monad.
class MonadTrans t => C t
point :: (C t, Monad m) => a -> t m a
bind :: (C t, Monad m) => t m a -> (a -> t m b) -> t m b
-- | Build a regular monad for generic monad transformer and monad. The
-- Const type allows us to force the kind (m :: * -> *) without
-- using ExplicitKindSignatures.
newtype Wrap t m a
Wrap :: (Const (t m a) (m a)) -> Wrap t m a
wrap :: t m a -> Wrap t m a
unwrap :: Wrap t m a -> t m a
lift :: (C t, Monad m) => m a -> Wrap t m a
instance (UniqueLogic.ST.MonadTrans.C t, GHC.Base.Monad m) => GHC.Base.Functor (UniqueLogic.ST.MonadTrans.Wrap t m)
instance (UniqueLogic.ST.MonadTrans.C t, GHC.Base.Monad m) => GHC.Base.Applicative (UniqueLogic.ST.MonadTrans.Wrap t m)
instance (UniqueLogic.ST.MonadTrans.C t, GHC.Base.Monad m) => GHC.Base.Monad (UniqueLogic.ST.MonadTrans.Wrap t m)
instance UniqueLogic.ST.MonadTrans.C Control.Monad.Trans.Identity.IdentityT
instance GHC.Base.Monoid w => UniqueLogic.ST.MonadTrans.C (Control.Monad.Trans.Writer.Lazy.WriterT w)
instance UniqueLogic.ST.MonadTrans.C (Control.Monad.Exception.Synchronous.ExceptionalT e)
instance UniqueLogic.ST.MonadTrans.C Control.Monad.Trans.Maybe.MaybeT
module UniqueLogic.ST.System
data Variable w s a
globalVariable :: (C w, C a) => SimpleUpdater w s a -> ST s (Variable w s a)
class C w => C w
doUpdate :: (C w, (C a)) => Updater w s a
doUpdate :: (C w, (C a)) => Updater w s a
simpleUpdate :: (C w, C a) => SimpleUpdater w s a
updateIfNew :: (C w, C a) => Updater w s a
updateAndCheck :: (C w, C a) => (a -> a -> Wrap w (ST s) ()) -> Updater w s a
class C t => Fragile t
break :: (Fragile t, Monad m) => Wrap t m a
data T w s a
localVariable :: (C w, C a) => T w s (Variable w s a)
constant :: (C w, C a) => a -> T w s (Variable w s a)
assignment2 :: C w => (a -> b) -> Variable w s a -> Variable w s b -> T w s ()
assignment3 :: C w => (a -> b -> c) -> Variable w s a -> Variable w s b -> Variable w s c -> T w s ()
data Apply w s a
-- | This function allows to generalize assignment2 and
-- assignment3 to more arguments. You could achieve the same with
-- nested applications of assignment3 (,).
arg :: Variable w s a -> Apply w s a
runApply :: C w => Apply w s a -> Variable w s a -> T w s ()
solve :: C w => T w s a -> w (ST s) a
query :: Variable w s a -> ST s (Maybe a)
queryForbid :: Variable w s (Forbid a) -> ST s (Maybe a)
queryIgnore :: Variable w s (Ignore a) -> ST s (Maybe a)
queryVerify :: Variable w s (Verify a) -> ST s (Maybe a)
instance GHC.Base.Functor (UniqueLogic.ST.System.Apply w s)
instance GHC.Base.Applicative (UniqueLogic.ST.System.Apply w s)
instance UniqueLogic.ST.System.Inconsistency e => UniqueLogic.ST.System.Fragile (Control.Monad.Exception.Synchronous.ExceptionalT e)
instance UniqueLogic.ST.System.Inconsistency e => UniqueLogic.ST.System.C (Control.Monad.Exception.Synchronous.ExceptionalT e)
instance UniqueLogic.ST.System.C Control.Monad.Trans.Identity.IdentityT
instance GHC.Base.Monoid w => UniqueLogic.ST.System.C (Control.Monad.Trans.Writer.Lazy.WriterT w)
instance GHC.Base.Functor (UniqueLogic.ST.System.T w s)
instance GHC.Base.Applicative (UniqueLogic.ST.System.T w s)
instance GHC.Base.Monad (UniqueLogic.ST.System.T w s)
module UniqueLogic.ST.Rule
generic2 :: (C w) => (b -> a) -> (a -> b) -> Variable w s a -> Variable w s b -> T w s ()
generic3 :: (C w) => (b -> c -> a) -> (c -> a -> b) -> (a -> b -> c) -> Variable w s a -> Variable w s b -> Variable w s c -> T w s ()
equ :: (C w) => Variable w s a -> Variable w s a -> T w s ()
-- | You might be tempted to use the pair rule to collect parameters
-- for rules with more than three arguments. This is generally not a good
-- idea since this way you lose granularity. For building rules with more
-- than three arguments, please build according assignments with
-- arg and runApply and bundle these assignments to rules.
-- This is the way, generic2 and generic3 work.
pair :: (C w) => Variable w s a -> Variable w s b -> Variable w s (a, b) -> T w s ()
max :: (Ord a, C w) => Variable w s a -> Variable w s a -> Variable w s a -> T w s ()
add :: (Num a, C w) => Variable w s a -> Variable w s a -> Variable w s a -> T w s ()
mul :: (Fractional a, C w) => Variable w s a -> Variable w s a -> Variable w s a -> T w s ()
square :: (Floating a, C w) => Variable w s a -> Variable w s a -> T w s ()
pow :: (Floating a, C w) => Variable w s a -> Variable w s a -> Variable w s a -> T w s ()
module UniqueLogic.ST.Expression
-- | An expression is defined by a set of equations and the variable at the
-- top-level. The value of the expression equals the value of the top
-- variable.
data T w s a
-- | Make a constant expression of a simple numeric value.
constant :: (C w, C a) => a -> T w s a
fromVariable :: Variable w s a -> T w s a
fromRule1 :: (C w, C a) => (Variable w s a -> T w s ()) -> (T w s a)
fromRule2 :: (C w, C b) => (Variable w s a -> Variable w s b -> T w s ()) -> (T w s a -> T w s b)
fromRule3 :: (C w, C c) => (Variable w s a -> Variable w s b -> Variable w s c -> T w s ()) -> (T w s a -> T w s b -> T w s c)
data Apply w s f
-- | This function allows to generalize fromRule2 and
-- fromRule3 to more arguments using Applicative
-- combinators.
--
-- Example:
--
--
-- fromRule3 rule x y
-- = runApply $ liftA2 rule (arg x) (arg y)
-- = runApply $ pure rule <*> arg x <*> arg y
--
--
-- Building rules with arg provides more granularity than using
-- auxiliary pair rules!
arg :: T w s a -> Apply w s (Variable w s a)
runApply :: (C w, C a) => Apply w s (Variable w s a -> T w s ()) -> T w s a
(=:=) :: (C w) => T w s a -> T w s a -> T w s ()
infix 0 =:=
(=!=) :: (C w) => T w s a -> T w s a -> T w s a
infixl 4 =!=
sqr :: (C w, C a, Floating a) => T w s a -> T w s a
sqrt :: (C w, C a, Floating a) => T w s a -> T w s a
-- | We are not able to implement a full Ord instance including Eq
-- superclass and comparisons, but we need to compute maxima.
max :: (C w, Ord a, C a) => T w s a -> T w s a -> T w s a
maximum :: (C w, Ord a, C a) => [T w s a] -> T w s a
-- | Construct or decompose a pair.
pair :: (C w, C a, C b) => T w s a -> T w s b -> T w s (a, b)
instance GHC.Base.Functor (UniqueLogic.ST.Expression.Apply w s)
instance GHC.Base.Applicative (UniqueLogic.ST.Expression.Apply w s)
instance (UniqueLogic.ST.System.C w, UniqueLogic.ST.Duplicate.C a, GHC.Real.Fractional a) => GHC.Num.Num (UniqueLogic.ST.Expression.T w s a)
instance (UniqueLogic.ST.System.C w, UniqueLogic.ST.Duplicate.C a, GHC.Real.Fractional a) => GHC.Real.Fractional (UniqueLogic.ST.Expression.T w s a)
-- | Deprecated: This module is intended for documentation purposes. Do
-- not import it!
module UniqueLogic.ST.Example.Verify
data Assign
Assign :: Name -> T -> Assign
type Assigns = [Assign]
data TrackedNumber a
TrackedNumber :: T -> a -> TrackedNumber a
tn1 :: (T -> T) -> (a -> b) -> TrackedNumber a -> TrackedNumber b
tn2 :: (T -> T -> T) -> (a -> b -> c) -> TrackedNumber a -> TrackedNumber b -> TrackedNumber c
newtype Track m a
Track :: ExceptionalT Exception (WriterT Assigns m) a -> Track m a
[runTrack] :: Track m a -> ExceptionalT Exception (WriterT Assigns m) a
data Exception
Exception :: Name -> (TrackedNumber Rational) -> (TrackedNumber Rational) -> Exception
AnonymousException :: Exception
type Variable s = Variable Track s (TrackedNumber Rational)
globalVariable :: Name -> ST s (Variable s)
inconsistency :: Monad m => Name -> TrackedNumber Rational -> TrackedNumber Rational -> Wrap Track m ()
update :: Name -> MaybeT (ST s) (TrackedNumber a) -> MaybeT (Wrap Track (ST s)) (TrackedNumber a)
example :: (Exceptional Exception (Maybe (TrackedNumber Rational), Maybe (TrackedNumber Rational)), Assigns)
instance GHC.Show.Show UniqueLogic.ST.Example.Verify.Exception
instance GHC.Show.Show a => GHC.Show.Show (UniqueLogic.ST.Example.Verify.TrackedNumber a)
instance GHC.Show.Show UniqueLogic.ST.Example.Verify.Assign
instance GHC.Base.Monad m => GHC.Base.Functor (UniqueLogic.ST.Example.Verify.Track m)
instance GHC.Base.Monad m => GHC.Base.Applicative (UniqueLogic.ST.Example.Verify.Track m)
instance GHC.Base.Monad m => GHC.Base.Monad (UniqueLogic.ST.Example.Verify.Track m)
instance Control.Monad.Trans.Class.MonadTrans UniqueLogic.ST.Example.Verify.Track
instance UniqueLogic.ST.MonadTrans.C UniqueLogic.ST.Example.Verify.Track
instance UniqueLogic.ST.System.C UniqueLogic.ST.Example.Verify.Track
instance GHC.Num.Num a => GHC.Num.Num (UniqueLogic.ST.Example.Verify.TrackedNumber a)
instance GHC.Real.Fractional a => GHC.Real.Fractional (UniqueLogic.ST.Example.Verify.TrackedNumber a)
instance GHC.Classes.Eq a => UniqueLogic.ST.Duplicate.C (UniqueLogic.ST.Example.Verify.TrackedNumber a)
module UniqueLogic.ST.System.Label
type Variable w = Variable (WriterT w)
globalVariable :: (Monoid w, C a) => (a -> Writer w a) -> ST s (Variable w s a)
type T w = T (WriterT w)
localVariable :: (C w, C a) => T w s (Variable w s a)
constant :: (C w, C a) => a -> T w s (Variable w s a)
assignment2 :: C w => (a -> b) -> Variable w s a -> Variable w s b -> T w s ()
assignment3 :: C w => (a -> b -> c) -> Variable w s a -> Variable w s b -> Variable w s c -> T w s ()
data Apply w s a
-- | This function allows to generalize assignment2 and
-- assignment3 to more arguments. You could achieve the same with
-- nested applications of assignment3 (,).
arg :: Variable w s a -> Apply w s a
runApply :: C w => Apply w s a -> Variable w s a -> T w s ()
solve :: C w => T w s a -> w (ST s) a
query :: Variable w s a -> ST s (Maybe a)
queryForbid :: Variable w s (Forbid a) -> ST s (Maybe a)
queryIgnore :: Variable w s (Ignore a) -> ST s (Maybe a)
queryVerify :: Variable w s (Verify a) -> ST s (Maybe a)
-- | Deprecated: This module is intended for documentation purposes. Do
-- not import it!
module UniqueLogic.ST.Example.Label
data Assign
Assign :: Name -> T -> Assign
type Assigns = [Assign]
type Variable s = Variable Assigns s (Ignore T)
globalVariable :: Name -> ST s (Variable s)
constant :: Rational -> T Assigns s (Variable s)
-- |
-- x=1
-- y=2
-- z=3
--
--
--
-- x+y=3
-- y*z=6
-- z=3
--
rule :: ((Maybe T, Maybe T, Maybe T), Assigns)
expression :: ((Maybe T, Maybe T), Assigns)
instance GHC.Show.Show UniqueLogic.ST.Example.Label.Assign
module UniqueLogic.ST.System.Simple
type Variable s a = Variable IdentityT s (Ignore a)
globalVariable :: ST s (Variable s a)
type T = T IdentityT
localVariable :: T s (Variable s a)
constant :: a -> T s (Variable s a)
assignment2 :: C w => (a -> b) -> Variable w s a -> Variable w s b -> T w s ()
assignment3 :: C w => (a -> b -> c) -> Variable w s a -> Variable w s b -> Variable w s c -> T w s ()
data Apply w s a
-- | This function allows to generalize assignment2 and
-- assignment3 to more arguments. You could achieve the same with
-- nested applications of assignment3 (,).
arg :: Variable w s a -> Apply w s a
runApply :: C w => Apply w s a -> Variable w s a -> T w s ()
solve :: T s a -> ST s a
query :: Variable s a -> ST s (Maybe a)
-- | Deprecated: This module is intended for documentation purposes. Do
-- not import it!
module UniqueLogic.ST.Example.Rule
-- |
-- x=1
-- y=2
-- z=3
-- w=3
--
--
--
-- x+y=3
-- y*z=6
-- z=3
-- y^w=8
--
example :: (Maybe Double, Maybe Double, Maybe Double, Maybe Double)
-- | Deprecated: This module is intended for documentation purposes. Do
-- not import it!
module UniqueLogic.ST.Example.Expression
example :: (Maybe Double, Maybe Double)