News for hackers. Stuff that matters.
Re:Learn Physics by Programming in Haskell by Maro on Saturday February 24, 2018 @09:48AM (#16453425)
I've seen this before and it's terrible. Haskell's type system is terrible for this, ie. instead of plain vanilla operator overloading they have (^+^)... I once played around with a way to automatically track units when writing code like this, and I gave up on Haskell, doing it in C++ was much easier/sane.

https://github.com/mtrencseni/physcode

Re:Learn Physics by Programming in Haskell by jason-johnson on Sunday February 25, 2018 @08:59AM (#16458968)
Actually the issue you're complaining about is not a Haskell problem, it's entirely a library problem. It so happens that the standard library has a really poorly defined numeric hierarchy at the moment. If you want to fix it right now, you can avoid importing the prelude and import a different numeric library which has more sensible definitions for everything.

The issue is that for most things you'd want (+) to be defined like: (+) :: a -> a -> a

But actually, to be more flexible it could be defined as: (+) :: a -> b -> a

So then all the normal stuff would supported, as now, but you could also support things like:

    instance BetterNum DateTime where
      (+) :: DateTime -> Duration -> DateTime
And so on.
Re:Learn Physics by Programming in Haskell by runeks on Saturday February 24, 2018 @11:29AM (#16453928)
> Haskell's type system is terrible for this, ie. instead of plain vanilla operator overloading they have (^+^)...

Haskell’s type system does not prevent you from overloading the + operator. It’s the standard library (“Prelude”) that defines + to only work on numbers, and not on vectors, but with Haskell’s type system allows you to define + to do anything you want.

Re:Learn Physics by Programming in Haskell by tome on Saturday February 24, 2018 @01:22PM (#16454516)
Yes indeed. Not only is it possible to overload + but Haskell's type system has absolutely nothing to say on the matter.
Re:Learn Physics by Programming in Haskell by Maro on Sunday February 25, 2018 @02:36AM (#16457712)
Yes but if you want to use libs, and they don't do it this way, you're f--ked. This is one of the things I ran into.
Re:Learn Physics by Programming in Haskell by dllthomas on Sunday February 25, 2018 @06:46PM (#16461935)
Well, no, you're not "f--ked", there's just a pile of boilerplate. Not fun - and likely not worth it for the marginal benefit of being able to reuse + that way - but plenty possible.

And for what it's worth, I certainly agree that the way Num is structured is a wart in Haskell, and I think the previous two commenters agree. It's just that we should be clear about where the blame lies - and that's not the type system.

Re:Learn Physics by Programming in Haskell by KirinDave on Saturday February 24, 2018 @10:36AM (#16453651)
You say it's terrible to use an alternative operator but then don't really say why

Why is that bad? In Haskell, + is reserved for Num, which require some other properties that you can't guarantee for vectors. It'd be pretty bad to run afoul of that in a physics conversation.

Haskell certainly can let the + operator mean multiple things in multiple contexts.

Re:Learn Physics by Programming in Haskell by thepratt on Saturday February 24, 2018 @10:14AM (#16453543)
There isn't operator overloading as other languages have it, but you can write instances for your types. It seems you're just complaining about using vector addition operator (^+^) instead of a different addition operator (+); if it bothered you that much an instance would create a link defining + as ^+^.
Re:Learn Physics by Programming in Haskell by marcosdumay on Saturday February 24, 2018 @10:53AM (#16453750)
> but you can write instances for your types

Yes, as long as they have the structure your classes demand. It's not exactly Haskell's type system that is bad, it's the numeric classes that are a bit of a mess.

Math has a much cleaner structure that, for example, does not demand that you can convert any integral value into an instance of your numbers. Haskell tried to simplify it, but wasn't successful.

Re:Learn Physics by Programming in Haskell by nickpeterson on Saturday February 24, 2018 @11:34AM (#16453959)
This might work well in F#. It has the ability to use most basic mathematical operators with different numerical types, so it cuts down on the multiple symbols Haskell and ocaml get forced into, and has native units of measure functionality baked into the language.
Re:Learn Physics by Programming in Haskell by professor_plum on Saturday February 24, 2018 @12:25PM (#16454241)
F# also handles physical units in a really nice way.
Re:Learn Physics by Programming in Haskell by sitkack on Saturday February 24, 2018 @01:08PM (#16454450)
See, "F# For Scientists"