gelisam / hawk

Haskell text processor for the command-line
Apache License 2.0
361 stars 20 forks source link

auto-lambda #11

Open gelisam opened 11 years ago

gelisam commented 11 years ago

Swap can be written as:

> printf "1\t2" | hsl swap

or equivalently

> printf "1\t2" | hsl '\(x,y) -> (y,x)'

With implicit arguments, we should be allowed to write it as:

> printf "1\t2" | hsl '(_2, _1)'

I think it would also be useful to support _0 for the entire argument, for those cases where pointfree style is inconvenient:

> seq 10 | hsl 'take 2 _0 ++ drop (length _0 - 2) _0'
1
2
9
10
ssadler commented 11 years ago
> printf "1\t2" | hsl '(x,y) -> (y,x)'

Wait... This works?

gelisam commented 11 years ago

No, it doesn't, I forgot a lambda (fixed now). Or are you surprised because the types are ambiguous?

gelisam commented 11 years ago

The whole point of my type-inference experiments is to see what I can achieve by modifying the inferred type of expressions before I give them to GHC. In this case, the inferred type of the expression \(x,y) -> (y,x) is (t,t1) -> (t1,t), which is too ambiguous. GHC won't resolve the ambiguity, but we can! I observe this type and change it to (ByteString,ByteString) -> (ByteString,ByteString), which is concrete enough for the typeclasses to decide how to parse the input. I then change it again to [(ByteString, ByteString)] -> [(ByteString, ByteString)] and wrap the entire expression in an fmap.