Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lisp-like configuration using code in Haskell

Tags:

haskell

I'm writing a raytracer in Haskell and, currently, I define my scene in code like so:

(Scene [(Sphere (Vec3 1 0 0) 4 (PhongMaterial (color 1 0 0) (color 1 1 1) 4))]
       [(PhongLight (Vec3 0 0 0) (color 1 1 1) (color 1 1 1))])

This works really well in terms of expressivity, and it's great because I don't have to write any sort of parser, but it means that I have to recompile every time I want to render a different scene. I've come to Haskell through Lisp, where this would be simple (load a file, eval the contents and then render the result) but I recognize that Haskell has traits that makes that, if not impossible, then very difficult.

Do any of you much more experienced Haskellers have any suggestions for the best way to go around solving this? In an ideal world, I'd have some file external to my code that defined the scene in Haskell syntax which I could load; in the least-ideal world possible I'd be writing a parser in Parsec. Thanks!

like image 347
Haldean Brown Avatar asked Oct 31 '11 13:10

Haldean Brown


2 Answers

If you make sure all of your data are instances of Read (... deriving Read), then you can just read them :: Withatypeifnecessary.

An intermediate solution would be to use json; parsing is easier than using Parsec, but of course it's a bit harder than just read-ing in code.


Update: if there are non-constructor functions, the read approach won't work.

like image 68
Matt Fenwick Avatar answered Oct 07 '22 00:10

Matt Fenwick


In this case, could you simply use read (assuming that everything does deriving Read). It's obviously a little clunky but chances are it would probably work (and it's less work than going down the parsec route).

like image 22
Jeff Foster Avatar answered Oct 07 '22 00:10

Jeff Foster