Author Topic: Lisp in Basic  (Read 208227 times)

0 Members and 2 Guests are viewing this topic.

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #765 on: September 12, 2014, 11:11:21 AM »
Hi Mike,

"that ancient"  -- I have a copy of the original Lisp, rewritten in ...  Lisp  !!
(I was born the eleventh year after the end of the Great War .. it was dark and snowing , that's all I remember  8)

"Did you know that almost all ..  " Never been looking under the hood of a Lisp-system .. but I had a book describing how to build a Lisp compiler , ...  in the end the compiler compiles itself ..

As for Common Lisp (and very similar Scheme) the macro's work as this

::  CL flavor ;;

(defvar a 2)
(defvar b 3)
(defvar operator '+)
(defvar L '(1 2 3))

the mechanisms are the backquote (quasiquote)  `  and the ,   (comma)

the backquote starts the copy , the comma forces the evaluation.

`( * ,a ,b)  copies to (* 2 3)   
`( , operator ,a ,b) copies to (+ 2 3)  , so : (eval `( , operator ,a ,b)) gives 5

it also has the "splicer"  ,@  (removes the parentheses)

`( ,op ,@L) copies to  (+ 1 2 3)   so (eval `( ,op ,@L)) -> 6

the expansion is defined by : p.e.

(defmacro f (operator a b)
  `( ,operator ,a ,b))

(f * 5 6)  -> 30

...  or something as :

(defmacro infix (a operator b)
 `( ,operator ,a ,b))

(infix 6 + 7) -> 13

NewLisp expands its macro's during run-time /  CL during read-time   (the latest NewLisp now also has (macro   ) doing the expanding as well during the program initiation.

best Rob
« Last Edit: September 12, 2014, 11:32:39 AM by RobbeK »

JRS

  • Guest
Re: Lisp in Basic
« Reply #766 on: September 12, 2014, 11:46:18 AM »
Quote
I was born the eleventh year after the end of the Great War .. it was dark and snowing , that's all I remember

My memory of that time was moving from Cleveland OH (across from the steel mills) to the country where my Dad bought a farm. Mom almost burnt down the house when she was burning cardboard boxed in the fireplace and forgot to open the vent.  ::) Love you Mom! (Mom and Dad are gone now - passed last year)  :'(

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #767 on: September 12, 2014, 11:01:23 PM »
1945 plus 11 is 1956 which makes us coevals. :)

I was born on the isle of Sakhalin (that's on the Pacific coast of Russia near the Japanese islands) where my Dad, an army officer, served after the war. The maternity hospital was a two storey building but there's so much snow in March on Sakhalin that my Dad had to lie down on his stomach in the snow to peep through the top of the window into the room on the upper floor where my Mom was holding me high up in her hands showing me to him. And when it was time to take us home from the hospital, we rode on the armor of a battle tank because no other vehicle could break its way through the snow. :) My parents passed away 35 years ago in a car crash but I keep seeing them in my dreams, and now more and more often than before.

Yes Rob, I've looked through the LISP macro basics in the r6rs sources. They look almost as powerful as a full-blown C preprocessor and I'm sure they've been brought to perfection over the countless decades that this language exists. Frankly, I'm becoming more and more possessed by LISP and I would gladly include its clone in the FBSL armory if only I manage to design a reliable JIT compiler for it. But this will take time.

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #768 on: September 13, 2014, 06:08:00 AM »
Hi, John , Mike

Life , sometimes isn't pleasant -- , father drowned in 2006 (and I had to identify him) -- there's more water than land here ....

...   March 27th to be exact and it was snowing , .. is that the Vladivostok regio, Mike ??

" more and more possessed by LISP"  8)  héhé

It's such a simple language , just like the "Elements of Euclides" , it is based on almost nothing -- Euclides used the point, the line and the circle, and lisp does     list , cons , car , cdr , let , quote , eval

-------   another sample   ---------    AI like

;; NewLISP flavor ;;;
;; code by RobbeK
(define-macro (car x) (first (eval x)))
(define-macro (cdr x) (rest (eval x)))
(define-macro (cadr x) (first (rest (eval x))))              ;; i don't like the modernistic (first ) and (rest )

(define (mono? L)
  (apply and (map (curry = (car L)) (cdr L))))
 

(define (dif* L M)
   (if (= (length L) 1) (reverse M)
          (dif* (cdr L) (cons (- (cadr L) (car L)) M) )))

