Hacker News new | ask | show | jobs
by WJW 241 days ago
Haskell is much like this? A function like `borp :: a -> b` maps from type a to type b. If you want to have side effects like mutable state, you need to encode that in the function signature, like `borpWithState :: a -> State s b`, where s is the type of the mutable state.

In this case it's almost the opposite of most programming languages. In (say) Ruby or Java, any function or method can do anything; write to stdout, throw exceptions, access the network, mutate global state, etc. In haskell, a function can only do calculations and return the result by default. All the other things are still possible, but you do have to encode it in the type of the function.

EDIT: The annotations you mention with regards to identity elements etc do exist, but they live mostly on the data structures rather than on the functions that operate on those data structures.

1 comments

Languages with dependent types (Agda, Idris, Coq, Lean) seem even closer to the description, with a user supplying proofs of properties. For instance, as in idris2-algebra [1]. One can similarly define isomorphisms, or other kinds of morphisms, by listing their properties, and requiring proofs to construct.

[1] https://github.com/stefan-hoeck/idris2-algebra

Thanks. Maybe I should have stressed this more, but what I had in mind was a more mainstream language suitable for everyday programming too. All the languages mentioned in this thread are great but they are not getting any more popular and you would not use them to build, say, a game or a linear algebra library, right?
Well, at least Idris aimed to become practical, and there is the CompCert compiler in Coq (called Rocq now, actually), demonstrating viability of large verified projects. Besides, one does not have to verify everything in those, and could program more or less as in Haskell (which may also seem impractical, but it is used for all sorts of things, and is my primary language these days, for both work and hobby). I think the primary issues with those have to do with their unpopularity, and particularly lack of libraries. Though C libraries are usually just an FFI call away, so even such smaller languages are not that painful if you try to simply get things done in those.

I recall there being at least SDL2 bindings for Idris (not to mention those for Haskell, which also has game libraries), and some linear algebra libraries in those languages (complete with verification), but probably not particularly extensive.

They are not the most practical choice if you do not need verification, but if you would like to use languages like that, they are available and usable, but with additional effort/drawbacks. I wish they were more mature and had a better infrastructure, too, but that would take people pushing them to that point.