agrafix / agrafix/elm-bridge

How to deal with # (magic hash) in Haskell data types?

Ouverte
#51 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
Langage dominant
Elm
Étoiles
107
Forks
28
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

I started to learn about Servant from the [Building an API with Servant!](https://mmhaskell.com/real-world/servant) tutorial. When I later got interested in Elm I tried to automatically generate Elm code for this Servant API which uses `Int64` and I discovered that `Int64` is [defined](https://hackage.haskell.org/package/base-4.16.0.0/docs/src/GHC.Int.html#I64%23) as a primitive unboxed Haskell type:

data {-# CTYPE "HsInt64" #-} Int64 = I64# Int#

, which make use of `#` ([magic hashes](https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/magic_hash.html)).

In the generated Elm module the Haskell type `Int64` is represented by the following Elm type:

type Int64 =
I64# Int#

, which is not valid Elm syntax:

-- UNEXPECTED SYMBOL /Users/XXX/elm-frontend/src/Derived.elm

I got stuck on this symbol:

14| I64# Int#
^
It is not used for anything in Elm syntax. Try removing it?

I've initially posted a [question on Stack Overflow](https://stackoverflow.com/questions/69937285/using-servant-elm-and-elm-derive-with-int64-deriving-and-generating-definition) about this. The obvious solution is to simply avoid data types with magic hashes in the Servant API. But, even if Elm may not have an analogue to boxed vs unboxed types, I cannot help wondering if it wouldn't be useful to be able to generate Elm code for arbitrary Servant APIs including data types with magic hashes. In this example, if the ranges of Haskell `Int` and Elm `Int` are the same, the magic hashes could simply be removed and the Haskell `Int#` be represented by `Int` in Elm:

type Int64 =
I64 Int

There may be more to this topic than I currently understand and there are probably a lot to consider to make this work for all general uses of magic hashes in Haskell data types. Anyway, since I stumbled upon this I thought I should create a GitHub issue.

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.