Hacker News new | past | comments | ask | show | jobs | submit login
Learn Physics by Programming in Haskell (arxiv.org)
468 points by jxub on Feb 24, 2018 | hide | past | web | favorite | 80 comments




yes, the second paragraph in the paper mentions it

''' The purpose of the course is to strengthen a student’s understanding of basic physics by learning a new language (Haskell), and instructing the computer to do physics in that language. Our attitude was strongly influenced by the work of Papert[6], and the subsequent work of Sussman and his coworkers[7, 8]. '''

[7] Gerald Jay Sussman & Jack Wisdom (2001): Structure and Interpretation of Classical Mechanics. The MIT Press.

[8] Gerald Jay Sussman & Jack Wisdom (2013): Functional Differential Geometry. The MIT Press


I haven't looked at this or the haskell arxiv pdf but as an enthusiast in CS, math and physics, I'm wondering if any physicist can compare this form of physics to the "rational mechanics" program started by Truesdell in the 50s and 60s. It never gained mainstream acceptance but the idea sounded good: to axiomatize classical mechanics and make it more of a mathematically rigorous discipline.


Never heard of that book. I enjoyed SICP a lot so if this is anything like SICP was then I would be very happy.


It's SICP for Classical Mechanics, all the way up to Hamiltonian/phase-space formalism, including perturbation theory and canonical transformations.


I took Classical Mechanics in college and laughed many times watching this https://www.youtube.com/watch?v=O3tVctB_VSU as Sussman was right on point about many things.


Can you share your experience with sicp? I'm self taught in JavaScript, go, python, ruby and taken Coursera for algorithms and read up on the operating system. Will sicp help me?


It had a nice way of solidifying certain (especially functional) programming concepts for me. However, one potential either annoyance or advantage to the book, depending on your perspective, is that it at times teaches relatively simple programming concepts by using what could be considered more difficult math problems.

For example, finding a square root using Newton's method and implementing the Fermat test in Lisp. Such problems could either be seen as fun or annoying depending on your background and/or enjoyment of learning this type of stuff in addition to programming concepts. They do tie in together nicely, it's just something I'd mention for someone looking into studying the book.


I kind of randomly implemented Newton's method in Go once, just because it looks so pretty: https://github.com/aybabtme/newtonmethod#newtons-method


The SICP lectures are on YouTube [0], so you can grab a taste to see if you want it.

SICP covers a lot of the higher level concepts we use day-to-day, but in such a way that you can grasp lower-down concepts along the way.

That's let me write code faster, understand compiler stacktraces better, and grasp why some things work in a language, but similar code doesn't, and get a handle on optimisation where I need it.

Everyone seems to get a little different benefit out of it, but most people I know have got some benefit, even just from the first few exercises or lectures.

Unfortunately, it gave me my favourite area of CS: Language design. Which isn't exactly a career track, being so niche.

[0] https://www.youtube.com/playlist?list=PLB63C06FAF154F047


SICP lives up to the hype in my experience. The pacing is perfect. It will take awhile to work through though. I'd suggest planning to skip some exercises and pick them up on a second read. Don't let yourself get bogged down and lose motivation over one exercise.


I would recommend anyone who has not implemented an interpreter to implement the scheme interpreter in sicp (in any language of their choosing). At heart sicp is not about scheme but about computation systems in general and at that it's the most elegant book I've come across.


Yes! SICP is, without a doubt, one of the very best books on programming I have ever read and I’ve read quite many. I too program a lot in JavaScript and Python (in addition to Rust and C and C++), so some might think SICP would not be relevant just because it’s using Scheme but in fact SICP has had a very significant impact on how I write software.

SICP is a book that I took my time to read. I read the whole thing front to cover. Some days I would read many pages some days fewer pages. I read a lot of it on a vacation. Don’t remember if I got through the whole thing then or if it took even more time.

SICP was so good that I plan on reading it again.

I recommend looking at the exercises and thinking about them a bit but like the other guy said don’t get bogged down by them.


I recently looked at SICP again after first reading it many years ago. I had retained the overall impression that SICP demonstrates how to build many of the celebrated creations of computer science - interpreters, compilers, databases, logic programming systems, circuit simulators - using a very simple programming language with just a few constructs.

