simple-expr-0.1.0.2: Minimalistic toolkit for simple mathematical expression.
Copyright(C) 2023 Alexey Tochin
LicenseBSD3 (see the file LICENSE)
MaintainerAlexey Tochin <Alexey.Tochin@gmail.com>
Safe HaskellNone
LanguageHaskell2010
Extensions
  • ScopedTypeVariables
  • ConstraintKinds
  • InstanceSigs
  • DeriveFunctor
  • TypeSynonymInstances
  • FlexibleContexts
  • FlexibleInstances
  • ConstrainedClassMethods
  • MultiParamTypeClasses
  • RankNTypes
  • ExplicitForAll

Debug.SimpleExpr.Expr

Description

Simple expressions base types and manipulations.

Synopsis

Expression manipulation

number :: Integer -> SimpleExpr Source #

Initializes a single integer number expression.

Examples of usage

Expand
>>> a = number 42
>>> a
42
>>> :t a
a :: SimpleExpr

variable :: String -> SimpleExpr Source #

Initializes a single symbolic variable expression.

Examples of usage

Expand
>>> x = variable "x"
>>> x
x
>>> :t x
x :: SimpleExpr

unaryFunc :: String -> SimpleExpr -> SimpleExpr Source #

Inituialize unarry function

Examples of usage

Expand
>>> x = variable "x"
>>> f = unaryFunc "f"
>>> f x
f(x)
>>> :t x
x :: SimpleExpr
>>> :t f
f :: SimpleExpr -> SimpleExpr

binaryFunc :: String -> SimpleExpr -> SimpleExpr -> SimpleExpr Source #

Inituialize unarry function

Examples of usage

Expand
>>> x = variable "x"
>>> y = variable "y"
>>> (-*-) = binaryFunc "-*-"
>>> x -*- y
x-*-y
>>> :t x
x :: SimpleExpr
>>> :t (-*-)
(-*-) :: SimpleExpr -> SimpleExpr -> SimpleExpr
>>> :t x-*-y
x-*-y :: SimpleExpr

simplify :: SimpleExpr -> SimpleExpr Source #

Simplify expression using some primitive rules like '0 * x -> 0' specified in simplifyStep implementation.

Examples of usage

Expand
>>> import Prelude (($))
>>> import Debug.SimpleExpr (variable, simplify)
>>> import NumHask ((+), (-), (*))
>>> x = variable "x"
>>> simplify $ (x + 0) * 1 - x * (3 - 2)
0

simplifyStep :: (SimpleExpr -> SimpleExpr) -> SimpleExpr -> SimpleExpr Source #

Minimalistic simplification step.

Examples of usage

Expand
>>> import Prelude (($), id)
>>> import NumHask ((+), (*), (**))
>>> simplifyStep id (0 + (0 + (0 + 10)))
0+(0+10)
>>> simplifyStep id (1 * (0 + (10 ** 1)))
0+(10^1)

Base types

data SimpleExprF a Source #

Expression F-algebra functional.

Instances

Instances details
Functor SimpleExprF Source # 
Instance details

Defined in Debug.SimpleExpr.Expr

Methods

fmap :: (a -> b) -> SimpleExprF a -> SimpleExprF b #

(<$) :: a -> SimpleExprF b -> SimpleExprF a #

Num SimpleExpr Source # 
Instance details

Defined in Debug.SimpleExpr.Expr

Show SimpleExpr Source # 
Instance details

Defined in Debug.SimpleExpr.Expr

Eq1 SimpleExprF Source # 
Instance details

Defined in Debug.SimpleExpr.Expr

Methods

liftEq :: (a -> b -> Bool) -> SimpleExprF a -> SimpleExprF b -> Bool #

Field SimpleExpr Source # 
Instance details

Defined in Debug.SimpleExpr.Expr

ExpField SimpleExpr Source # 
Instance details

Defined in Debug.SimpleExpr.Expr

TrigField SimpleExpr Source # 
Instance details

