'How to get haskell compiler version within code without a long list of CPP if/else conditionals?

Something along the lines of:

import GHC.Version qualified

main :: IO ()
main = print $ GHC.Version.current
-- would print Version 8 10 7

A roundabout way is to execute ghc --version from shell:

import System.Process

main = system "ghc --version"

but this may be incorrect when the running GHC isn't the same as the ghc in PATH.



Solution 1:[1]

Yes, in two ways. The first using System.Info.compilerVersion

import System.Info

main :: IO ()
main = print compilerVersion
-- prints "Version {versionBranch = [8,6], versionTags = []}"

Second via the CPP language extension:

{-# LANGUAGE CPP #-}

main :: IO ()
main = print __GLASGOW_HASKELL__
-- prints "806" (of type Integer)

Docs on how that number has been come up with.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cigarette Smoking Man