Tagged Lisp
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))))
Blog reboot!
Posted on by Idorobots
λ-blog
Finally got around to rebooting my old dev-blog! This time instead of fighting my way against some new unforgiving piece of blogging software I've decided to write my own piece of even less forgiving blogging software.
Let's see how it goes this time...
In case you are wondering, here are some questions & answers regarding λ-blog:
Continue readingASM development log 4: Tiny MOP
Posted on by Idorobots
Finals are comming, so I might as well hack around a bit with ASM...
It's been a while since the last status update and I hate to admit it, but not much has changed in ASM repo. I was messing around with combinators in an attempt to simplify ASM' environments, and I was experimenting with new immutability semantics, since at the moment it's pretty much a one big copy-vs-alias mess. There are still a bunch of unresolved issues here and there concerning efficiency mainly, but if ASM is to be relevant in 100 years of time I'd much rather sacrifice efficiency for simpler semantics and expressive power.
Lastly, I was thinking about a way of exposing as much of ASM' semantics as possible and allowing their run-time overriding, using a Metaobject protocol:
(import 'samples.tinyclos)
(defmethod apply ((v Vector) n)
(write "Applying a vector to something? Are you " n "?"))
(apply '[1 2 3 4] '(nuts))
ASM development log 3: DSLs
Posted on by Idorobots
"Kajtek, you incredibly handsome stallion, what have you been doing these past few months?" - you might ask, concerned about the lack of ASM dev logs recently...
Well, I've mostly been studying various PL design quirks and prototyping neat features such as vau calculus flavoured fexprs or the following piece of what I consider art.
As mentioned before, I was working on an extensible reader that would support user-defined reader macros for easy DSL programming. It took me quite some time, effort and researching but eventually I came up with an awesome solution. It's worthy to mention here, that it's in no way a new solution. I prefer to figure stuff out myself and often times it turns out that similar concept already existed a few years before mine. Bummer u_u
.
One of the most distinctive features of Common Lisp and Lisp in general, are its code-generation and code-manipulation capabilities.
Probably the best example is the LOOP
macro - a Swiss Army knife of iteration that can do pretty much anything. The following snippet iterates a list of random numbers collecting some statistics of its contents and does that while being very concise and readable:
(let ((random (loop with max = 500
for i from 0 to max
collect (random max))))
(loop for i in random
counting (evenp i) into evens
counting (oddp i) into odds
summing i into total
maximizing i into max
minimizing i into min
finally (format t "Stats: ~A"
(list min max total evens odds))))
Stats: (0 499 120808 261 240)
Instant docs in StumpWM
Posted on by Idorobots
Here's a cool hack I use to optimize my docs searching.
Let's start off with DuckDuckGo search engine. By itself it's a pretty powerful tool thanks to its numerous features like the !bang syntax. For example searching for:
!cpp std::string::clear
...takes me exactly where I want.
Let's use it to our advantage, shall we?
StumpWM is a tailing window manager that allows you to define system-wide key bindings that work and feel pretty much like Emacs ones. Combining that with DuckDuckGo'es !bang syntax makes you just a few clicks away from anything out there:
(defcommand duckduckgo (phrase) ((:string "Search: "))
"Searches for something on DuckDuckGo."
(run-shell-command
(concatenate 'string
*your-fav-webbrowser*
" http://duckduckgo.com/?q="
(substitute #\+ #\Space phrase))))
(define-key *root-map* (kbd "d") "duckduckgo")
Now, if you want to find out if I used substitute
correctly all you have to do is:
C-t d !lisp substitute
...what will take you directly there. Turns out I did.
But wait, there's more!
Continue readingASM heatmap
Posted on by Idorobots
I had very little time for this blog lately, but this is about to end soon enough. Here's a quick post showing ASM' keyboard distribution.
Continue readingASM development log 1: OliviaBOT
Posted on by Idorobots
Hello fellow programmers. Look at your favourite programming language.
Now back to Lisp.
Now back to your favourite language.
Now back to Lisp.
Sadly, it isn't Lisp, but if you stopped wasting your time, it could feel like it's Lisp.
Look down.
Back up.
Where are you?
Reading in OldSpice-guy's voice as I introduce the ASM programming language.
What's on your mind?
Back at me.
ASM has it. The expressiveness and brevity augmented by powerful and extensible features.
Look again.
ASM is now usable.
Anything is possible when your programming language is functional and not imperative.
It's highly munctional.
ASM development log 0: The Game
Posted on by Idorobots
It's high time for more Another Syntactic Monstrosity examples, but first let's take a look at its development.
devLog
I spent the last few days on redesigning the parsing strategy, ATM the interpreter reads the raw input, lexes it using static, immutable grammar specs and then parses the token stream into an AST, all this hardcoded in D.This proves to be fast, but it's nearly non-tweakable. That's not what I intended ASM to be and it makes some neat features, such as overriding Tuple evaluator, impossible.The new parsing strategy reminds of CommonLisp reader and reader-macros:
- The raw input is passed to the lexer (written in D) that uses dynamic syntax table - a set of regular expressions describing the syntax (defined in ASM) to tokenize the input into a token stream.
- Next, the token stream is passed to the parser (written in ASM with default implementation in D), that uses ASM functions, correlated to the syntax table, to dispatch the token stream and translate it to basic, lispy S-expressions (see LR parser for details on how it's done).This will allow ASM to act like any other language, given there's the syntax table available, making it perfect for DSL programming.
For example:
(lambda (x y) (pow (+ x y) 2))
...might be written as:
[x y => (x + y)^2]
...and ASM will happily accept it. Awesome!
Continue reading(car '(3))
Posted on by Idorobots
Finally. Good job, Lazy-me, keep it up! Or shall I say, 'keep it down', dohoho!
So we've seen latmab - the calculator and Scheme - RNoRS Scheme interpreter, both leading to the following, somewhat fun story... After having basics of Scheme implemented even though I did not have any previous Lisp experience, I figured I could do even better. Hell, let's actually learn some Lisp, said my inner smoking-hot stallion-programmer-guy!
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