So- Apparently I have no idea what I’m talking about when it comes to scheme-style quoting in haskell. Augustss Says you can’t do it because of- well, some reason beyond my comprehension. He’s a smart guy, so I’m inclined to believe him. However, I still think that what I proved in my last post holds- if not for quoting in haskell, then for quoting in scheme. The operations are still the same, dequote is just eval, quote is ‘. In any case, my goal wasn’t to really come up with some fantastic new insight into haskell, it was mostly just something I hacked on in my spare time- and thought might interest others. In any case, like I said, Augustss knows what he’s talking about, so take the last post with a grain of salt.
June 1, 2008
Quotable Haskell
> module Quotable where
This is a nice little code dump I was trying to have posted yesterday, I’ve finally got most of the formatting bugs worked out. I wasn’t thinking I was going to post this till I got about 2/3rd the way through it, so it’s pretty rough. Think of it as a look into my notebook. Enjoy.
May 31, 2008
Thar’ Be Gremlins
So- I was going to treat you all with a nice brain-dump style posting about Scheme-style Quoting in Haskell. But Wordpress is being bitchy, so it’s going to take a while, disregard any spam in your RSS feeds, it’s unintentional, I promise.
Sorry.
May 25, 2008
Basic Math, how to use numbers any day.
On reddit today, there was a comment about a radio personality who argued with a Mathematician about how percentages work, specifically about how percentages are combined. I replied with a short explaination of how 35% + 15% = 55.25% and not latex 50%. Shortly thereafter, there were half-a-dozen comments, ranging from “That explaination was longwinded/overcomplicated”, to “Thanks, that really made the idea clear.” I realized that, while I don’t hold a Teaching License, or Advanced degree in math, I am pretty good at it, and maybe teaching a bit of basic, useful math or programming that anyone can understand and use might be a pretty popular topic for blog posts, enter the new Sporadic Series “Basic Math, how to use number any day.” (To bastardize the tagline from Numb3rs, which is an excellent show, by the way).
The posts will be aimed at laypersons with no mathematical experience beyond 10th grade or so, we’ll talk about geometry and algebra, basic statistics and numerical stuff, maybe even some more abstract stuff, but always presented from a practical point of view.
For the inagural post, Let’s talk about percentages, we’ll aim to look at percentages from a qualitative, practical viewpoint.
May 22, 2008
New digs
Welcome to the new site, posting will soon restart after the admittedly long hiatus.
To update the crowd, I’ve recently been working with Android, the Google Phone platform, specifically working on a simplified interface for interphone communication via the GTalk interface. I’ve also been fiddling with GPS stuff. I’ll be posting about that soon, and about a decentralized persistent game I’ve been hacking on in Haskell.
December 30, 2007
Note on formatting
Turns out the import of posts from the blogger account didn’t go all too smoothly, alot of the formatting was lost in the translation. If you’d like me to fix up any particular post, I’ll be sure to get to it, but there are quite a few, so unless I’m asked, I don’t intend to fix them right away, (I’m a bit strapped for time right now, working on some school extra credit stuff). Hopefully I’ll get caught up on it. Note though, that the text itself is fine, just the formatting tags (pre and code tags, specifically) are being interpreted as text, and not markup. So if you copy and paste it (if its a literate haskell file, for instance) it should still work (and even look okay, maybe).
Sorry,
~~Joe
Monkey Multiplication Table Problem
This is a response to R. Andrews post here. I have dubbed it, “The Monkey Multiplication Problem.” it was fairly neat, and I was going to post a response in the comments of his LJ, but I didn’t want to bother to sign up for an account over there, so instead I’ll post it here.
Enjoy. And forgive the lack of comments.
I managed to do it in 9 lines of haskell (not including module, import, or type declarations) however, I don’t have any datasets larger than your 12×12 table to test against, the printout is kind of funny looking, it’s designed so that you turn it 45 or so degrees clockwise and it looks right, (comes from the fact that I generate it by columns)
code follows (17 lines, with spaces and extra decls)
module Table where
import Data.List
genTable :: Int -> [[((Int, Int), Int)]]
genTable max = map (genCol max) [1..max]
genCol :: Int -> Int -> [((Int, Int), Int)]
genCol max n = [((n,n), n*n)]
++ zip z (map (\(x,y) -> x*y) z)
where z = zip [max, max - 1 .. n + 1] (repeat n)
printTable :: Show a => [[a]] -> IO ()
printTable = putStrLn
$ concat
$ intersperse "\n"
$ map (concatMap (\x -> show x ++ " "))
monkeyTable = printTable $ map (map (snd)) $ transpose $ genTable 12
You can load that up in ghci and type in “monkeyTable” to get the printout, printTable, btw, is general enough to apply to anything- so if you’d like to see the internal structure of the table, you can switch that “map (map (snd))” to “map (map (fst))”. note that the ugliness of the monkeyTable function is from the fact that I used tuples instead of a customized datatype, or just a more specific genCol function.
Anywho, fun problem, I think I might use it in my local coding dojo, have fun!
~~jfredett
October 1, 2007
On Raytracers, The Quantum Kind
Well, maybe not quantum — but wavestream based light ray tracing software didn’t have the same, y’know, ring to it. Let me give you the backstory.
This semester I’m taking one of my requisite natural science classes, In trying
to find a class which looked the least boring, I managed to find an Optics class. In Optics, we learn about the dual nature of light, a subject which has always fascinated me. In much of mathematics and physics, there is an inherent duality, a separation between two objects which simultaneously brings them together. When you get to Optics, we find that light- in on of the weirdest twists ever - manages do be its own dual, separate from itself, if you will. Light, as we (maybe) well know, has both wave-like and particle-like properties. My Optics Professor calls it the “Packet of Wiggling String” interpretation. This interpretation helps to explain things like the double slit experiment. It is this particular experiment that I want to talk about now.
I’ve fiddled with ray tracers before in my life, but I’ve never thought to try the double slit experiment in one of them, I cooked up a little pov-ray script to test my theory that, in fact, ray tracing is classical. Granted thats an obvious result to most, but think about it, raytracing is classical. You can’t replicate the double slit experiment in Povray, more accurately, you can’t treat light as a wave in Povray. only as a particle stream.[1] As far as I can tell, this gross approximation of light– limited reflection calculation, particle stream vs wave, etc — was based on limitations of hardware when the technique was invented, they simply _couldn’t_ simulate things like the Double slit experiment. Perhaps more surprisingly, Prism’s don’t work the way they should either, since they won’t separate light based on wavelength.
NOTE:: As an aside, probably the most fascinating thing we have learned in optics so far is how prisms separate light into its component colors. I thought a short description might pique your interest, so here you have it.
When light travels through certain substances (usual called media (singular medium)), it slows down and actually bends due to a phenomenon called refraction. Refraction is really just an application of Fermat’s Principle (that light will always take the fastest path between two points[2]). The easiest way to see refraction is by looking through a magnifying glass, a magnifying glass is a medium made of glass, which has a special, dimensionless number called an “index of refraction” at around 1.5, air has a IOR of about 1, and a vacuum (like space, not like a hoover) has a IOR of exactly 1, there is no substance with an IOR
$latex n\sin(I) = n’\sin(I’)$
where n, n’ are the IOR’s of the two media (n being the IOR of the medium from which the light originated.)
and I,I’ the angles at which the light (or lifeguard) “impacts” the medium
This equation, called Snell’s Law (not snail, snell, it rhymes with “sell”), gives us a simple way to solve the lifeguard problem. By knowing the distances from shore both we, and the victim are[5] we can determine a shortest path using some trigonometry, which I’ll leave as an exercise to the reader, since I don’t have any good visualization software to draw all the necessary pictures (xpaint will _not_ be sufficient, and the 15 minute time limit on Cinderella2 is beyond
annoying.) Regardless, this is the same math that governs refraction, however, there is something I have not explained.
n is not a constant.
This shook my soul, at first, how can n not be a constant? If we have one uniform material, we assume no inconsistencies in the material when we do our math- the only possible thing it could depend on would be light itself, but if light is a uniform particle stream then this couldn’t be the case.
Shocking revelation number two, light isn’t a particle stream.
Refraction works on a particle stream, it makes _sense_ on a particle stream, in fact, the very reason for refraction really doesn’t make sense on a standing wave- because how can a infinitely long wave slow down? Thats just silly. So really, this whole refraction business leads us to a more quantum interpretation, but for simplicity, we’ll pretend it all works with waves.
n is a function of the wavelength of the light approaching the medium, this is important, because it tells us something interesting about light. Consider the prism, we all have seen how the prism can split apart light in an amazing display of party trickery into all its very pretty colors. Prism’s truly are the life of the optical party, useful for all sorts of stuff, from periscopes to spectrometers. In any case, how can a prism split white light into a bunch of different wavelengths? We can’t create something out of nothing, so we are left with only one explaination, white light _is_ all of the component colors. This leads us to see that really- when we see white light, we are seeing the superposition of many different wavelengths of light, and this gives us why a prism works. If n is just a function of wavelength, and white light is a superposition of different wavelengths, then each wavelength will bend more or less depending on _its_ own value for n, this means that when the light exits the prism, due to it’s shape[6], it will remain separated, and create a beautiful collage of colorfulness on whatever the light happens to land on.
So, enough rambling about Optics, what has all this got to do with raytracing? Well, I realized that you can’t build a prism in a raytracer, because it treats its light not only as a simple particle stream, but also as having a unique wavelength (of sorts) for each of its colors. In Povray, you specify color as a vector, nothing special, just a vector. Why not treat color as a series of wavelengths? Heck, we don’t even need to give up our lovely RGB method, theres
very likely a way to convert from wavelengths to RGB and back. We would have a problem with the way Raytracers currently treat light and color, since we say that objects and light _have_ color, when in reality light is usually white, and the things it touches _absorb_ color and have some amount of transparency, which gives the illusion of colored light. Potentially we could specify the color of light which is emitted and the color(s) of light which are absorbed by the surfaces we create- but the latter of that bit might be more difficult. This is besides the point[7]. I suppose what I am suggesting is that we consider ways to incorporate the wave nature of light into our raytracers, since we could potentially add quite a bit of very interesting new capabilities, like prisms, interference effects, etc. It would also add to the wonderful photorealism effects, I think, since the light would be specified in a way that is more like
reality.
Just thoughts, I suppose, I’m certainly no expert in Raytracing. However, oh dear Lazyintarweb, if you are, please- tell me whether this could actually work, maybe I’ll try to build it, someday.
[1] In fact, only as a particle. Since we only view the ray’s reflections once.
[2] In reality, the principle is stated (mostly) as follows: Light will always
seek the path which minimizes its travel time. There is a subtle difference, but I think for our purposes, the simpler statement suffices. Also note that fastest doesn’t necessarily mean shortest, since we’re dealing with speed changes too.
[3] I don’t think I’m wrong, but maybe exotic substances or whatever creates wormhole things might? I’m not sure how that works, I’m just a mathematician who likes pretty lightshows, not a physicist.
[4] Yes, I know there is the whole non-euclidean geometry of space thing, geodesics and whatnot, but bear with me.
[5] I never realized lifeguarding was such a deep realm of math.
[6] Namely, the triangluar shape of the classic prism prevents the light from bending back towards the normal, and reforming the normal white light. A thoroughly less satisfying party trick, to be sure.
[7] In fact, at this point, I have practically forgotten what the point was.
August 10, 2007
Why Testing code should be Laissez-faire
I’ve been working on the HFA Library some more, and got to the point now were I really want to start testing it deeply. Since HFA is in Haskell, the weapon of choice for testing pure code is QuickCheck. The interesting bit about QC is that test data is fundamental random. This is great for two reasons.
1) It forces you to actually test for _general_ properties of objects/functions, rather than specific properties of certain instances of those structures, like a unit test does
2) By Generating Random Data, you don’t have to worry about being too nice or too mean to your program, as with unit testing.
I’ll state it now, for the record, Unit Testing is great. I wouldn’t test my Java (or soon enough, Scala, ^_^) programs with (mostly) anything else. However, when you have referential transparency on your side, QuickCheck is a superior system.
The real brilliance of QuickCheck though, is that it is extensible enough that you can define new “checkable” items, that is, instead of having to use the standard checkable types when checking a function, you can define how QuickCheck can generate random data for a particular type by making it an instance of the Arbitrary class, which is defined by QuickCheck. This means that, as long as you can define a couple of methods for your datatype, it is damn near trivial to have QC generate examples of your datatype and test them quickly.
Why is this good? Well, consider you’re writing unit tests for your code. You’ve been intimately involved with this mangled peice of imperatively worded text for days and weeks. You know every inch of it, and you have in your mind the next new feature you want to add. It is not uncommon (at least for me) to begin writing and toying with the code in your mind, figuring out where potential bugs might be. As a human being, you are typically not looking to make things harder for yourself than needbe. So maybe, when you’re writing those unit-tests that will guide your programming to ensure that the code is written correctly, you — maybe subconciously, maybe not — decide to make those few unit tests a little bit easier on the part of the code that you think is more fragile. I’m guilty of this, certainly, and I’m sure if you’re honest with yourself and you’ve developed a good bit of code with the test-first methodology (which I like only mildly less than the test-shortly-after-you-realize-you’re-supposed-to-write-tests-first methodology), that you would find that you’ve done it too. QuickCheck fixes that wagon pretty quickly, you don’t get to reason about how hard some tests will be on the code, QuickCheck enforces a “Hands Off” or “Laissez-faire” (I’m french, and I like history, sue me.) form of testing. You don’t get to decide what is tested, just what the result should be, which is how it _should_ be done. I _shouldn’t_ be thinking about what data I want to test, I shouldn’t have to write all the test-data, ideally, I should only have to say, “minimizing a DFA twice is equivalent to minimizing a DFA once” or “if the regular expression foo is generated by DFA f, and the expression foo’ is generated by the minimized version of f, then foo == foo’ for all f::DFA.” I guess the point is, the computer is unbiased, it won’t be nice to the code, it won’t be mean to it, it’ll be fair. Is that to say that coders will be biased towards or against their code? Yes, it is, we spend alot of time with these projects, we develop a vested interest in seeing them work, finding out that you did something wrong can be discouraging. Granted, small things may not devastate you, like using the wrong function name, or misplacing a variable. But if you’re unit test catches a major flaw in the structure of a program you designed, that represents alot of work that just got blown to peices. At the very least, if not for your pride, testing systems like QC are good for your productivity, they allow you to test a (potentially) huge number of arbitrary cases every time you run your program. Heck, you could even have QC running live against your code in the background, telling you in real time what tests your failing, what cases gave you failures, etc. All of that is Automatic and Vital data, its 24/7/365 testing of your code, for free. Slowly building assurance that your code is, in fact, doing the right thing on all inputs.
By the way, Yes, I know many, many people have written about QuickCheck before, and about how wonderful it is, but I think it’s always worth saying again. Good Ideas deserve to be talked about, QuickCheck is a _very_ good idea.
August 5, 2007
DFAs, Categories, and Typecheckers.
I’ve recently started reading (in parallel) “Type and Programming Languages” and “Basic Category for Computer Scientists.” The latter of which is really only interesting if you’re a math junky, like me. It’s somewhat dry, and very matter-of-fact, but the subject is terribly interesting.
Whilst reading these books, I’ve also been working on a library for Haskell called “HFA” (pronounced: “Huffa”, or if your feeling silly, “Hoffa”), for “Haskell Finite Automata.” The library’s purpose is to create a simple to use, generic, relatively fast implementation of various Automata (Notably (D|N)FA’s, PDAs, Turing Machines, etc.), so that anyone intending to use these abstractions will be able to without knowing much about the internal theory, eg how to minimize a DFA, or how to convert an NFA to a DFA, etc. It’s also intended to be available as a easy to understand tool for learning/teaching about automata, it will eventually have the ability to display Automata as Graphviz graphs, and is currently capable of generating state diagrams (with some extensions to mark final, initial, and initial-final states).
Recently, I had just finished writing some refactor code for HFA, and decided to take a break and read “Basic Category Theory” for a while, it dawned on me upon looking at the diagram of a category that what I was looking at was essentially a DFA, with all states final, and the arrows between them being processing parts of the Delta Functions. That is, if a Category is defined as a a set of objects, and a set of arrows (where an arrow is defined as f : A -> B, where A and B are objects in the category), then the equivalency is as follows:
Category DFAObjects StatesArrows Transitions
with delta(S,x) = S' iff there is an arrow between x is an arrow between S and S’.
Notably, we can also define a DFA as a Category by simply reversing the definition. I’m pretty sure this fact has been discovered before, its to obvious to believe otherwise (though it would be cool I could name this “The Fredette Isomorphism”, ^_^). The interesting thing about this Isomorphism is that, if we can generalize a DFA, whats to say that we couldn’t generalize the category in the same way? Take Imperative languages for instance. I don’t know if it works out (and I certainly don’t have the skill to prove it if it does work out, at least not yet), but it is a hypuothesis of mine that an imperative program can be represented in a category with multiple arrows going from one object to another simultaneously, that is, an imperative program is a kind of “Nondeterministic” category. Ring any bells? We know (by the simple fact of Turing completeness) that a TC imperative language program can be written in a TC Pure Functional language (assuming there’s a Pure Functional way to deal with State, eg Monads). Similarly (and this is almost to much of a guess to even _think_ of it as a hypothesis) if a TC Imperative Language is a “Nondeterministic” (ND) category, then if a ND Category is isomorphic to a NFA, then we know that NFA’s are isomorphic to DFA’s, and we know that Pure Functional Languages are isomorphic to operations withing a “Deterministic” Category, eg a “DFA”, so that would “prove” (I use that term _very_ loosely) that any old TC Imperative program has an equivalent TC Pure Functional Program.
Pretty Neat, huh? It probably doesn’t work somewhere, but still- it’s cool.
We can further use the DFACategory relationship as a kind of simple “composition” checker.
If the States of our DFA are types in a language, and the transitions functions mapping one type to another, then we can say that if the delta function is defined as above, and in the case there is no defined transition between some state S and some other state S’, and if such a transition is called for in the delta function, then we simply send the output to a non-accepting “fail” state.
Here, the simple language consists of the following.
The Category contains:
Objects = {Int, Float, Char, Bool, Unit}Arrows = {isZero :: Int -> Bool, ,ord :: Char -> Int ,asc :: Int -> Char ,sin :: Float -> Float ,not :: Bool -> Bool}Values = {zero :: Int, true :: Bool, ,false :: Bool, unit :: Unit}
The corresponding DFA works something like this
f1,f2, ... fn are symbols which have type an -> bn, where n is the same value as the nth symbol, and a and b are not type variables, eg: f1 :: Int -> Char, a1 = Int, b1 = Charv is a type (f1 . f2 . f3 . ... . fn) v => ([f1, f2, ..., fn], v)=> [a1,b1,a2,b2, ..., an,bn,v]=> [(init,a1),(b1,a2),(b2,a3),...,(bn,v)]
given this list of pairs, we define the DFA trace function as follows, this presumes a list like the one from above.
trace :: State -> [(Sym,Sym)] -> Booltrace st [] = (st /= failState)trace st [(s1,s2):syms] | s1 /= s2 = False | otherwise = trace (delta st (head syms)) syms
where failState is a pseudonym for whatever the magic non-accepting failure state is
and where delta simply points to the next state (be it the fail state, or otherwise). I’ll cover that in more detail in my next post (I’m actually going to build this little guy for a simple language like the one above.)
I’ve digressed a bit from my topic, my goal was to show that Categories are really terribly neat, and apparently related to automata, which most people understand pretty easily, if they are explained well. I don’t pretend to be an authority here, but hell, implementing a (very) simple type checker is a pretty cool feat, considering It was only a few months ago I started learning Haskell. I know that this isn’t a robust, powerful mechanism, but as far as I know, given Compose (the (.) function in Haskell) apply (($) in Haskell) and a few other functions, you have a TC language, a la Backus’ FP or FL systems.
Anyway, next time I intend to implement this little type checker, and (hopefully) eventually implement a (full) type checker for something akin to FP or FL, using this DFA style approach. Heck, I’d also be able to play with parsing (another automata rich subject).
Also, for those interesting in looking at HFA, it’s available at
http://code.haskell.org/HFA/testing
you can just do a darcs get to pull everything down.
###DISCLAIMER###
I don’t intend to present any of this as proven, either formally or by any other means, the ideas and conjectures in this post are just that, conjectures. Please, don’t believe I’m an expert, I’m still learning about all these things, and I don’t want to lead anyone down the wrong paths under the assumption I actually _know_ what I’m doing.
That said, I do think the conjectures made have some validity, if you know otherwise, please inform me. Thanks
~~Joe