LayeredHaskell-make-0.1.0.0: Superimposition of Happy and Haskell program fragments.
Copyright(c) Hangil Kim 2026
Kairon Johnson 2026
Leonidas Sallos 2026
Oscar Abelanet 2026
LicenseMIT
Maintainerkimhang@oregonstate.edu
Stabilityexperimental
Safe HaskellSafe-Inferred
LanguageHaskell2010

LHMake.Control.Superimposition

Description

This module provides a superimposition operator (-->) on Haskell and Happy abstract syntax trees. The following example illustrates its use on two Haskell files.

-- A.hs
module LH.Feature.A.Basic where

data Expr = Lit Int

eval :: Expr -> Int
eval (Lit n) = n

-- B.hs
module LH.Feature.B.Basic where

data Expr = Add Expr Expr

eval :: Expr -> Int
eval (Add e1 e2) = eval e1 + eval e2

-- B.hs --> A.hs
module LH.Feature.BA.Basic where

data Expr = Lit Int | Add Expr Expr

eval :: Expr -> Int
eval (Lit n) = n
eval (Add e1 e2) = eval e1 + eval e2
Synopsis

Superimposition

class Superimposable a where Source #

A Superimposable type allows merging of two values \(L\) and \(R\), which range over anything from Haskell modules to Happy productions. \(L\) and \(R\) are assumed to have the same Identifier, as defined by the Identifiable typeclass.

By default, \(L\) overrides the contents of \(R\). Specific instances replace this behavior with a merging semantics.

Minimal complete definition

Nothing

Methods

(-->) :: a -> a -> LHsMakeError a infixl 6 Source #

Superimposes two values.

Note that superimposition can fail for various reasons, such as when differing module names are encountered.

Instances

Instances details
Superimposable BookendedAbsSyn Source #

Happy grammars are superimposed by first superimposing the header and footer as a Haskell module. Then, each rule is superimposed.

Instance details

Defined in LHMake.Control.Superimposition

Methods

(-->) :: BookendedAbsSyn -> BookendedAbsSyn -> LHsMakeError BookendedAbsSyn Source #

Superimposable (AbsSyn String) Source #

Directives are first flattened (see flattenTokens), then merged as much as possible. Precedence directives are merged with respect to numeric precedences (see precs).

Instance details

Defined in LHMake.Control.Superimposition

Methods

(-->) :: AbsSyn String -> AbsSyn String -> LHsMakeError (AbsSyn String) Source #

Superimposable (Directive String) Source #

%token directives are merged and %name directives are overridden from the left. Other differing directives are simply rejected.

Instance details

Defined in LHMake.Control.Superimposition

Methods

(-->) :: Directive String -> Directive String -> LHsMakeError (Directive String) Source #

Superimposable (Prod String) Source # 
Instance details

Defined in LHMake.Control.Superimposition

Methods

(-->) :: Prod String -> Prod String -> LHsMakeError (Prod String) Source #

Superimposable (Rule String) Source # 
Instance details

Defined in LHMake.Control.Superimposition

Methods

(-->) :: Rule String -> Rule String -> LHsMakeError (Rule String) Source #

Superimposable (CName ()) Source # 
Instance details

Defined in LHMake.Control.Superimposition

Methods

(-->) :: CName () -> CName () -> LHsMakeError (CName ()) Source #

Superimposable (Decl ()) Source #

Datatype declarations, functions, and typeclass instance declarations are merged. Type synonyms, newtypes, type signatures, and fixity declarations are overridden from the left.

Instance details

Defined in LHMake.Control.Superimposition

Methods

(-->) :: Decl () -> Decl () -> LHsMakeError (Decl ()) Source #

Superimposable (Deriving ()) Source #

A deriving clause is identified by its strategy (e.g., DerivStock). Clauses with the same strategy are merged, and the classes they derive are merged by their names.

Instance details

Defined in LHMake.Control.Superimposition

Methods

(-->) :: Deriving () -> Deriving () -> LHsMakeError (Deriving ()) Source #

Superimposable (ExportSpec ()) Source # 
Instance details

Defined in LHMake.Control.Superimposition

Superimposable (ExportSpecList ()) Source # 
Instance details

Defined in LHMake.Control.Superimposition

Superimposable (ImportDecl ()) Source # 
Instance details

Defined in LHMake.Control.Superimposition

Superimposable (ImportSpec ()) Source # 
Instance details

Defined in LHMake.Control.Superimposition

Superimposable (ImportSpecList ()) Source # 
Instance details

Defined in LHMake.Control.Superimposition

Superimposable (InstDecl ()) Source # 
Instance details

Defined in LHMake.Control.Superimposition

Methods

(-->) :: InstDecl () -> InstDecl () -> LHsMakeError (InstDecl ()) Source #

Superimposable (InstRule ()) Source # 
Instance details

Defined in LHMake.Control.Superimposition

Methods

(-->) :: InstRule () -> InstRule () -> LHsMakeError (InstRule ()) Source #

Superimposable (Match ()) Source # 
Instance details

Defined in LHMake.Control.Superimposition

Methods

(-->) :: Match () -> Match () -> LHsMakeError (Match ()) Source #

Superimposable (Module ()) Source #

To superimpose two Haskell modules, they must have the same name. Then, their pragmas, exports, and imports are merged. Pragmas and declarations are flattened before they are merged. See flattenPragmas and flattenDecls.

Instance details

Defined in LHMake.Control.Superimposition

Methods

(-->) :: Module () -> Module () -> LHsMakeError (Module ()) Source #

Superimposable (ModuleHead ()) Source #

A module header holds the module name and its exports. The names must be the same, and then the exports are merged.

Instance details

Defined in LHMake.Control.Superimposition

Superimposable (ModulePragma ()) Source # 
Instance details

Defined in LHMake.Control.Superimposition

Superimposable (QualConDecl ()) Source # 
Instance details

Defined in LHMake.Control.Superimposition

(Identifiable a, Eq (Identifier a), Superimposable a) => Superimposable [a] Source #

Lists are superimposed element by element. Elements with the same identifier are superimposed, and elements that only the right list has are added at the end.

Instance details

Defined in LHMake.Control.Superimposition

Methods

(-->) :: [a] -> [a] -> LHsMakeError [a] Source #

class Identifiable a where Source #

An Identifiable type a has an Identifier, which is a value of some type identifying a value of type a. Identifier is defined on a case-by-case basis, since a type a might need a different identification mechanism than another type b. The identify function then takes a value and produces its Identifier. This allows us to decide whether two values can be merged in a general way.

Associated Types

type Identifier a Source #

Methods

identify :: a -> LHsMakeError (Identifier a) Source #

Identifies a value.

Note that identification can fail for various reasons, most notably when superimposition has not been defined for a given language construct out of a lack of necessity.

Instances

Instances details
Identifiable (Directive String) Source # 
Instance details

Defined in LHMake.Control.Superimposition

Associated Types

type Identifier (Directive String) Source #

Methods

identify :: Directive String -> LHsMakeError (Identifier (Directive String)) Source #

Identifiable (Prod String) Source #

A production is identified by its RHS.

Instance details

Defined in LHMake.Control.Superimposition

Associated Types

type Identifier (Prod String) Source #

Methods

identify :: Prod String -> LHsMakeError (Identifier (Prod String)) Source #

Identifiable (Rule String) Source # 
Instance details

Defined in LHMake.Control.Superimposition

Associated Types

type Identifier (Rule String) Source #

Methods

identify :: Rule String -> LHsMakeError (Identifier (Rule String)) Source #

Identifiable (CName ()) Source # 
Instance details

Defined in LHMake.Control.Superimposition

Associated Types

type Identifier (CName ()) Source #

Identifiable (Decl ()) Source #

Only a subset of Haskell declarations have been given superimposition definitions. These are as follows.

  • Type synonyms
  • Datatype declarations
  • Newtype declarations
  • Function bindings
  • Type signatures
  • Fixity declarations
  • Typeclass instance declarations.

See DeclId.

Instance details

Defined in LHMake.Control.Superimposition

Associated Types

type Identifier (Decl ()) Source #

Identifiable (Deriving ()) Source # 
Instance details

Defined in LHMake.Control.Superimposition

Associated Types

type Identifier (Deriving ()) Source #

Identifiable (ExportSpec ()) Source # 
Instance details

Defined in LHMake.Control.Superimposition

Associated Types

type Identifier (ExportSpec ()) Source #

Identifiable (ImportDecl ()) Source # 
Instance details

Defined in LHMake.Control.Superimposition

Associated Types

type Identifier (ImportDecl ()) Source #

Identifiable (ImportSpec ()) Source # 
Instance details

Defined in LHMake.Control.Superimposition

Associated Types

type Identifier (ImportSpec ()) Source #

Identifiable (InstDecl ()) Source # 
Instance details

Defined in LHMake.Control.Superimposition

Associated Types

type Identifier (InstDecl ()) Source #

Identifiable (InstRule ()) Source # 
Instance details

Defined in LHMake.Control.Superimposition

