]> git.leonardobizzoni.com Git - ObjectOriented-Prolog-Lisp/commitdiff
Iniziato a fare il predicato `def_class`
authorLeonardoBizzoni <leo2002714@gmail.com>
Tue, 12 Dec 2023 08:37:07 +0000 (09:37 +0100)
committerLeonardoBizzoni <leo2002714@gmail.com>
Tue, 12 Dec 2023 08:37:07 +0000 (09:37 +0100)
Non so se è legale aggiungere altri predicati non specificati nel PDF
ma non idea di come altro fare.

"Il simbolo *this* all’interno di <form> 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
Prolog/oop.pl

index da01cfd3b7ff9d3742ea78c58832a6632de18726..066d17f6c7508d4eb97e77d751dd1b2e8b1d48e9 100644 (file)
 %%%% <Cognome> <Nome> <Matricola>
 %%%% <eventuali collaborazioni>
 
-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
index a75f57a1e827a1a67ce5ebca3aca9bb1a4baabd5..3852766e2548bd9e261b683a5f952cf300502f24 100644 (file)
@@ -1,10 +1,46 @@
 %%%% <Cognome> <Nome> <Matricola>
 %%%% <eventuali collaborazioni>
 
-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).