Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detailed information about type instances

I am a beginner with Elm and as a former Haskell programmer I wonder, if there is a way like in Haskell to type in the interpreter/REPL:

:i number

So I know that this does not work in Elm, but is there something equivalent to Haskell's:

:i Num

I want to get detailed information about a type class and all its instances.

Furthermore in the Haskell documentation there is information available about the type hierarchy of the basic Haskell classes:

https://www.haskell.org/onlinereport/basic.html

Is something like that available for Elm too?

Thanks in advance.

like image 599
Christoph W. Avatar asked Feb 08 '23 20:02

Christoph W.


1 Answers

Elm doesn't have type classes, so it's not really applicable for it to show information about them, as GHCi's :i does. The scope of Elm's built in types, like strings, numbers, and bools, is fairly obvious.

Elm's type hierarchy is pretty simple. There are three built-in type-class-like type variables. Unlike Haskell which puts constraints before a =>, just use these type variables directly. Fair warning: Haskell users typically find Elm's type system limiting. (The tool ecosystem is also less advanced, by virtue of Haskell's 20+ year head start.)

  • A number is either an Int or Float. A number literal without a decimal point is a number. Arithmetic other than division can be done to either number type.
  • A comparable can be a number, character, string, or recursively a list or tuple of comparables. Comparables support <, >=, and similar.
  • An appendable can be a string, text (i.e. with typesetting information), or a list (containing any type). Appendables can be appended with (++).
  • There is no Eq type. Attempting to equate functions or signals causes a runtime error.

The practical implications of this are:

  • Strings (and sometimes lists) are both appendable and comparable. A bug in the 0.15.1 compiler prevents them from being unified. (This will be fixed in 0.16.)
  • If you call Signal.dropRepeats on a signal of functions, or containers of functions, it can crash.
  • Because Elm's dictionaries are implemented as binary search trees, their keys must be comparable. Same goes for set elements.
  • If one of these types appears multiple times in a type annotation, all occurrences must unify to the same type. If you want to allow two different appendables, call one of them appendable1 or similar.
  • There does not seem to be a way to specify that a type must meet more than one of these interfaces (except that all numbers are comparable). I've never had this come up, but if it does, just use the concrete type.

If you want Haskell in the browser, try PureScript.

like image 160
mgold Avatar answered Feb 16 '23 21:02

mgold