But I had forgotten many of the details. Here is just one: in 3.1.1, Local State Variables, SICP shows how to, in effect, define classes and create objects using only function definition with lambda and set! (that is, assignment) in the definition body. The function you define this way is a like a class, the functions it returns are like instances. So you can do all this without any specifically object-oriented language features. This occupies just a few pages near the beginning of a long book - the whole book is dense with worked-out ideas like this.

The cumulative effect of all these examples in SICP is to demonstrate that you can solve any programming problem from first principles - that is, by combining a few simple but powerful constructs in the several ways they describe. Moreover, a solution constructed this way might be simpler and easier to understand than a solution made the more usual way: by looking for a specialized language construct or library that seems to offer an already-made solution to the problem.


One odd thing for me was that example problems seem to taken from physics and engineering. Which is natural for the authors and the time -- computer science departments typically started out as part of the Physics or Engineering departments.

One of the first examples is Newton's method for approximation. Which many beginning programmers have never encountered.


In particular CS at MIT ~was~is in the EECS department. MIT also begins with calculus (18.01), not algebra or pre-calculus or anything else. Newton's method, at least in my undergrad (Georgia Tech, not MIT), did introduce Newton's method at some point (I cannot remember the context, but I do remember it in my first or second calculus course). Since SICP students would have taken or be taking Calculus at the same time, it wouldn't be a stretch for them to have seen those sorts of problems already.


cs at mit is in the eecs department.


Oops, yeah. That wasn't supposed to be past tense.


haha. no worries. :)


I posted the link elsewhere but if you take a look at this talk https://www.youtube.com/watch?v=O3tVctB_VSU it will give you an idea of where Sussman is coming from.

Also, I think right in the beginning of the very first lecture of the series Abelson talks about Computing as a way of codifying processes. In the link I included, Sussman says flat out that what he's interested in is using Computing as a better way to teach physics.


On Lisp by Paul Graham is more exciting and inspirational.

Sicp is solid fundamentals.

Read both.


Here’s a link to modern versions of the code from “On Lisp.” The README includes a link to a (legal) PDF of the book and to Graham’s site:

https://github.com/DalekBaldwin/on-lisp/blob/master/README.m...


You might in fact love it. You'll get a nice overview of how interpreters actually work, for one thing.


You can take a look at the book How To Design Programs (HTDP) [1]. It's similar. The 2nd edition printed book is going to be released soon [2]. There is a paper from the authors of HTDP comparing it to SICP [3]. By the way, there is an couple of online courses at EDX that covers content of HTDP [4][5].

[1] http://www.ccs.neu.edu/home/matthias/HtDP2e/

[2] https://www.amazon.com/How-Design-Programs-Introduction-Prog...

[3] https://en.wikipedia.org/wiki/The_Structure_and_Interpretati...

[4] https://www.edx.org/course/how-code-simple-data-ubcx-htc1x

[5] https://www.edx.org/course/how-code-complex-data-ubcx-htc2x


The Scheme code that inspired this can work symbolically as well as numerically, since it contains a computer algebra system at its core.


Is this available in a proper typesetted form?



heh first thing that came to mind. I'm still curious about repl lagrangian mechanics


Bootstrap is working on a high school physics curriculum for ninth graders, which integrates functional programming as a fundamental component. They only have a landing page up at the moment [0], but I attended a very cool workshop they gave this summer. They’ve designed the programming language to avoid any “incidental complexity”—Physics teachers only have to teach the CS that’s absolutely necessary for the physics education goals they’re trying to achieve.

[0] http://www.bootstrapworld.org/materials/spring2018/courses/p...


as a note for others, the programming language they developed and use is pyret.

https://www.pyret.org



There's also this one, "Structure and Interpretation of Classical Mechanics" (by the same authors): https://mitpress.mit.edu/books/structure-and-interpretation-...


And here are some source files and setup notes for the underlying SICM scheme library pertinent to macOS: https://github.com/hnarayanan/sicm


