
Hack: a new programming language for HHVM - bos
https://code.facebook.com/posts/264544830379293/hack-a-new-programming-language-for-hhvm/
======
reikonomusha
I am baffled as to why you'd build your castle atop a crumbling foundation.

I have wondered why FB didn't use a proper language with proper typing to
begin with. I mean, I "understand" logistically: they already had a giant
codebase in PHP, migrating a codebase is expensive, and it's difficult to hire
and train 1000s of hackers in e.g., OCaml. (They _do_ have some OCaml people,
but they are outliers. OCaml was my favorite thing to write there, though it
didn't afford some of the same niceties and interactivity as the PHP code they
had, only because the support was down by several orders of magnitude.)

But at the same time, layering FP with a home rolled static type checking
server (??) is bug prone and is certainly yak shaving (which they have time
and money to do). Now they've written (1) a compiler to C++, (2) a compiler to
VM byte code, (3) a corresponding runtime for each, (4) extensions to PHP, (5)
a type checker, and (6) an inference engine. That's a lot of stuff. And in the
end, it's still PHP, which is duly disliked. (Though Facebookers don't seem to
care. The prevalent attitude toward it is that "PHP, as it's coded here, is
mostly like C++, and that's OK.")

Writing correct type checkers and inference engines is kind of difficult. They
seemed to take the approach of just building onto it incrementally until it
just seems to work. That approach led to many bugs in many cases that just
simply aren't thought of when one is trying to build inference engines by
hand, as opposed according to theory. Type checking and inference is an area
ripe with theory and attached formal, mathematical semantics. Standard ML's
standard is perhaps the most infamous; it's a collection of mathematical
statements about the language. That way, the compiler is now almost an engine
to prove your code is correct. I don't see how the same guarantee can be made
with something that is just cobbled together.

~~~
toast0
The HN crowd seems to dislike (or despise perhaps?) PHP, but it's really not
that bad. Yes it has a lot of warts, but it has a lot of things that make it
nice for web development.

a) try your new code by saving in your editor, and hitting reload in your web
browser.

b) it's very approachable. People who only know HTML and CSS can be expected
to do a little bit of PHP work to integrate their changes. If you setup the
right network mounts, they just need to edit files and reload (see a)

c) it's not super high overhead at runtime. If you're not using a framework,
and you don't build up a crazy object hierarchy, it's not too hard to get your
page out with about 10 ms of overhead beyond data fetching. For very simple
webservices (fetch data, possibly from multiple sources, and do a little
formatting for the consumer), I was able to get the overhead down to 2ms. You
can certainly do better with other languages, but you can usually get better
throughput improvement by working on getting data quickly. Btw, all the
frameworks are terrible; many of them add 100 ms to the page just for the
privilege of loading the includes; PHP is a framework for web programming
thank you very much.

d) cleanup; you don't have to worry about it. If you don't do anything weird
(c extensions, with non-preferred malloc), at the end of the request,
everything is thrown away.

That said, there are plenty of things PHP isn't good at: I wouldn't run a long
running process in PHP; and multithreaded PHP sounds like a bad idea.

~~~
Lennie
If you want to know: just checked the one project where I did a long running
PHP-process.

It is a job scheduler which takes jobs from a Redis queue and executes it.

It does fork a new process for 1 type of task (image resizing in this case) to
make sure it doesn't leak memory.

It also reuses these forked processes so it doesn't need to fork for every
task.

It was last started on Jan. 2013 (because of maintenance) and still running
fine.

The reason ? I didn't want to introduce an extra language for project for
which all the server code was already written in PHP.

So if it fits the task, you can do it.

~~~
toast0
Duly noted, will not be so negative on long running processes. :)

------
bos
I'm the manager of the team that developed Hack, and I'm sitting here with
some of the language designers. Happy to answer your questions.

~~~
wambotron
Why use

public string $x = '';

instead of

public $x:string = '';

It seems inconsistent to me, probably because I've used AS3/Haxe.

~~~
wvenable
PHP is based on C. It's way more consistent with C, Java, C# , etc to do the
former. So the real question is why

    
    
        function foobar() : int { .. }
    

