Tagged Scheme
Promise as a basic language feature
Posted on by Idorobots
Just the other day, while programming in Scala, I was thinking how nice it is that Haskell hides non-strictness (a.k.a. laziness) underneath, so no obnoxious lazy stream creation/forcing needs to be done. And then it struck me: how cool would it be to treat Promises in a language the same way that Haskell treats lazy values? Would such a language even make sense? How would you program in such a language?
Let's explore! You can find all the code included in this post in a more useful form here.
Promise
Here's a very simplePromise
implementation written in Scheme, with support for asynchronous resolving & error rejection, which we'll need in order to explore this weird proposition:(define *promises* '())
(define (make-promise value state then handle thunk)
(list '&promise value state then handle thunk))
;; Field accessors omitted.
(define (promise fun)
(let* ((val '())
(state 'pending)
(on-resolve id)
(on-reject id)
(resolve (lambda (v)
(set! val v)
(set! state 'resolved)
(on-resolve val)))
(reject (lambda (e)
(set! val e)
(set! state 'rejected)
(on-reject val)))
(then (lambda (t)
(if (equal? state 'resolved)
(t val)
(set! on-resolve t))))
(handle (lambda (h)
(if (equal? state 'rejected)
(h val)
(set! on-reject h))))
(p (make-promise (lambda () val)
(lambda () state)
then
handle
(lambda () (fun resolve reject)))))
(set! *promises* (cons p *promises*))
p))
Recently, I came across this post about implementing custom type systems using Prolog and it inspired me to revisit an old logic-programming-related project of mine.
About a year and a half ago I took a shot at using Prolog for type reconstruction (a.k.a. type inference), but that resulted in a load of not much for one reason or another. I did write this Rule-based system shortly thereafter, however I did not give much thought to using it for type reconstruction. Let's change it...
(assert! (: map (-> ((-> (?a) ?b) (list ?a)) (list ?b))))
Templated Monads? Monadic Templates?
Posted on by Idorobots
So, I finally found some time for a Template Metaranting follow-up post. This time let's get down to business as this one contains a fair amount of code.
Sadly, I won't rant as much but instead I'll try to show how awesome D's templates really are. We'll write a piece of code, based on this Scheme implementation, that is, a simple monad) that we'll use to build a binary tree, with uniquely numbered nodes containing their height, without any global state (therefore purely) entirely at compile time.
Continue reading(car '(2 3))
Posted on by Idorobots
It's been a while since my last post, more than I'd wish for actually, and that is because I was hunting a man by the name Iain Buclaw on the IRC. I had to make sure he was ok with me using some of his work in this next project/post (and he obviously is, oh the kind gentleman he is).
Last time I shared some thoughts on latmab, a simple algebraic calculator, I have... commited and todays topic is somewhat related. It's a continuation I'd say, but let's go on with the story...
Continue reading