I had ordered one of those books (don't really remember which of them), and it is to this day the only book I sent back to amazon for a refund.


ok?

This comment doesn't really tell us much. Was the book missing its cover? Did you not realize what the book was about? Did you dislike the presentation style?


okay, I dug up my amazon review again. It was "Functional Differential Geometry" and my main complaint was, that they claim to replace a fuzzy mathematical notation with a Scheme DSL which would make things more clear. In the end, it all felt like they replaced mathematical notation with one that THEY were just more familiar.

I am a scheme/lisp/clojure fan by heart, so it definitely wasn't the parens.


I actually did the same with SICM, because it arrived with some of the pages stuck together. In the chapter on rigid body dynamics, as it happens.

(This really did occur, but it was just glue.)


A great book that walks the reader through simulations to learn Physics is, "From Newton to Mandelbrot". [0]

[0] https://www.amazon.com/Newton-Mandelbrot-Theoretical-Physics...


And the previous HN discussion on this exact topic had some good notes: https://news.ycombinator.com/item?id=9003898


There is also the sequel “Learn Quantum Mechanics with Haskell”[0].

[0]: https://arxiv.org/abs/1611.09471



Wow this is beautiful! The "main" equation for the physics of projectile motion is less than 10 lines of code: https://github.com/walck/learn-physics/blob/master/examples/...

The "state" of the system St is defined here https://github.com/walck/learn-physics/blob/f6ce9bbaece24f48... It's amazing to see code and implementation details that match the physics language.

I wonder if this codebase is ELM-able? i.e. does it use any specific features from Haskell that are not in ELM? I'd love to see this in the browser. Weekend project anyone?


Typeclasses - thought you could use that whole dictionary work around, or write namespaced variants.


Compile it with ghcjs


"Learn <subject> by Programming in <language>" would be a popular series of books. I hope someone writes them.


I recommend “Music for geeks and nerds”, which is an intro to music theory/composition with python

https://pedrokroger.net/mfgan/


Looks interesting. Music theory and harmony would be good topics to approach with computers.

I think that there could be books on poetry (analyze meters, language, cross-references, symbolism, poet connections), literature, biology, math, psychology, sociology, economics, ecology, chemistry, etc. through programming.

Some of those exist, but there should be more.


Wow, this sounds so good. Please someone, more like this.


As a physicist I really really enjoyed this. Really cool. I have a swift library (using Foundation’s Measurement API) that I use for so much of my calculations and the one really important thing that has helped me solve problems is the type system. Units are types and I see a lot of young physicists students make mistakes where if they just looked at the units they’d have gotten it right. Physics is some sense just dimensional analysis. This library would be great if it were able to add unit types to really ensure that people understand _why_ a formula is the way it is.


Somewhat related, does anyone have a reccomendation for learning Newtonian Physics? I took a semester as an undergrad, but I didn't understand it as well as I would like.


[I'm a physics professor, for what it's worth, and I've spent more time than I care to admit trying to find a good textbook for this.]

For folks with a programming background, I suspect it would be hard to do better than Matter and Interactions by Chabay and Sherwood. (Someone already mentioned this elsewhere in the comments here.) It makes substantial use of Python code in the text and problems, and (partly enabled by that) it takes a delightfully modern approach that discusses the true (relativistic) formula for momentum (etc.) very early on. I haven't taught out of it yet, but I've been very tempted.

I'm currently teaching the intro course using Tom Moore's Six Ideas that Shaped Physics series. His approach is a little quirky at times, but usually for good reasons. I like it a lot.

Those are both purely introductory texts (though both cover topics up through "modern" physics: relativity and basic quantum phenomena). Beyond that, well, that's a bigger question.


My favorite go-to book for basic physics is the 1966 edition of Haliday & Resnick, which you can still find on Amazon as a combined two volume book, I think. The newer editions are quite watered down, but they do an acceptable job. There's also a newer, more advanced version of these books called Physics, Volume 1 & 2 by the same authors that you may consider checking out.

Books are hard reading though, so video lectures are also the next best option. Walter Lewin does a good job with this, so try googling his lectures.

Also, you'll probably want to stay away from Feynman lectures until you actually understand the theory.


Leonard Susskind's Classical Mechanics course: http://theoreticalminimum.com/courses/classical-mechanics/20...


Julius Wess: Theoretische Mechanik

The book is amazing, since it introduces advanced concepts like group theory and greens functions very early with very simple examples from classical mechanics. Unfortunately it is only available in german, yet:

http://www.springer.com/de/book/9783540885740


Feynman lectures -> Landau Lifshitz, when you can make the jump.

This will show you how to do the physics, then (Landau Lifshitz) show you how to reformulate this physics (again, it is actually in the feynman lectures too) in the terms we use in modern physics (Variational methods etc.)


Feynman lectures are excellent for physical intuition but if someone doesn't have a few years of practice with physics it will be difficult to get much out of it. Feynman also doesn't have a lot in the way of problem solving, even with the companion problem solving book.

With that in mind, Landau/Lifshitz is an entirely different class. I'm a practicing astrophysical theorist and I would never claim to have grokked these volumes in anything nearing completeness. A serious post-PhD program of study can be undertaken to understand to the finer points of those books; this is a pursuit that would challenge any practitioner. (I place Physics of Shock Waves and High Temperature Hydrodynamic Phenomena by Zel'dovich and Razier in the same category of extremely information dense Soviet tracts on physics.) They are incredibly valuable, although I do think there is a bit of an element of "if you don't know L/L, you aren't a real theorist."

I think that recommending these texts is great for someone down the road, but if someone hasn't even had a full year of intro physics recommending these is not appropriate. Intro-level textbooks exist for a reason - not all of them are cheap, watered down versions of the "real stuff." (Some are, of course.) There are also some texts I've found (but don't remember, unfortunately) in the computer vision community that introduce some physics that would be great for someone with a computer science/engineering background.

edit: "cheap" as in "cheap feeling" not inexpensive, as most of those intro texts are more expensive than the graduate level


The first L&L book is what I'm thinking of: It's actually pretty manageable. I'm talking out of my ass beyond here in the series, I don't have any others in physical form so I've read bits of QM and The classical theory of fields

The downside to them is that they don't come into contact with the "mathematical" physics side e.g. Treatment of Lagrangian/Hamiltonian formalism through differential geometry.


If you want a good textbook, try David Morin's "Introduction to Classical Mechanics: With Problems and Solutions". Rather rigorous book with a good writing style and a lot of exercises.


You can take a look at Feynman lecture notes at caltech.edu.


Mentioned in another branch of this discussion, but I really liked "From Newton to Mandelbrot" for a computational take on learning Physics.


John Taylor "Classical Mechanics"


Matter and Interactions [1] is a great introductory physics course that (among other innovations) makes extensive use of simple simulations (by students).

Here [2] is an example of a program, complete with 3D graphics.

[1] https://matterandinteractions.org/

[2] http://www.glowscript.org/#/user/GlowScriptDemos/folder/Exam...


" In electromagnetic theory, the type signatures of functions that calculate electric and magnetic fields clearly express the functional dependency on the charge and current distributions that produce the fields"

Maybe a student learning this will go on to make a HDL for analog circuits in Haskell that are easier to get correct or simulate for being in Haskell. Prior work includes using Haskell for digital design (i.e. Bluespec) and non-Haskell HDL's for analog components. There's definitely more possibilities for developing or modelling electronics in Haskell. :)


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


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.


> 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.


Yes indeed. Not only is it possible to overload + but Haskell's type system has absolutely nothing to say on the matter.


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.


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.


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.


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 ^+^.


> 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.


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.


F# also handles physical units in a really nice way.


See, "F# For Scientists"


This 1994 paper (using Scheme) might be pertinent:

Fields in Physics are like Curried Functions or Physics for Functional Programmers: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.49.1...


Haskell seems to be the language to learn if you're into physics, math and blockchains. I feel exactly like I did right before I became a web developer. I felt it was a no-brainer to learn Javascript. Is Haskell a no-brainer for 2025?


I find this is more of a distraction than abstraction at the moment, to be honest.

How would this approach generalize to more modern physics?


Very interesting Paper, though i would really want to see if games like Besiege could actually be used to teach classical Physics.


Are the lecture notes for the class available anywhere?




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: