Dhall-haskell: [Question] Reading a file to Text

Created on 25 Sep 2017  Â·  11Comments  Â·  Source: dhall-lang/dhall-haskell

Hey!

How can I read a file into a : Text value? Like let text = builtins.readFile ./some-file.txt in Nix.

I could wrap it in ''s and then do a standard import, but it’s not exactly convenient. :stuck_out_tongue:

Thanks!

Most helpful comment

Like this: ./some-file.txt as Text

All 11 comments

Like this: ./some-file.txt as Text

Incredible! :green_heart: Thanks so much, I didn’t think this would be possible.

Maybe it should be added to Dhall.Tutorial? I can’t see any as mentioned there. =)

Oops! For some reason I thought that the tutorial did mention this feature, but I now see that it's not there. I'll update the documentation

One last question: can I do substitution/templating on such Text in pure Dhall easily? Or am I left with Haskell later? For example:

  • hello.txt:
    Hello, {name}!
  • config.dhall:
    let helloRaw = ./hello.txt as Text in { hello = Text/replace "{name}" "Gabriel439" helloRaw }

Normally, if this Text was inlined, I’d just use ${name}, but here?

The best you can do in Dhall is to go back to hello.txt being Dhall code, like this:

-- hello.dhall

λ(name : Text) → ''
    Hello, ${name}!
''

... and then in config.dhall you do:

{ hello = ./hello.dhall "Gabriel439"
}

... or if you want to pass named arguments to hello.dhall then you can pass a record like this:

-- hello.dhall

λ(args : { name : Text }) → ''
    Hello, ${args.name}!
''

... and then reference that like this:

{ hello = ./hello.dhall { name = "Gabriel439" }
}

However, you can't retroactively modify a value of type Text. The only thing you can actually do with Text values in Dhall is concatenate them, but there is no way to inspect, slice, compare, or otherwise modify them in Dhall. Other than concatenation, Text values are opaque values (like Integer)

In fact, the string templating syntax in Dhall is syntactic sugar for Text concatenation. This Dhall expression:

λ(name : Text) → ''
    Hello, ${name}!
''

... is the same as this Dhall expression:

λ(name : Text) → "Hello, " ++ name ++ "!"

Thank you for this detailed answer. :heart_eyes_cat: Amazing support! :blue_heart:

Lack of retroactive modification makes a lot of sense (do _$x_ well from the beginning), but still I had this slight hope. :sweat_smile: But then, after a while of initial happiness, I wouldn’t like having the possibility of defining Text/replace in a configuration language.

Thank you!

You're welcome! Also, I will reopen this issue to remind myself to fix the documentation to mention support for as Text

Is it possible to contruct a filename and then read it as raw text?
e.g.

let var = "Szenario1"
in { contents = readFile "${var}.txt" }

@michelk: No, Dhall does not support computed imports. See: https://github.com/dhall-lang/dhall-lang/issues/187

There are two reasons: one is technical and one is a security reason.

  • The first reason is that import resolution strictly precedes type-checking and type-checking strictly precedes evaluation, so we'd have to overhaul the standard to support interleaving evaluation with import resolution

  • The second reason is that computed imports pose a security risk for leaking program state. This is true even for file imports, since a relative file import (like ./foo.dhall) can become a URL import if the parent import is a URL import.

Thanks for clarification

@michelk: You're welcome! 🙂

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DrSensor picture DrSensor  Â·  6Comments

mgajda picture mgajda  Â·  3Comments

vmchale picture vmchale  Â·  5Comments

DrSensor picture DrSensor  Â·  5Comments

Gabriel439 picture Gabriel439  Â·  6Comments