Generating dhall types containing references to other types from Haskell

I have kinda the inverse problem of makeHaskellTypeFromUnion and fields with sum types

[Context:
I am in a setting where I want configuration to reach my ghcjs frontend, which can‘t depend on dhall.
But I still want to share types between frontend and backend so I can‘t generate my Haskell types from dhall.]

Let’s say I have a Haskell Type

data A = A1 | A2 deriving (Eg, Show, Generic, FromDhall)
data B = B { a :: A} deriving (Eg, Show, Generic, FromDhall)

Now I can write a Dhall file using
pretty . expected $ auto @A and pretty . expected $ auto @B (I need to write e.g. the let A = myself of course).

But the generated Code for B will contain a < A1 | A2 > and not refer to A. Which makes the dhall file much less readable when the types get bigger.

Is there a way to work around this?

1 Like

@maralorn: One possible way to address this is to add a utility to the API for simplifying an expression given an existing named type. In other words, something like:

factor
    :: Text
    -- ^ Name of the type to factor out (e.g. `"A"` in your case)
    -> Expr s a
    -- ^ The type to factor out (e.g. `expected (auto @A)` in your case)
    -> Expr s a
    -- ^ The expression to factor (e.g. `expected (auto @B)` in your case)
    -> Expr s a
    -- ^ The expression with the type factored out into a `let` binding
    -- (e.g. `let A = A1 | A2 in { a :: A }`)

Would that address your use case?

Yeah, that would be great although it is maybe a tiny bit to unflexible.
In my situation I’d prefer to return something like

let ... in
{ B = B, A = A}

So actually a function that would just return { a : A } would be enough. (I guess the Expr Type does not prevent free variables?). Then I can manufacture the let anyway I want.