Portability | portable |
---|---|
Stability | experimental |
Maintainer | Joey Eremondi <jmitdase@gmail.com> |
Safe Haskell | None |
The given functions can be used to convert Haskell source code into Elm source code.
Example usage:
elmSource = $(translateToElm defaultOptions "path/to/myFile.hs")
Here, elmString1
will be a String variable which you can use in your Haskell code.
Note that the Haskell functions in the file you give are not imported.
If you would like to use them, you must import them the normal way.
Haskelm can currently translate most basic Haskell, including functions, algebraic data types, newtypes, and type synonyms. Support is now in place for records, guarded-function-bodies, list-ranges, where-declarations, as-patterns, and multi-clause function definitions (pattern matching).
Translation of class or instance declarations is not supported, and will not likely be supported in the near future, as Elm does not support Type classes. However, if your Haskell code contains Class or Instance declarations, they will simply be ignored by Haskelm.
Most GHC extensions are unsupported, with the exception of Multi-Way-If statements, since they have a direct translation into Elm.
If JSON deriving is enabled, in addition to translating Haskell functions and types,
Elm functions will be generated to transform data to and from the JSON format.
This follows the format used by Data.Aeson.TH, so you can automatically derive your Haskell JSON definitions.
For a type FOO, the functions toJson_FOO
and fromJson_FOO
will be added to the Elm code
Returning and taking values of type Json.JsonValue respectively.
The module contains instances of ToJSON and FromJSON for Map
which match the format used by Elm's JsonUtils
If you use the JSON functionality, the generated Elm code will depend on the JsonUtils
library,
which can be obtained from http://library.elm-lang.org.
- translateToElm :: TranslateOptions -> FilePath -> ExpQ
- data TranslateOptions = Options {
- makeJson :: Bool
- qualifiedImports :: [String]
- openImports :: [String]
- moduleName :: String
- class ToJSON a where
- class FromJSON a where
- defaultOptions :: TranslateOptions
Documentation
translateToElm :: TranslateOptions -> FilePath -> ExpQSource
Given options for translation, and the file path of a Haskell module, generate the String literal which is the corresponding Elm source code. This must be invoked using Template Haskell For example:
elmSource = $(translateToElm defaultOptions "path/to/myFile.hs")
data TranslateOptions Source
Options for how to generate Elm source code
Options | |
|
class ToJSON a where
A type that can be converted to JSON.
An example type and instance:
{-# LANGUAGE OverloadedStrings #-} data Coord = Coord { x :: Double, y :: Double } instance ToJSON Coord where toJSON (Coord x y) =object
["x".=
x, "y".=
y]
Note the use of the OverloadedStrings
language extension which enables
Text
values to be written as string literals.
Instead of manually writing your ToJSON
instance, there are three options
to do it automatically:
- Data.Aeson.TH provides template-haskell functions which will derive an instance at compile-time. The generated instance is optimized for your type so will probably be more efficient than the following two options:
- Data.Aeson.Generic provides a generic
toJSON
function that accepts any type which is an instance ofData
. - If your compiler has support for the
DeriveGeneric
andDefaultSignatures
language extensions (GHC 7.2 and newer),toJSON
will have a default generic implementation.
To use the latter option, simply add a deriving
clause to your
datatype and declare a Generic
ToJSON
instance for your datatype without giving a
definition for toJSON
.
For example the previous example can be simplified to just:
{-# LANGUAGE DeriveGeneric #-} import GHC.Generics data Coord = Coord { x :: Double, y :: Double } deriving Generic instance ToJSON Coord
Note that, instead of using DefaultSignatures
, it's also possible
to parameterize the generic encoding using genericToJSON
applied
to your encoding/decoding Options
:
instance ToJSON Coord where toJSON =genericToJSON
defaultOptions
class FromJSON a where
A type that can be converted from JSON, with the possibility of failure.
When writing an instance, use empty
, mzero
, or fail
to make a
conversion fail, e.g. if an Object
is missing a required key, or
the value is of the wrong type.
An example type and instance:
{-# LANGUAGE OverloadedStrings #-} data Coord = Coord { x :: Double, y :: Double } instance FromJSON Coord where parseJSON (Object
v) = Coord<$>
v.:
"x"<*>
v.:
"y" -- A non-Object
value is of the wrong type, so usemzero
to fail. parseJSON _ =mzero
Note the use of the OverloadedStrings
language extension which enables
Text
values to be written as string literals.
Instead of manually writing your FromJSON
instance, there are three options
to do it automatically:
- Data.Aeson.TH provides template-haskell functions which will derive an instance at compile-time. The generated instance is optimized for your type so will probably be more efficient than the following two options:
- Data.Aeson.Generic provides a generic
fromJSON
function that parses to any type which is an instance ofData
. - If your compiler has support for the
DeriveGeneric
andDefaultSignatures
language extensions,parseJSON
will have a default generic implementation.
To use this, simply add a deriving
clause to your datatype and
declare a Generic
FromJSON
instance for your datatype without giving a definition
for parseJSON
.
For example the previous example can be simplified to just:
{-# LANGUAGE DeriveGeneric #-} import GHC.Generics data Coord = Coord { x :: Double, y :: Double } deriving Generic instance FromJSON Coord
Note that, instead of using DefaultSignatures
, it's also possible
to parameterize the generic decoding using genericParseJSON
applied
to your encoding/decoding Options
:
instance FromJSON Coord where parseJSON =genericParseJSON
defaultOptions
defaultOptions :: TranslateOptionsSource
Default options for translation:
Generates toJson
and fromJson
functions,
has no open or qualified imports, and has
module name Main
.