Write CLI scripts in Elm (IO Monad)

I’ll look into this later :slight_smile:

After seeing this video where @rtfeldman talks about the Roc programming language I started thinking about IO and security, and that it would maybe be useful to add some safety features to this package that leverages the type system.
My work-in-progress solution is to add a phantom type to the IO type:

type IO permission error ok
    = ...

All IO functions would “express” which permissions they need using extensible record:

readFile : String -> IO { a | fs_read : Allow } Error String

I’ve made a more in-depth example in ellie explaining the details that you also can play around with.

Thoughts:

  • I don’t know how useful it would be to add this to the type system vs. using some runtime restrictions instead, similar to what deno does?
  • Can this type system safety be circumvented somehow?
2 Likes

@albertdahlin, I would recommend looking into papers on “effect systems” and “effect types” to get a sense of the ideas that have been tried out. I remember really liking Koka from Daan Leijen, and Elm’s records are based on one of his earlier papers.

Two potentially interesting data points:

  • When doing the design for Cmd, I considered using something like OCaml’s “polymorphic variants” to have a permissions system for commands. Instead it might have been Cmd [Http,Time] Msg or something like that. Ultimately we did not think the complexity/usefulness balance was right for Elm specifically.
  • I believe PureScript used to have Eff { ... } a instead of IO a and they eventually dropped the effect restriction part. This is something I half-remember hearing, and I guess it’s true based on this?

That’s not to say this is a bad idea or anything! Just that people have struggled to get the right balance of complexity/usefulness right in some prior efforts I know about. Daan Leijen is really good at looking at a complex research problem and finding a very simple approach that feels like a great balance. That said, I haven’t been following “effect systems” research for a few years now, so hopefully there is enough in this post to be a workable starting point for looking into more prior work!

7 Likes

If you haven’t seen it, GitHub - ianmackenzie/elm-script: Experimental command-line scripting for Elm may provide a little inspiration as well.

2 Likes

@evancz Thank you for the input, I will definitely look into these sources for some future projects I have in mind. For the moment I will not use typed effects in this package, I’d rather keep things as simple as possible.

@wolfadex Thank you, I have already peeked at Ian’s lib for inspiration.

2 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.