Meta-objective
    Lisp
          @dico_leque
 2011/02/26 NagoyaRejectKaigi
n   Common Lisp Object System     Metaobject
    Protocol

n                 Gauche Scheme interpreter
Lisp
n   CLOS -- Common Lisp Object System
n


    n   ISLISP Object System -- ILOS

n   Ruby

n   ANSI Common Lisp                    Lisp
CLOS (1)

n


    n   (define-class <foo> () ((slot1) (slot2)))
n


    n   (define-class <baz> (<foo> <bar>) ())
CLOS (2)
n                                                         generic-
    function

n

     (define-generic foo)
     (define-method foo ((x            <sequence>) (y <sequence>)) 0)
     (define-method foo ((x            <sequence>) (y <string>)) 1)
     (define-method foo ((x            <string>) (y <sequence>)) 2)
     (define-method foo ((x            <string>) (y <string>)) 3)

     (foo   '()   '())    ;   =>   0
     (foo   '()   "a")    ;   =>   1
     (foo   "a"   '#())   ;   =>   2
     (foo   "a"   "b")    ;   =>   3
CLOS (3)
n   CLOS   CLOS

n




n
MOP
n   Metaobject Protocol
    n   introspection:
                          API

    n   intercessory:
                         API

n          intercessory
Metaobjects

n


    n   <class> <generic> <method>
n

    (make <class>) (apply-generic-function <generic>)
(1)
n


n   <singleton-meta>
         make

n   (instance-of <     >)



n   singleton               singleton
(1)
(define-class <singleton-meta> (<class>)
  (%the-singleton-instance)
  )

(define-method make ((class <singleton-meta>) . initargs)
  ...)

(define-method instance-of ((class <singleton-meta>) . initargs)
  (apply make class initargs))

(define-class <singleton-mixin> ()
  ()
  :metaclass <singleton-meta>)



            from gauche/lib/gauche/mop/singleton.scm
(2) O/R

n   DB

n   <orm>             DB
(2) O/R
(define-class <orm> (<class>) ())

(define-method compute-slots ((cl <orm>))
  (let ((super-slots (next-method)))
    (lset-union
     (lambda (x y)
       (eq? (car x) (car y)))
     (map (lambda (name)
            `(,name
              :init-keyword ,(make-keyword name)
              :accessor ,(string->symbol (format "~A-of" name))))
          (load-slot-names cl))
     super-slots)))

(define-class <foo> () ()
  :metaclass <orm>)
                               ※ load-slot-names
(3)
n   Ruby

n


n   (eql 0)           0



n   <generic>             (eql x)
(3)

(define-generic fact
  :class <eql-specializable-generic>)

(define-method fact ((n (eql 0)))
  1)

(define-method fact ((n <integer>))
  (* n (fact (- n 1))))
(3)
(define-class <eql-specializer> (<class>)
  ((object
    :getter eql-specializer-object
    :init-keyword :eql-specializer-object)))

(define (eql obj)
  (make <eql-specializer> :eql-specializer-object obj))

(define-class <eql-specializable-generic> (<generic>) ())

(define-method compute-applicable-methods
    ((gf <eql-specializable-generic>) args)
  ...)

(define-method sort-applicable-methods
    ((gf <eql-specializable-generic>) methods args)
  ...)
      ※                60       http://d.hatena.ne.jp/leque/20110105/p1
CLOS / MOP

n




    n               DB



    n


    n   validator
CLOS / MOP
n


    n

        Pascal Costanza, Charlotte Herzeel, Jorge Vallejos, Theo D’Hondt:
        “Filtered dispatch”, Proceedings of the 2008 symposium on Dynamic
        languages

n


    n                                before, after, around

    n
CLOS / MOP

n


    n


    n   lazy initialization

n
DSL       MOP

n   DSL    Domain



n
n   Lisper
    DSL
n   CLOS MOP

Meta-objective Lisp @名古屋 Reject 会議

  • 1.
    Meta-objective Lisp @dico_leque 2011/02/26 NagoyaRejectKaigi
  • 2.
    n Common Lisp Object System Metaobject Protocol n Gauche Scheme interpreter
  • 3.
    Lisp n CLOS -- Common Lisp Object System n n ISLISP Object System -- ILOS n Ruby n ANSI Common Lisp Lisp
  • 4.
    CLOS (1) n n (define-class <foo> () ((slot1) (slot2))) n n (define-class <baz> (<foo> <bar>) ())
  • 5.
    CLOS (2) n generic- function n (define-generic foo) (define-method foo ((x <sequence>) (y <sequence>)) 0) (define-method foo ((x <sequence>) (y <string>)) 1) (define-method foo ((x <string>) (y <sequence>)) 2) (define-method foo ((x <string>) (y <string>)) 3) (foo '() '()) ; => 0 (foo '() "a") ; => 1 (foo "a" '#()) ; => 2 (foo "a" "b") ; => 3
  • 6.
    CLOS (3) n CLOS CLOS n n
  • 7.
    MOP n Metaobject Protocol n introspection: API n intercessory: API n intercessory
  • 8.
    Metaobjects n n <class> <generic> <method> n (make <class>) (apply-generic-function <generic>)
  • 9.
    (1) n n <singleton-meta> make n (instance-of < >) n singleton singleton
  • 10.
    (1) (define-class <singleton-meta> (<class>) (%the-singleton-instance) ) (define-method make ((class <singleton-meta>) . initargs) ...) (define-method instance-of ((class <singleton-meta>) . initargs) (apply make class initargs)) (define-class <singleton-mixin> () () :metaclass <singleton-meta>) from gauche/lib/gauche/mop/singleton.scm
  • 11.
    (2) O/R n DB n <orm> DB
  • 12.
    (2) O/R (define-class <orm>(<class>) ()) (define-method compute-slots ((cl <orm>)) (let ((super-slots (next-method))) (lset-union (lambda (x y) (eq? (car x) (car y))) (map (lambda (name) `(,name :init-keyword ,(make-keyword name) :accessor ,(string->symbol (format "~A-of" name)))) (load-slot-names cl)) super-slots))) (define-class <foo> () () :metaclass <orm>) ※ load-slot-names
  • 13.
    (3) n Ruby n n (eql 0) 0 n <generic> (eql x)
  • 14.
    (3) (define-generic fact :class <eql-specializable-generic>) (define-method fact ((n (eql 0))) 1) (define-method fact ((n <integer>)) (* n (fact (- n 1))))
  • 15.
    (3) (define-class <eql-specializer> (<class>) ((object :getter eql-specializer-object :init-keyword :eql-specializer-object))) (define (eql obj) (make <eql-specializer> :eql-specializer-object obj)) (define-class <eql-specializable-generic> (<generic>) ()) (define-method compute-applicable-methods ((gf <eql-specializable-generic>) args) ...) (define-method sort-applicable-methods ((gf <eql-specializable-generic>) methods args) ...) ※ 60 http://d.hatena.ne.jp/leque/20110105/p1
  • 16.
    CLOS / MOP n n DB n n validator
  • 17.
    CLOS / MOP n n Pascal Costanza, Charlotte Herzeel, Jorge Vallejos, Theo D’Hondt: “Filtered dispatch”, Proceedings of the 2008 symposium on Dynamic languages n n before, after, around n
  • 18.
    CLOS / MOP n n n lazy initialization n
  • 19.
    DSL MOP n DSL Domain n
  • 20.
    n Lisper DSL n CLOS MOP