r/haskell • u/szpaceSZ • Oct 20 '20
TopShell: Purely functional, reactive scripting language
https://github.com/topshell-language/topshell29
u/continuational Oct 20 '20
Hi, author here, thanks for posting!
TopShell is an experiment to see if some of the tasks you might use Bash for, could instead be done conveniently in a typed, purely functional programming language.
Fetch some data from a couple of servers via SSH, get some data via HTTP, join it all up and visualize it as a tree, table or graph. Maybe poll it every few seconds. That kind of task.
As such, it's a very small language, suitable for writing very small programs. It has anonymous record types and sum types. No recursive (user defined) types yet.
There are some examples in the Readme.
6
u/sullyj3 Oct 20 '20
Looks cool!
magnitude : a -> Float | a.x: Float | a.y: FloatWhy not use purescript/elm style syntax for this? It feels a little neater, to me at least:
magnitude : { x: Float, y: Float | r} -> Float2
u/szpaceSZ Oct 21 '20
what's the
| rpart standing for? Seems superfluous; if it is a type variable standing for the whole structure (like Haskell'sr@(Point x y)in pattern matching then it could be optional, only needed if you use the type variable again, e.g. on the other side of the->.(I understand that
x,yin the above syntax it is likely structural, rather than nominal as in Haskell, but the optionality is orthogonal to the question).7
u/dbramucci Oct 21 '20 edited Oct 21 '20
To make an analogy to linked lists, it is not like
foo r@(x : y : _) = ...instead, it is like
foo (x : y : r) = ...Except it doesn't figure out which is
xvsyby looking at their positions, it figures them out by querying the record for "what is the value of thexfield?" and "what is the value of theyfield?".For example, you might be rendering points to a screen and as part of that, you need to project 3d coordinates into a 2d plane. The type signature
project : {x : Float, y : Float, z : Float | r} -> {x : Float, y : Float | r} -- Kinda like a type signature level -- foo (x : y : z : r) = x : y : r -- but it's type level and based on labels, not positions.says that the
zcoordinate will disappear from the record and this function will preserve all other attributes. So, we might have records of the formproject { x : 3.2, y : 4.6, z : 23, color : Green } = { x : ???, y : ???, color : Green }Instead of being an error, the
rtakes on the type{ color :: Color }and the type signature at the end guarentees we will get thatcolorout the other side.Meanwhile a function signature like
findCenterOfMass : [{x : Float, y : Float, weight : Float | r}] -> {x : Float, y: Float, weight : Float} -- Kinda like a type signature level -- foo (x : y : w : r) = x : y : w : [] -- but it's type level and based on labels, not positions.tells us that while it's fine for the other points to have extra data, when we construct the center of mass, we won't have a sane way to preserve that extra data so it should be considered lost. (Afterall, it would be misleading to say that we could track the colors of points in a sane way and any other random extension to the point type our user provides us). Here being explicit let's us inform the developer that we cannot preserve their extra data.
You may also want something for processing metadata like
processDoc : ({ | r} -> Html) -> [{md : CommonMarkdown | r}] -> Html -- disclaimer: I don't know how to write a function with this typeThe idea here is that we the user provdes a way to handle any extra extensions they might add (e.g. citations) and we will take the markdown portion of the document, combine it with their provided extension data and produce a single HTML document at the end. We need to be explicit about
rbecause the function they provide needs to handle the extra data they provided us in the list entries. If we don't check that their function's input records and the metadata are the same type, we can't safely call their function.We could also write
moveToCoords : {x : Float, y : Float} -> {x : Float, y : Float | r} -> {x : Float, y : Float | r}Being explicit that we don't want extra records in the first field is useful because it
Prevents us from accidentally moving an abstract point to a person (who will have extra fields like
Name : String) andKeep us from moving complex objects to other complex objects like snapping people to people in a simulation (I'll just assert that that's a rare/never-done use-case and 99% of the time that would be a logic error).
I've also seen them used for effects management, think of
polysemyin Haskell for something in the same spirit.Disclaimer: I've yet to use row-types in practice, so I'm hoping I've not made any mistakes in my explanation. I'm particularly worried about the
processDocand theprojectsignatures because I don't know how/if it is possible to drop 1 concrete field while preserving the unknown field. The other examples rely on changing existing fields while ignoring the rest or building a record from scratch which are simpler operations. More complicated stuff seems to rely onPrim.Rowfor getting a little more information aboutrfrom the compiler.3
3
u/sullyj3 Oct 21 '20
It's not quite the whole structure, but rather the whole structure excluding the x and y.
2
2
u/watsreddit Oct 21 '20
It is indeed structural as Purescript supports row polymorphism (though Purescript also supports nominal typing as well). It basically means “labels
xandywith these types as well as a rowrcontaining an arbitrary number of additional labels”. Sinceris a type variable, you can use it to ensure that two data structures have the same structure, even if you don’t care about the specific labels within. It’s pretty cool. Definitely handy for scripting.2
u/szpaceSZ Oct 21 '20
That's what I inferred, but I thought if you don't need the whole row's type (as in the above example) it could be omitted.
Maybe the
{ x : Float, y : Float }syntax is to meant to only match exactly this type but not wider ones. But why would I want to restrict that ever, if one of the strengths of the language is structural typing and the inference of the most generic type?So the question was, why not make
| roptional, so that you would need it intranslateXY : {x : Float, y : Float | r } -> rbut not in
magnitude : { x : Float, y : Float } -> Float2
u/watsreddit Oct 21 '20
Well first off, that
translateXYsignature wouldn’t make any sense because it’s returning a row of types (r), but I imagine that’s probably a typo.The reason it’s explicit is that you can do a lot with first class row types, like placing typeclass constraints on them. It gives you a lot of expressive power. You can add rows together (basically allowing you to compose larger product types from smaller ones without nesting), cons a label onto a row, etc. A lot of Purescript code makes use of explicit row variables. Plus, there can be times when you want to have a narrower type, like for reasoning about memory consumption. The less we hide about our data, the better, imo.
5
u/continuational Oct 21 '20
I also like
{ x: Float, y: Float | r}better syntactically, but it requires extending the type system with row polymorphism, whereas the current solution uses the same mechanism as type classes (e.g.a.x: Floatis encoded asHasField "x" a Float). So there's a tradeoff - is simpler syntax worth a more complicated type system?3
1
u/dbramucci Oct 21 '20
Isn't it possible to do something like
GADTSyntaxwhere you enable a syntax for a generalized form, but only permit the subset that fits in the existing type system.1
u/szpaceSZ Oct 21 '20
So it has some overlap with turtle?
Obviously, a tailored scripting language can be more ergonomic.
1
2
1
u/runeks Oct 21 '20
Would be nice with a comparison to Haskell.
I see it has row types (anonymous records), which sounds interesting. What else stands out compared to Haskell?
6
u/continuational Oct 21 '20
TopShell is heavily inspired by Haskell, but here are a few ways in which TopShell is different:
Strict evaluation
Anonymous records and field constraints
Anonymous sum types and constructor constraints
Tasks and Streams instead of IO
The syntax is not indentation based
Lambda functions are written
x -> y -> x + yCase lambdas are written
{| A x -> ... | B y -> ...}If then else is written
a ? b; cDefinitions are written
f : Int -> Int = x -> x * xNo do notation - use
x <- m, ...andm; ...in any expressionNo keywords
Example code:
interval = duration -> Stream.forever 0.0 t1 -> t2 <- Task.now, delta = t2 - t1, delta >= duration ? Task.of t2 ; Task.sleep (duration - delta); Task.nowTopShell comes with a split screen editor with code and output (inferred types, values, visualizations), a bit like a notebook, but where your code is less "boxed in".
The readme tries to give a quick overview of the entire language.
2
11
u/szpaceSZ Oct 20 '20
I stumbled upon this shell / scripting language today. (I have no affiliation).
The syntax is heavily influenced by Haskell. I thought it might be interesting, if only a curiosity, for the community here as well.
There's also a playground (limited, eg. without file IO).