(define (dif L) (dif* L '()))

(define (integrate* L M)  ;; needs a start value !!!!
   (if (null? L) (reverse M)
       (integrate* (cdr L) (cons (+ (car L) (car M)) M))))

(define (integrate L startvalue)
   (integrate* L (list startvalue)))

(define (analyze L)
   (let ((depth 0) (M L) (zo '()) )
     (while (and (not (mono? M)) (!= (length M) 1))
        (push (car M) zo)
        (++ depth)
        (setq M (dif M))
        )
 
     (if (= 1 (length M))  (println "I'm not clever enough to do such things")
         (begin
           (println "found a solution at depth " depth)
           (println "I'll do a reconstruction ")
           (print "Number of elements in the list to expanded : ")
           (letn (( nr (integer (read-line)))
                 ( list0 (dup (+ (car M) 0L ) nr)))
             (dotimes (i depth t)
                (setq list0 (integrate list0 (zo i) ))
                )
             (println "")
             (println "The result is " list0))))))

(define (demo)
  (println "This is a demo of differentations / integrations of sequences")
  (println "-------------------------------------------------------------")
  (println " ")
  (println "something simple to start with ")
  (let (( s "(analyze (list 1 2 3 4 5 6 7 8))"))
     (println s)
     (println " ")
     (eval-string s)
     (print "\nSomewhat more difficult : ")
     (setq s "(analyze (list 1 4 9 16))")
     (println s)
     (eval-string s)
     (println " ")
     (print "\nOK, try this : ")
     (setq s "(analyze (list 0 3 8 15))")
     (println s)
     (eval-string s)
     (print "\n")
     (println "OK -- humanoid, try this one yourself")
     (setq s "(analyze (list 6 25 62 123 214))")
     (println s)
     (eval-string s)
     (println "did you see it ?  -- it was x^3 - 2 ")
     (print "\n")
     (println "However :-( ")
     (setq s "(analyze (list 2 3 5 7 11 13))")
     (println s)
     (eval-string s)
     
  ))

(demo)

------------------------------------------

Code differentiates sequences till a list with constant values is reached :
tested with

(define (mono? L)
  (apply and (map (curry = (car L)) (cdr L))))

this is almost natural language -- it means the list shows identical numbers when the first number is the same as the rest of the list ...      the currying comes from Haskell , but it is easy to write as a macro I think , here it is short for
 (lambda (x) (= x (car L)))

When a "mono list" is reached the mechanism is reversed and integration starts -- with the advantage the sequence is expandable to any number of elements ....

however, as you can see in the demo , primes , n! , fibo's etc... will not reveal any working pattern ...

best Rob
there's always music to comfort us https://www.youtube.com/watch?v=JxPj3GAYYZ0  ..  we're not alone !!           
       



     
 

.
« Last Edit: September 13, 2014, 07:05:35 AM by RobbeK »

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #769 on: September 13, 2014, 12:40:35 PM »
Hi Rob,


In my case it's March 3, kid! :D


No, Sakhalin is a separate region. Japanese Hokkaido is immediately to the south of it as seen in this map:




Hehe, your demo seems clever! And thanks for the clip too. It goes very well with the recent videos posted on the thinBasic forum. :)

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #770 on: September 14, 2014, 01:21:22 AM »
Hi John, Mike,

"nclude its clone in the FBSL armory if only I manage to design a reliable JIT compiler for it."

both CLisp and Racket Scheme , use GNU Lightning JIT :

http://www.gnu.org/software/lightning/manual/lightning.html

from : https://wiki.aalto.fi/download/attachments/40010375/intro-report.pdf
"First JIT compilation system according to Aycock [4] were McCarthy's Lisp
systems in the 1960s. "

best Rob


Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #771 on: September 14, 2014, 02:32:29 AM »
Hi John,

Is that a farewell from China? :)

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #772 on: September 14, 2014, 02:56:30 AM »
Addendum --

"SyntAna" does fractions and real numbers now -- compiled with the Austin-Kyoto CL...

(took me 20' to find out "" is not a valuable string and GCL doesn't do "appendtext" on textareas .... :(

best Rob
(Japi is a static lib here, will be hard to update it , ???? )

.

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #773 on: September 14, 2014, 03:07:12 AM »
Hi Rob,

No no, no GNU for me, please. That's first and foremost. Secondly, I can cope with machine code generation myself -- FBSL already has two very compact JIT compilers incorporated in its binary, one for Intel assembly, and the other one, for ANSI C. What I actually need is a system that would do LISP (Scheme) syntax translation to either asm or C so that one of my existing machine code generators could do the rest of the work. And that system must be written in C (or assembly) to be compilable with the rest of FBSL sources into the same binary, and it shouldn't add more than 45 or 50KB to its size when compiled.

There are no such systems on the net worth speaking of. Gambit-C and the like are huge and generate terrifically inefficient C code (been there, done that), others that are outputting assembly code are hopelessly incomplete even by r4rs standards.

There is only one project on the entire net that does what I need, and it is Abdulaziz Ghuloum's An Incremental Approach to Compiler Construction. This is a LISP-to-assembly translator that covers the entire r5rs Scheme in its Petite flavor. And it goes under a MIT license too which suits my intentions perfectly.

Regretfully, the compiler itself is written in Scheme and I'm facing a tremendous problem with its portability to C or assembly for compilation into the FBSL binary. The compiler (in fact, translator) is not self-translating. I.e. it runs perfectly from within Petite to translate other scripts' code to asm but it can't translate its own Petite Scheme body to assembly because of a few syntactic constructs that are specific to Petite and do not belong to r5rs proper.

Can I ask you to help me out with this problem? I have about two dozens very similar macros from the compiler code and a couple of their corresponding syntactic prototypes. I need to exclude the prototypes from the code altogether and have the macros unrolled to their pure explicit r5rs compatible form by hand. Perhaps this will make the compiler self-compilable, and then I will be able to include its code regenerated by itself into assembly, in my FBSL compilation.

JRS

  • Guest
Re: Lisp in Basic
« Reply #774 on: September 14, 2014, 07:45:46 AM »
Quote
Is that a farewell from China?

Actually it was from an article about fashion statements in NY.

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #775 on: September 14, 2014, 08:49:12 AM »
Quote
Is that a farewell from China?
Actually it was from an article about fashion statements in NY.

Oh, so it was a farewell from China Town. :)

JRS

  • Guest
Re: Lisp in Basic
« Reply #776 on: September 14, 2014, 08:59:56 AM »
You missed the humor of the original post. Faking happy when things aren't going as planned.

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #777 on: September 14, 2014, 09:17:16 AM »
Thanks for making me aware.

Faking happy when things aren't going as planned...

... in NYC? (that's a joke)

JRS

  • Guest
Re: Lisp in Basic
« Reply #778 on: September 14, 2014, 10:59:30 AM »
Quote
... in NYC? (that's a joke)

That was the funnest part.  ;D

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #779 on: September 14, 2014, 01:40:13 PM »
OK Rob,

I can narrow down the problem to just the following two cases. Given syntactic Rules I and II and given that I know and understand what such names and procedure calls as e.g. emit-expr, putprop, etc. actually mean, can you please:

i) unroll for me the respective macros 1 to 3 and 1 to 4 to how Scheme would actually see their true code on program load; and

ii) explain to me what the difference is between the arguments of macros 3 and 4 under syntactic Rule II? (why is arg b parenthesized in macro 4?)


Rule I

(define-syntax define-primitive
  (syntax-rules ()
    [(_ (prim-name si env arg* ...) b b* ...)
     (begin
       (putprop 'prim-name '*is-prim* #t)
       (putprop 'prim-name '*arg-count*
         (length '(arg* ...)))
       (putprop 'prim-name '*emitter*
         (lambda (si env arg* ...) b b* ...)))]
    [(_ (prim-name si env arg* ... . vararg) b b* ...)
     (begin
       (putprop 'prim-name '*is-prim* #t)
       (putprop 'prim-name '*arg-count*
         (length '(arg* ...)))
       (putprop 'prim-name '*vararg* #t)
       (putprop 'prim-name '*emitter*
         (lambda (si env arg* ... . vararg) b b* ...)))]))

1.
(define-primitive (fxadd1 si env arg)
  (emit-expr si env arg)
  (emit "  add $~s, %eax" (immediate-rep 1)))

2.
(define-primitive (fx+ si env arg1 arg2)
  (emit-binop si env arg1 arg2)
  (emit "  add ~s(%esp), %eax" si))

3.
(define-primitive (foreign-call si env name . args)
  (let ([new-si (let loop ([si (+ si wordsize)]
            [args (reverse args)])
                  (cond
                   [(null? args) si]
                   [else
                    (emit-expr-save (next-stack-index si) env (car args))
                    (loop (next-stack-index si) (cdr args))]))])
    (emit-adjust-base new-si)
    (emit-call name)
    (emit-adjust-base (- new-si))))


Rule II

(define lib-primitives '())
(define-syntax define-lib-primitive
  (syntax-rules ()
    [(_ (prim-name arg* ...) b b* ...)
     (begin
       (set! lib-primitives (cons 'prim-name lib-primitives))
       (putprop 'prim-name '*is-lib-prim* #t)
       (putprop 'prim-name '*arg-count*
         (length '(arg* ...)))
       (putprop 'prim-name '*lib-code*
         (make-lambda '(arg* ...) (make-begin '(b b* ...)))))]
    [(_ (prim-name . varargs) b b* ...)
     (begin
       (set! lib-primitives (cons 'prim-name lib-primitives))
       (putprop 'prim-name '*is-lib-prim* #t)
       (putprop 'prim-name '*arg-count* 0)
       (putprop 'prim-name '*vararg* #t)
       (putprop 'prim-name '*lib-code*
         (make-lambda 'varargs (make-begin '(b b* ...)))))]
    [(_ prim-name b)
     (begin
       (set! lib-primitives (cons 'prim-name lib-primitives))
       (putprop 'prim-name '*is-lib-prim* #t)
       (putprop 'prim-name '*lib-code* 'b))]))

1.
(define-lib-primitive (length lst)
  (if (null? lst)
      0
      (fxadd1 (length (cdr lst)))))

2.
(define-lib-primitive (vector . args)
  (let ([v (make-vector (length args))])
    (fill args (lambda (index arg) (vector-set! v index arg)))
    v))

3.
(define-lib-primitive stdout
  (make-output-port "" 1))

4.
(define-lib-primitive (current-output-port)
  stdout)