Learn Physics by Programming in Haskell

(arxiv.org)

468 points | by jxub 2253 days ago

16 comments

  • alew1 2253 days ago
    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...

  • privong 2253 days ago
  • amelius 2253 days ago
    • privong 2253 days ago
      There's also this one, "Structure and Interpretation of Classical Mechanics" (by the same authors): https://mitpress.mit.edu/books/structure-and-interpretation-...
      • hnarayanan 2253 days ago
        And here are some source files and setup notes for the underlying SICM scheme library pertinent to macOS: https://github.com/hnarayanan/sicm
      • wirrbel 2253 days ago
        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.
        • tntn 2253 days ago
          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?

          • wirrbel 2252 days ago
            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.

          • abecedarius 2253 days ago
            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.)

    • sitkack 2253 days ago
      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...

    • BaryonBundle 2253 days ago
      And the previous HN discussion on this exact topic had some good notes: https://news.ycombinator.com/item?id=9003898
  • kuwze 2253 days ago
    There is also the sequel “Learn Quantum Mechanics with Haskell”[0].

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

  • JoshMnem 2253 days ago
    "Learn <subject> by Programming in <language>" would be a popular series of books. I hope someone writes them.
    • GuiA 2253 days ago
      I recommend “Music for geeks and nerds”, which is an intro to music theory/composition with python

      https://pedrokroger.net/mfgan/

      • JoshMnem 2252 days ago
        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.

    • JoelMcCracken 2252 days ago
      Wow, this sounds so good. Please someone, more like this.
  • 77pt77 2253 days ago
    • signa11 2252 days ago
      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

    • fizixer 2253 days ago
      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.
    • bringtheaction 2253 days ago
      Never heard of that book. I enjoyed SICP a lot so if this is anything like SICP was then I would be very happy.
      • 77pt77 2252 days ago
        It's SICP for Classical Mechanics, all the way up to Hamiltonian/phase-space formalism, including perturbation theory and canonical transformations.
      • tchow 2253 days ago
        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?
        • jupiter90000 2253 days ago
          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.

        • shakna 2252 days ago
          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

        • Buttons840 2253 days ago
          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.
        • fsloth 2253 days ago
          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.
        • bringtheaction 2252 days ago
          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.

        • jonjacky 2251 days ago
          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.

        • emmelaich 2252 days ago
          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.

          • Jtsummers 2252 days ago
            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.
            • nikofeyn 2252 days ago
              cs at mit is in the eecs department.
              • Jtsummers 2252 days ago
                Oops, yeah. That wasn't supposed to be past tense.
                • nikofeyn 2252 days ago
                  haha. no worries. :)
          • 0x445442 2252 days ago
            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.

        • billsix 2253 days ago
          On Lisp by Paul Graham is more exciting and inspirational.

          Sicp is solid fundamentals.

          Read both.

        • professor_plum 2253 days ago
          You might in fact love it. You'll get a nice overview of how interpreters actually work, for one thing.
        • albertvila 2252 days ago
          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

    • lrc 2251 days ago
      The Scheme code that inspired this can work symbolically as well as numerically, since it contains a computer algebra system at its core.
    • nickysielicki 2252 days ago
      Is this available in a proper typesetted form?
    • agumonkey 2253 days ago
      heh first thing that came to mind. I'm still curious about repl lagrangian mechanics
  • pducks32 2252 days ago
    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.
  • huntie 2253 days ago
    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.
    • Steuard 2252 days ago
      [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.

    • adyavanapalli 2252 days ago
      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.

    • siddboots 2252 days ago
      Leonard Susskind's Classical Mechanics course: http://theoreticalminimum.com/courses/classical-mechanics/20...
    • amai 2251 days ago
      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

    • mhh__ 2252 days ago
      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.)

      • contact_fusion 2251 days ago
        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

        • mhh__ 2250 days ago
          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.

    • Disolation 2253 days ago
      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.
    • lymitshn 2253 days ago
      You can take a look at Feynman lecture notes at caltech.edu.
    • sitkack 2253 days ago
      Mentioned in another branch of this discussion, but I really liked "From Newton to Mandelbrot" for a computational take on learning Physics.
    • Goosee 2252 days ago
      John Taylor "Classical Mechanics"
  • voidmain 2252 days ago
    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...

  • nickpsecurity 2253 days ago
    " 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. :)

  • Maro 2253 days ago
    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

    • jason-johnson 2252 days ago
      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.
    • runeks 2253 days ago
      > 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.

      • tome 2253 days ago
        Yes indeed. Not only is it possible to overload + but Haskell's type system has absolutely nothing to say on the matter.
        • Maro 2252 days ago
          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.
          • dllthomas 2251 days ago
            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.

    • KirinDave 2253 days ago
      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.

    • thepratt 2253 days ago
      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 ^+^.
      • marcosdumay 2253 days ago
        > 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.

    • nickpeterson 2253 days ago
      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.
      • professor_plum 2253 days ago
        F# also handles physical units in a really nice way.
        • sitkack 2253 days ago
          See, "F# For Scientists"
  • jonjacky 2252 days ago
    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...

  • mhh__ 2252 days ago
    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?

  • anonytrary 2252 days ago
    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?
  • pankajdoharey 2251 days ago
    Very interesting Paper, though i would really want to see if games like Besiege could actually be used to teach classical Physics.
  • garfieldnate 2252 days ago
    Are the lecture notes for the class available anywhere?