Where is Prim defined?


#1

newspecies [12:16 PM]

-- | Adds import declarations to a module for an implicit Prim import and Prim
-- | qualified as Prim, as necessary.
importPrim :: Module -> Module
importPrim =
 let
  primModName = C.Prim
 in
  addDefaultImport (Qualified (Just primModName) primModName)
   . addDefaultImport (Qualified Nothing primModName)

newspecies [12:17 PM]
I am trying to understand purescript compiler and I am not able to understand where Prim module is coming from? Is it not there in the Purescript compiler and gets added when you build the code? Till now I have found this code. Can someone please explain how this works.

lunaris [12:45 PM]
@newspecies Shameless plug – my PR at https://github.com/purescript/purescript/pull/3351 might help with this. I doubt it’s the only one (the newest seems to involve Prim too) but it shows a few things:

src/Language/PureScript/Constants.hs – this module defines a lot of things, but one of them is the names of primitive modules and types. Importantly it exports primModules, which is a list of Prim and Prim.* modules which is used in various parts of the compiler to provide the Prim modules magically as you describe.
src/Language/PureScript/Docs/Prim.hs – this module defines documentation for Prim and Prim.* modules, since they don’t have “real” .purs files to which documentation can be added.
src/Language/PureScript/Environment.hs – this module is the meat in many ways; it shows how Prim modules and their members are added to the environment the compiler uses for type checking, name resolution etc. This environment population is also performed for every “real” module too, there are just some special pre-populations for Prim stuff.

In the case of my PR, I’m adding a module Prim.Coerce which exposes a class Coercible, so the things I’ve described above should relate to those things in a sensible way, hopefully. Hope that helps. (edited)

newspecies [2 days ago]
Will check it out. Thanks for the help :slightly_smiling_face: