[Haskell-cafe] Compilers: Why do we need a core language?
sturdyi12 at mail.wlu.edu
Tue Nov 20 20:01:08 CET 2012
> * It can also be easier to apply high level optimization techniques;
> if you go straight from the highest level code to the lowest level,
> you are likely to miss optimization opportunities that are only
> revealed (or only sanely implementable) at intermediate levels.
Not to mention that since optimizations are often expressed as replacements (note how stream fusions are implemented), optimizing after desugaring reduces the number of patterns an optimization needs to match against.
By my understanding, GHC does not compile to core and then reparse the core file; 'core' is merely the name given to a certain stage in syntax tree manipulations. Thus, a compiler with no analogue of core would forego all syntax tree manipulations, instead taking the raw parse tree and running the code generator on that. And while that can be done for a sufficiently simple language, for a language with the complexity of Haskell I dare say that it is impossible:
> Because... have you ever tried to write a type-checker for character sequence?
>I'm sure some mad genius can do it, but I don't want to be that mad genius.
sturdyi12 at mail.wlu.edu
From: haskell-cafe-bounces at haskell.org [haskell-cafe-bounces at haskell.org] on behalf of Albert Y. C. Lai [trebla at vex.net]
Sent: Tuesday, November 20, 2012 12:47 PM
To: haskell-cafe at haskell.org
Subject: Re: [Haskell-cafe] Compilers: Why do we need a core language?
On 12-11-20 06:54 AM, citb at lavabit.com wrote:
> I know nothing about compilers and interpreters. I checked several
> books, but none of them explained why we have to translate a
> high-level language into a small (core) language. Is it impossible
> (very hard) to directly translate high-level language into machine
Let the overall distance be fixed. More steps over that same distance
means each step is smaller, easier to design, easier to understand,
easier to correct if there are mistakes.
Forget code generation. Just parse and validate and then discard.
Already there are like 4 steps. Translate character sequence into token
sequence. Translate token sequence into grammar tree, while checking
grammar. Translate grammar tree into declaration groups, identifier
lookup tables, etc., while checking whether every used identifier is
declared or imported. Translate those groups and tables into
type-annotated groups and tables, while checking types.
Whew! After 4 steps of translating this to that, we still haven't
reached the core language! Why?
Because... have you ever tried to write a type-checker for character
I'm sure some mad genius can do it, but I don't want to be that mad genius.
Haskell-Cafe mailing list
Haskell-Cafe at haskell.org
More information about the Haskell-Cafe