Defined in Debug.SimpleExpr.Expr

Distributive SimpleExpr Source # 
Instance details

Defined in Debug.SimpleExpr.Expr

Multiplicative SimpleExpr Source # 
Instance details

Defined in Debug.SimpleExpr.Expr

Divisive SimpleExpr Source # 
Instance details

Defined in Debug.SimpleExpr.Expr

Additive SimpleExpr Source # 
Instance details

Defined in Debug.SimpleExpr.Expr

Subtractive SimpleExpr Source # 
Instance details

Defined in Debug.SimpleExpr.Expr

Eq a => Eq (SimpleExprF a) Source # 
Instance details

Defined in Debug.SimpleExpr.Expr

FromIntegral (Fix SimpleExprF) Integer Source # 
Instance details

Defined in Debug.SimpleExpr.Expr

FromIntegral (SimpleExprF a) Integer Source # 
Instance details

Defined in Debug.SimpleExpr.Expr

type SimpleExpr = Fix SimpleExprF Source #

Simple expression type, see tutorial

type Expr = ListOf SimpleExpr Source #

Expression typeclass. It includes SimpleExpr as well as list and tuples of SimpleExpr etc.

Auxiliary functions

class ListOf inner outer Source #

Entity that is representable as a list of in general other entities. In particular, X is a list of single [X], see the example below.

Examples of usage

Expand
>>> data Atom = Atom String deriving Show
>>> type Particle = ListOf Atom
>>> content (Atom "He") :: [Atom]
[Atom "He"]
>>> content (Atom "H", Atom "H") :: [Atom]
[Atom "H",Atom "H"]
>>> content [Atom "H", Atom "O", Atom "H"] :: [Atom]
[Atom "H",Atom "O",Atom "H"]

Minimal complete definition

content

Instances

Instances details
ListOf inner inner Source # 
Instance details

Defined in Debug.SimpleExpr.Expr

Methods

content :: inner -> [inner] Source #

ListOf inner () Source # 
Instance details

Defined in Debug.SimpleExpr.Expr

Methods

content :: () -> [inner] Source #

ListOf inner outer => ListOf inner [outer] Source # 
Instance details

Defined in Debug.SimpleExpr.Expr

Methods

content :: [outer] -> [inner] Source #

(ListOf inner outer1, ListOf inner outer2) => ListOf inner (outer1, outer2) Source # 
Instance details

Defined in Debug.SimpleExpr.Expr

Methods

content :: (outer1, outer2) -> [inner] Source #

(ListOf inner outer1, ListOf inner outer2, ListOf inner outer3) => ListOf inner (outer1, outer2, outer3) Source # 
Instance details

Defined in Debug.SimpleExpr.Expr

Methods

content :: (outer1, outer2, outer3) -> [inner] Source #

(ListOf inner outer1, ListOf inner outer2, ListOf inner outer3, ListOf inner outer4) => ListOf inner (outer1, outer2, outer3, outer4) Source # 
Instance details

Defined in Debug.SimpleExpr.Expr

Methods

content :: (outer1, outer2, outer3, outer4) -> [inner] Source #

(ListOf inner outer1, ListOf inner outer2, ListOf inner outer3, ListOf inner outer4, ListOf inner outer5) => ListOf inner (outer1, outer2, outer3, outer4, outer5) Source # 
Instance details

Defined in Debug.SimpleExpr.Expr

Methods

content :: (outer1, outer2, outer3, outer4, outer5) -> [inner] Source #

content :: ListOf inner outer => outer -> [inner] Source #

Returns a list of entities the argument consists of.

dependencies :: SimpleExpr -> [SimpleExpr] Source #

Returns the list of head dependencies of an expression.

Examples of usage

Expand
>>> import Prelude (($), id)
>>> import NumHask ((+), (*))
>>> dependencies (variable "x" + (variable "y" * variable "z"))
[x,y·z]

showWithBrackets :: SimpleExpr -> String Source #

Shows expression adding brackets if it is needed for a context.