From f6bee9aa437367241e780583deea75dd93ab3c93 Mon Sep 17 00:00:00 2001 From: LeonardoBizzoni Date: Tue, 12 Dec 2023 09:37:07 +0100 Subject: [PATCH] Iniziato a fare il predicato `def_class` MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Non so se è legale aggiungere altri predicati non specificati nel PDF ma non idea di come altro fare. "Il simbolo *this* all’interno di
si riferisce all’istanza stessa." Non ho idea di cosa voglia dire e non so nemmeno come far matchare `_Form` con una congiunzione di predicati nella `add_part_to`. Sarebbe utile definire un predicato `stampa(Class)` che stampa le info dei predicati dinamici `is_class` / `is_child_of` / `is_part_of` giusto per non spammare `listing` per ogniuno. --- Prolog/README.org | 46 ++++++++++++++++++++++++++++++++++++++++++---- Prolog/oop.pl | 44 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 82 insertions(+), 8 deletions(-) diff --git a/Prolog/README.org b/Prolog/README.org index da01cfd..066d17f 100644 --- a/Prolog/README.org +++ b/Prolog/README.org @@ -14,7 +14,16 @@ %%%% %%%% -writeln(user_output, "Incredibile definizione del predicato `def_class`"). +def_class(ClassName, []) :- asserta(is_class(ClassName)), !. +def_class(ClassName, [Parent | OtherParents]) :- + is_class(Parent), + asserta(is_child_of(Parent, ClassName)), + def_class(ClassName, OtherParents). + +def_class(ClassName, [], []) :- def_class(ClassName, []), !. +def_class(ClassName, Parents, Parts) :- + def_class(ClassName, Parents), + add_part_to(ClassName, Parts). #+end_src *** Esempio pratico @@ -26,7 +35,6 @@ writeln(user_output, "Incredibile definizione del predicato `def_class`"). *** Implementazione #+begin_src prolog :tangle oop.pl -writeln(user_output, "Incredibile definizione del predicato `make`"). #+end_src *** Esempio pratico @@ -38,7 +46,6 @@ writeln(user_output, "Incredibile definizione del predicato `make`"). *** Implementazione #+begin_src prolog :tangle oop.pl -writeln(user_output, "Incredibile definizione del predicato `field`"). #+end_src *** Esempio pratico @@ -49,10 +56,41 @@ writeln(user_output, "Incredibile definizione del predicato `field`"). *** Implementazione #+begin_src prolog :tangle oop.pl -writeln(user_output, "Incredibile definizione del predicato `fields`"). #+end_src *** Esempio pratico * Predicati helper +** Predicati dinamici +#+begin_src prolog :tangle oop.pl +:- dynamic is_class/1. +:- dynamic is_child_of/2. +:- dynamic is_part_of/2. +#+end_src + +** add_part_to +#+begin_src prolog :tangle oop.pl +add_part_to(ClassName, []) :- is_class(ClassName), !. +add_part_to(ClassName, [Part | OtherParts]) :- + field(_Name, _Value) = Part, + !, + is_class(ClassName), + asserta(is_part_of(ClassName, Part)), + add_part_to(ClassName, OtherParts). + +add_part_to(ClassName, [Part | OtherParts]) :- + field(_Name, _Value, _Type) = Part, + !, + is_class(ClassName), + asserta(is_part_of(ClassName, Part)), + add_part_to(ClassName, OtherParts). + +add_part_to(ClassName, [Part | OtherParts]) :- + method(_Name, ArgList, _Form) = Part, + is_list(ArgList), + !, + is_class(ClassName), + asserta(is_part_of(ClassName, Part)), + add_part_to(ClassName, OtherParts). +#+end_src diff --git a/Prolog/oop.pl b/Prolog/oop.pl index a75f57a..3852766 100644 --- a/Prolog/oop.pl +++ b/Prolog/oop.pl @@ -1,10 +1,46 @@ %%%% %%%% -writeln(user_output, "Incredibile definizione del predicato `def_class`"). +def_class(ClassName, []) :- asserta(is_class(ClassName)), !. +def_class(ClassName, [Parent | OtherParents]) :- + is_class(Parent), + asserta(is_child_of(Parent, ClassName)), + def_class(ClassName, OtherParents). -writeln(user_output, "Incredibile definizione del predicato `make`"). +def_class(ClassName, [], []) :- def_class(ClassName, []), !. +def_class(ClassName, Parents, Parts) :- + def_class(ClassName, Parents), + add_part_to(ClassName, Parts). -writeln(user_output, "Incredibile definizione del predicato `field`"). -writeln(user_output, "Incredibile definizione del predicato `fields`"). + + + + + +:- dynamic is_class/1. +:- dynamic is_child_of/2. +:- dynamic is_part_of/2. + +add_part_to(ClassName, []) :- is_class(ClassName), !. +add_part_to(ClassName, [Part | OtherParts]) :- + field(_Name, _Value) = Part, + !, + is_class(ClassName), + asserta(is_part_of(ClassName, Part)), + add_part_to(ClassName, OtherParts). + +add_part_to(ClassName, [Part | OtherParts]) :- + field(_Name, _Value, _Type) = Part, + !, + is_class(ClassName), + asserta(is_part_of(ClassName, Part)), + add_part_to(ClassName, OtherParts). + +add_part_to(ClassName, [Part | OtherParts]) :- + method(_Name, ArgList, _Form) = Part, + is_list(ArgList), + !, + is_class(ClassName), + asserta(is_part_of(ClassName, Part)), + add_part_to(ClassName, OtherParts). -- 2.52.0