Associated Types

type Identifier (InstRule ()) Source #

Identifiable (Match ()) Source #

A function is identified by its patterns.

Instance details

Defined in LHMake.Control.Superimposition

Associated Types

type Identifier (Match ()) Source #

Identifiable (ModulePragma ()) Source # 
Instance details

Defined in LHMake.Control.Superimposition

Associated Types

type Identifier (ModulePragma ()) Source #

Identifiable (QualConDecl ()) Source #

A constructor is identified by its name.

Instance details

Defined in LHMake.Control.Superimposition

Associated Types

type Identifier (QualConDecl ()) Source #

Parsing

parseMode :: ParseMode Source #

defaultParseMode but with fixities set to Nothing.

Operator precedence resolution is disabled since superimposition does not merge expressions (e.g., RHSs of function clauses).

Identifiers

data PragmaId Source #

Identifiers for module pragmas.

Constructors

Extension (Name ())

A LANGUAGE pragma.

Options (Maybe Tool) [String]

An OPTIONS pragma, such as OPTIONS_GHC.

Annotated (ModulePragma ())

An ANN pragma, identified by its contents.

Instances

Instances details
Eq PragmaId Source # 
Instance details

Defined in LHMake.Control.Superimposition

data DeclId Source #

Identifiers for Haskell declarations.

Only these declarations can be merged. Everything else (classes, default declarations, foreign declarations, pattern bindings) is rejected by identify.

Constructors

Value (Name ())

Functions and variables.

Sig (Name ())

Type signatures.

Type (Name ())

Type synonyms, datatype declarations, and newtypes.

Fixity (Name ())

Fixity declarations.

Instance (InstHead ())

Typeclass instance declarations.

Instances

Instances details
Eq DeclId Source # 
Instance details

Defined in LHMake.Control.Superimposition

Methods

(==) :: DeclId -> DeclId -> Bool #

(/=) :: DeclId -> DeclId -> Bool #

data DirectiveId Source #

Identifiers for Happy directives.

Constructors

Token String

A %token directive, one per token.

Name String

The %name directive.

Attribute String

The %attribute directive.

Prec String

A %left, %right, or %nonassoc directive, one per token.

Other String

Any other directive.

Instances

Instances details
Eq DirectiveId Source # 
Instance details

Defined in LHMake.Control.Superimposition

Flattening lists

flattenPragmas :: ModulePragma () -> [ModulePragma ()] Source #

Splits a LANGUAGE pragma holding several extensions into one pragma per extension.

For instance, {-# LANGUAGE A, B #-} is flattened into {-# LANGUAGE A #-} and {-# LANGUAGE B #-}. All other pragmas are left as is.

flattenDecls :: Decl () -> [Decl ()] Source #

Splits a declaration holding several names into one declaration per name. Variable bindings (such as x = e) are also rewritten as function bindings, since they get parsed as a pattern binding.

For instance, the Haskell type signature f g :: A is flattened into f :: A and g :: A.

Note that pragmas attached to declarations (such as WARNING) are dropped by flattenDecls.

flattenTokens :: [Directive String] -> [Directive String] Source #

Splits x a b into x a and x b, where x ranges over the %token, %left, %right, and %nonassoc directives. All other directives are left as is.

Token precedences

precs :: Map String Int Source #

Precedences of tokens, where a higher number binds tighter than a lower one. The (relative) precedences of tokens corresponding to operators borrow directly from Haskell. Operator associativity is defined within the parser files themselves.

Note that the precedence resolution mechanism here is slightly weaker than precedence climbing. In Happy files, tokens with the same precedence but different associativity cannot share a precedence level. Consequently, superimposition must order such tokens sequentially in precedence lists. As a result, Layered Haskell accepts expressions that GHC would normally reject, like x . y !! z.

Precedences

Expand

Tokens where superimposition diverges from GHC are marked with a !. There is currently only one such precedence level.

LAM                0
'in'               1
'else'             2
'$'                3
'||'               4
'&&'               5
'==' '/='          6
'<' '>' '<=' '>='  7
':' '++'           8
'+' '-'            9
'*'               10
NEG               11
'.' '!!'          12 !

groupPrecs :: [Directive String] -> LHsMakeError [Directive String] Source #

Groups precedence directives by the (numeric) precedence and associativity of the involved tokens. If a token with an unknown precedence is encountered, an error is emitted.

Note that the numeric precedences come from precs, while the associativities come from the parser files themselves. Consequently, the order in which token precedence is specified within the parser files is ignored, since groupPrecs sorts tokens into the expected order anyway.