Îõ³h$@Ì>Çæ      !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdeNone !/5>?ÀÁÂÄÉÎÔ×Ùì>e numhask-freeinformal test suiteempty expression freeExp "0""0"&plain (with multiplicative precedence)2forget $ parseExp "1+2*3" :: FreeRing RingLaws Int˜FreeR (fromList [Bag {unbag = fromList [(FreeV 1,1),(FreeR (fromList [Bag {unbag = fromList [(FreeV 2,1)]},Bag {unbag = fromList [(FreeV 3,1)]}]),1)]}])freeExp "1+2*3" "(1+(2*3))"Additive unitalfreeExp "0+(2+0)*3+0""(2*3)",General additive associative and commutationfreeExp "(1+2)*3+(4+5)+6*7""(4+5+((1+2)*3)+(6*7))"Multiplicative unitalfreeExp "1*3+4*1+1*(5*6)" "(3+4+(5*6))",Multiplicative association (not commutative)freeExp "(2*6)*((4*5)*2)" "(2*6*4*5*2)" absorptivefreeExp "0*1+3*(3+4)*0""0"additive invertiblefreeExp "(1+2)+(-1+-2)""0" distribution ?a ÅE (b + c) = (a · b) + (a · c) (b + c) · a = (b · a) + (c · a)leftfreeExp "2*(3+4)+2*5+2*6""(2*(3+4+5+6))"rightfreeExp "(3+4)*2+5*2+6*2""((3+4+5+6)*2)"mixed (left then right checks)freeExp "2*(3+4)*2+5*2+2*6*2""((5+(2*(3+4))+(2*6))*2)" Note that (2*(3+4+6)*2+5*2), is a valid alternative to what the current  % function comes up with.TODO: optional extras:If +one is faster than +a (a . b) + a ==> a . (b + one)If a is scalar ...6lifting an (additive bag) to a multiplication sequence 3+3+3+3 ==> 3*4introducing exponents 3*3*3*3 ==> 3^4 numhask-free.The free ring is a recursive sequence of bags.áGiven multiplication is monoidal (with the free object a list) and addition is a commutative group (with the free object a bag), it seems intuitively the case that the free object for a ring is a recursive list of bags. It is recursive because the ordering of +'s and *'s does not reduce, so that the tree-like nature of the expression is not forgotten.ÅAbstractly, the choice of what goes in what should be an arbitrary one; the free object could also be a (recursive) bag of lists. The addition collection structure feels like it should be within the multiplication structure, however, because of the distribution law equivalence that need to be honoured in the representation: ?a ÅE (b + c) = (a · b) + (a · c) (b + c) · a = (b · a) + (c · a)›It is likely, in most endeavours, that multiplication is more expensive than addition, and the left hand side of these equations have less multiplications.×Because the distribution laws are substitutions to both the left and the right, use of fã is indicated instead of a list (which is isomorphic to a list and thus allowed as an alternative).ÁThe free ring is the same general shape as the free monad in the  Ëhttps://hackage.haskell.org/package/free-5.1.4/docs/Control-Monad-Free.htmlfree library ,data Free f a = Pure a | Free (f (Free f a))0which in turn is almost the same shape as Fix eg newtype Fix f = Fix (f (Fix f))ÏIf Bag could form a Functor instance, then the Free Ring could be expressed as  (   f) awhich is a very clean result. numhask-freešWhere an algebra involves two (or more) operators, the initial structure (the expression) is arrived at by grafting new types of branches using sum types. numhask-free Ring Laws ˆa + b is closed zero + a = a a + zero = a (a + b) + c = a + (b + c) a + b == b + a a + negate a = zero a * b is closed one * a = a a * one = a (a * b) * c = a * (b * c) a * zero = zero zero * a = zero a * (b + c) = (a * b) + (a * c) (b + c) * a = (b * a) + (c * a)  numhask-freeAdditive Commutative Group Laws æa + b is closed zero + a = a a + zero = a (a + b) + c = a + (b + c) a + b == b + a a + negate a = zero€Adding invertibility to the list of laws for a commutative monoid gets us to the definition of a Commutative (or Abelian) Group.ðInvertible (in combination with commutation) means forgetting a value when the inversion of the value is contained somewhere within the expression. For example, armed with a definition of what a negative number is, integer addition such as:  1+2+3+-1+-4+2>Can be represented as a bag of 2 2's, one 3 and minus one 4's.=let exbag = fromList [1,2,3,-1,-4,-2] :: Bag AddCommGroup Intexbag%Bag {unbag = fromList [(3,1),(4,-1)]} toList exbag[3,-4]&exAdd = toTreeL [0,1,2,3,0,-1,-4,-2,0]8putStrLn $ printf (forget exAdd :: Bag AddCommGroup Int)(3+-4)  numhask-free%The Free commutative monoid is a Bag.ÂIn addition to the forgetting needed for the free monoid, forgetting additions of zero and forgetting brackets, a commutative law means forgetting the order of the original expression structure.ðA list that has lost it's order is sometimes referred to as a bag. An efficient representation of a bag is a (key,value) pair where the keys are elements in the initial expression and values are the number of times the element has occurred.¤In the usual surface-paradox typical of adjointness, the forgetting of the ordering of the initial structure induces a requirement that the carrier type be ordered.  numhask-freeMultiplicative monoid laws Áa * b is closed one * a = a a * one = a (a * b) * c = a * (b * c) one :: FreeMonoid MultMonoid IntFreeMonoid {leaves = []}ex1: (1 * 2) * (4 * 5) * 1Ølet ex1 = Branch (Branch (Branch (Leaf 1) (Leaf 2)) (Branch (Leaf 4) (Leaf 5))) (Leaf 1);putStrLn $ printf (forget ex1 :: FreeMonoid MultMonoid Int)(2*4*5)1algebra (forget ex1 :: FreeMonoid MultMonoid Int)40 numhask-freeThe free monoid is a list.•Applying unital and associativity laws in the context of converting an expression tree into a free monoid, the simplest structure possible, involves:îforgetting whenever an element in the initial structure in the unit (one, say, in the case of multiplication).forgetting the brackets.#So, starting with the initial tree: /data Tree a = Leaf a | Branch (Tree a) (Tree a)6We graft on a sum tag to represent an empty structure: ;data Tree a = EmptyTree | Leaf a | Branch (Tree a) (Tree a)To %® the left/right structure of the tree we force the left side of the branch to be a value rather than another tree branch, so that the whole tree always branches to the right: 4data Tree a = EmptyTree | Leaf a | Branch a (Tree a)ÌLeaf a can be represented as Branch a EmptyTree, so we can simplify this to: +data Tree a = EmptyTree | Branch a (Tree a)ÁAnd this is the classical Haskell cons list with different names: data [] a = [] | a : [a] numhask-free 6e •E a == e left absorbing a •E e == e right absorbing"The absorbed element is forgotten.ab1: 0 * (2 * 5) == 0ab2: (2 * 5) * 0 == 0Òlet ab1 = Branch (Leaf (Example 0)) (Branch (Leaf (Example 2)) (Leaf (Example 5)))Òlet ab2 = Branch (Branch (Leaf (Example 2)) (Leaf (Example 5))) (Leaf (Example 0))(forget ab1 :: Tree AbsorbingOnly ExampleLeaf 0(forget ab2 :: Tree AbsorbingOnly ExampleLeaf 0 numhask-free  a •E a = aÌImmediately repeated elements are forgotten in the equivalence class object.idem1: (5 •E 5) •E 1 == 5 •E 1#idem2: (1 •E 5) •E (1 •E 5) == (1 •E 5)but!idem3: (1 •E 5) •E 5 == (1 •E 5) •E 5(because we don't yet have associativity.Ôlet idem1 = Branch (Branch (Leaf (Example 5)) (Leaf (Example 5))) (Leaf (Example 1))ñlet idem2 = Branch (Branch (Leaf (Example 1)) (Leaf (Example 5))) (Branch (Leaf (Example 1)) (Leaf (Example 5)))Ôlet idem3 = Branch (Branch (Leaf (Example 1)) (Leaf (Example 5))) (Leaf (Example 5))?putStrLn $ printf (forget idem1 :: Tree IdempotentOnly Example)(5 o 1)?putStrLn $ printf (forget idem2 :: Tree IdempotentOnly Example)(1 o 5)?putStrLn $ printf (forget idem3 :: Tree IdempotentOnly Example) ((1 o 5) o 5)5algebra (forget idem3 :: Tree IdempotentOnly Example)5 numhask-free Ôinv a •E (a •E b) == b -- left cancellation (a •E b) •E inv b == a -- right cancellationbut inv a •E a == unit.is not a thing yet without a unit to equal to.¨The cancellation (or reversal or negation) of a value and the value are both lost in forming the equivalence relationship. Editing and diffing are two obvious examples.ÑThe data structure for the equivalence class is unchanged, so Tree can be reused.inv1: -1 •E (1 •E 5) == 5inv2: (1 •E 5) •E -5 == 1"inv3: (1 •E 5) •E -1 == (1 •E 5) •E -1Ölet inv1 = Branch (Leaf (Example (-1))) (Branch (Leaf (Example 1)) (Leaf (Example 5)))Ölet inv2 = Branch (Branch (Leaf (Example 1)) (Leaf (Example 5))) (Leaf (Example (-5)))Ølet inv3 = Branch (Branch (Leaf (Example 1)) (Leaf (Example 5))) (Leaf ((Example (-1))))*forget inv1 :: Tree InvertibleOnly ExampleLeaf 5ÀputStrLn $ printf $ (forget inv3 :: Tree InvertibleOnly Example) ((1•E5)•E-1) numhask-free a •E b == b •E abut non-associative, so (a •E b) •E c == (b •E a) •E cbut (a •E b) •E c /= a •E (b •E c)áCommutation requires a •E b and b •E a to be represented the same, and this induces a preordering: someè form of (arbitrary) ordering is needed to consistently and naturally represent a •E b and b •E a as "ab".íIn structural terms, a commutative tree is a mobile; a tree that has lost it's left and rightedness. To implement this forgetting, the left element of BranchC is arbitrarily chosen as always being less than or equal to the right element.c1: 3 •E (2 •E 1)c2: 3 •E (1 •E 2)c3: (1 •E 2) •E 3úlet c1 = forget $ Branch (Leaf (Example 3)) (Branch (Leaf (Example 2)) (Leaf (Example 1))) :: Tree CommutativeOnly Exampleúlet c2 = forget $ Branch (Leaf (Example 3)) (Branch (Leaf (Example 1)) (Leaf (Example 2))) :: Tree CommutativeOnly Exampleúlet c3 = forget $ Branch (Branch (Leaf (Example 1)) (Leaf (Example 2))) (Leaf (Example 3)) :: Tree CommutativeOnly Examplec1 == c2Truec1 == c3True numhask-free–Introduction of an associative law induces an equivalence class where, for example, (1 •E 2) •E 3 and 1 •E (2 •E 3) should be represented in the same way.%‚, the free object constructor, thus needs to forget about the tree shape (the brackets or parentheses of the original expression).ÅAs an algebra consumes an expression one element at a time, branches (or "links") still exist from one element to the next. The free object is still a tree structure, but it is the same tree shape.ÂForcing one side of the branch to be a value provides a tree structure that branches to the other side. The left branch as the value has been chosen in this representation but this is arbitrary.,let exl = toTreeL $ Example <$> [1,4,7,12,0]8putStrLn $ printf (forget exl :: Tree MagmaOnly Example)((((1•E4)•E7)•E12)•E0),let exr = toTreeR $ Example <$> [1,4,7,12,0]8putStrLn $ printf (forget exr :: Tree MagmaOnly Example)(1•E(4•E(7•E(12•E0))))?putStrLn $ printf (forget exl :: TreeA AssociativeOnly Example) 1•E4•E7•E12•E0“(\x -> (forget $ toTreeL x :: TreeA AssociativeOnly Example) == (forget $ toTreeR $ x :: TreeA AssociativeOnly Example)) (Example <$> [1,4,7,12,0])True numhask-free (a •E b) •E c = a •E (b •E c) numhask-freeãThe introduction of unital laws to the algebra changes what the free structure is, compared to the £ case. From this library's point of view, that an algebra is an instruction kit for constructing an object, the unital laws are an instruction to substitute "a" for whenever "unit •E a" occurs. Where an element is combined with the unit element, this operation should be erased and forgotten.ÏFor example, from the point of view of the free algebra, ((0 •E 4) •E 0) •E 12 and 4 •E 12 (say) are the same. The initial structure can be divided into equivalence classes where trees are isomorphic (the same).ŽIn contrast to the MagmaOnly case, the forgetting of unit operations means that an empty tree can result from an initially non-empty initial structure. The easiest way to represent this potential free object is simply to graft an EmptyTree tag to a Tree with a sum type.öAn EmptyTree represents a collapse of an initial structure down to nothing, as a result of applying the unital laws eg?let init = toTreeL $ Example <$> [0,0,0] :: Tree NoLaws Example'forget init :: TreeU UnitalOnly Example EmptyTreešBy forgetting instances of the unital laws in the original expression, the unital laws cannot be violated in the free object because they no longer exist.Èlet init = toTreeL $ Example <$> [0,1,4,0,7,12,0] :: Tree NoLaws Example=putStrLn $ printf $ (forget init :: TreeU UnitalOnly Example)(((1•E4)•E7)•E12) numhask-free unit •E a = a a •E unit = a numhask-freeFree Algebra for a Magma a •E b is closed'Given an initial binary Tree structure: /data Tree a = Leaf a | Branch (Tree a) (Tree a)Ù, a closed binary operation (a magma) and no other laws, the free algebra is also a Tree.Älet init = toTreeL $ Example <$> [1,4,7,12,0] :: Tree NoLaws Example0let free = forget init :: Tree MagmaOnly ExampleputStrLn $ printf $ free((((1•E4)•E7)•E12)•E0) algebra free24 numhask-free example type  numhask-freeËA binary tree is a common initial structure when considering free algebras.›The initial object for a Magma algebra is typically a tree-like structure representing a computation or expression; a series of binary operations, such as: (1 •E 4) •E ((7 •E 12) •E 0)¤let m1 = Branch (Branch (Leaf (Example 1)) (Leaf (Example 4))) (Branch (Branch (Leaf (Example 7)) (Leaf (Example 12))) (Leaf (Example 0))) :: Tree MagmaOnly ExampleputStrLn $ printf m1((1•E4)•E((7•E12)•E0))# numhask-freeòStarting from a particular initial structure, different sets of laws may lead to the same actual structure (or free object). Informal phantom type are included in most structures to help distinguish these cases and supply differing instances.$ numhask-freeçA free algebra is a construction kit of operations and axioms that combine to produce values of a type.% numhask-freeœConvert from a structure (the initial type) to another structure, the free object, forgetting the algebraic laws encapsulated in the free object definition.& numhask-free3Create a free object from a carrier type singleton.' numhask-freeThe algebra of the free object. lift . algebra == id( numhask-freePretty print the free object.) numhask-freeÑConvenience function to construct a Tree from a list with left bracket groupings.toTreeL [1,4,7,12,0]ÏBranch (Branch (Branch (Branch (Leaf 1) (Leaf 4)) (Leaf 7)) (Leaf 12)) (Leaf 0)* numhask-free Ord but, sadly, not a functor from Hask -> Hask, numhask-free?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijkì)numhask-free-0.0.3-G5egGP3uaP34evoRsNzHSHNumHask.FreeAlgebraControl.Monad.FreeFreeData.Functor.ComposeCompose InformalTestsFreeRingFreeVFreeRExpValueAddMultRingLaws AddCommGroupBagunbag MultMonoid FreeMonoidleaves AbsorbingOnlyIdempotentOnlyInvertibleOnlyCommutativeOnlyTreeALeafABranchAAssociativeOnlyTreeU EmptyTree NonEmptyTree UnitalOnly MagmaOnlyExampleTreeLeafBranchNoLaws FreeAlgebraforgetliftalgebraprintftoTreeLtoTreeRmapBagfreeExpparseExpcalate $fShowExample$fInvertibleExample$fAbsorbingExample$fUnitalExample$fMagmaExample$fFreeAlgebraTreeTreea$fFreeAlgebraTreeTreeUa$fFreeAlgebraTreeTreeAa$fFreeAlgebraTreeTreea0$fFreeAlgebraTreeTreea1$fFreeAlgebraTreeTreea2$fFreeAlgebraTreeTreea3$fFreeAlgebraTreeFreeMonoida$fMultiplicativeFreeMonoid$fFreeAlgebraTreeBaga$fSubtractiveBag $fAdditiveBag $fIsListBag$fFreeAlgebraExpExpa$fFreeAlgebraExpFreeRinga$fSubtractiveFreeRing$fAdditiveFreeRing$fMultiplicativeFreeRing$fExceptionBadExpParse$fShowBadExpParse $fEqFreeRing $fOrdFreeRing$fShowFreeRing$fEqExp$fOrdExp $fShowExp $fFunctorExp$fEqBag$fOrdBag $fShowBag$fEqFreeMonoid$fOrdFreeMonoid$fFoldableFreeMonoid$fShowFreeMonoid $fEqTreeA $fShowTreeA$fFunctorTreeA $fEqTreeU $fOrdTreeU $fShowTreeU$fFunctorTreeU $fEqExample $fOrdExample$fAssociativeExample$fCommutativeExample$fIdempotentExample$fEqTree $fOrdTree $fShowTree $fFunctorTreecontainers-0.6.2.1Data.Sequence.InternalSeq