Safe Haskell | None |
---|---|
Language | Haskell98 |
Core Public API (provisional)
Intended for constructing basic Core Programs. Use the binary serialization from Binary
to produce a core file, which can be compiled by UHC.
You will need to install the full UHC Compiler Suite in addition to uhc-light for this.
See https://github.com/UU-ComputerScience/uhc for more details.
A small example program can be found at https://github.com/UU-ComputerScience/uhc/tree/master/EHC/demo/CoreApi/ .
In general, it is also a good idea to see what kind of Core UHC generates for Haskell files. To do this,
call UHC with the option --coreopt=dump
. This will produce an additional XXX.tcr file which you can
read in any text editor. It may also be a good idea to add the NoGenericDeriving
pragma
to your haskell files, as this will make the produced Core code much smaller.
Restrictions:
- Extendable data types are not supported
- Generated code is not (type-)checked, might cause runtime crashes
- Core parsing/Pretty printing is incomplete and might be partially broken. The pretty printing should work good enough for dumping generated core code as debug output.
- Calling Haskell functions which use the haskell class system is not (yet?) supported.
- Avoiding name clashes is the responsibility of the user. The behaviour if duplicate names exists is undefined.
TODO:
- Should we add PatRest_Var? Does it actually work? (The HS frontend doesn't seem to use it?)
- Float, Double literals
- data CModule
- data CImport
- data CExport
- data CDeclMeta
- data CDataCon
- data CExpr
- data CBind
- data CAlt
- data CPat
- data CPatFld
- mkUnit :: EHCOpts -> CExpr
- mkInt :: EHCOpts -> Int -> CExpr
- mkInteger :: EHCOpts -> Integer -> CExpr
- mkChar :: Char -> CExpr
- mkString :: EHCOpts -> String -> CExpr
- mkError :: EHCOpts -> String -> CExpr
- mkUndefined :: EHCOpts -> CExpr
- mkVar :: HsName -> CExpr
- mkLet1Plain :: HsName -> CExpr -> CExpr -> CExpr
- mkLet1Strict :: HsName -> CExpr -> CExpr -> CExpr
- mkLetRec :: [CBind] -> CExpr -> CExpr
- mkLam :: [HsName] -> CExpr -> CExpr
- mkApp :: CExpr -> [CExpr] -> CExpr
- mkBind1 :: HsName -> CExpr -> CBind
- mkBind1Nm1 :: HsName -> CBind
- mkCTag :: HsName -> HsName -> Int -> Int -> CTag
- destructCTag :: a -> (HsName -> HsName -> Int -> Int -> a) -> CTag -> a
- ctagUnit :: CTag
- ctagTup :: CTag
- ctagTrue :: EHCOpts -> CTag
- ctagFalse :: EHCOpts -> CTag
- ctagCons :: EHCOpts -> CTag
- ctagNil :: EHCOpts -> CTag
- mkCase :: CExpr -> [CAlt] -> CExpr
- mkAlt :: CPat -> CExpr -> CAlt
- mkPatCon :: CTag -> CPatRest -> [CPatFld] -> CPat
- mkPatRestEmpty :: CPatRest
- mkPatFldBind :: (HsName, CExpr) -> CBind -> CPatFld
- mkTagTup :: CTag -> [CExpr] -> CExpr
- mkModule :: HsName -> [CExport] -> [CImport] -> [CDeclMeta] -> CExpr -> CModule
- mkImport :: HsName -> CImport
- mkExport :: HsName -> CExport
- mkExportData :: HsName -> Maybe [HsName] -> CExport
- mkMetaData :: HsName -> [CDataCon] -> CDeclMeta
- mkMetaDataCon :: HsName -> Int -> Int -> CDataCon
- mkMetaDataConFromCTag :: CTag -> Maybe CDataCon
- mkMain :: HsName -> CExpr
- parseExpr :: EHCOpts -> String -> Either [String] CExpr
- printModule :: EHCOpts -> CModule -> PP_Doc
- module UHC.Light.Compiler.Base.API
Core AST
The datatypes making up a Core program.
Construction functions
Constants
Creates a string expression. The expression represents a packed String, which can be passed to Haskell generated Core functions.
Generates an error expression, failing with the given string when evaluated. (error
in haskell)
mkUndefined :: EHCOpts -> CExpr Source
Generates an undefined expression, failing when evaluated. (undefined
in haskell)
Variables
Let Bindings
Creates a (non-recursive) let binding.
:: HsName | The identifer. |
-> CExpr | The expression to bind. Will be evaluated to WHNF, before the body is evaluated. |
-> CExpr | The body. |
-> CExpr |
Creates a let binding, which is strict in the bound expression.
Creates a let binding, where the bindings may be mutually recursive.
Abstraction
Application
Applies the first expression to all given arguments.
Binds/Bounds
mkBind1Nm1 :: HsName -> CBind Source
Constructor tags
:: HsName | Fully qualified Datatype name. |
-> HsName | Fully qualified Constructor name. |
-> Int | Tag number. |
-> Int | Arity. |
-> CTag |
Creates a constructor tag.
:: a | Algebra for record/tuple case. |
-> (HsName -> HsName -> Int -> Int -> a) | Algebra for datatype case. Order of arguments is the same as in |
-> CTag | |
-> a |
Destruct a CTag
.
Case
Scrutinizes an expression and executes the appropriate alternative. The scrutinee of a case statement is required to be in WHNF (weak head normal form).
A Case expression. The alternatives must be exhaustive, they must cover all possible constructors.
:: CPat | The pattern with which to match the case scrutinee. |
-> CExpr | The value of this alternative. |
-> CAlt |
Creates an alternative of a case statement.
Matches the case scrutinee with the given constructor tag.
mkPatRestEmpty :: CPatRest Source
The whole case scrutinee has already been matched on. There is nothing left. (If there is still something left, runtime behaviour is undefined)
Datatypes
mkTagTup :: CTag -> [CExpr] -> CExpr Source
Creates a new tuple/record with the given values. Has to be fully applied, partial application is not allowed.
Module
:: HsName | The name of the module. |
-> [CExport] | The exports. |
-> [CImport] | The imports (only direct imports, not transitive ones). |
-> [CDeclMeta] | The meta information. |
-> CExpr | The body of the module. |
-> CModule |
Creates a module.
mkExport :: HsName -> CExport Source
Create a plain export. Exported names are unqualified, implicitly qualified by module name.
Create an export for data. Exported names are unqualified, implicitly qualified by module name. Constructor name exports may not overlap with plain exported names.
Creates the metadata for one datatype.
:: HsName | The fully qualified name of the constructor. |
-> Int | The tag of this constructor. |
-> Int | The arity of this constructor. |
-> CDataCon |
Creates the metadata for one constructor.
Utilities
Creates the main entry point, calling the given function when run. The given function to call has to be in scope (either define it in the same module, or import it). In addition, the module UHC.Run has to be imported!
parseExpr :: EHCOpts -> String -> Either [String] CExpr Source
Parses an expression. The parser is not complete and may fail for complicated core code. For small fragments it should work.
printModule :: EHCOpts -> CModule -> PP_Doc Source
Re-exports (or not???)
module UHC.Light.Compiler.Base.API