ghc
Mirror of the Glasgow Haskell Compiler. Please submit issues and patches to GHC's Gitlab instance (https://gitlab.haskell.org/ghc/ghc). First time contributors are encouraged to get started with the newcomers info (https://gitlab.haskell.org/ghc/ghc/wikis/contributing).
Top Related Projects
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
The Kotlin Programming Language.
Empowering everyone to build reliable and efficient software.
The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
The Swift Programming Language
The core OCaml system: compilers, runtime system, base libraries
Quick Overview
GHC (Glasgow Haskell Compiler) is the leading implementation of the Haskell programming language. It is a state-of-the-art, open-source compiler and interactive environment for the functional programming language Haskell. GHC provides a powerful platform for developing robust, concise, and correct software.
Pros
- Highly optimizing compiler with advanced features like lazy evaluation and type inference
- Extensive library ecosystem (Hackage) with a wide range of packages
- Strong static typing system that helps catch errors at compile-time
- Excellent support for parallel and concurrent programming
Cons
- Steep learning curve for developers new to functional programming
- Compilation times can be slow for large projects
- Error messages can be cryptic and difficult to understand for beginners
- Limited adoption in industry compared to more mainstream languages
Code Examples
- Hello World example:
main :: IO ()
main = putStrLn "Hello, World!"
This simple example demonstrates the basic structure of a Haskell program and how to print to the console.
- Fibonacci sequence using recursion:
fibonacci :: Int -> Int
fibonacci 0 = 0
fibonacci 1 = 1
fibonacci n = fibonacci (n-1) + fibonacci (n-2)
This function calculates the nth Fibonacci number using recursive calls.
- List comprehension example:
evenSquares :: [Int] -> [Int]
evenSquares xs = [x^2 | x <- xs, even x]
This function takes a list of integers and returns a list of squares of the even numbers.
Getting Started
To get started with GHC:
- Install GHC and Cabal (package manager) from https://www.haskell.org/ghcup/
- Create a new Haskell file (e.g.,
Main.hs
) with your code - Compile and run the program:
ghc Main.hs
./Main
For interactive development, use GHCi:
ghci
Prelude> :load Main.hs
*Main> main
This will load your file and allow you to interact with your code in the REPL.
Competitor Comparisons
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
Pros of TypeScript
- Larger community and more widespread adoption in web development
- Easier learning curve for developers familiar with JavaScript
- More extensive tooling and IDE support
Cons of TypeScript
- Less powerful type system compared to GHC's advanced features
- Slower compilation times for large projects
- Limited support for functional programming paradigms
Code Comparison
TypeScript:
interface Person {
name: string;
age: number;
}
function greet(person: Person): string {
return `Hello, ${person.name}!`;
}
GHC (Haskell):
data Person = Person { name :: String, age :: Int }
greet :: Person -> String
greet person = "Hello, " ++ name person ++ "!"
The TypeScript example showcases its object-oriented nature and structural typing, while the GHC example demonstrates Haskell's algebraic data types and pattern matching. TypeScript's syntax is more familiar to JavaScript developers, whereas GHC's Haskell syntax emphasizes its functional programming roots.
TypeScript is better suited for web development and large-scale JavaScript projects, offering gradual typing and easier integration with existing codebases. GHC, on the other hand, excels in areas requiring high-performance, strong type safety, and advanced functional programming concepts.
The Kotlin Programming Language.
Pros of Kotlin
- More modern language design with null safety and coroutines
- Easier learning curve and better interoperability with Java
- Backed by JetBrains, with strong IDE support
Cons of Kotlin
- Smaller ecosystem and community compared to Haskell
- Less emphasis on pure functional programming concepts
- Potentially slower compilation times for large projects
Code Comparison
Kotlin:
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
val doubled = numbers.map { it * 2 }
println(doubled)
}
GHC (Haskell):
main :: IO ()
main = do
let numbers = [1, 2, 3, 4, 5]
let doubled = map (*2) numbers
print doubled
Both examples demonstrate list manipulation and functional programming concepts, but Kotlin's syntax is more approachable for developers coming from imperative languages. Haskell's syntax is more concise and emphasizes its pure functional nature.
Kotlin offers a balance between object-oriented and functional programming, making it versatile for various applications. GHC, being a Haskell compiler, focuses on advanced functional programming concepts and type theory, which can be powerful but may have a steeper learning curve.
Empowering everyone to build reliable and efficient software.
Pros of Rust
- Larger community and more active development, with more frequent updates and releases
- Better documentation and learning resources for newcomers
- Stronger focus on memory safety and concurrent programming
Cons of Rust
- Steeper learning curve, especially for developers new to systems programming
- Longer compile times compared to GHC
- Less mature ecosystem for certain specialized domains (e.g., formal verification)
Code Comparison
Rust example (error handling):
fn main() -> Result<(), Box<dyn Error>> {
let file = File::open("example.txt")?;
// Process file
Ok(())
}
GHC (Haskell) example (error handling):
main :: IO ()
main = do
result <- try (readFile "example.txt")
case result of
Left err -> putStrLn $ "Error: " ++ show err
Right contents -> -- Process file
Both examples demonstrate error handling, but Rust uses the ?
operator for concise error propagation, while Haskell uses the try
function and pattern matching. Rust's approach is more explicit about error handling, while Haskell's is more functional in nature.
The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
Pros of Roslyn
- More extensive documentation and community resources
- Broader language support (C# and Visual Basic)
- Designed for easier extensibility and tooling integration
Cons of Roslyn
- Larger codebase and potentially steeper learning curve
- More complex build process and dependencies
- Primarily focused on .NET ecosystem, less versatile for other platforms
Code Comparison
Roslyn (C#):
var tree = CSharpSyntaxTree.ParseText(@"
class Program {
static void Main() {
System.Console.WriteLine(""Hello, World!"");
}
}");
GHC (Haskell):
module Main where
main :: IO ()
main = putStrLn "Hello, World!"
Summary
Roslyn is a more modern compiler platform with extensive tooling support, while GHC is a mature and highly optimized compiler for Haskell. Roslyn excels in .NET ecosystem integration and extensibility, whereas GHC offers powerful type inference and functional programming features. The choice between them depends on the target language, ecosystem, and specific project requirements.
The Swift Programming Language
Pros of Swift
- More modern language design with focus on safety and performance
- Larger and more active community, especially for mobile development
- Easier learning curve for developers coming from C-like languages
Cons of Swift
- Less mature ecosystem for server-side and systems programming
- More frequent language changes and potential instability
- Limited cross-platform support compared to GHC/Haskell
Code Comparison
Swift:
func fibonacci(_ n: Int) -> Int {
guard n > 1 else { return n }
return fibonacci(n - 1) + fibonacci(n - 2)
}
GHC (Haskell):
fibonacci :: Int -> Int
fibonacci 0 = 0
fibonacci 1 = 1
fibonacci n = fibonacci (n-1) + fibonacci (n-2)
Additional Notes
- Swift is primarily used for iOS and macOS development, while GHC (Haskell) is more focused on functional programming and academic research
- GHC has a longer history and is known for its strong type system and lazy evaluation
- Swift has better integration with Apple's ecosystem and tooling
- Both projects are open-source and actively maintained, but Swift has more contributors and frequent updates
- GHC offers more advanced features for type-level programming and compiler extensions
The core OCaml system: compilers, runtime system, base libraries
Pros of OCaml
- Simpler and more accessible syntax, making it easier for beginners to learn
- Faster compilation times, allowing for quicker development cycles
- Better support for imperative and object-oriented programming paradigms
Cons of OCaml
- Smaller ecosystem and community compared to Haskell
- Less advanced type system, lacking some features like type classes
- More limited support for parallel and concurrent programming
Code Comparison
OCaml:
let rec factorial n =
if n = 0 then 1
else n * factorial (n - 1)
let result = factorial 5
GHC (Haskell):
factorial :: Integer -> Integer
factorial 0 = 1
factorial n = n * factorial (n - 1)
result = factorial 5
Both OCaml and GHC (Haskell) are functional programming languages with strong type systems. OCaml offers a more balanced approach between functional and imperative programming, while Haskell focuses more on pure functional programming. The code examples demonstrate similar implementations of a factorial function, showcasing the syntactic differences between the two languages.
OCaml's syntax is generally considered more approachable, using keywords like let
and if
for definitions and conditionals. Haskell's syntax is more concise and relies heavily on pattern matching, as seen in the factorial function definition.
Both languages have their strengths and are well-suited for different types of projects and programming styles.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
The Glasgow Haskell Compiler
This is the source tree for GHC, a compiler and interactive environment for the Haskell functional programming language.
For more information, visit GHC's web site.
Information for developers of GHC can be found on the GHC issue tracker, and you can also view proposals for new GHC features.
Getting the Source
There are two ways to get a source tree:
-
Download source tarballs
Download the GHC source distribution:
ghc-<version>-src.tar.xz
which contains GHC itself and the "boot" libraries.
-
Check out the source code from git
$ git clone --recurse-submodules git@gitlab.haskell.org:ghc/ghc.git
Note: cloning GHC from Github requires a special setup. See Getting a GHC repository from Github.
See the GHC team's working conventions regarding how to contribute a patch to GHC. First time contributors are encouraged to get started by just sending a Merge Request.
Building & Installing
For full information on building GHC, see the GHC Building Guide. Here follows a summary - if you get into trouble, the Building Guide has all the answers.
Before building GHC you may need to install some other tools and libraries. See, Setting up your system for building GHC.
NB. In particular, you need GHC installed in order to build GHC, because the compiler is itself written in Haskell. You also need Happy, Alex, and Cabal. For instructions on how to port GHC to a new platform, see the GHC Building Guide.
For building library documentation, you'll need Haddock. To build the compiler documentation, you need Sphinx and Xelatex (only for PDF output).
Quick start: GHC is built using the Hadrian build system. The following gives you a default build:
$ ./boot
$ ./configure
$ hadrian/build # can also say '-jX' for X number of jobs
On Windows, you need an extra repository containing some build tools. These can be downloaded for you by configure. This only needs to be done once by running:
$ ./configure --enable-tarballs-autodownload
Additionally, on Windows, to run Hadrian you should run hadrian/build.bat
instead of hadrian/build
.
(NB: Do you have multiple cores? Be sure to tell that to hadrian
! This can
save you hours of build time depending on your system configuration, and is
almost always a win regardless of how many cores you have. As a simple rule,
you should have about N+1 jobs, where N
is the amount of cores you have.)
The ./boot
step is only necessary if this is a tree checked out
from git. For source distributions downloaded from GHC's web site,
this step has already been performed.
These steps give you the default build, which includes everything
optimised and built in various ways (eg. profiling libs are built).
It can take a long time. To customise the build, see the file HACKING.md
.
Filing bugs and feature requests
If you've encountered what you believe is a bug in GHC, or you'd like to propose a feature request, please let us know! Submit an issue and we'll be sure to look into it. Remember: Filing a bug is the best way to make sure your issue isn't lost over time, so please feel free.
If you're an active user of GHC, you may also be interested in joining the glasgow-haskell-users mailing list, where developers and GHC users discuss various topics and hang out.
Hacking & Developing GHC
Once you've filed a bug, maybe you'd like to fix it yourself? That
would be great, and we'd surely love your company! If you're looking
to hack on GHC, check out the guidelines in the HACKING.md
file in
this directory - they'll get you up to speed quickly.
Governance and Acknowledgements
GHC is a community project developed by a team of highly-talented researchers, individual contributors, and full-time developers. We are indebted to the many people whose work has brought GHC to its current state.
Some larger decisions are made by a smaller group of core contributors, as described in our governance documentation.
Top Related Projects
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
The Kotlin Programming Language.
Empowering everyone to build reliable and efficient software.
The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
The Swift Programming Language
The core OCaml system: compilers, runtime system, base libraries
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot