HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleghc-9.10.3GHC2021

GHC.JS.Make

  • Domain and Purpose

    GHC.JS.Make defines helper functions to ease the creation of JavaScript ASTs as defined in GHC.JS.Syntax. Its purpose is twofold: make the EDSL more ergonomic to program in, and make errors in the EDSL look obvious because the EDSL is untyped. It is primarily concerned with injecting terms into the domain of the EDSL to construct JS programs in Haskell.

  • Strategy

    The strategy for this module comes straight from gentzen; where we have two types of helper functions. Functions which inject terms into the EDSL, and combinator functions which operate on terms in the EDSL to construct new terms in the EDSL. Crucially, missing from this module are corresponding elimination or destructing functions which would project information from the EDSL back to Haskell. See GHC.StgToJS.Utils for such functions.

    • Introduction functions

      We define various primitive helpers which introduce terms in the EDSL, for example jVar, jLam, and var and jString. Similarly this module exports four typeclasses ToExpr, ToStat, JVarMagic, JSArgument. ToExpr injects values as a JS expression into the EDSL. ToStat injects values as JS statements into the EDSL. JVarMagic provides a polymorphic way to introduce a new name into the EDSL and JSArgument provides a polymorphic way to bind variable names for use in JS functions with different arities.

    • Combinator functions

      The rest of the module defines combinators which create terms in the EDSL from terms in the EDSL. Notable examples are |= and ||=, |= is sugar for AssignStat, it is a binding form that declares foo = bar assuming foo has been already declared. ||= is more sugar on top of |=, it is also a binding form that declares the LHS of |= before calling |= to bind a value, bar, to a variable foo. Other common examples are the if_ and math_ helpers such as math_cos.

  • Consumers

    The entire JS backend consumes this module, e.g., the modules in GHC.StgToJS.*.

  • Notation

    In this module we use ==> in docstrings to show the translation from the JS EDSL domain to JS code. For example, foo ||= bar ==> var foo; foo = bar; should be read as foo ||= bar is in the EDSL domain and results in the JS code var foo; foo = bar; when compiled.

    In most cases functions prefixed with a j are monadic because the observably allocate. Notable exceptions are jwhenS, jString and the helpers for HashMaps.

  • 1 type
  • 4 classes
  • 96 values
  • Packageghc-9.10.3
  • Exports102
  • LanguageGHC2021
  • LicenceBSD-3-Clause
  • SourceMake.hs

Injection Type classes

4 declarations

The ToJExpr class handles injection of of things into the EDSL as a JS expression

classclass ToJExpr a where
#

Things that can be marshalled into javascript values. Instantiate for any necessary data structures.

Methods

Instances25ToJExpr, …
classclass ToStat a where
#

The ToStat class handles injection of of things into the EDSL as a JS statement. This ends up being polymorphic sugar for JS blocks, see helper function expr2stat. Instantiate for any necessary data structures.

Methods

Instances4ToStat
classclass JVarMagic a where
#

Type class that generates fresh a's for the JS backend. You should almost never need to use this directly. Instead use JSArgument, for examples of how to employ these classes please see jVar, jFunction and call sites in the Rts.

Methods

Instances3JVarMagic
classclass JSArgument args where
#

Type class that finds the form of arguments required for a JS syntax object. This class gives us a single interface to generate variables for functions that have different arities. Thus with it, we can have only one jFunction which is polymorphic over its arity, instead of jFunction2, jFunction3 and so on.

Methods

Instances10JSArgument, …

Introduction functions

12 declarations
valuejLam :: JSArgument args => (args -> JSM JStgStat) -> JSM JStgExpr
#

Create a new anonymous function. The result is a JExpr expression. Usage:

jLam $ \x -> jVar x + one_
jLam $ \f -> (jLam $ \x -> (f `app` (x `app` x))) `app` (jLam $ \x -> (f `app` (x `app` x)))
valuejLam' :: JStgStat -> JStgExpr
#

Special case of jLam where the anonymous function requires no fresh arguments.

valuejFunction
  1. :: JSArgument args
  2. => Ident

    global name

  3. -> (args -> JSM JStgStat)

    function body, input is locally unique generated variables

  4. -> JSM JStgStat
#

Construct a top-level function subject to JS hoisting. This combinator is polymorphic over function arity so you can you use to define a JS syntax object in Haskell, which is a function in JS that takes 2 or 4 or whatever arguments. For a singleton function use the Solo constructor MkSolo. Usage:

an example from the Rts that defines a 1-arity JS function > jFunction (global "h$getReg") ((MkSolo n) -> return $ SwitchStat n getRegCases mempty)

an example of a two argument function from the Rts > jFunction (global "h$bh_lne") ((x, frameSize) -> bhLneStats s x frameSize)

valuejFunctionSized
  1. :: Ident

    global name

  2. -> Int

    Arity

  3. -> ([JStgExpr] -> JSM JStgStat)

    function body, input is locally unique generated variables

  4. -> JSM JStgStat
#

Construct a top-level function subject to JS hoisting. Special case where the arity cannot be deduced from the args parameter (atleast not without dependent types).

valuejFunction'
  1. :: Ident

    global name

  2. -> JSM JStgStat

    function body, input is locally unique generated variables

  3. -> JSM JStgStat
#

Construct a top-level function subject to JS hoisting. Special case where the function binds no parameters

valuejVar :: (JVarMagic t, ToJExpr t) => (t -> JSM JStgStat) -> JSM JStgStat
#

Introduce only one new variable into scope for the duration of the enclosed expression. The result is a block statement. Usage:

'jVar $ x -> mconcat [jVar x ||= one_, ...'

valuejVars :: JSArgument args => (args -> JSM JStgStat) -> JSM JStgStat
#

Introduce one or many new variables into scope for the duration of the enclosed expression. This function reifies the number of arguments based on the container of the input function. We intentionally avoid lists and instead opt for tuples because lists are not sized in general. The result is a block statement. Usage:

jVars $ (x,y) -> mconcat [ x |= one_,  y |= two_,  x + y]

Combinators

51 declarations

Combinators operate on terms in the JS EDSL domain to create new terms in the EDSL domain.

value(||=) :: Ident -> JStgExpr -> JStgStat
#

Declare a variable and then Assign the variable to an expression

foo |= expr ==> var foo; foo = expr;
valueif10 :: JStgExpr -> JStgExpr
#

if-expression that returns 1 if condition = true, 0 otherwise

if10 e ==> e ? 1 : 0
valueif01 :: JStgExpr -> JStgExpr
#

if-expression that returns 0 if condition = true, 1 otherwise

if01 e ==> e ? 0 : 1

Hash combinators

Literals

8 declarations

Literals in the JS EDSL are constants in the Haskell domain. These are useful helper values and never change

Math functions

Math functions in the EDSL are literals, with the exception of math_ which is the sole math introduction function.

Statement helpers

3 declarations
datadata Solo a
#

Solo is the canonical lifted 1-tuple, just like Tuple2 is the canonical lifted 2-tuple (pair) and Tuple3 is the canonical lifted 3-tuple (triple).

The most important feature of Solo is that it is possible to force its "outside" (usually by pattern matching) without forcing its "inside", because it is defined as a datatype rather than a newtype. One situation where this can be useful is when writing a function to extract a value from a data structure. Suppose you write an implementation of arrays and offer only this function to index into them:

index :: Array a -> Int -> a

Now imagine that someone wants to extract a value from an array and store it in a lazy-valued finite map/dictionary:

insert "hello" (arr index 12) m

This can actually lead to a space leak. The value is not actually extracted from the array until that value (now buried in a map) is forced. That means the entire array may be kept live by just that value! Often, the solution is to use a strict map, or to force the value before storing it, but for some purposes that's undesirable.

One common solution is to include an indexing function that can produce its result in an arbitrary Applicative context:

indexA :: Applicative f => Array a -> Int -> f a

When using indexA in a pure context, Solo serves as a handy Applicative functor to hold the result. You could write a non-leaky version of the above example thus:

case arr indexA 12 of
  Solo a -> insert "hello" a m

While such simple extraction functions are the most common uses for unary tuples, they can also be useful for fine-grained control of strict-spined data structure traversals, and for unifying the implementations of lazy and strict mapping functions.

Constructors

Instances29Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …
patternpattern Solo :: a -> (a)
#

Deprecated. The Solo constructor has been renamed to MkSolo to avoid punning.

Orphan instances

2 instances