instead of

    
    
        function int foobar() { .. }

~~~
amenghra
1\. function foobar() makes it easier to grep for. 2\. if I recall correctly,
it made some parts of the grammar easier.

------
jlebar
So argue. Much armchair quarterback. Very flame. Wow.

It's entirely appropriate and reasonable to say "I don't understand why
Facebook would do this; please explain it to me." But it reflects a supreme
lack of humility to say "I don't understand why Facebook would do this; they
must be idiots, and when they, armed with inside information I don't even know
I don't have, come to defend themselves, I will angrily try to convince them
of the error of their ways."

If you think what Facebook has done here is stupid, why is that something to
get upset over? In fact, why is it even something worth arguing about? You're
not going to convince anyone who matters, and anyway you have nothing to gain
by doing so: Instead, just short Facebook stock and be smug in the knowledge
that, when Facebook announces next year that they're abandoning PHP in favor
of a ménage à trois of OCaml, F#, and Clojure, you'll be able to say "I told
you so".

~~~
cpayne624
Wish I could upvote this a bunch of times

------
habosa
Facebook is always pushing PHP to new places. Would it be too broad to say PHP
is the worst thing that ever happened to Facebook's engineering but Facebook's
engineering is the best thing that ever happened to PHP?

~~~
ecaron
I'd rephrase the question to be "Would Facebook be Facebook without PHP?"

~~~
thescrewdriver
I don't see why not. It's possible to have done it any any mainstream
language.

~~~
nbm
Which mainstream language would you have chosen in 2003 that you think would
have had similar success?

~~~
gtirloni
Any. Really, any. Facebook is throwing a lot of engineering effort into fixing
things that now matter to them, but didn't when they started (and were small).

They could have picked Perl CGI's and it'd still have happened (perhaps Perl 6
would be a success these days).

I don't think PHP has played any role in bringing FB to the size it is today
(programming language does not translate to number of users signing up). So
yes, any mainstream language available in 2003 would do it.

~~~
wmil
Perl was a possibility, but CGI is/was a pain to use. To get started you had
to understand things like headers. You frequently had scripts refusing to run
for whitespace issues relating to CRLF vs LF. You'd need to come up with your
own templating language... It was just much more difficult.

Also Perl libraries typically require superuser rights to install. That's a
big deal if you're a poor student using shared hosting on your fun hacking
projects.

Python really wasn't an option. mod_python by default required you to restart
Apache whenever a python file changed. It was designed to be configured by
sysadmins on dedicated servers... if you wanted something you could hack
together an put on a shared host, it just wasn't an option.

Also the hot Python framework in 2003 was Zope. No one uses Zope anymore. For
good reason.

Ruby was much smaller, and this was pre-rails so it didn't have any
significant web framework. I'm not even sure if there was a production ready
way of hosting ruby scripts.

~~~
rafekett
> To get started you had to understand things like headers

since when is this a problem? really.

~~~
wmil
To make something accessible you need to let people learn it one small chunk
at a time.

PHP lets you ease in, starting at html with some inserted php code and working
your way up to more complex code.

CGI, at least as I recall, required you to return valid HTTP responses with
correct headers. That's a big jump from simple HTML.

