From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Damien Mattei Newsgroups: gmane.lisp.guile.user Subject: Re: access slot of a class Date: Tue, 21 Nov 2023 19:10:01 +0100 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="33531"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Nate Rosenbloom , guile-user To: mikael@djurfeldt.com Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Tue Nov 21 19:11:05 2023 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1r5VDF-0008YY-HC for guile-user@m.gmane-mx.org; Tue, 21 Nov 2023 19:11:05 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r5VCd-000738-UX; Tue, 21 Nov 2023 13:10:27 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r5VCW-0006z6-4Z for guile-user@gnu.org; Tue, 21 Nov 2023 13:10:22 -0500 Original-Received: from mail-ed1-x533.google.com ([2a00:1450:4864:20::533]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1r5VCR-0007OF-31 for guile-user@gnu.org; Tue, 21 Nov 2023 13:10:17 -0500 Original-Received: by mail-ed1-x533.google.com with SMTP id 4fb4d7f45d1cf-548f6f3cdc9so1800230a12.2 for ; Tue, 21 Nov 2023 10:10:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700590213; x=1701195013; darn=gnu.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=uK5bg2TsN4bshgB8rMwz6EAdiLyidEqjBqmwwIumox8=; b=Lt+LureBW5pxi6af+RQzvcR+zcP2A5Oq6dkQG7Ll4nxkfdqW/1PuNLiXrXPZrqeJ+a ixEt5+45ADxnph4IiyhHB28LxbL0NGkwWc3zHGVFrBd+BGu42NXjDVcUWQUJ57FUtlAJ sydTb56OwefMJEYt5w7fU9uU3zJOXPAfan0YDtFqM5qtYlSp8+1zRw7ppSAn9/jOcm8s z8dXF64CcWOA06zAlfyl+w7HEMC+PaLCD/crhM3wWErRhNmf2Bj9q+SuY2hJ7NfYUmvf VOvsugcB5PSiLwYGEeZFpsPMkQ5ZvAYL7lIbBz/r1tiWpkBF7DGOyBWRA0haHYv3E4+E jjgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700590213; x=1701195013; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=uK5bg2TsN4bshgB8rMwz6EAdiLyidEqjBqmwwIumox8=; b=BZgOTwb2Q4xA7sRF3kZ5lm0jO1KGJD1cHsur9aSZvWFYocNYJold+pqZxFQjQJ2dPo tqo2StaKF4eIlslEkb6arm9Y7amSCIeUO5rZj2Lh0gxr3Ehws38NBQyca+Ou4JEDdYt4 IDoF+vUpRKwqKI+1nda5c1EvEdwOFxDBPk7bL3HA5IMD0hk4hs+Z2FsYTHGT/3Z8GhpV iGLONjUZ3RgLdkKcYlpNgmNfreW1wgFAMGGBH2/Y2/ZkufPPYLSDbJhwvDDaunGOE+sp LWQMku39CLtYttkubopxaj5+24dj6r+kiu99xVHVZDcYI5TGBqTebqvEs18yRBf+uZEr 1lJw== X-Gm-Message-State: AOJu0Ywyg9wlS2lHjB53CKCc3oSO3nKBH8H1a0OQxTtdqZzZtiY1WEZr EmX4B9iczHNh5wzl7F1B4CXQQxNacs3nvFQVZ2re6SZzZgtgren2 X-Google-Smtp-Source: AGHT+IG7i6zNnOneHKSAJ5M18yXt3v1x93faWMGWAQAHXAP+YJgbNysS9MKYOE69vjL/mMUTJ6bhJmmeRHBFpTv9tlo= X-Received: by 2002:a05:6402:1a49:b0:543:5fc0:de22 with SMTP id bf9-20020a0564021a4900b005435fc0de22mr105404edb.24.1700590213123; Tue, 21 Nov 2023 10:10:13 -0800 (PST) In-Reply-To: Received-SPF: pass client-ip=2a00:1450:4864:20::533; envelope-from=damien.mattei@gmail.com; helo=mail-ed1-x533.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.user:19321 Archived-At: yes , and the solution i found is to pass the object to the method as argument, then assigning slot to local variables with accessors in the method and the source code from Racket or Kawa. Original code was from Python and i had a bit the same problem, not that slot are not accessible from method, they are in Python, but you have to use self.v to access v and that is boring in math expression, so i copy like that v=3Dself.v and then i use v, after computation, of course i have to update self.v with self.v=3Dv. There is no other solution in Python. Example: class ReseauRetroPropagation(): def __init__(self,nc=3D[2,3,1], nbiter=3D3, =CE=B7=E2=82=9B=3D1.0 , #= =CE=B7=E2=82=91=3D0.0001 , activation_function_hidden_layer=3Dtanh, activation_function_output_layer=3Dtanh, activation_function_hidden_layer_derivative=3Dder_tanh, activation_function_output_layer_derivative=3Dder_tanh): lnc =3D len(nc) # the total of all layer including input, output an= d hidden layers self.z =3D [ [0] * n for n in nc ] # les entr=C3=A9es concr=C3=A8te= s seront fournies avec la m=C3=A9thode accepte self.z=CC=83 =3D [ [0] * n for n in nc ] # z=CC=83[0] is not used a= s z[0] is x, the initial data .... def accepte_et_propage(self,x): # on entre des entr=C3=A9es et on = les propage # note: i just reference the variables for code readness (hide all the self keyword) z =3D self.z z=CC=83 =3D self.z=CC=83 M =3D self.M if len(x) !=3D len(z[0]): raise ValueError("Mauvais nombre d'entr=C3=A9es !") #z[0] =3D x # on ne touche pas au biais self.z[0] =3D z[0] =3D x n =3D len(z) # hidden layers for i in range(n-2) : # create a list with 1 in front for the bias coefficient z_1 =3D [1] + z[i] z=CC=83[i+1] =3D M[i] * z_1 # z=CC=83 =3D matrix * iterable (li= st here) , return a list z[i+1] =3D list(map(self.activation_function_hidden_layer,z=CC=83[i+1])) # update the variable when necessary self.z[i+1] =3D z[i+1] self.z=CC=83[i+1] =3D z=CC=83[i+1] this is consuming processor cycles of course but the coding is more easy. finally the solution in Guile looks like that: (define-class ReseauRetroPropagation () (nbiter #:init-value 3 #:init-keyword #:nbiter) (activation_function_hidden_layer #:init-keyword #:activation_function_hidden_layer) (activation_function_output_layer #:init-keyword #:activation_function_output_layer) (activation_function_hidden_layer_derivative #:init-keyword #:activation_function_hidden_layer_derivative) (activation_function_output_layer_derivative #:init-keyword #:activation_function_output_layer_derivative) (=CE=B7=E2=82=9B #:init-value 1.0 #:init-keyword #:=CE=B7=E2=82=9B) (z #:getter nbp-get-z #:setter nbp-set-z!) .... (define (*init* nc nbp) (display "*init* : nc=3D") (display nc) (newline) {lnc <+ (vector-length nc)} (define (make-vector-zero lg) (make-vector lg 0)) {z <+ (nbp-get-z nbp)} {z <- (vector-map make-vector-zero nc)} (display "z=3D") (display z) (newline) (nbp-set-z! nbp z) ... ) ; end method *init* Damien On Tue, Nov 21, 2023 at 11:31=E2=80=AFAM Mikael Djurfeldt wrote: > OK, now I see what you are asking. You want the slots to be available as > local variables in the lexical scope of the method. The answer is that th= at > is not possible "out of the box". I guess it *could* be made available > through some contortionist coding using a combination of GOOPS and the > syntax-case macro system, but it goes against the design of GOOPS and CLO= S. > There are macro systems where the class definition provides a lexical sco= pe > and there are macro systems based on generic functions. It's two differen= t > takes on object orientation. > > (Note though regarding the citation about a method not being formally > associated with a single class: In the case of accessor methods, which ta= ke > a single argument, they are in a sense associated with a single class.) > > On Tue, Nov 21, 2023 at 9:19=E2=80=AFAM Damien Mattei > wrote: > >> that is interesting answer about guile's features i did not know , >> it is related with code i'm writing that instead of fetching the value >> modify it using the fetch syntax in Scheme+ but my question was just >> about how to have a class method in a class that can set! the value of >> a slot. This does nt seem to be available in Guile because "A method >> is not formally associated with any single class (as it is in many >> other object oriented languages)" [0]. >> >> i will explain the problem more in my answer to Mikael and you, i just >> wanted to be able to create a class method that can access the slot >> directly , more than in the example [2] : >> >> (define-class () >> (r #:init-value 0 #:accessor real-part #:init-keyword #:r) >> (i #:init-value 0 #:accessor imag-part #:init-keyword #:i)) >> >> (set! (real-part c) new-value) >> >> in othe Scheme i can do simply: >> >> (set! r new-value) >> >> for example in Kawa: >> >> (define-simple-class ReseauRetroPropagation () >> >> (nbiter init-value: 3) >> (activation_function_hidden_layer) >> (activation_function_output_layer) >> (activation_function_hidden_layer_derivative) >> (activation_function_output_layer_derivative) >> >> (=CE=B7=E2=82=9B 1.0) >> >> (z) >> (z=CC=83) >> >> (M) >> >> (=E1=90=81) >> >> (error 0) >> >> ((*init* nc nbiter0 =CE=B7=E2=82=9B0 activation_function_hidden_layer0 >> activation_function_output_layer0 >> activation_function_hidden_layer_derivative0 >> activation_function_output_layer_derivative0) >> >> (display "*init* : nc=3D") (display nc) (newline) >> {nbiter <- nbiter0} >> {=CE=B7=E2=82=9B <- =CE=B7=E2=82=9B0} >> >> ... >> >> or in Racket : >> >> (define ReseauRetroPropagation >> >> (class object% >> >> (super-new) >> >> ; this is the initialisation parameters >> (init-field (nc #(2 3 1)) ;; on cr=C3=A9e le tableau des couches du= r=C3=A9seau >> (nbiter 10000) >> (=CE=B7=E2=82=9B 1.0) >> (activation_function_hidden_layer tanh) >> (activation_function_output_layer tanh) >> (activation_function_hidden_layer_derivative der_tanh) >> (activation_function_output_layer_derivative der_tanh)) >> >> {lnc <+ (vector-length nc)} >> >> ; les entr=C3=A9es concr=C3=A8tes seront fournies avec la m=C3=A9th= ode accepte >> ;; (field (z (vector-ec (: i (vector-length nc)) (make-vector >> {nc[i]} 0)))) >> ;;(field (z (vector-ec (:vector lg nc) >> ;; (make-vector lg 0)))) >> (field (z (vector-map (lambda (lg) (make-vector lg 0)) >> nc))) >> >> ;; (field (z (for/vector ([lg nc]) >> ;; (make-vector lg 0)))) >> >> (display "z=3D") (display z) (newline) >> >> >> ; z=CC=83[0] is not used as z[0] is x, the initial data >> ;;(field (z=CC=83 (vector-ec (:vector lg nc) >> ;; (make-vector lg 0)))) >> (field (z=CC=83 (vector-map (lambda (lg) (make-vector lg 0)) >> nc))) >> >> >> (display "z=CC=83=3D") (display z=CC=83) (newline) >> >> >> {M <+ (vector-ec (: n {lnc - 1}) ; vectors by eager comprehension >> (SRFI 42) >> create-matrix-vect-by-function(uniform-dummy nc[n + 1] >> {nc[n] + 1}))} ;; Matrix-vect >> >> ;(field (M (vector-ec (: n {lnc - 1}) ; vectors by eager >> comprehension (SRFI 42) >> ; (create-matrix-vect-by-function uniform-dummy {nc[n + >> 1]} {nc[n] + 1})))) ;; Matrix-vect >> >> >> (display "M=3D") (display M) (newline) >> >> (field (=E1=90=81 (for/vector ([lg nc]) >> (make-vector lg 0)))) >> >> >> (display "=E1=90=81=3D") (display =E1=90=81) (newline) >> >> (display "nbiter=3D") (display nbiter) (newline) >> >> (field (error 0)) >> >> >> ; forward propagation >> >> ; z_* sans le coef. 1 constant pour le bias >> (define (accepte_et_propage x) ; on entre des entr=C3=A9es et on le= s >> propage >> >> (when {vector-length(x) =E2=89=A0 vector-length(z[0])} >> (display "Mauvais nombre d'entr=C3=A9es !") (newline) >> (exit #f)) >> >> {z[0] <- x} ; on ne touche pas au biais >> >> in those 2 example z is a slot (or field in Racket) of the class >> ReseauRetroPropagation that can directly be accessed from a class >> method without using the accessors (getter and setter) >> >> Regards, >> Damien >> >> >> >> >> [0] 8.6 Methods and Generic Functions >> >> https://www.gnu.org/software/guile/manual/html_node/Methods-and-Generic-= Functions.html >> >> [2] 8.5 Illustrating Slot Description: >> >> https://www.gnu.org/software/guile/manual/html_node/Slot-Description-Exa= mple.html >> >> On Mon, Nov 20, 2023 at 11:43=E2=80=AFPM Nate Rosenbloom >> wrote: >> >> >> >> is there a way to access a slot of a class like other variable with >> set! , >> >> Racket,Kawa allow that, but it does not seem to be the case in Guile. >> > >> > >> > Hello Damien: >> > Does 'generalized set'[0] do what you want? >> > You can create accessor procedures that have an attached setter - >> > >> > Or were you speaking specifically about class slots? In which case it >> looks like the accessor can be used in in set!: [2] >> > Thanks, >> > Nate >> > [0] 7.5.14 sfri 17 Generalized set!: >> https://www.gnu.org/software/guile/manual/html_node/SRFI_002d17.html >> > [1] 6.7.8 Procedures with Setters: >> https://www.gnu.org/software/guile/manual/html_node/Procedures-with-Sett= ers.html >> > [2] 8.5 Illustrating Slot Description: >> https://www.gnu.org/software/guile/manual/html_node/Slot-Description-Exa= mple.html >> > >> >