| Copyright | (c) Hangil Kim 2026 Kairon Johnson 2026 Leonidas Sallos 2026 Oscar Abelanet 2026 |
|---|---|
| License | MIT |
| Maintainer | kimhang@oregonstate.edu |
| Stability | experimental |
| Safe Haskell | Safe-Inferred |
| Language | Haskell2010 |
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
- class Superimposable a where
- (-->) :: a -> a -> LHsMakeError a
- class Identifiable a where
- type Identifier a
- identify :: a -> LHsMakeError (Identifier a)
- parseMode :: ParseMode
- data PragmaId
- data DeclId
- data DirectiveId
- flattenPragmas :: ModulePragma () -> [ModulePragma ()]
- flattenDecls :: Decl () -> [Decl ()]
- flattenTokens :: [Directive String] -> [Directive String]
- precs :: Map String Int
- groupPrecs :: [Directive String] -> LHsMakeError [Directive String]
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
| Superimposable BookendedAbsSyn Source # | Happy grammars are superimposed by first superimposing the header and footer as a Haskell module. Then, each rule is superimposed. |
Defined in LHMake.Control.Superimposition Methods (-->) :: BookendedAbsSyn -> BookendedAbsSyn -> LHsMakeError BookendedAbsSyn Source # | |
| Superimposable (AbsSyn String) Source # | Directives are first flattened (see |
Defined in LHMake.Control.Superimposition | |
| Superimposable (Directive String) Source # |
|
Defined in LHMake.Control.Superimposition | |
| Superimposable (Prod String) Source # | |
Defined in LHMake.Control.Superimposition | |
| Superimposable (Rule String) Source # | |
Defined in LHMake.Control.Superimposition | |
| Superimposable (CName ()) Source # | |
Defined in LHMake.Control.Superimposition | |
| Superimposable (Decl ()) Source # | Datatype declarations, functions, and typeclass instance declarations are merged.
Type synonyms, |
Defined in LHMake.Control.Superimposition | |
| Superimposable (Deriving ()) Source # | A |
Defined in LHMake.Control.Superimposition | |
| Superimposable (ExportSpec ()) Source # | |
Defined in LHMake.Control.Superimposition Methods (-->) :: ExportSpec () -> ExportSpec () -> LHsMakeError (ExportSpec ()) Source # | |
| Superimposable (ExportSpecList ()) Source # | |
Defined in LHMake.Control.Superimposition Methods (-->) :: ExportSpecList () -> ExportSpecList () -> LHsMakeError (ExportSpecList ()) Source # | |
| Superimposable (ImportDecl ()) Source # | |
Defined in LHMake.Control.Superimposition Methods (-->) :: ImportDecl () -> ImportDecl () -> LHsMakeError (ImportDecl ()) Source # | |
| Superimposable (ImportSpec ()) Source # | |
Defined in LHMake.Control.Superimposition Methods (-->) :: ImportSpec () -> ImportSpec () -> LHsMakeError (ImportSpec ()) Source # | |
| Superimposable (ImportSpecList ()) Source # | |
Defined in LHMake.Control.Superimposition Methods (-->) :: ImportSpecList () -> ImportSpecList () -> LHsMakeError (ImportSpecList ()) Source # | |
| Superimposable (InstDecl ()) Source # | |
Defined in LHMake.Control.Superimposition | |
| Superimposable (InstRule ()) Source # | |
Defined in LHMake.Control.Superimposition | |
| Superimposable (Match ()) Source # | |
Defined in LHMake.Control.Superimposition | |
| 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 |
Defined in LHMake.Control.Superimposition | |
| 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. |
Defined in LHMake.Control.Superimposition Methods (-->) :: ModuleHead () -> ModuleHead () -> LHsMakeError (ModuleHead ()) Source # | |
| Superimposable (ModulePragma ()) Source # | |
Defined in LHMake.Control.Superimposition Methods (-->) :: ModulePragma () -> ModulePragma () -> LHsMakeError (ModulePragma ()) Source # | |
| Superimposable (QualConDecl ()) Source # | |
Defined in LHMake.Control.Superimposition Methods (-->) :: QualConDecl () -> QualConDecl () -> LHsMakeError (QualConDecl ()) Source # | |
| (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. |
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
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
Identifiers for module pragmas.
Identifiers for Haskell declarations.
Only these declarations can be merged. Everything else (classes, default
declarations, foreign declarations, pattern bindings) is rejected by identify.
data DirectiveId Source #
Identifiers for Happy directives.
Constructors
| Token String | A |
| Name String | The |
| Attribute String | The |
| Prec String | A |
| Other String | Any other directive. |
Instances
| Eq DirectiveId Source # | |
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
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.