What is the point of Solo? (beginner question)

question

Playing around with GHCi I stumbled upon the Solo type (by looking at which types are an instance of Traversable).

Googling tells me that Solo is a singleton tuple, but it is not clear to me what is the usefulness of having a singleton tuple over just an unwrapped value, and if the singleton tuple is important why isn't there some behind-the-scene-syntactic-sugar-magic to use the usual brackets notation for tuples also for singleton tuples?

Thanks in advance for your help!

Posted Mar 12, 2022, 5:50 PM UTC

1618
tomejaguar•

Basically it's for consistency. If there is a zero tuple () and n-ary tuples for n >=2 (a1, ..., an) then for consistency there really ought to be something that provides a 1-ary tuple. Consistency can be useful for code generation applications, for example. But the occasions where it is actually useful for a user to use it are very few and far between. I've never used it, for example, and I don't expect ever to.

20
CoAnalyticSetOP•

Makes sense, thanks

2
bss03•

Solo a has one more bottom value than a; it's hardly ever relevant.

An identity functor is useful for higher-kinded data, and data types ala carte, and general algebraic manipulation of functors. But, it needs a name (can't be a type alias) to get various instances. Identity is usually used for this, but in some special cases, it could be important that whether that "extra bottom" exists or not.

Also, in GHC there's different treatment of boxed and unboxed types. I think maybe Identity can only be applied to boxed types, were Solo can be applied to any type. (EDIT: Nope!) (The result of applying Identity or Solo is always boxed.)

why isn't there some behind-the-scene-syntactic-sugar-magic to use the usual brackets notation for tuples also for singleton tuples

That's a hell of a lot more work than a new datatype. New syntax is almost always incompatible with a close reading of the report, whereas adding new data types is completely in line with the report. Even if you completely discard the report, new syntax is still a lot bigger change to GHC that just defining a new "pure Haskell" data type.

9
phadej•

Also, in GHC there's different treatment of boxed and unboxed types. I think maybe Identity can only be applied to boxed types, were Solo can be applied to any type.

If you meant to say that Solo is levity-polymorphic, i.e. we could write Solo Int#, than that' iss wrong: Solo is not levity-polymorphic.

2
bss03•

Aw, nuts. :(

I was guessing.

1
CoAnalyticSetOP•

Boxed and unboxed types? It feels like there is an endless amount of haskell to learn about! Thanks for your answer

1
PM_ME_UR_OBSIDIAN•

Boxed/unboxed is not a Haskell specific concept. A boxed value is one that you hold a pointer to, corresponding to a reference type; an unboxed value is one you hold directly, corresponding to a value type.

6
CoAnalyticSetOP•

I see, thanks! I wasn't even aware that GHC supports boxed types then!

2
bss03•

Int is a pointer. String is a pointer, to a singly-linked list of Chars which are also pointers.

2
bss03•

Haskell-by-the-Report does not make such a distinction.

GHC makes the distinction in order to generate better code. It also exposes the distinction so that authors that are willing to depend on GHC-specifics can provide that information to the optimizer instead of having it synthesized from the Haskell code.

EDIT: "Boxed" means the runtime representation of values of that type are pointers. "Lifted" means the type can "contain bottom" / be lazy. Proper Haskell types are all lazy / lifted, and every implemention of lifted types (AFAIK) uses pointers for sharing, so are boxed. But, those pointers have runtime costs, both direct and indirect.

5
phadej•

Solo's documentation says

The most important feature of Solo is that it is possible to force its "outside" (usually by pattern matching) without forcing its "inside", because it is defined as a datatype rather than a newtype. One situation where this can be useful is when writing a function to extract a value from a data structure. Suppose you write an implementation of arrays and offer only this function to index into them:

... https://hackage.haskell.org/package/base-4.16.0.0/docs/Data-Tuple.html#t:Solo

EDIT: if you don't understand that use-case, that is fine, it's quite obscure.

4
TechnoEmpress•

Before Solo existed, we had a multitude of similarly-named 1-tuples like https://hackage.haskell.org/package/postgresql-simple-0.6.4/docs/Database-PostgreSQL-Simple.html#t:Only for a variety of purposes. The documentation of Only is actually quite clear on its purpose.

1
phadej•

Only is a newtype, not data. (I.e. Identity vs Solo).

2
r3dnaz•

Googling

i think you misspelled "hoogling" there. https://hoogle.haskell.org/?hoogle=Solo

0