Write a Haskell macro (via CPP) to conditionally compile code based on the GHC version.
- User
Write a Haskell macro (via
CPP) to conditionally compile code based on the GHC version. Assistant
Write a Haskell macro (via CPP) to conditionally compile code based on the GHC version.
The user wants a Haskell macro using the C Preprocessor (CPP) to conditionally compile code based on the GHC version.
Understand the Goal: The objective is to have code that only compiles (or is included in the source) if the compiler (GHC) is a specific version or within a specific range.
Identify the Tool: Haskell uses the C Preprocessor (cpphs or native cpp) via the CPP language extension (Pragma: {-# LANGUAGE CPP #-}).
Identify the Variables: GHC defines specific macros during compilation. The most relevant ones for version checking are:
__GLASGOW_HASKELL__: A version code in the format MMmm (Major, Minor). Note: For GHC 7.10, it's 710; for GHC 8.0.1, it's 800.MIN_VERSION_GLASGOW_HASKELL(major1, major2, minor, patch): A helper macro introduced in older GHC versions (around 7.10/8.0) to make comparisons easier. However, standard CPP numerical comparisons are the most robust and universally understood "low-level" way. Let's focus on __GLASGOW_HASKELL__ first as it's the standard, and then mention MIN_VERSION_GLASGOW_HASKELL.Constructing the Logic (Mental Draft):
Scenario A: Check if GHC >= 8.0.
__GLASGOW_HASKELL__ for 8.0 is 800.
#if __GLASGOW_HASKELL__ >= 800
Scenario B: Check if GHC < 7.10.
__GLASGOW_HASKELL__ for 7.10 is 710.
#if __GLASGOW_HASKELL__ < 710
Scenario C: Check for a specific minor version (e.g., GHC 9.2 vs 9.0).
This is tricky because __GLASGOW_HASKELL__ usually only encodes Major.Minor (e.g., 902 for 9.2). Wait, let me verify the format.
Format: Major * 100 + Minor.
Example: GHC 9.2.1 -> 902. GHC 8.10.1 -> 810.
Correction on Patch versions: __GLASGOW_HASKELL__ doesn't track patch versions (the third number). It tracks Major and Minor.
If specific patch level checks are needed, __GLASGOW_HASKELL_PATCHLEVEL__ might be relevant, but usually, API changes happen at the minor level.
Refining the Solution:
I should provide:
MIN_VERSION_GLASGOW_HASKELL macro (which is actually defined by the Cabal library usually, or specifically base, but wait... MIN_VERSION_GLASGOW_HASKELL is often available in modern GHCs via the ghc-platform or similar logic, but historically it's __GLASGOW_HASKELL__).
Self-Correction: MIN_VERSION_GLASGOW_HASKELL is not a standard CPP macro provided by the compiler binary itself in the same way __GLASGOW_HASKELL__ is. It is typically generated by Cabal when building a package. However, for a generic "macro via CPP" request, relying on the numeric value __GLASGOW_HASKELL__ is safer because it works even without Cabal (e.g., running ghc manually).
Actually, checking the GHC docs: MIN_VERSION_GLASGOW_HASKELL(x,y,z,w) is indeed a macro provided by GHC (specifically ghcplatform.h) since version 7.10? No, checking documentation...
Okay, __GLASGOW_HASKELL__ is the raw integer.
MIN_VERSION_GLASGOW_HASKELL is arguably cleaner syntax if available. It is available if you include the right headers, but CPP in Haskell doesn't always pull in C headers.
The most reliable standard method is comparing __GLASGOW_HASKELL__.Structuring the Code Examples:
Basic Setup:
{-# LANGUAGE CPP #-}
The Macro Variable:
Explain __GLASGOW_HASKELL__.
Formula: Major * 100 + Minor.
Example: 8.6.5 -> 806.
Example 1: Minimum version:
#if __GLASGOW_HASKELL__ >= 806
-- Code for GHC 8.6+
#endif
Example 2: Specific Range:
#if __GLASGOW_HASKELL__ >= 800 && __GLASGOW_HASKELL__ < 810
-- Code for GHC 8.x (before 8.10)
#endif
Example 3: Handling Imports (Very common use case):
Showing how to handle modules that moved (e.g., Semigroup moving into Prelude in GHC 9? Or specific base version checks).
Correction: Usually checking GHC version is a proxy for checking base version, but sometimes the compiler features change (like plugin APIs).
Let's stick to a compiler-specific change, or just a generic code block.
Handling Patch Levels (Advanced):
Mention __GLASGOW_HASKELL_PATCHLEVEL1__ etc., though rarely needed.
Refining the "Macro" part: The user asked for a "Macro". In C terms, this usually means defining a helper macro to make the code prettier. I can define a helper macro like:
#if __GLASGOW_HASKELL__ >= 900
#define GHC_IS_AT_LEAST_9
#endif
Or even better, show how to simulate the MIN_VERSION macro if they want cleaner syntax, or just use the raw integer comparison which is standard practice.
Drafting the Final Response:
{-# LANGUAGE CPP #-}.__GLASGOW_HASKELL__.Major * 100 + Minor.Let's verify the value for GHC 9.2.
GHC 9.2 -> 902.
GHC 8.10 -> 810.
GHC 7.10 -> 710.
GHC 7.8 -> 708.
This is consistent.
Caveat: CPP in Haskell (via cpphs or native) can sometimes be finicky with complex logic. Standard arithmetic works fine.
Alternative: MIN_VERSION_GLASGOW_HASKELL is actually defined in ghcplatform.h. Haskell's CPP does implicitly include standard platform headers? No, it doesn't. It's often defined by Cabal in the setup-config or similar, but relying on it in a raw .hs file might fail if compiling strictly with ghc Main.hs without Cabal.
Decision: Stick to __GLASGOW_HASKELL__ as it is the robust, raw compiler definition.
Final Polish: Provide a clean example that defines a function with different implementations based on the version. This makes it concrete.
Example:
A function ghcVersionString that returns a string.
Check: Does show work on CPP macros? No.
I need to use stringification or just simple logic.
Let's do an #if / #elseif / #else block.
Also, mention how to check for base version, as that is often what people actually want (API changes usually happen in libraries, not the compiler binary itself). But the prompt specifically asked for "GHC version". I will answer the prompt directly but add a note about base.
Construction:
Self-Correction on __GLASGOW_HASKELL__ format:
The docs say: major * 100 + minor.
So GHC 8.10.1 is 810.
GHC 9.0.1 is 900.
GHC 9.2.1 is 902.
Correct.
Let's write the response.
To conditionally compile Haskell code based on the GHC version, you need to use the CPP language extension and check the predefined macro __GLASGOW_HASKELL__.
Here is the standard pattern and a complete working example.
The macro __GLASGOW_HASKELL__ follows the format MMmm (Major, Minor).
906810710{-# LANGUAGE CPP #-}
module Main where
#if __GLASGOW_HASKELL__ >= 900
-- Code for GHC 9.0 or newer
import GHC.Stack (withFrozenCallStack)
#else
-- Code for older versions
#endif
main :: IO ()
main = putStrLn "Hello"
Save this code as VersionCheck.hs.
This example demonstrates how to:
{-# LANGUAGE CPP #-}
module Main where
-- 1. Define a helper macro for cleaner logic (Optional)
-- This checks if Major >= 9
#if __GLASGOW_HASKELL__ >= 900
#define IS_GHC_9 1
#endif
-- 2. Conditional Imports
-- Text.Printf is available everywhere, but let's pretend we need
-- a specific import for newer GHCs.
#ifdef IS_GHC_9
import Data.Kind (Type) -- 'Type' is prominent in modern GHC
#else
import Data.Kind (Type) -- In older GHC this might be in different modules or aliases
#endif
main :: IO ()
main = do
putStrLn "---------------------------------"
putStrLn "GHC Version Check Report"
putStrLn "---------------------------------"
-- 3. Basic Version Check
let versionCode = __GLASGOW_HASKELL__
putStrLn $ "Raw Version Code: " ++ show versionCode
-- 4. Conditional Logic
-- Check for GHC 9.0 or later
#ifdef IS_GHC_9
putStrLn "Detected: GHC 9.0 or newer."
#else
putStrLn "Detected: GHC older than 9.0."
#endif
-- Check for a specific range (e.g., GHC 8.10)
-- 8.10 is encoded as 810
#if __GLASGOW_HASKELL__ == 810
putStrLn "Specific Action: You are running exactly GHC 8.10."
#endif
-- 5. Checking Patch Levels (Minor minors)
-- GHC defines __GLASGOW_HASKELL_PATCHLEVEL1__ for the 3rd digit.
-- e.g., GHC 9.2.4 defines PATCHLEVEL1 as 4.
#if __GLASGOW_HASKELL__ == 902
if __GLASGOW_HASKELL_PATCHLEVEL1__ >= 3
then putStrLn "GHC 9.2.3 or higher detected (Bug fix applied)."
else putStrLn "GHC 9.2.0 - 9.2.2 detected."
#endif
putStrLn "---------------------------------"
The __GLASGOW_HASKELL__ integer is constructed as follows:
9).2).| GHC Version | Macro Value |
|---|---|
| 7.10.3 | 710 |
| 8.0.2 | 800 |
| 8.10.7 | 810 |
| 9.0.2 | 900 |
| 9.2.1 | 902 |
| 9.6.3 | 906 |
While CPP is standard, checking GHC versions inside your source code can be brittle. If you are building a library, it is often better to use Cabal Conditional Compilation.
Instead of #if in the Haskell file, you list different modules in your .cabal file:
library
if impl(ghc >= 9.0)
exposed-modules: MyLib.NewStuff
else
exposed-modules: MyLib.LegacyCompat
This keeps your Haskell code cleaner and your build system smarter.