question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cannot use laws/functor to test composition property

See original GitHub issue

identityʹ:

const identityʹ = t => eq => x => {
    const a = t(x)[map](identity);
    const b = t(x);
    return eq(a, b);
};

composition:

const composition = t => eq => x => {
    const a = t(x)[map](compose(identity)(identity));
    const b = t(x)[map](identity)[map](identity);
    return eq(a, b);
};

Consider a in composition:

a = t(x)[map](compose(identity)(identity))

Assume a proof for compose(identity)(identity) = identity:

a = t(x)[map](identity)

The identity property tells us that t(x)[map](identity) = t(x), so:

a = t(x)

Consider b in composition:

b = t(x)[map](identity)[map](identity)

The identity property tells us that t(x)[map](identity) = t(x), so:

b = t(x)[map](identity)
b = t(x)

I think all we’re actually proving is compose(identity)(identity) = identity, which isn’t at all what we’re hoping to prove. 😜

For composition to be useful I think it needs to take f :: b -> c and g :: a -> b as arguments.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
rpominovcommented, Oct 21, 2016

TIL: Parametricity also makes any function t :: (Functor f, Functor g) => f a -> g a natural transformation. In other words just by looking at signature we get t(v.map(f)) == t(v).map(f) for free. https://youtu.be/2LJC-XD5Ffo?t=31m22s

1reaction
joneshfcommented, Sep 22, 2016

I don’t have much want for it to be removed. So, if it’s worse, then no change 😃.

But honestly, you can do similar hacks in haskell with Data.Typeable. That doesn’t mean we need to stop believing the proof to be true. If a data type doesn’t respect parametricity, I don’t know that we should care about that data type.

I mean, the data type’s map procedure could look at the current timestamp and throw an exception only when it is congruent to 0 mod 123456789. You’d almost never see it blow up in practice, so both laws would pass, effectively, all of the time. Did the second law help us catch the problem?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Methods on an object created via composition can't access all ...
I was expecting john 's method isOfAge to be able to access the property isAdult , since it's in the object. However, conceptually...
Read more >
Verifying inverse functions by composition - Khan Academy
Learn how to verify whether two functions are inverses by composing them. For example, are f(x)=5x-7 and g(x)=x/5+7 inverse functions?
Read more >
Plugging One Function Into Another: Composition Explained
With no number to evaluate at, how do you compose the functions? Study these worked examples to learn how!
Read more >
Composition of Functions - Definition, Properties and Examples
In maths, solving a composite function signifies getting the composition of two functions. A small circle (∘) is used to denote the composition...
Read more >
Composition of functions - Mathcentre
We can build up complicated functions from simple functions by using the process of composition, where the output of one function becomes the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found