Git Product home page Git Product logo

aljebra's People

Contributors

joneshf avatar markandrus avatar mortonfox avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

joneshf tzkmx

aljebra's Issues

Semigroup & Monoid Hierarchy

The Haskell library Data.Semigroup defines a few semigroups that are not monoids, two of which are

  • Min
  • Max

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

  • Less duplicate code
  • Fewer unique names, e.g. Semigroup can refer to Sum rather than Add, Product rather than Mult, etc.

@joneshf

Dual Monoid

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.

Extending Instances

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:

  • Determine a canonical location for such constructors. Perhaps in the most "powerful" algebraic structure (i.e. move Either to ./lib/Monad.js), or in a separate ./lib/constructors.js?
  • Determine best way to extend these instances without mutating them. There is a pure 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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.