Live data from Hacker News

Kamby – A programming language based on Lisp that doesn't seem like Lisp

kamby.org

51–53 of 53 posts

Re: Kamby – A programming language based on Lisp that doesn't seem like Lisp

#51
I made a small evaluator in TXR Lisp for what appears to be my understanding "Kamby-like" semantics.

  $ txr -i kamby.tl 
  Syntactic toffee recipe: melt butter over low heat, stir in Lisp macros.
  1> (kamby-repl)
  kmb> (let dz (/ 10 0))
  ** error: /: division by zero
  kmb> (let a 1)
  a
  kmb> a
  1
  kmb> (let foo [(let bar1 42) (let bar2 73)])
  foo
  kmb> foo
  #
  kmb> foo.bar1
  42
  kmb> foo.bar2
  73
  kmb> (set a 4)
  4
  kmb> a
  4
  kmb> (set a (* 2 a))
  ** warning: unbound variable a
  ** error: unbound variable a
Errors out at the end because unknown forms are evaled as Lisp, and Kamby variables are not Lisp variables.

Mostly this was to play with the "construct environment tree as-you-go, with qualified pathname access" idea.

Code:

  (defstruct kamby-env ()
    bindings      ;; assoc list
    next-env      ;; kamby-env object or nil

    (:method extend (me sym value)
      (upd me.bindings (acons sym value)))

    (:method lookup (me sym)
      (assoc sym me.bindings))

    (:method delete (me sym)
      (let ((binding (assoc sym me.bindings)))
        (upd me.bindings (remq binding))))

    (:method print (me stream pretty-p)
      (format stream "#" 'kamby-env [mapcar car me.bindings])))

  (defvar *kamby-env* (new kamby-env))

  (defun kamby-error (form fmt . args)
    (error `~s: ~s: @fmt` 'kamby-eval (if (atom form) form (car form)) . args))

  (defun kamby-eval (form)
    (let ((e *kamby-env*))
      (match-case form
        ((let @var @expr) e.(extend var (kamby-eval expr))
                          var)
        ((set @var @expr) (let ((binding e.(lookup var)))
                            (if binding
                              (rplacd binding (kamby-eval expr))
                              (kamby-error form "no such variable: ~s" var))
                            (cdr binding)))
        ((del @var) (unless e.(delete var)
                    (kamby-error form "no such variable")))
        (@(symbolp @var) (let ((binding e.(lookup var)))
                            (if binding
                              (cdr binding)
                              (kamby-error form "no such variable: ~s" var))))
        ;; TXR Lisp qref syntax   a.b.c.d   (qref a b c d)
        ((qref . @vars) (let (value)
                          (while vars
                            (let* ((var (pop vars))
                                   (binding e.(lookup var)))
                              (unless binding
                                (kamby-error form "no such variable: ~s (when resolving ~s)" var form))
                              (set value (cdr binding))
                              (if (and vars (not (typep value 'kamby-env)))
                                (kamby-error form "~s isn't an environment object; cannot lookup ~s"
                                             value (car vars)))
                              (set e value)))
                          value))
        ;; TXR Lisp dwim brackets syntax   [a b c]   (dwim a b c)
        ((dwim . @forms) (let ((*kamby-env* (new kamby-env next-env e)))
                           [mapdo kamby-eval forms]
                           *kamby-env*))
        (@else (eval else)))))

  (defun kamby-repl ()
    (whilet ((line (progn (put-string "kmb> ") (get-line))))
      (catch
        (prinl (kamby-eval (read line)))
        (error (x)
          (put-line `** error: @x`)))))

Re: Kamby – A programming language based on Lisp that doesn't seem like Lisp

#52
post #22

> Variables are not unique. You can declare multiple values for the same variable name using ":=" and append in the stack. If you want to edit the last declaration, just user simple "=" operator. To remove the last variable with specific name in stack, use "del varname" Isn't this very confusing in almost all instances?

Forth behaves the same way. The vocabulary is really a stack, and you can define words, redefine them, and then pop them back off to return to the old value.

It's really not useful for variables, but can be very useful for functions, and since they're pretty much the same thing, you get the behavior for free. Although I haven't touched Lisp, my understanding is that Forth and Lisp have some conceptual similarities, but Forth is written by an engineer and Lisp by a computer scientist. Limit you for your own safety? Not a thing in Forth.

Re: Kamby – A programming language based on Lisp that doesn't seem like Lisp

#53

By the way, I always wondered why not just take a lisp, put every single thing (even an operator) on a separate line and indent every level of parentheses with a space or two (4 spaces is too much - deep nesting would go too much to the right). So there would be no need for the parentheses in most cases.

Something like Hebigo? https://github.com/gilch/hebigo#examples
Post reply on HN