0

I am working through the documentation on Sanctuary.js. I've been working on learning Haskell also, and struggled a bit with FP concepts.

  1. I understand that a type value is an object that has (a) a constructor (the type representative), (b) a type identifier (the name of the type as a property named @@type, and (c) any methods required to be implemented by the type. What is the difference between a FP type value and normal object-oriented objects, besides the type value not having any state? The Fantasy-Land specification gives standard types to implement, including methods on them. Some (all?) of those methods obey algebraic laws, like Functors requiring a map method that obeys the identity and composition laws. Am I free to create my user types and require the methods I like on them? Do those methods have to conform to any algebraic design principles or laws? If not, it sure seems like OO design (minus state in the object) to me! I don't know what I'm missing. Are types like interfaces? Parent objects?

  2. The Sanctuary documentation talks about an "accessible pseudotype" - the type of values which support property access, or every value except null and undefined. It says that "Object is close, but Object.create(null) produces a value which supports property access but which is not a member of the Object type". But printing an object created with null shows {}, and its typeof being object. It seems like a member of the Object type to me. What am I misunderstanding? Why is the Accessible psuedo-type necessary? And what is the difference between a pseudo-type and a regular type? Integer, for example, is called a pseudo-class, but it seems like an extension of the Number class to me.

webstackdev
  • 850
  • 15
  • 23
  • You shouldn't start with advanced type system features like type classes (provided you're referring to type classes with _type values_). Start with function types, tagged unions and algebraic data types. Study parametric polymorphism, parametricity and how this sort of polymorphism differs from subtyping in OOP. Then go on with bounded polymorphism (often implemented as type classes like Functor), kinds of types and higher kinded types. –  Nov 24 '17 at 13:42
  • Type classes are similar to function overloading in OOP. But they are more flexible, since you can also overload functions based on their return type. –  Nov 24 '17 at 13:57
  • The inelegant Accessible type class was removed in [sanctuary-js/sanctuary#425](https://github.com/sanctuary-js/sanctuary/pull/425) and will disappear from the library in the next release. :) – davidchambers Nov 24 '17 at 15:50
  • @ftor - thanks for the roadmap! – webstackdev Nov 25 '17 at 15:38
  • @davidchambers - thank you for all the work you've done on the sanctuary.js library. I feel like by working with it I'll be able to grasp FP concepts, something I've worked on a bit here and there for the past year or so. For some reason it's making more sense to me with sanctuary than with Haskell. The library is really nice also, and your explanations of the why of it compared to Ramda has been helpful. Thanks again! – webstackdev Nov 25 '17 at 15:41

1 Answers1

5

What is the difference between a FP type value and normal object-oriented objects, besides the type value not having any state?

It's not only that they don't have any mutable state, they don't carry any values like OOP instances do. They're more like the class of OOP object. They are JS objects with only static methods, or put differently: a record that contains plain functions.

Am I free to create my user types and require the methods I like on them?

Yes!

Do those methods have to conform to any algebraic design principles or laws?

No. Yes. You can write unlawful methods, and nothing will stop you. Even in Haskell, these laws are not enforced by the compiler/typechecker. (Usually).

They might even work. But they break the assumptions of other developers, and they break the assumptions that code written by those developers relies on to work.

Are types like interfaces?

Yes, that might be a good metaphor. Or at least, the type classes instantiated by the type objects (a reification necessary in JavaScript) are the interfaces that are implemented by the type.

Printing an object created with null shows {}, and its typeof being object. It seems like a member of the Object type to me. What am I misunderstanding?

"Member" probably refers to instanceof here - and Object.create(null) instanceof Object is false, because it does not inherit from Object.prototype. Other accessible but non-Object objects might be values from other realms, like an iframe environment.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • _No. Yes. You can write unlawful methods, and nothing will stop you._ Not every type class must have algebraic laws, e.g. `Show`. So there are also useful "lawless" type classes. –  Nov 24 '17 at 14:38
  • @ftor Ah, right, good point. However I think unlawful != lawless. – Bergi Nov 24 '17 at 15:17
  • @Bergi you mentioned FP type values don't carry any values; can they carry constants? – webstackdev Nov 25 '17 at 15:42
  • @calligraphic-io Yes, the static methods that don't take any arguments (nullary functions) are essentially constants. An example would be the `empty` value of all monoids, like [Maybe](https://github.com/sanctuary-js/sanctuary#maybefantasy-landempty-----maybea) – Bergi Nov 25 '17 at 16:24