Live data from Hacker News

Use Haskell for shell scripting

haskellforall.com

31–40 of 105 posts

Re: Use Haskell for shell scripting

#31
post #27
post #14

I don't know much about Haskell, but I thought it had some properties to isolate side effects, but the code he gives: main = do cd "/tmp" mkdir "test" output "test/foo" "Hello, world!" -- Write "Hello, world!" to "test/foo" stdout (input "test/foo") -- Stream "test/foo" to stdout rm "test/foo" rmdir "test" sleep 1 die "Urk!" Clearly doesn't (it creates a directory, writes in a file, removes that file and that directo…

It's in a do block, so you can see that these are not simple function calls; they're being composed via some monad. In cases where you're actually chaining values together it's more obvious which things are which: do value1 But the notation is a bit more magic for this "no return" case; I prefer the Scala approach where even if you don't care about the return values you'd have to write this as for { _ ← cd "/tmp" //…

If you enable -Wall, Haskell forces you to use "_ But the "let" vs no "let" is a pretty strong hint anyway :)

Re: Use Haskell for shell scripting

#32

Earlier quoted context omitted.

Quite a lot of libraries here: https://wiki.haskell.org/Command_line_option_parsers

That's not the issue. The issue is that, if you want to simulate both "grep" and "grep -r", you need to different functions, or you need to have your "grep" function accept a record of parameters.

I saw those as regular parser that returns text but not just grep wrapper. Yes It has function named 'grep', but its not grep wrapper. It looks like `lstree` could be combined with `grep` function for emulate `grep -r`.

Re: Use Haskell for shell scripting

#33
post #29
post #18

Earlier quoted context omitted.

You are right. In this case effects are not isolated. But in this particular script, there are no interesting things to move into a pure function. It does not mean that it wouldn't be the case in a more complex script. Like everything, you have to learn to balance your IO code and your pure code. A bit like learning when to factor something into a separate class, or leave it in a few statement/methods. If you write e…

I agree about the benefits of isolating side effects and IO - I generally code in python, and my code tends to look like: def main_function(args): data = get_data(args) result = do_calculations(data) push_results(result, args) Where the function do_calculations is somewhat pure - no side effects, but I do use local variables that I modify inside the function. > You are right. In this case effects are not isolated. Bu…

You're right that Haskell doesn't distinguish different IO actions other than by the type they return. There are certainly libraries that do this though, although they aren't widely used.

Even though Haskell doesn't distinguish different classes of IO actions, it still distinguishes IO actions from other kinds of actions (such as stateful actions as per your example), and pure computations and that provides a hell of a lot of bang for buck.

The Idris language has the notion of effect types [1] to make achieving the goal of categorising the kinds of effects being used in a function easier to deal with, but that uses the dependent capabilities of the language.

[1] http://eb.host.cs.st-andrews.ac.uk/drafts/eff-tutorial.pdf

Re: Use Haskell for shell scripting

#34
post #28

Who's the target audience of this exactly? I already see a language pragma, do notation, liftIO, parser combinators. Hamming has this great set of lectures on how he became a world renowned scientist and in one of the lectures he explains why Ada failed and other languages succeeded. The difference was that Ada was designed logically and most successful languages were designed psychologically. Even when government co…

Ada might have failed on OS, but that is just because few startups that based their workstation OS in UNIX succeeded in the market at large. C goes hand-in-hand with UNIX, so clearly no UNIX vendor would have it in their SDK and UNIX developers weren't willing to pay for tools. As history has shown, the moment UNIX vendors started doing "Home" and "Pro" editions, GCC got lots of help. As Ada talks at FOSDEM show, it…

I think there's much (~10x) more C code than Ada code everywhere where safety matters. (No hard data, just a feeling from experience - if you have hard data proving me wrong, do share.)

Also, it's not just Unix that's written in C or a descendant - there's also, well, Windows, and a load of embedded RTOSes.

If Ada made you as productive as C with extra benefits or something to that effect, you'd expect Ada to succeed at the marketplace at a scale at least comparable to C's - especially with the government support it had which put C at a disadvantage, not?

Re: Use Haskell for shell scripting

#35
post #31
post #27

Earlier quoted context omitted.

It's in a do block, so you can see that these are not simple function calls; they're being composed via some monad. In cases where you're actually chaining values together it's more obvious which things are which: do value1 But the notation is a bit more magic for this "no return" case; I prefer the Scala approach where even if you don't care about the return values you'd have to write this as for { _ ← cd "/tmp" //…

If you enable -Wall, Haskell forces you to use "_ But the "let" vs no "let" is a pretty strong hint anyway :)

Control.Monad.void is your friend.

Re: Use Haskell for shell scripting

#36
post #10

I don't really see the point of this, apart from academic research values. POSIX shell is everywhere - your current Linux and OS X machines, old UNIX workstations, home routers, servers... Just drop in a file and it will probably run just fine, unless the author screwed something up completely. POSIX shell scripts are the perfect bootstrap mechanisms that will run almost anywhere regardless of architecture. Haskell,…

What is the point of PowerShell on Windows, you can just use COMMAND.COM.

Re: Use Haskell for shell scripting

#37
post #2

The tutorial does a great job of explaining why this is interesting: http://hackage.haskell.org/package/turtle-1.0.0/docs/Turtle-... For example, the pwd function returns a FilePath type rather than a String: Prelude Turtle> :type pwd pwd :: IO Turtle.FilePath The datefile function is also typed: Prelude Turtle> :type datefile datefile :: Turtle.FilePath -> IO UTCTime So this really does seem to structure the data pa…

Are those types just aliases of String?

Re: Use Haskell for shell scripting

#38
post #32

Earlier quoted context omitted.

That's not the issue. The issue is that, if you want to simulate both "grep" and "grep -r", you need to different functions, or you need to have your "grep" function accept a record of parameters.

I saw those as regular parser that returns text but not just grep wrapper. Yes It has function named 'grep', but its not grep wrapper. It looks like `lstree` could be combined with `grep` function for emulate `grep -r`.

Take lstree then. How would you give it an option giving the kind of ordering you want?

Re: Use Haskell for shell scripting

#39
post #29
post #18

Earlier quoted context omitted.

You are right. In this case effects are not isolated. But in this particular script, there are no interesting things to move into a pure function. It does not mean that it wouldn't be the case in a more complex script. Like everything, you have to learn to balance your IO code and your pure code. A bit like learning when to factor something into a separate class, or leave it in a few statement/methods. If you write e…

I agree about the benefits of isolating side effects and IO - I generally code in python, and my code tends to look like: def main_function(args): data = get_data(args) result = do_calculations(data) push_results(result, args) Where the function do_calculations is somewhat pure - no side effects, but I do use local variables that I modify inside the function. > You are right. In this case effects are not isolated. Bu…

`main` is the entry point of the program. Its type is `IO ()`. It is a warning to omit a type signature in a top level definition, so in a normal program you would have a `main :: IO ()` type definition. The `IO` allows any kind of effect.

Re: Use Haskell for shell scripting

#40
post #25

OK. How do you easily fork to run a command in the background? How does setting up pipes work? What's the idiom for chdir'ing to a subdirectory such that you pop back out again when you're done (I'd use a subshell with (ch xxx; ...) in bash)? Getting into more tricky stuff, what's the equivalent of This doesn't really demonstrate anything that shell scripts are actually written for: orchestrating and composing other…

Did we read the same article? The entire 'streaming section' is about pipes and I/O redirects. Running a command in the background is just forkIO $ proc ..etc.., as in regular Haskell.

Nothing tricky about it.

Post reply on HN