------
jtreminio
If you want to try out Hack (and HHVM) quickly, my FOSS project lets you
quickly set up a VM using Vagrant and Puppet:
[https://puphpet.com](https://puphpet.com)

~~~
gaoshan
PuPHPet is great for getting a ready to go VM with a PHP and all the
trimmings. Super easy, super fast.

------
georgebarnett
My favourite comments are the ones where some random geeks tell the guys who
have built a multi-billion dollar business with hundreds of millions of daily
active users how they're doing it wrong.

Congrats to Facebook on taking PHP forward. It powers a vast amount of the web
and it's great to see that it's getting some engineering love!

~~~
tszming
>> where some random geeks tell the guys who have built a multi-billion dollar
business with hundreds of millions of daily active users how they're doing it
wrong.

Remind me the words from Steve Jobs:

"By the way, what have you done that’s so great? Do you create anything, or
just criticize others work and belittle their motivations?"

------
treblig
Man, it's going to be really hard to search the Internet to troubleshoot this.

Search: "Hack Anonymous Function" Search: "Hack Background Process"

~~~
psychometry
As a frequent user of R, this is one of my biggest pet peeves. There are a
practically infinite number of unique, interesting, pronounceable, google-able
names for a new programming language, yet we continue to get stuck with this
shit.

~~~
nobleach
A few months ago, "Martini" came out for "Go". I love the framework, but man,
searching for stuff on Google was a PITA.

~~~
jamieomatthews
At least go says in their official docs, when posting/searching for the
language, use "Golang"

------
octo_t
See this presentation[1] from Strange Loop 2013 about some design decisions on
Hack.

[1] -
[https://github.com/strangeloop/StrangeLoop2013/blob/master/s...](https://github.com/strangeloop/StrangeLoop2013/blob/master/slides/sessions/Adams-
TakingPHPSeriously.pdf)

------
vanilla
Naming a language hack ... very poor choice. Searching for Go, C, C++ is
already bad, now you can add hack to the list.

~~~
VeejayRampay
Hacklang. There, just like go, problem solved.

~~~
psychometry
That would only solve the problem if everyone referred to it as "Hacklang",
which will surely not be the case.

------
vezzy-fnord
Looks like Facebook might have inadvertently turned PHP trendy again.

Expect "Why I migrated from Go to Hack" articles soon enough.

Either way, the name is very fitting. I have no use for this, but good for
Facebook that they've managed to (at least to some extent) evade some of the
many PHP pitfalls.

~~~
roberto
I started web programming using PHP back in 2000, but quickly moved to Python
a couple of years later and never looked back. That is, until I joined FB this
year as a data engineer. Programming in PHP with Hack/XHP is awesome.

~~~
vineet
Is Hack an evolution of XHP, or is XHP a subcomponent of Hack?

~~~
dparoski
Engineer working on Hack here.

Historically XHP was developed before Hack. It's available as an add-on for
PHP but never became a formal part of the PHP language. When Hack was being
designed it needed to work well with XHP (as XHP is used a fair amount with
Facebook's code base), so over time it made more sense to start thinking of
XHP as being one the language features that Hack offers that is not available
in stock PHP.

------
cpeterso
Is Facebook's code so monolithic that they can't deploy new, decoupled
services written in new languages? Twitter did this with Ruby, Java, and
Scala. Didn't Facebook create Thrift RFC for exactly this purpose??

~~~
nbm
Facebook already has several decoupled services written in other languages.
The PHP codebase is mostly the front-end code which, beyond some database
work, dispatches heavy lifting to back-end services written in C++, Java, and
other languages.

As you say, Thrift connects this all. You can look into our C++ core library,
Folly, and other C++, C, Python, and Java applications and libraries like
Rocksdb, Presto, watchman, Buck, flint, scribe, and so forth on github.

------
rarestblog
PhpStorm support issue
[http://youtrack.jetbrains.com/issue/WI-21737](http://youtrack.jetbrains.com/issue/WI-21737)

------
wes-exp
Sounds like they added optional type declarations, lambda expressions, and a
few more goodies to PHP.

I am reminded of
[http://en.wikipedia.org/wiki/Greenspun%27s_tenth_rule](http://en.wikipedia.org/wiki/Greenspun%27s_tenth_rule).

Facebook is now at sufficient scale where they are reimplementing Common Lisp
on top of PHP.

------
wambotron
I haven't looked this over too much but I'm curious as to why they did this:
<?hh class MyClass { const int MyConst = 0; private string $x = ''; public
function increment(int $x): int { $y = $x + 1; return $y; } }

instead of this: <?hh class MyClass { const MyConst: int = 0; private $x:
string = ''; public function increment(int $x): int { $y = $x + 1; return $y;
} }

The first seems inconsistent to me. Especially coming from AS3/Haxe where the
function return value is indicated in the same manner.

~~~
jdp
Would be more consistent to suffix the types in function decls as well, e.g.
`increment($x: int)` vs. `increment(int $x)`.

~~~
wambotron
Yep. So much weirdness here when they had a really good shot at "fixing" it.

------
fmitchell0
Very excited to play with this. While I know many poo-poo PHP, it still is a
pretty decent gateway language.

If these type of innovations introduce hobby programmers into more
professional practices and concepts, I'm all for it.

~~~
VeejayRampay
Well, it's been poo-pooed for obvious reasons (like Javascript) but those are
the languages that are getting all the love in the end, so maybe I should stop
programming in a language that I find beautiful (Ruby) and go with the flow.

Yes, I'm slightly bitter :/

~~~
ProNoob13
You should develop in whatever you love. It's a great thing to know multiple
languages, though.

------
bowlofpetunias
Every negativity thread on PHP on HN just serves as a reminder how poorly
informed and inexperienced most developers here really are.

Good to keep in mind the next time you read a topic on how awesome language
X/framework Y/method Z is.

It's depressing to see how much of our profession is informed by the desire to
being cool and fashionable, like kids in schoolyard.

------
mlubin
"Hack provides instantaneous type checking via a local server that watches the
filesystem."

What does this mean?

~~~
bos
On Linux, we use the inotify subsystem to be informed every time you save a
source file or switch branches. The hh_server process can then update its data
structures immediately, without being explicitly asked.

~~~
wink
I don't really get why this is the second paragraph on the page of a
_language_ though. It's like touting the jar command in an overview of Java.

~~~
bos
Because this is tremendously important to the usability of the language.

One of the reasons that PHP has been such a success is that you can simply
save a source file and reload a web page to see what's going on.

The server-based typechecker that runs instantaneously is what makes it
possible for Hack to replicate this experience: you save a file and reload a
web page, but you have the safety net of a typechecker that told you about
your type errors as soon as you saved the file.

I can't overstate how important this instant feedback is: it's really the
thing that distinguishes Hack from working in a more traditional compile-based
static language.

------
inglor
Am I the only one who finds it ironic they have the old deprecated `mysql_*`
API in the sample?

------
paulyg
Are there any plans for a Hack -> PHP transpiler? Or is that impossible? It
would be nice to develop on Hack, transpile back to vanilla PHP where we still
have to use the PHP.net runtime (shared hosts, Google app engine, Engine Yard,
etc)

~~~
amenghra
Hack is more than just a type system. At the type level, it would be trivial
to erase the types and run the code on vanilla PHP. Dealing with everything
else is much harder (xhp, containers, lambda, etc.), at which point it just
makes more sense to use HHVM.

------
yulaow
As said on reddit, that is really a poor choice of name. Good luck looking
something related to this language on google putting "hack" in the
searchbox... It is even worse than "Go"

~~~
varkson
I imagine everyone is going to start calling it hacklang.

------
argimenes
I don't understand why there is so much fuss about PHP in 2014. Hasn't most
serious web development moved beyond scripting languages? Java and C# have
been mature languages for statically-typed web development for years and are
not difficult to achieve competence in. And for more fluid yet terse server-
side languages you could always go with Ruby, or Smalltalk, or Racket, or F#,
and their associated frameworks. Defending PHP in 2014 is almost as surreal as
defending Classic ASP in 2014.

------
Touche
This rubs me the wrong way.

> Thus, Hack was born. We believe that it offers the best of both dynamically
> typed and statically typed languages, and that it will be valuable to
> projects of all sizes.

In which way does it offer the benefits of dynamic typing? The entire point
seems to be to abandon dynamic typing, which is fine, but not what that
sentence says.

I'm guessing, for example, you can't really do meta-programming with Hack in
the way you can with dynamic languages, is that correct?

~~~
camus2
AFAIK large PHP libraries(Doctrine/Symfony) generate PHP code (from config
files for instance) that is then required and executed.

You cant be more meta than that.

I dont know if you can do that with Hack, but that's definetly a big PHP
pattern.

~~~
jwatzman
It depends on what you mean by "with Hack". You cannot do this in Hack code --
we disallow the dynamic features of the language you need to do this; they are
impossible to statically typecheck and verify. However, Hack code iteroperates
seamlessly with PHP code, so there's nothing stopping you from having some PHP
files in your codebase that do anything that standard PHP can do.

~~~
Touche
So.. my question remains unanswered... if you cannot do meta-programming in
hack, how can you say it "offers the best of both dynamically typed and
statically typed languages"?

------
aleem
I wish these extensions came by way of the PHP core rather than a language
that is superimposed on PHP. However, this is awesome.

Is this a layer that is superimposed on PHP that falls back to the default
interpreter for unimplemented features or is this a fresh implementation. I
suppose my question is, how reliable is this. Are the core PHP bugs going to
manifest here? If a bug gets fixed on core PHP, will hack be lagging behind?

~~~
camus2
> I wish these extensions came by way of the PHP core

Well where is PHP core then? to my knowledge while PHP 5 was a huge step
forward, PHP core did little to fix PHP stupid design flaws,for the sake of
backward compatibility.

We are in a new era, libraries are more and more decoupled,so they are easy to
write or rewrite,backward compatibily shouldnt be an issue when libraries are
well versioned and tested across different versions.

And Wordpress folks dont care about PHP.next.Why bother with them?

~~~
rabino
Can you expand on "Wordpress folks dont care about PHP.next" ?

~~~
camus2
look at Wordpress codebase.It's not modern PHP because old PHP versions
support.

~~~
rabino
And your best explanation is that WordPress core devs just don't care?

------
cab_codespring
Odd, just two days ago I was remembering a college class where we had to
create a new programming language, and I named mine "Hack". The cover of the
paper was a copy of K&R only "C" was replaced by "Hack" and of course I was
the author.

------
mogui
It seems like a strange monster with PHP body and C++ head or the other way
around you choose

~~~
amenghra
Yes, pieces of Hack will feel similar to other languages.

bos said in another thread: "Yep, we're happy to be inspired by good ideas
when they're obviously the right path to walk."

------
lukashed
Off topic: Can anyone else see a trend to name projects so that they're
impossible to google? Hack, Go, Celery, Fig, Cucumber, Lettuce, you name it.

E.g. the only way how I could find useful resources on how to run celery with
fig was to exclude -gorgonzola.

------
bsaul
I'm really curious to know if any __new __project will start using this
language. I really wouldn 't understand why they would. I think that would be
an argument to explain why some consider this whole project a loss of time in
the long run ( although probably something really great from a purely CS pov)

Edit : i just forgot that i still haven't found any server side language i'm
satisfied with. I'm still waiting for a type-annotated variant of python to
catch on.... So maybe hack would be a good choice after all. It's such a pity
they started this work based on php rather than any other cleaner language.

------
jdp
Why a brand new language, instead of building or optimizing the PHP backend
for HHVM with an existing language like Haxe?
[http://haxe.org/](http://haxe.org/)

~~~
bos
It was very important to us to have a gradual and smooth upgrade path, such
that you can start using Hack by just tweaking the "<?php" header at the top
of a source file.

Obviously switching to a syntactically incompatible programming language would
be at odds with that goal.

------
programminggeek
I'm really intrigued by the concept of array Shapes. I actually implemented
that for Obvious and use it for type checking hashes in ruby with Contracts.
[https://github.com/RetroMocha/obvious/blob/master/lib/obviou...](https://github.com/RetroMocha/obvious/blob/master/lib/obvious/contract.rb#L176)

It's cool to see the same idea implemented in Hack. I don't know if anyone at
FB has even heard of Obvious, but it's cool that they had the same idea in
their language.

------
amaks
Hack to PHP is as Dart to JavaScript.

------
kackontent
This is great and exactly what I need in my PHP. Glad to see types making sort
of a come back (see also: TypeScript) as I think they're, in one way or
another, necessity in a large scale applications.

One note though - the success of this depends on the success of HHVM.
Hopefully FB guys understand that and will push even more to make HHVM the
best platform to have for running PHP on.

------
thinkindie
everyone has it's own <opinion> over the fact that facebook had to use another
language rather than php. On the other side there is the fact that facebook
managed to get one of the most visited website on top of php, period. This is
a <fact> and we would never know what would be facebook if it was built on top
of another language. Probably they had to shut down early just because they
were not able to hire enough developers to keep pace with new stuff and scale.
At the same time they haven't suffered the same amount of issues twitter had
to deal with (remember the landing whale?) with much less users and traffic
just because they were using RoR and that lead them to move to something
completely different with scala and a different architecture. But still,
facebook has about 4x times users and much more data to deal with (more text,
images, video and so on).

Nevertheless, let's remember that web applications are not a one-shop
monolithic anymore.

------
himelnagrana
Firstly, Awesome! Need to traverse more in it - definitely.

But as the documentation says primarily "HHVM can run both your PHP and Hack
code, either separately or when both are part of one project." \-- does that
means we can imagine a framework or something like that - where parts of it is
in Hacklang and parts are in old PHP?

or such project is somewhere there already?

------
elwell
_Somewhat unrelated, but curious_ : Does anyone know if Facebook still uses
MySQL heavily?

~~~
HarrisonFisk
Yes, we are still very active users of MySQL. Most of the primary portions of
facebook.com are still served from a backend MySQL system (with lots of
caching and many other services involved). Some data is not stored in MySQL,
but in other systems such as HBase (messages being a big one).

------
devwebee
It looks promising, I highly dislike writing PHP and this seems like it might
ease the pain, but they could've gotten rid of the damn dollar sign in front
of variables, how ugly is this? "return ($y) ==> { return $y + 1; }"

~~~
dparoski
Engineer working on Hack here.

You can actually write you example a bit more concisely in Hack: "$y ==> $y +
1".

~~~
iso8859-1
Return statements are optional? So it's just the last statement in a function
that'll get returned?

~~~
dparoski
The "return" keyword is not needed if the right hand side of the lambda arrow
("==>") is an expression that is not wrapped in curly braces.

If the right hand side _is_ wrapped in curly braces then it is treated as a
list of statements, in which case you do need to use the "return" keyword.

~~~
dparoski
Edit: My previous comment was referring to the second occurrence of "return"
in your example (after the "==>" arrow). Just noticed I mistakenly dropped the
first occurrence of "return"; the first occurrence of "return" in your example
is needed.

------
zmanian
Provides some context on the language development.
[http://www.wired.com/wiredenterprise/2014/03/facebook-
hack/](http://www.wired.com/wiredenterprise/2014/03/facebook-hack/)

------
sesm
It's good to see gradual typing rising!

2 things are not obvious from the article:

1\. Why a new language, and not just a gradual typing tool for PHP (like Typed
Racket and Typed Clojure)?

2\. Is type information somehow passed to runtime to assist JIT compilation?

------
dghf
"You have reinvented PHP better, but that's still no justification":
[http://colinm.org/language_checklist.html](http://colinm.org/language_checklist.html)

------
srg0
Any¹ sufficiently advanced social network contains an ad-hoc, statically
typed² implementation of half of PHP.

¹ YMMV ² sometimes

See also:

KPHP (subset of PHP to C++ translator, released by Vkontankte).

------
Crito
Does anybody know if Hack can correctly parse integer literals, properly
rejecting malformed ones?

PHP cannot, so I wonder how deep the relationship goes.

------
thatmiddleway
Looks like another way to polish the PHP turd.

------
nness
> PHP only supports a 256-character set, and hence does not offer native
> Unicode support

:(

This was their opportunity to to get multi-byte strings right...

------
TazeTSchnitzel
Oh great. Now when PHP expands its type annotations, they'll be incompatible
with Hack. :(

------
z3phyr
They have best functional programming language geeks around and they made
this?

~~~
pjscott
I realize it must be shocking to see functional programming language geeks
solving real-world problems in a pragmatic way. :-)

Seriously, though, they wanted to move a large PHP codebase over to a better
language, at the scale of a multi-billion-dollar tech company, in a reasonable
amount of time. They accomplished this. What's the criticism here?

~~~
z3phyr
I am actually impressed. What shocked me was, functional programming
technomancers are not like that at all!

------
msie
Wanted: Backend developer. Must have 5 years of experience with Hack.

~~~
Moru
Wanted: Backend developer. Must have max 1 year of experience in PHP.

------
islon
It seems like the name really suits the language.

------
elwell
I thought Hack was already released months ago?

