MyNixOS website logo
Description

Generate ELM types/encoders/decoders from Haskell types.

Please see the README on GitHub at https://github.com/sras/elminator#readme

Elminator Build Status

Generate Elm type definitions and JSON encoders/decoders from Haskell source (for Elm 0.19 and 0.18)

  1. Supports generation of polymorphic types (as well as concrete ones) in Elm from possibly polymorphic Haskell types, including types with phantom type variables.
  2. Supports generation of recursively defined types.
  3. Generates code that does not depend on external Elm libraries.
  4. Does not have limits on the number of fields that the constructors of your type can have.
  5. Supports JSON encoding options exported by the Aeson library comprehensively. The tests in TravisCI exhaustively check the Elm/Haskell round tripping of values for all possible configurations of Aeson.options
  6. Supports generation of code that depend on user defined types and encoders/decoders in Elm.

Hackage page: https://hackage.haskell.org/package/elminator

How to use?

To generate Elm code for a Haskell type, the Haskell type needs to have an instance of the ToHType type class. This can be automatically derived, provided all your constructor field types have ToHType instances. A sample can be seen below. Please note that language extensions DeriveGeneric and DeriveAnyClass should be enabled to make this work.

{-# Language DeriveGeneric #-}
{-# Language DeriveAnyClass #-}

module Lib  where

import Elminator
import GHC.Generics (Generic)

data SingleCon = SingleCon Int String deriving (Generic, ToHType)

Since this library uses template Haskell to look up type information (in addition to Generics), we need to run the code generation code in a template Haskell splice. A usage sample can be seen in the following code used in the round trip tests for this library.

{-# Language OverloadedStrings #-}
{-# Language TemplateHaskell #-}

module CodeGen where

import Data.Proxy
import Elminator
import Data.Text.IO
import Data.Text

import Lib

elmSource :: Text
elmSource =
  $(generateFor Elm0p19 myDefaultOptions "Autogen" (Just "./elm-app/src/Autogen.elm") $ do
      include (Proxy :: Proxy SingleCon) $ Everything Mono
      include (Proxy :: Proxy SingleRecCon) $ Everything Mono
      include (Proxy :: Proxy SingleConOneField) $ Everything Mono
      include (Proxy :: Proxy SingleRecConOneField) $ Everything Mono
      include (Proxy :: Proxy TwoCons) $ Everything Mono
      include (Proxy :: Proxy TwoRecCons) $ Everything Mono
      include (Proxy :: Proxy BigCon) $ Everything Mono
      include (Proxy :: Proxy BigRecCon) $ Everything Mono
      include (Proxy :: Proxy MixedCons) $ Everything Mono
      include (Proxy :: Proxy Comment) $ Everything Mono
      include (Proxy :: Proxy WithMaybes) $ Everything Mono
      include (Proxy :: Proxy WithSimpleMaybes) $ Everything Mono
      include (Proxy :: Proxy (WithMaybesPoly (Maybe String) Float)) $
        Definiton Poly
      include
        (Proxy :: Proxy (WithMaybesPoly (Maybe String) Float))
        EncoderDecoder
      include (Proxy :: Proxy (Phantom ())) $ Everything Poly
      include (Proxy :: Proxy (TypeWithPhantom Float)) $ Everything Poly
      include (Proxy :: Proxy RecWithList) $ Everything Mono
      include (Proxy :: Proxy IndRecStart) $ Everything Mono
      include (Proxy :: Proxy IndRec2) $ Everything Mono
      include (Proxy :: Proxy IndRec3) $ Everything Mono
      include (Proxy :: Proxy NTSingleCon) $ Everything Mono
      include (Proxy :: Proxy NTSingleCon2) $ Everything Poly
      include (Proxy :: Proxy Tuples) $ Everything Mono
      include (Proxy :: Proxy NestedTuples) $ Everything Mono
      include (Proxy :: Proxy (NestedTuplesPoly ())) $ Definiton Poly
      include (Proxy :: Proxy (TypeWithExt ())) $ Everything Poly
      include (Proxy :: Proxy (WithEmptyTuple ())) $ Everything Poly
      include (Proxy :: Proxy (Phantom2 ())) $ Everything Poly
      include (Proxy :: Proxy PhantomWrapper) $ Everything Poly)

-- The `generateFor` function accepts an elm version (Elm0p19 or Elm0p18), a value of type `Options` from the Aeson library
-- , a module name for the generated module, and an optional `FilePath` to which the generated source will be written to, and a `Builder` value.
-- The `Builder` is just a `State` monad that aggregates the configuration parameters from the include
-- calls. The first parameter of the include function is a `proxy` value that denotes the type that requires Elm code generation.
-- The second value is a value of type `GenOption` that selects which entities needs to be generation, and also selects if the
-- type generated at Elm should be polymorphic. It is defined as follows.

data GenOption
  = Definiton PolyConfig  -- Generate Type definition in Elm. PolyConfig field decides if the type has to be polymorphic
  | EncoderDecoder -- Generate Encoder and Decoder in Elm
  | Everything PolyConfig -- Generate both type definition, encoders and decoders. PolyConfig field decides if the type has to be polymorphic.

data PolyConfig
  = Mono | Poly 

A sample of generated Elm code can be seen here

How to explicitly map a Haskell type to an Elm type

Say you have this type defined in Haskell

  data Product = Product { pName :: String, pWeight :: Decimal }

We can derive ToHType for the above type just fine. This is because we have this general ToHType instance that use the Typeable instances to create primitive type representation.

instance {-# OVERLAPPABLE #-} (Typeable a) => ToHType a where
  toHType p = pure $ mkHType p

Even though we are able to derive HType instance, the generated code end up looking something like the following

type Product = Product { pName : String, pWeight : DecimalRaw } 

encodeProduct : Product  -> E.Value
encodeProduct a = 
 case a of
  Product x -> E.object ([ ("pName", E.string (x.pName))
                         , ("pWeight", encodeDecimalRaw (x.pWeight))])

decodeProduct : D.Decoder Product 
decodeProduct  = 
 D.oneOf ([ let
             mkProduct a1 a2 = 
              Product ({pName = a1, pWeight = a2})
            in D.map2 (mkProduct) (D.field ("pName") (D.string)) (D.field ("pWeight") (encodeDecimalRaw))])

But there is no DecimalRaw type on the Elm side. So in this case, we might want to use Float on Elm side whenever we have a Decimal field in Haskell.

This can be done as follows

  instance ToHType Decimal where
    toHType _ = toHType (Proxy :: Proxy Float)

This gives us usable Elm code.

type Product = Product { pName : String, pWeight : Float } 

encodeProduct : Product  -> E.Value
encodeProduct a = 
 case a of
  Product x -> E.object ([ ("pName", E.string (x.pName))
                         , ("pWeight", E.float (x.pWeight))])

decodeProduct : D.Decoder Product 
decodeProduct  = 
 D.oneOf ([ let
             mkProduct a1 a2 = 
              Product ({pName = a1, pWeight = a2})
            in D.map2 (mkProduct) (D.field ("pName") (D.string)) (D.field ("pWeight") (D.float))])

Note that this only works if both types have compatible JSON representations. The Aeson instances should take care of this on the Haskell side.

Tests

This is being tested by round tripping a bunch of JSON encoded values from an Elm front end to a Haskell back end, where it is decoded and sent back to Elm where it is again decoded and checked for equality with the value that was initially sent. These right now, are in the form of a Python script that walks through the full range of Aeson options, make the Haskell build and auto generated Elm source for each, and then test the round tripping of included types using a headless Chromium browser. The tests at TravisCI use this process as well. The test repo is separate for now and is available at https://bitbucket.org/sras/elminator-test.

Installing

If you are using the Stack tool, then for the time being, you have to add Elminator to the 'extra-deps' section of stack.yaml as follows (Please use the latest available version here).

extra-deps:
  elminator-0.2.1.0
Metadata

Version

0.2.4.2

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows