99 points · by voihannena · 8y ago · 133 comments
Not sure why you felt the need to include this. How did you come to this conclusion? Is this in comparison to Haskell, or what?
From that viewpoint, I think there is a fair bit of beauty in being able to pick and choose your approach based on the problem at hand without worrying if that is an idiomatic choice dictated by language designers. P6 goes to great lengths to give you all the expressiveness you could hope for, regardless if you are doing functional, OOP, or dealing with concurrency etc...
Perl is a great experiment in language design, or, rather, a battery of experiments. It's the opposite of a cleanly designed language.
One of the places this shows up to me is being able to say "it" (in essence). That is, we humans, when talking to each other, we say things like "Read in a line of input. If it looks like an XML tag, pass it to the XML handler function". But you can't talk to a computer that way. The computer says, in effect, "Read in a line of input from where? And put it where? And what do you mean 'if it ends in a newline'? If what ends in a newline?"
But in Perl, you can write it just like the humans say it, and Perl says, "Well, you didn't say where to read the input from, so I assume I should read it from the standard place (which is perfectly well defined in Perl). You didn't say where to put the line of input, so I'll put it in the default variable. You didn't say what to check to see if it looks like an XML tag, so I will check the default variable." The default variable, essentially, plays the role of "it" in human language.
And the whole language is like that. There are shortcuts that work most of the time, and precise ways of saying things that work when the shortcut isn't what you want. And what the shortcut does is well defined (in the docs; it can be very opaque in code).
None of this makes Perl an ideal language. But it has a perspective on language that is (as far as I can tell) unique among programming languages. It is only fair to judge the coherence of the design within the framework of what Perl is trying to be.
Here's 'it' in lisp: https://en.wikipedia.org/wiki/Anaphoric_macro
Another internal use is an AWS infrastructure management tool that we built. A cli util that handles the initial bootstrapping for new AWS projects and a few helper features to simplify some of our common management tasks.
Perl 6 has been pretty easy to work with. Even in situations where a module isn't available or the requirements are too complex to build it with Perl 6 within our project time constraints, its facilities as a glue language really make it simple to work with external tools, modules, libs.
If you like the Perl 6 syntax then I'd recommend just spending some time with it. It's stable and fast enough for regular use now.
She didn't question whether Haskellers needed something like P6, she just implemented it and pushed Haskell to greater heights. I think that is a worthwhile point.
But I don't understand why you've introduced your point about what you perceive as the level of need for this document.
Given a non-zero number of lang X to lang Y documents, one or more of them will be the most likely needed, one or more the least likely. And given such a scenario, it'll likely seem one way to some folk and another to others. So?
----
[1] The first P6 compiler prototype was written by Audrey Tang. https://en.wikipedia.org/wiki/Audrey_Tang The P6 project follows several principles she established such as being optimized for fun, and a "forgiveness > permission" policy for most contributions. So someone wrote a nice doc and it's been made available.
So which is it? Complex enough for only elites to grasp? Or just some toy language with no real capabilities, let alone unique features of interest to elite programmers?
Nevertheless it shows that Perl 6 can do real functional programming. Even if the syntax looks exceptional sometimes, even for people familiar with Perl 5.
It's for someone who has already decided they like P6, has learned the basics, and wants to tighten up their understanding of P6 as it relates to Haskell things they know. It deliberately picks things that are basic in Haskell even if they're not so basic in P6.
That's why it says:
> Note that this should not be mistaken for a beginner tutorial or overview of Perl 6; it is intended as a technical reference for Perl 6 learners with a strong Haskell background.
This conclusion is clearly unfair because the author tried to port simple Haskell solutions to Perl.
Sure. But that's an inevitable consequence of what the document aims to achieve.
The problems chosen were the ones for which Haskell had simpler solutions.
The point of the document was to help someone who is already motivated to learn P6 and knows Haskell to get to grips with P6.
If the point were to motivate someone who knows Haskell to want to learn P6 then the examples chosen would be ones for which the P6 solution is better than the Haskell one.
I don't use either day to day but have had some exposure to both. I think both languages will have influence on future of language design at a minimum. Doubtful Perl does a phoenix routine but weirder things have happened.
> There is also say, which behaves similarly, but will call the gist method of its argument.
And after having read the linked-to docs for say[2] and gist[3], I not only cannot understand what is being said, I have no idea how anyone else could, either.
1) https://docs.perl6.org/language/py-nutshell 2) https://docs.perl6.org/routine/say 3) https://docs.perl6.org/routine/gist
> The default gist method in Mu re-dispatches to the perl method for defined invocants, and returns the type name in parenthesis for type object invocants. Many built-in classes override the case of instances to something more specific that may truncate output.
And I have no idea how anyone that isn't already fluent in Perl 6 is expected to read that.
EDIT: My point is mostly that it just seems... weird. Like, I get the animating idea of Haskell, even if I don't understand all of the concepts. Perl 6 just seems... alien. It's incredibly funky.
If you want to start from the bottom out, there's the ever improving language docs: https://docs.perl6.org/language.html
Quite a few languages have that kind of feature though I believe. PHP has print_r and var_dump, Perl 5 had Data::Dumper, Python has tools too which I forget the name of.
I like how Perl 6 has baked it into the language though rather than making it a part of the standard library.
But IMO, like others have said, it is not really that difficult to figure out what it means (if you know some OOP) (although some of the rarely-used words (e.g. "invocants") get in the way a bit.
Basically when I read your comment, I figured out that the default gist method (even the word "gist" is a clue) was something like a default-valued method (to print the representation of any object) that can be overridden on a case-by-case basis by any other object - something like Python's __str__() and __repr__ special methods, like others have said.
However, having read some of it, I'll have to agree with you that the Perl6 docs can do with some improvement (check out Phasers in the Perl 6 manual for an example of what I mean) - whereas, in contrast, the Perl 5 docs are considered by some (me too) to be good-to-excellent. But then Perl6 has been evolving for less time (yes, I know about the long time for the 1st stable release).
This specific example seems similar to the __repr__ vs __str__ thing in Python, and not very alien at all.
Other resources exist which are targeted at newcomers and explain things differently.
put/say are output functions which automatically add a newline at the end.
If you want fancier formatting, printf is available as well.
And notice the relative lengths of these names. As much as possible, p6 is organized so that the more commonly used thing will have the shorter name.
PS I don't think I have ever used put in p6. My code uses say, or print if I don't want a newline added to the output.
class Chemical {
has $.formula;
method gist {
my $output = $!formula;
$output ~~ s:g/(<[0..9]>)/{(0x2080+$0).chr}/;
$output;
}
}
my $chem = Chemical.new(formula => 'Al6O13Si2');
say $chem; # Al₆O₁₃Si₂
#`[
Without overriding the `gist` method:
say $chem; # Chemical.new(formula => 'Al6O13Si2')
]Perl 6 is alarmingly concise in a lot of cases (hyper-operators come to mind), so it's super weird to see it called verbose. I'd wager that the ideal, or at least shortest, Perl 6 solution to most problems is shorter than most other languages...I don't know if it's necessarily more readable (you have to know a lot of the language to read really concise Perl 6 code, and there are many new concepts for most developers), but it's definitely not verbose.
Int:D, Int:U, etc. provide information about types. They aren't arbitrary extra boilerplate. Every language with types specifies them in somewhat comparable ways. Perl 6 has gradual typing, you can use as much or as little of it as you want, and as suits your problem domain. You don't need those types, but if you use them well, you can do things more concisely...e.g. for multi-method dispatch based on type of arguments.
given/when are keywords. Every language has keywords, and sometimes they'll be new ones that you haven't seen in other languages.
Sigils, again, provide additional information, both to the reader and the compiler. Perl 6 is probably an improvement, or at least simplification, over Perl 5 in that the sigil is constant and doesn't reflect the usage. e.g. in Perl 5, when you want a single element of an array, you use $name[1], but when you want the whole array you use @name. The idea (sort of) being that $ is singular (a "scalar" value), while @ is plural. Some people don't like sigils, but I don't see any point in re-litigating it 30 years on. Perl has sigils. That's just how it do. Some like it, some don't. Enough Perl developers like it that it's never gone away.
This assumes I think Perl (6) is precise and comprehensible.
In terms of interpolation, that's the variant that I like best.
"I have {some-complex-calc($i)} frobitzes"
It's 100% not added verbosity in any way. Added complexity, sure, but it's a pretty straightforward application of some of the main ideas of the language, usable in the standard C-language-family way out of the box, and powerful and flexible when you understand it.
Given and when were listed as separate items, not “given/when”; I agree with you about given (encompassing the given/when construct), but I can understand seeing “when” as an oddity (though I think it's a plus for comprehensibility, because it's general behavior is a consistent generalization of it's behavior in given/when.)
So really...all depending on what you mean.
Given/when is not really a syntax oddity, it's (in the simplest case), a structurally and semantically common construct in other languages that goes by various names (case/when, switch/case, etc.), the name is unique to Perl but the naming fis all over the map between other languages, and Perl matches English much better than most others (though it would do so more precisely if “when” was “when it is”), so that's a plus for readability except for people who come to Perl more fluent in some other programming language than in English.
Other uses of when might be unique to Perl, but they generalize the use from given/when (and have the much the relation to English), so there is a deep consistency there; learning a concept that is more limited in other languages buys you more in Perl.
my \foo = $ = 42; # bind sigilless name to a container with 42
say foo; # 42
foo = 666;
say foo; # 666
See https://opensource.com/article/18/9/using-sigils-perl-6 for more information. </plug>It doesn't sound to me like you are the right audience for the document you're reading.
Quoting from that page:
> this should not be mistaken for a beginner tutorial or overview of Perl 6; it is intended as a technical reference for Perl 6 learners with a strong Haskell background.
It is not wise to start with a technical reference unless you are already fairly well versed in the basics of the technology you're learning and are motivated to deepen your knowledge of it.
If you are interested in P6 you should start with overview or beginner material. Then, if you have a strong Haskell background, you can refer to the technical reference if and when the overview or beginner material isn't working for you.
(If you are not genuinely motivated to have fun learning P6 then nothing related to P6 will work for you. Are you genuinely motivated to have fun learning P6?)
----
> I'd like to hear opinions on how the use of sigils and the other syntax oddities
If you view childishly simple and natural syntax as "oddities" then your mind is already made up that they are oddities.
So the rest of this comment is intended primarily for other readers and secondarily for you on the off chance that you are able to transcend your mind's confirmation bias against the following constructs actually being simple and natural.
> use of sigils
Many programmers think in terms of three fundamental primitive data types from which all other datatypes are composed:
* single items
* collections of numbered things
* collections of named things
P6 adopts this view and assigns three sigils to these three abstract notions.
This makes it both easier and quicker to both read and write code -- once you've learned how they work. In P5 sigils were onerously complex. In P6 they are childishly simple. So now it's also easy and quick to learn them as well as to use them.[1]
> Int:D, Int:U
In P6, every type that's created using any of its type constructors is a sum type with two sub-types. This includes types defined in the standard language such as Int. The sub-types are named D and U. For example, to refer to Int's D sub-type, use `Int:D`.
This makes it easy and quick to read and write code once you've learned what they are. Fortunately, it's childishly simple to learn what they are.[2]
> given
The `given` keyword means essentially the same as the English word `given`.
This makes it easy and quick to read and write code once you've learned the English meaning of `given`.[3]
> when
The `when` keyword hides a good deal of complexity. But one can hop on Wittgenstein's Ladder easily. Because for the first rung, it just means the same as the meaning of the English word `when` used in the context of something that's `given`.
The keyword `when` makes it easy and quick to read and write code once you've learned the English meaning of `when` in the context of `given` -- and it can then be used in other contexts.[4]
----
[1] 27 second vide clip covers it all: https://www.youtube.com/watch?v=Nq2HkAYbG5o&t=10m&list=PLRuE...
[2] They're called "type smilies" for a reason. :D is the definite, happy, valuable type. What's `42`? Simple. Definite. Happy. Valuable. Unlike :D, the other subtype, :U, is undefined, unhappy, not a value. What's `Int`? Is it `42`? No, because that would be defined. Every value/object is either definite/happy or it isn't.
[3] From dictionary.com: given: noun: "an established fact"
[4] From dictionary.com: when: conjunction: "in the event that"
Probably your comment hints to this already in which case the following will be redundant but I wanted to mention them more explicitly. This is mainly for people unfamiliar with Perl 6.
Using the type smileys(:U, :D) are also ways to specifically match against a type object (`Int`, `Str`, `Rat`, etc.) and an instance object of a type object (-5, 'Hello', 1/5, etc.):
Int ~~ Int:U; # True
-5 ~~ Int:U; # False
-5 ~~ Int:D; # True
Int ~~ Int:D; # False
There is also the :_ smiley which is used by default whenever
the other two aren't specified: Int ~~ Int; # True, same as: Int ~~ Int:_;
-5 ~~ Int; # True, same as: -5 ~~ Int:_;Regardless, I'm glad you've written your comment to clarify. :)
I'd be delighted to hear about any of the several big or small mistakes I am about to make in the following.
Also an honest opinion about whether the following approach I take boils down to one that is complete gibberish, vaguely interesting but terribly complex, OK but meh, good but a bit abstract, enlightening, or something else. If you do let me know, please clarify whether you mean what you say for just you, or for what you think might be true for others that don't know P6 at all. TIA.
----
Like a lot of P6 stuff this basic aspect of its type system is both childishly simple but also so general, abstract, and high level, and comes at things from such a different angle than pretty much all other languages, that it can be difficult to explain.
Another attempt at what I was trying to get at in my second footnote in my GP comment...
----
A P6 type and its two standard sub-types are a simple and fun unification of the universal/existential "quantification" that our inner logician loves and the "article definiteness" distinction recognized by our linguistic side.
From https://en.wikipedia.org/wiki/Article_(grammar):
> The articles in English grammar are "the" and "a/an" ... In languages that employ articles, every common noun, with some exceptions, is expressed with a certain definiteness, definite or indefinite
From https://en.wikipedia.org/wiki/Quantifier_(logic):
> The two most common quantifiers mean "for all" [Universal] and "there exists" [Existential].
----
:D denotes a definite thing.
It is directly analogous to "the".
The value `42` is considered definite.
Definiteness implies that a thing actually exists. Its the linguistic analog of existential quantification.
Many coders talk about happy/sad path processing. :D is associated with the happy path, perhaps because it has no existential crisis. :D
----
:U is related to Universal quantification.
As a type, it is also related to "a". Quoting wikipedia:
> An indefinite article indicates that its noun is not a particular one identifiable to the listener.
:U corresponds to an indefinite thing, something that's only identifiable as a Universal, not a Definite thing.
:U is also associated with Undefined things, even if they are supposed to be undefined.
In the context of talk about happy/sad path processing, :U is associated with the Unhappy path, because it can denote a thing that should be definite but isn't.
----
As a value, a type name without a `:D` or `:U` smiley is the same as the type name with a `:U`.
As a type, a type name without a smiley is the same as the type name with a `:_`, where the `_` denotes either `D` or `U`.
Thus:
* `Int`, as a thing in of itself, is the same as an `Int:U`.
* `Int`, as a type of thing, denotes either an `Int:U` or an `Int:D`.
In cperl I rather use proper maybe types as in every other language. ?Int is (Int | Undef) you cannot use Int? as this would confuse the parser to look for the matching :
P6 supports something more powerful and uses that instead:
https://en.wikipedia.org/wiki/Tagged_union
In P6 the symbol Int, when treated as a type by the compiler, denotes Int:_, which is a sum type, with two subtypes, namely a generically existentially quantified Int (Int:D) and a generically universally quantified Int (Int:U).
Thus Int can be used in the role of a nullable type, but is an improvement in the sense that the equivalent to None (Int:U) encodes its type.
In P6 all constructable types are automatically sum types with :U and :D subtypes.
> need to check for definedness
There is relatively little need for users to check definedness. The language and compiler ensure basic type/memory safety and the user can just write code and it'll automatically be type safe at this basic level.
When the need arises, the user only need type one or two characters in almost all cases (either `:D` if writing a type constraint or `if foo` or `?foo` if writing an explicit check).
> at run-time
In P6 definedness refers to the notion that something actually exists and this is deliberately aligned with the notion that something exists at run-time.
Users seldom need to concern themselves with this but when they do the P6 approach is simple and convenient.
> one of its major design blunders. Also this syntax!
I see the Int, Int:D, Int:U semantics and syntax as a design triumph. See another comment in this thread for an introduction to my view: https://news.ycombinator.com/item?id=18647590
> I rather use proper maybe types as in every other language.
When I read things like "proper" and "as in every other language" in this manner I experience them as appeals to authority.
Perl 6 is not a typical language. Just as Perl 5 isn't either.
For example, a P6 compiler is allowed to statically check that types match at compile time:
sub foo (Str $bar) {}
foo 42
yields the compile time error: Error while compiling .code.tio
Calling foo(Int) will never work...
but it doesn't view static type checking as a panacea any more than it considers dynamic type checking as a panacea.P6, like Perl, is designed around the notion of a this and that view of things rather than an either/or view.