Home > Blockchain >  Source-level definition of `seq` in Haskell
Source-level definition of `seq` in Haskell

Time:01-28

I'm trying to understand seq in Haskell. I've got some understanding of seq in English, so now I'd like to read its implementation.

However, the source code says

infixr 0 `seq`
seq :: a -> b -> b
seq = seq

How can I read this? It this an infinitely recursive definition? I suspect it's not, though.

CodePudding user response:

seq cannot be implemented in plain Haskell. That is a "dummy" definition. It is "replaced" with the actual seq behavior by GHC. So the real implementation (so to speak) of seq is inside the compiler.

To put it another way: If we ignore the name of the function in that code snippet, that definition has no relationship to seq. The compiler sees the name "seq" (and maybe the fact that it's defined in that specific module; I'm not sure about the details) and that tells it to replace the definition with the actual seq behavior during compilation.

CodePudding user response:

The file you linked starts with

{- This is a generated file (generated by genprimopcode). It is not code to actually be used. Its only purpose is to be consumed by haddock. -}

hence it is only generated so that it can in turn cause documentation to be generated.

Every single definition in the file is given by a dummy recursion x = x. That is not the real definition, but only a placeholder.

Haskell's primitives in GHC.Prim like seq can not be simply implemented in a Haskell module (otherwise they would not be called primitives). Instead, they are handled in a special way during compilation.

  • Related