haskelm-0.1.12.0: Elm to Haskell translation

Portabilityportable
Stabilityexperimental
MaintainerJoey Eremondi <jmitdase@gmail.com>
Safe HaskellNone

Language.Elm.TH

Description

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.

Synopsis

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

Constructors

Options 

Fields

makeJson :: Bool

When true, generates toJson and fromJson for translated type declarations. The format used by the Json is the same as the one used by Data.Aeson.TH. This is handy for passing data between a Haskell server and an Elm client.

qualifiedImports :: [String]

Each module name given will be imported in Elm by `import Module`

openImports :: [String]

Each module name given will be imported in Elm by `import Module (..)`

moduleName :: String

The name of the elm module generated. i.e. prepends `module ModuleName` to the generated Elm source.

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 of Data.
  • If your compiler has support for the DeriveGeneric and DefaultSignatures language extensions (GHC 7.2 and newer), toJSON will have a default generic implementation.

To use the latter option, simply add a deriving Generic clause to your datatype and declare a 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

Methods

toJSON :: a -> Value

Instances

ToJSON Bool 
ToJSON Char 
ToJSON Double 
ToJSON Float 
ToJSON Int 
ToJSON Int8 
ToJSON Int16 
ToJSON Int32 
ToJSON Int64 
ToJSON Integer 
ToJSON Word 
ToJSON Word8 
ToJSON Word16 
ToJSON Word32 
ToJSON Word64 
ToJSON () 
ToJSON Scientific 
ToJSON Number 
ToJSON Text 
ToJSON UTCTime 
ToJSON Value 
ToJSON DotNetTime 
ToJSON IntSet 
ToJSON Text 
ToJSON ZonedTime 
ToJSON [Char] 
ToJSON a => ToJSON [a] 
ToJSON (Ratio Integer) 
ToJSON a => ToJSON (Maybe a) 
HasResolution a => ToJSON (Fixed a) 
ToJSON a => ToJSON (Dual a) 
ToJSON a => ToJSON (First a) 
ToJSON a => ToJSON (Last a) 
ToJSON v => ToJSON (Tree v) 
ToJSON a => ToJSON (IntMap a) 
ToJSON a => ToJSON (Set a) 
ToJSON a => ToJSON (HashSet a) 
ToJSON a => ToJSON (Vector a) 
(Vector Vector a, ToJSON a) => ToJSON (Vector a) 
(Storable a, ToJSON a) => ToJSON (Vector a) 
(Prim a, ToJSON a) => ToJSON (Vector a) 
(ToJSON a, ToJSON b) => ToJSON (Either a b) 
(ToJSON a, ToJSON b) => ToJSON (a, b) 
ToJSON v => ToJSON (HashMap String v) 
ToJSON v => ToJSON (HashMap Text v) 
ToJSON v => ToJSON (HashMap Text v) 
ToJSON v => ToJSON (Map String v) 
(ToJSON a, ToJSON b, Ord a) => ToJSON (Map a b)

ToJSON instance for Data.Map which matches the format used by Elm's JsonUtils

ToJSON v => ToJSON (Map Text v) 
ToJSON v => ToJSON (Map Text v) 
(ToJSON a, ToJSON b, ToJSON c) => ToJSON (a, b, c) 
(ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON (a, b, c, d) 
(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) => ToJSON (a, b, c, d, e) 
(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) => ToJSON (a, b, c, d, e, f) 
(ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f, ToJSON g) => ToJSON (a, b, c, d, e, f, g) 

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 use mzero 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 of Data.
  • If your compiler has support for the DeriveGeneric and DefaultSignatures language extensions, parseJSON will have a default generic implementation.

To use this, simply add a deriving Generic clause to your datatype and declare a 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

Methods

parseJSON :: Value -> Parser a

Instances

FromJSON Bool 
FromJSON Char 
FromJSON Double 
FromJSON Float 
FromJSON Int 
FromJSON Int8 
FromJSON Int16 
FromJSON Int32 
FromJSON Int64 
FromJSON Integer 
FromJSON Word 
FromJSON Word8 
FromJSON Word16 
FromJSON Word32 
FromJSON Word64 
FromJSON () 
FromJSON Scientific 
FromJSON Number 
FromJSON Text 
FromJSON UTCTime 
FromJSON Value 
FromJSON DotNetTime 
FromJSON IntSet 
FromJSON Text 
FromJSON ZonedTime 
FromJSON [Char] 
FromJSON a => FromJSON [a] 
FromJSON (Ratio Integer) 
FromJSON a => FromJSON (Maybe a) 
HasResolution a => FromJSON (Fixed a) 
FromJSON a => FromJSON (Dual a) 
FromJSON a => FromJSON (First a) 
FromJSON a => FromJSON (Last a) 
FromJSON v => FromJSON (Tree v) 
FromJSON a => FromJSON (IntMap a) 
(Ord a, FromJSON a) => FromJSON (Set a) 
(Eq a, Hashable a, FromJSON a) => FromJSON (HashSet a) 
FromJSON a => FromJSON (Vector a) 
(Vector Vector a, FromJSON a) => FromJSON (Vector a) 
(Storable a, FromJSON a) => FromJSON (Vector a) 
(Prim a, FromJSON a) => FromJSON (Vector a) 
(FromJSON a, FromJSON b) => FromJSON (Either a b) 
(FromJSON a, FromJSON b) => FromJSON (a, b) 
FromJSON v => FromJSON (HashMap String v) 
FromJSON v => FromJSON (HashMap Text v) 
FromJSON v => FromJSON (HashMap Text v) 
FromJSON v => FromJSON (Map String v) 
(FromJSON a, FromJSON b, Ord a) => FromJSON (Map a b)

FromJSON instance for Data.Map which matches the format used by Elm's JsonUtils

FromJSON v => FromJSON (Map Text v) 
FromJSON v => FromJSON (Map Text v) 
(FromJSON a, FromJSON b, FromJSON c) => FromJSON (a, b, c) 
(FromJSON a, FromJSON b, FromJSON c, FromJSON d) => FromJSON (a, b, c, d) 
(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e) => FromJSON (a, b, c, d, e) 
(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f) => FromJSON (a, b, c, d, e, f) 
(FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e, FromJSON f, FromJSON g) => FromJSON (a, b, c, d, e, f, g) 

defaultOptions :: TranslateOptionsSource

Default options for translation: Generates toJson and fromJson functions, has no open or qualified imports, and has module name Main.