Haskell fun

Nov 14, 2007 12:16

I've been working on implementing games in Haskell. Specifically, rules-driven board/card games.

When these games run, they often need to ask a player to make a move. This involves interacting with the outside world in some way. The most obvious way to do this is to embed your computation in the IO Monad:

type Game a = IO a ( Read more... )

haskell, geeky

Leave a comment

Comments 9

sarahnade November 14 2007, 23:14:16 UTC
I don't understand this, can you explain it in a cat macro plz?

Reply

OK ryani November 15 2007, 22:23:36 UTC
Look here.

In particular,

... )

Reply

Re: OK sarahnade November 16 2007, 00:26:08 UTC
It's a kitty!!

Reply


jccw November 15 2007, 17:47:29 UTC
Cool. I knew GADTs would turn out to be useful for something...

BTW, this reminds me of various encodings of things like state or regions using polymorphism, like in a paper by Fluet and Morrisett (ICFP '04, I think).

Reply

GADT usefulness ryani November 15 2007, 22:22:18 UTC
The best uses of GADT's I've seen all end up using them in some form or another as type witnesses; evidence that an object of a certain type is supplied and/or required.

I wrote a strongly typed lambda-calculus interpreter using GADTs; it's a tougher problem than it looks. Give it a shot if you feel like a little thought-challenge, starting with this:

data NDecl -- no variables
data Decl a as -- top-level variable of type a, along with more variables
type Name = String

data Expr ty dcl where
EPrim :: Prim ty -> Expr ty dcl -- optional (advanced implementation)
EAp :: Expr (t0->t1) dcl -> Expr t0 dcl -> Expr t1 dcl
EFix :: Expr (ty->ty) dcl -> Expr ty dcl
EVar :: Expr ty (Decl ty dcl)
ELam :: Name -> Expr ty (Decl a dcl) -> Expr (a -> ty) dcl
EPush :: Expr ty dcl -> Expr ty (Decl ty2 dcl)

type NExpr ty = Expr ty NDecl

expr_id :: NExpr (x -> x)
expr_id = ELam "x" EVar

expr_const :: NExpr (x -> y -> x)
expr_const = ELam "x" (ELam "y" (EPush EVar))

eval :: ExprNoFreeVars ty -> ty
eval expr = ?Hint: I ( ... )

Reply

Re: GADT usefulness ryani November 15 2007, 22:24:36 UTC
oops, eval should have type NExpr ty -> ty

Reply

Re: GADT usefulness jccw November 15 2007, 22:40:35 UTC
No, sorry, I was being facetious.... I was more amused because (with Ralf Hinze) I did some of the early work (concurrently with papers by Baars/Swierstra, Xi/Chen/Chen) on GADT-like things a while ago... at the time, my feeling was "neat, but probably not useful for anything in the real world". Maybe I should have stuck with it.

I should take a shot at the your example, though, since I haven't actually used GADTs as implemented in recent ghc's.

Reply


shkoo March 10 2008, 23:08:27 UTC
Thanks for turning me on to haskell. It's been a while since anything has made my brain hurt (but in a good way).

I'm trying to solve some of the project euler problems, and I'm trying to use the backtracking of the list monad to find solutions (working on 185 right now). It's cool to have a completely different way to think about control flow.

Reply


Leave a comment

Up