[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

power of pattern matching (toy language

Taoufik Dachraoui

8/14/2015 8:24:00 PM

Hi

Using pattern matching I designed a toy kernel language (lazy, complete sharing, partial evaluation, ...)

the standard syntax of the language is minimal and based on lambda calculus:

(:and ?x (:or |#T| |#F|)) `(:boolean . ,x)
nil (list :cons)
(:and ?x (:symbol))
(if (eq #\- (char (symbol-name x) 0))
`(:one-word-comment . ,x)
x)
(:and ?x (:integer)) `(:integer . ,x)
(:and ?x (:float)) `(:float . ,x)
(:and ?x (:string)) `(:string . ,x)
(:and ?x (:array)) `(:array . ,x)
;
(set (:and ?x (:symbol)) ?value) `(set ,x (%%lazy-syntax ,value))
; define variable in current context
(def (:and ?x (:symbol)) ?value) `(def ,x (%%lazy-syntax ,value))
(def (:and ?value (rec ?x . _))) `(def ,x (%%lazy-syntax ,value))
;
(fn (:and ?x (:symbol)) ?e0) `(fn ,x (%%lazy-syntax ,e0))
(rec ?name (fn (:and ?x (:symbol) (:not (:keyword))) ?e0))
`(rec ,name (fn ,x (%%lazy-syntax ,e0)))
(catch ?e0 ?e1) `(catch (%%lazy-syntax ,e0) (%%lazy-syntax ,e1))
(throw ?e0 ?e1) `(throw (%%lazy-syntax ,e0) (%%lazy-syntax ,e1))
(eval ?e) `(eval (syntax ,e))
(compile ?e) `(compile (%%lazy-syntax ,e))
(:and ?e (:cons))
(loop for i in e
for x = (if (and (symbolp i) (eq #\- (char (symbol-name i) 0)))
nil
(syntax i))
then (if (and (symbolp i) (eq #\- (char (symbol-name i) 0)))
x
(if (null x) `(%%lazy-syntax ,i) `(,x (%%lazy-syntax ,i))))
finally (return x))

Using patterns I define new syntaxes as follows (for example):

(defsyn (labels (((:and (:symbol) ?label) ?args (:lone (declare . _)) . ?e0) . ?rest) . ?s)
(if (null rest)
`((fn ,label . ,s) (rec ,label (fn ,args . ,e0)))
`((fn ,label (labels ,rest . ,s)) (rec ,label (fn ,args (labels ,rest . ,e0))))))


? (kr '(labels ((foo (x) ((= x 0) 1 (add x (bar (sub x 1))))) (bar (x) ((= x 0) 1 (add x (foo (sub x 1)))))) (add
(foo 1) (bar 2))))
(:INTEGER . 6)


? (labels ((foo (x) (if (= x 0) 1 (+ x (bar (- x 1))))) (bar (x) (if (= x 0) 1 (+ x (foo (- x 1)))))) (+ (foo 1) (bar
2)))
6

K SYNTAX is a generalisation of CL macros, this is how defmacro CL is defined using defsyn:

(defsyn (defmacro ?name (:and ?args (:cons)) . ?rest)
`(defsyn
(,name . ,(loop for x in args collect (intern (format nil "?~A" (symbol-name x)))))
`,,@rest))


(defmacro foo (x y) `(list ,x ,y))
=> (defsyn (foo ?x ?y) `(list ,x ,y))

The signature of the CL macro is a pattern where the first atom is the name of the macro and
the remaining arguments are matched to the corresponding variables; defsyn accepts any pattern.

I am planning to define the whole CL using defsyn (it is just an attempt, the kernel language
must have the right set of features to make it possible)



-Taoufik
5 Answers

The Peeler

6/5/2013 6:00:00 PM

0

On Wed, 05 Jun 2013 06:00:21 -0700, The Rectum, the resident psychopath of
sci and scj, FAKING his time zone again and IMPERSONATING his master, The
Peeler, wrote:


>>''The UK has to open it's borders to Romania shortly.''
>
> And one, day it will, open its borders to Israel innit! <G>

Will it, poor blithering idiot? <BG>

--
Retarded, anal, subnormal and extremely proud of it: our resident
psychopath, The Retard (aka "The Rectum").

The Peeler

6/6/2013 3:56:00 AM

0

On Wed, 5 Jun 2013 19:59:32 +0200, The Peeler
<finishingoff@themoronicRevd.invalid> wrote:

>On Wed, 05 Jun 2013 06:00:21 -0700, The Rectum, the resident psychopath of
>sci and scj, FAKING his time zone again and IMPERSONATING his master, The
>Peeler, wrote:
>
>
>>>''The UK has to open it's borders to Romania shortly.''
>>
>> And one, day it will, open its borders to Israel innit! <G>
>
>Will, it to the poor blithering Jewish idiots? <BG>

Won't, it poor blithering Grik anus? <GB>

Taoufik Dachraoui

8/15/2015 12:09:00 PM

0

I am currently designing the K language using SYNTAX (a generalization of CL MACROs)

Using K SYNTAX is nice but may render code unreadable and difficult to find
the errors; I am trying to find a way to make it easy to find errors at compile
and run time

An example of a syntax follows:

? (defsyn (?x .+ . ?rest) (if (null rest) x `(add ,x (,(car rest) .+ . ,(cdr rest)))))
? (kr '(2 .+ 3 4 5))
(:INTEGER . 14)

All K code is translated to minimal code (only standard syntax) by expanding the
code using the defined syntaxes.

I am hoping to implement CL using K by defining all the CL syntaxes

-Taoufik

rpw3

8/16/2015 10:27:00 AM

0

Taoufik Dachraoui <dachraoui.taoufik@gmail.com> wrote:
+---------------
| I am currently designing the K language using SYNTAX
| (a generalization of CL MACROs)
....
| An example of a syntax follows:
| ? (defsyn (?x .+ . ?rest) (if (null rest) x `(add ,x (,(car rest) .+ . ,(cdr rest)))))
| ? (kr '(2 .+ 3 4 5))
| (:INTEGER . 14)
+---------------

Just so you know, there is already a language named K
which is a variant/descendent of APL (and A+), and is
used especially in the financial industry:

https://en.wikipedia.o...(programming_language)


-Rob

-----
Rob Warnock <rpw3@rpw3.org>
627 26th Avenue <http://rpw...
San Mateo, CA 94403

Marco Antoniotti

8/17/2015 6:37:00 AM

0

On Sunday, August 16, 2015 at 1:35:06 PM UTC+3, Rob Warnock wrote:
> Taoufik Dachraoui <dachraoui.taoufik@gmail.com> wrote:
> +---------------
> | I am currently designing the K language using SYNTAX
> | (a generalization of CL MACROs)
> ...
> | An example of a syntax follows:
> | ? (defsyn (?x .+ . ?rest) (if (null rest) x `(add ,x (,(car rest) .+ . ,(cdr rest)))))
> | ? (kr '(2 .+ 3 4 5))
> | (:INTEGER . 14)
> +---------------
>
> Just so you know, there is already a language named K
> which is a variant/descendent of APL (and A+), and is
> used especially in the financial industry:
>
> https://en.wikipedia.o...(programming_language)
>

Yep.

And there is also a long-standing "pattern language" (the template sublanguage) in (shameless plug) CL-UNIFICATION (just a quicklisp away).

Cheers
--
MA