markandrus / aljebra Goto Github PK
View Code? Open in Web Editor NEWToy implementations of the algebraic structures defined in the Fantasy Land specification, mostly borrowed from Haskell libraries.
Toy implementations of the algebraic structures defined in the Fantasy Land specification, mostly borrowed from Haskell libraries.
The Haskell library Data.Semigroup defines a few semigroups that are not monoids, two of which are
The rest of the library re-exports instances from the Haskell library Data.Monoid.
I'm going to look at refactoring the instances to model Data.Semigroup's re-export of Data.Monoid, as well as writing test/Monoid/laws.js
in a way that "extends" the associativity law defined in test/Semigroup/laws.js
. Benefits are
Sum
rather than Add
, Product
rather than Mult
, etc.How is the Dual monoid supposed to work? As it is right now, I'm not sure how to test it. The way I understand it, this should work.
var instances = {
Dual: {
constructor: Monoid.instances.Dual(Monoid.instances.Array),
domain: [[1,2], [3,4]],
check: complex
}
}
However, both of the identites fail. The Left identity fails but the test suite keeps running, while the right identity has an error:
/home/joneshf/programming/javascript/aljebra/Monoid.js:59
return b.concat(a);
^
TypeError: Object function () {
return new Array();
} has no method 'concat'
at /home/joneshf/programming/javascript/aljebra/Monoid.js:59:14
at MonoidInstance.Monoid.MonoidInstance.concat (/home/joneshf/programming/javascript/aljebra/Monoid.js:18:18)
at laws.Right Identity (/home/joneshf/programming/javascript/aljebra/MonoidTests.js:32:16)
at /home/joneshf/programming/javascript/aljebra/MonoidTests.js:157:18
at Array.reduce (native)
at /home/joneshf/programming/javascript/aljebra/MonoidTests.js:156:17
at Array.map (native)
at TestLaw (/home/joneshf/programming/javascript/aljebra/MonoidTests.js:155:16)
at TestLaws (/home/joneshf/programming/javascript/aljebra/MonoidTests.js:176:5)
at TestInstances (/home/joneshf/programming/javascript/aljebra/MonoidTests.js:184:5)
Throwing in a couple of console.log
's into the complex check function, we can see what is causing the issue:
aljebra [master●] % node MonoidTests.js
All
Associativity
Pass
Left Identity
Pass
Right Identity
Pass
Any
Associativity
Pass
Left Identity
Pass
Right Identity
Pass
Array
Associativity
a: [[1,2],[1,2],[1,2]] b: [[1,2],[1,2],[1,2]]
a: [[3,4],[1,2],[1,2]] b: [[3,4],[1,2],[1,2]]
a: [[1,2],[3,4],[1,2]] b: [[1,2],[3,4],[1,2]]
a: [[3,4],[3,4],[1,2]] b: [[3,4],[3,4],[1,2]]
a: [[1,2],[1,2],[3,4]] b: [[1,2],[1,2],[3,4]]
a: [[3,4],[1,2],[3,4]] b: [[3,4],[1,2],[3,4]]
a: [[1,2],[3,4],[3,4]] b: [[1,2],[3,4],[3,4]]
a: [[3,4],[3,4],[3,4]] b: [[3,4],[3,4],[3,4]]
Pass
Left Identity
a: [[1,2]] b: [[1,2]]
a: [[3,4]] b: [[3,4]]
Pass
Right Identity
a: [[1,2]] b: [[1,2]]
a: [[3,4]] b: [[3,4]]
Pass
Dual
Associativity
a: {"value":[1,2,1,2,1,2,1,2,1,2]} b: {"value":[1,2,1,2,1,2,1,2,1,2]}
a: {"value":[1,2,1,2,1,2,1,2,3,4]} b: {"value":[1,2,1,2,1,2,1,2,3,4]}
a: {"value":[1,2,3,4,1,2,3,4,1,2]} b: {"value":[1,2,3,4,1,2,3,4,1,2]}
a: {"value":[1,2,3,4,1,2,3,4,3,4]} b: {"value":[1,2,3,4,1,2,3,4,3,4]}
a: {"value":[3,4,1,2,3,4,1,2,1,2]} b: {"value":[3,4,1,2,3,4,1,2,1,2]}
a: {"value":[3,4,1,2,3,4,1,2,3,4]} b: {"value":[3,4,1,2,3,4,1,2,3,4]}
a: {"value":[3,4,3,4,3,4,3,4,1,2]} b: {"value":[3,4,3,4,3,4,3,4,1,2]}
a: {"value":[3,4,3,4,3,4,3,4,3,4]} b: {"value":[3,4,3,4,3,4,3,4,3,4]}
Pass
Left Identity
a: {"value":[1,2,null]} b: {"value":[1,2]}
a: {"value":[3,4,null]} b: {"value":[3,4]}
Fail
Right Identity
The first issue is that it's adding more elements than it should. The next issue is that it's modifying the domain. The final thing to note is that the only reason Associativity, and Left Identity even ran was because Array
's have a concat
method. Any other monoid would fail with a TypeError of not having concat
.
I haven't actually tried to figure out how to fix anything, just some things I found after looking around.
As an aside. I think this is a pretty good example of the whole point of this. With a little analysis we can clearly see that the current implementation of Dual is not actually a monoid. However, it does almost look like a Flattener or something.
I think the equality check for Cont
will need to synchronize results and use Mocha's done
function. See error
branch.
Currently, ./lib/Applicative.js
mutates the Monad instances in ./lib/Monad.js
if it is ever required (in order to derive ap
). This is hard to reason about.
There are also instances like EIther whose constructor is defined in ./lib/Semigroup.js
. Other files—such as ./lib/Functor.js
—mutate the original instance before re-exporting it.
Goals:
./lib/Monad.js
), or in a separate ./lib/constructors.js
?Object.prototype.extend
in ./index.js
. I don't think it can copy constructors; however, going with the latter option above would allow extending in ./lib/constructors.js
.A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.