Comments (11)
I think the law is there to serve as a more direct deterrent for unlawful implementations. Specifically, Maybe.of = x => x ? Just(x) : Nothing
kind of implementations were very common.
I also believe that the law is redundant with regards to preventing these bad implementations, and in fact, overly restrictive otherwise. Sanctuary breaks this law in multiple ways:
- Input type checking
- Constrained type instances
In my view, both of these cases should be allowed.
from fantasy-land.
Let's take simplest functions form the spec: of :: Applicative f => a -> f a
. it's followed with No parts of "a" should be checked
.
In Purescript similar function for Array will have signature like this:
of :: forall a. a -> Array a
if you have function like this you can't do anything to the input (you can't perform any "checking") except passing it to some other function or storing it in some other value. it's because it can be of any type and when you say your function works for all types it must work for all types otherwise compiler will reject you implementation.
In js there is no type system, and this kind of restriction is just specified verbally in fantasy land. for example the "No parts of <...> value should be checked"
from the spec rejects this implementation:
Array.prototype.of = function(val) {
if (val == []) {return []} else return [val]
// or
if (val == null) {return []} else return [val]
}
Which will be valid if that restriction is removed from the spec and we don't want such implementations as it would result in unlawful behavior.
We could use forall <...>
in signature instead of the "No parts of <...> value should be checked"
but it would not change the fact that "No parts of <...> value should be checked
.
Sanctuary breaks this law in multiple ways:
Input type checking
Regarding Sanctuary breaking this law, i don't think it quite counts:
- Input type checking is done during development, it's is basically doing the job of a compiler and if something is off your app crashes. If it's inspecting the value and depending on it's contents returns different result that it would be violation for me.
- "Constrained type instances" it's also not a violation to me as it's not changing the value itself it's just adding some "instances" (it's just poor mans instance declaration).
from fantasy-land.
I guess I'd simply like to see a section in the prefix similar to the "Terminology", "Type Signatures", etc. ones, something like:
forAll
Parametric polymorphism is essential to these algebras. When the rules for any type are specified as applied to all values (
forall a
or∀ a
) the rule must hold for any Javascript values unless otherwise specified. This implies, for instance, that code that behaves differently for thenull
orundefined
value than for other values is unlikely to comply with the rule.
... and then simply use <forall ...>
where needed.
from fantasy-land.
I think the law is there to serve as a more direct deterrent for unlawful implementations. Specifically, Maybe.of = x => x ? Just(x) : Nothing kind of implementations were very common.
It is indeed to prevent that, also "were" should be "are". If someone comes up with a better phrasing of this, then I don't see why it shouldn't be amended.
from fantasy-land.
The point here is that seems that these kind of laws are too much restrictive:
Sanctuary breaks this law in multiple ways:
- Input type checking
- Constrained type instances
and redundant (it seems that each algebraic type that is not "No parts of <...> value should be checked" compliant, it's not compliant with other of its laws).
from fantasy-land.
Which will be valid if that restriction is removed from the spec and we don't want such implementations as it would result in unlawful behavior.
But if there's some other law that the implementation would be breaking, isn't this one then redundant? I think that's the main point.
I know that FantasyLand is written as a specification with little room for exposition, but I would rather see this as a note, or perhaps as forall <...>
.
Again, is there any reason that this should be unlawful?:
class Just extends Maybe {
...
map(f) {
const r = f(this.val)
return r === null ? Maybe.of(r) : Maybe.of(r);
}
}```
from fantasy-land.
The inspection happening in the example you provided has no observable effect, therefore I consider it to be valid implementation.
Having forall <..>
to me is same as the "No part ...." and if we start using forall
the spec should still describe what a forall means and in that description we would still have something like:
forall <var>.
- means that no part ofvar
shouldn't be checked, i.e. implementation should not perform any inspection of a value so it works for all possible values in the same way.
from fantasy-land.
That sounds good to me! Unless others disagree on this, I think a way forward would be to open a PR with changes discussed here.
from fantasy-land.
See #311.
from fantasy-land.
Again, is there any reason that this should be unlawful?:
class Just extends Maybe { ... map(f) { const r = f(this.val) return r === null ? Maybe.of(r) : Maybe.of(r); } }
It is not unlawful because both of the branches of the ternary operator return the same value (Maybe.of(r)
). As @safareli said, the inspection has no observable effect.
The given implementation is equivalent to:
class Just extends Maybe {
...
map(f) {
return Maybe.of( f(this.val) );
}
}
There is no inspection happening, which is exactly what the specification determines.
from fantasy-land.
I would like to point that specs talk about inspection (checked), not about inspection with observable effect.
Do specs really need to talk about what means obserbable effect when there are other more clear laws which avoid unlawful specs?
For example, the fantasy-land/map
type with the identity and composition functor laws are enough for spec completeness. The rest of definition rules are redundant:
u['fantasy-land/map'](f)
f must be a function
Of course, fantasy-land/map
type says that first param of fantasy-land/map
method must be a function (a -> b).
If f is not a function, the behaviour of fantasy-land/map is unspecified.
Of course, fantasy-land/map
laws are expressed assuming fantasy-land/map
type. There is nothing defined out of the scope.
f can return any value.
Of course, b
in fantasy-land/map
type means value of any type.
No parts of f's return value should be checked.
This is the law I started talking about. In my opinion too much restrictive and composition functor law avoids wrong implementations (see my first post).
fantasy-land/map must return a value of the same Functor
Of course, if it were not like that, the type of fantasy-land/map
would have been:
fantasy-land/map :: Functor f, Functor t => f a ~> (a -> b) -> t b
from fantasy-land.
Related Issues (20)
- First-Class Protocols HOT 5
- Why is the class/constructor required as a parameter for `traverse` (or sequence)? HOT 7
- [Question] Type signature for ap HOT 2
- Adding Folktale to implementations.md
- Equivalent for Serial typeclass? HOT 1
- Add Traversable1 HOT 3
- Help understanding use of identity in Traversable laws HOT 2
- Fantasy Land specification version number HOT 1
- Switch to Symbols HOT 2
- Wrong argument in Traversable HOT 4
- Is monet.js compatible? HOT 1
- Why FL specifies the ChainRec typeclass when there is the trampoline monad? HOT 7
- What does "and" mean? HOT 2
- Can someone explain me how I should understand Semigroup? HOT 5
- Remove all OOP features HOT 21
- Traversable and Foldable use multiple variables as arguments HOT 12
- Profunctor should require Contravariant HOT 10
- Implementation of `Compose` is confusing HOT 3
- should Chain properties depends on fantasy-land/ap (and possibly .map)? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fantasy-land.