From ec9861d23be27058a7ff0859cbf8badac6d7b978 Mon Sep 17 00:00:00 2001 From: LeonardoBizzoni Date: Sun, 24 Dec 2023 12:10:40 +0100 Subject: [PATCH] Fixato cancellazione definizione di `this` --- Prolog/README.org | 58 ++++++++++++++++++++++++++++++----------------- Prolog/oop.pl | 22 +++++++++++------- 2 files changed, 51 insertions(+), 29 deletions(-) diff --git a/Prolog/README.org b/Prolog/README.org index b23a6f2..5facf4d 100644 --- a/Prolog/README.org +++ b/Prolog/README.org @@ -1,5 +1,8 @@ #+title: 💀 OOΠ in Prolog 💀 -#+author: Bizzoni Leonardo (899629), Barone Matteo(894594), Kier Mirko Tolentino(899728) +#+author: Bizzoni Leonardo (899629), +#+author: Barone Matteo(894594), +#+author: Kier Mirko Tolentino(899728) +#+options: TOC:nil * Breve descrizione 42 @@ -60,18 +63,22 @@ def_class(ClassName, Parents, Parts) :- *** Esempio pratico #+name: def_class #+begin_src prolog -def_class(foo, [], [field(foo, 42), method(foo, [Number], (field(this, foo, Foo), - Sum is Number + Foo, - write("Sum: "), writeln(Sum)))]). - -def_class(bar, [], [field(bar, "42"), method(bar, [], (write("Favorite number is "), - field(this, bar, Bar), - writeln(Bar)))]). - -def_class(foobar, [foo, bar], [field(foobar, "Baz"), method(foobar, [X, X, [X]], (!)), method(foobar, [Start, End, [Start | Ls]], (Start =< End, - N is Start + 1, - foobar(this, N, End, Ls)))]). - +def_class(foo, [], [field(foo, 42), + method(foo, [Number], (field(this, foo, Foo), + Sum is Number + Foo, + write("Sum: "), writeln(Sum)))]). + +def_class(bar, [], [field(bar, "42"), + method(bar, [], (write("Favorite number is "), + field(this, bar, Bar), + writeln(Bar)))]). + +def_class(foobar, [foo, bar], [field(foobar, "Baz"), + method(foobar, [X, X, [X]], (!)), + method(foobar, [Start, End, [Start | Ls]], + (Start =< End, + N is Start + 1, + foobar(this, N, End, Ls)))]). #+end_src ** make *** Definizione @@ -165,7 +172,7 @@ fieldx(Instance, [FieldName], Res) :- !. fieldx(Instance, [FieldName | Others], Res) :- is_instance(Instance), - field(Instance, FieldName, Value), + field(Instance, FieldName, _Value), fieldx(Instance, Others, Res). #+end_src @@ -288,16 +295,25 @@ check_fields(InstanceName, [=(Field, Value) | Other]) :- ** define_this #+begin_src prolog :tangle oop.pl -define_this(Instance) :- +define_this(Instance, For) :- \+ is_list(Instance), !, findall(Key = Value, field(Instance, Key, Value), List), - define_this(List). + define_this(List, For). -define_this([]) :- !. -define_this([=(Key, Value) | Other]) :- +define_this([], []) :- !. +define_this([=(Key, Value) | Other], [Key | Rest]) :- asserta(field(this, Key, Value)), - define_this(Other). + define_this(Other, Rest). +#+end_src + +** undefine_this +#+begin_src prolog :tangle oop.pl +undefine_this([]) :- !. +undefine_this([FieldName | Other]) :- + retract(field(this, FieldName, _Value)), + !, + undefine_this(Other). #+end_src ** call_method @@ -305,10 +321,10 @@ define_this([=(Key, Value) | Other]) :- call_method(Instance, ClassName, Body) :- is_instance(Instance, ClassName), asserta(is_instance(this, ClassName)), - define_this(Instance), + define_this(Instance, For), call_cleanup(call(Body), ( retractall(is_instance(this, ClassName)), - retractall(field(this, _, _)))). + undefine_this(For))). #+end_src ** check_value_type diff --git a/Prolog/oop.pl b/Prolog/oop.pl index 3ec30c9..9b2c415 100644 --- a/Prolog/oop.pl +++ b/Prolog/oop.pl @@ -95,7 +95,7 @@ fieldx(Instance, [FieldName], Res) :- !. fieldx(Instance, [FieldName | Others], Res) :- is_instance(Instance), - field(Instance, FieldName, Value), + field(Instance, FieldName, _Value), fieldx(Instance, Others, Res). :- dynamic is_class/1. @@ -194,24 +194,30 @@ check_fields(InstanceName, [=(Field, Value) | Other]) :- field(InstanceName, Field, Value), check_fields(InstanceName, Other). -define_this(Instance) :- +define_this(Instance, For) :- \+ is_list(Instance), !, findall(Key = Value, field(Instance, Key, Value), List), - define_this(List). + define_this(List, For). -define_this([]) :- !. -define_this([=(Key, Value) | Other]) :- +define_this([], []) :- !. +define_this([=(Key, Value) | Other], [Key | Rest]) :- asserta(field(this, Key, Value)), - define_this(Other). + define_this(Other, Rest). + +undefine_this([]) :- !. +undefine_this([FieldName | Other]) :- + retract(field(this, FieldName, _Value)), + !, + undefine_this(Other). call_method(Instance, ClassName, Body) :- is_instance(Instance, ClassName), asserta(is_instance(this, ClassName)), - define_this(Instance), + define_this(Instance, For), call_cleanup(call(Body), ( retractall(is_instance(this, ClassName)), - retractall(field(this, _, _)))). + undefine_this(For))). check_value_type(nil, _X) :- !. check_value_type(var, X) :- var(X), !. -- 2.52.0