How to fix an empty list type problem with a custom data type in haskell


#1

With the setup below I get a dhall error, which says that the empty list needs a type annotation.
Whats the best way to solve it?

Thanks.

test.dhall:

{
 uno = 1
,dos = ["jaba", "daba"]
,tres = []
}

test.hs:

{-# LANGUAGE DeriveGeneric #-} 

import qualified Dhall
import GHC.Generics

data Test = Test {uno :: [Double], dos :: [T.Text], tres :: [Foo]}
     deriving (Show, Eq, Generic)   
data Foo = Foo {blub :: T.Text, bla :: T.Text} deriving (Show, Eq, Generic)
 
instance Dhall.FromDhall Test                                                                                                                                                                                                           
instance Dhall.FromDhall Foo

test :: IO ()
test = do
  x <- Dhall.input Dhall.auto "./test.dhall"
  print (x :: Test)

#2

You could simple add the list type annotation, e.g. tres = [] : List Text, or using the prelude function: tres = https://prelude.dhall-lang.org/List/empty Text.

Another solution is to use record completion, but i’m not sure what is the best way to do that from your haskell type. Here is an example with the command line:

-- record.dhall
{ Type = { uno : Natural, dos : List Text, tres : List Text }
, default.tres = [] : List Text
}
-- test.dhall
{ uno = 1, dos = [ "jaba", "daba" ] }
$ dhall <<< '(./record.dhall)::(./input.dhall)'
{ dos = [ "jaba", "daba" ], tres = [] : List Text, uno = 1 }

#3

Thanks for the reply.

Anonther related questions:

I know that I can generate dhall-types from haskell with

Dhall.Core.pretty (Dhall.expected (Dhall.auto @MyTypeName))

It would be nice, if I also could generate dhall-types with defaults where all Optional are None. Is that possible?


#4

@michelk not sure this is what you are looking for, but here is a function i’m using to ease new schemas creation: getDefaults converts a record type to a record value using None value for Optional attributes.