From: LeonardoBizzoni Date: Thu, 21 Dec 2023 19:52:00 +0000 (+0100) Subject: Type checks X-Git-Url: http://git.leonardobizzoni.com/?a=commitdiff_plain;h=9a30c7325ab0d7eb6839dcf163ff2823146b7208;p=ObjectOriented-Prolog-Lisp Type checks Non ho fatto molti test quindi potrebbero esserci errori. --- diff --git a/Prolog/README.org b/Prolog/README.org index 3f9ce21..499e417 100644 --- a/Prolog/README.org +++ b/Prolog/README.org @@ -190,6 +190,7 @@ add_part(ClassName, [field(Name, Value, Type) | OtherParts]) :- %% Se è già definito questo member cancellalo retractall(is_member(field(Name, _, _), ClassName)), + check_value_type(Type, Value), asserta(is_member(field(Name, Value, Type), ClassName)), add_part(ClassName, OtherParts). @@ -201,6 +202,7 @@ add_part(ClassName, [field(Name, Value, Type) | OtherParts]) :- %% Se è già definito questo member cancellalo retractall(is_member(field(Name, _, _), ClassName)), + check_value_type(Type, Value), asserta(is_member(field(Name, Value, Type), ClassName)), add_part(ClassName, OtherParts). @@ -236,6 +238,8 @@ set_fields_for(InstanceName, ClassName, [=(Name, Value) | OtherFields]) :- field(InstanceName, Name, OldValue), + is_member(field(Name, _Value, Type), ClassName), + check_value_type(Type, Value), retractall(field(InstanceName, Name, OldValue)), !, asserta(field(InstanceName, Name, Value)), @@ -279,3 +283,34 @@ call_method(Instance, ClassName, Body) :- call(Body), retractall(field(this, _, _)). #+end_src + +** check_value_type +#+begin_src prolog :tangle oop.pl +check_value_type(nil, _X) :- !. +check_value_type(var, X) :- var(X), !. +check_value_type(nonvar, X) :- nonvar(X), !. +check_value_type(integer, X) :- integer(X), !. +check_value_type(float, X) :- float(X), !. +check_value_type(rational, X) :- rational(X), !. +check_value_type(number, X) :- number(X), !. +check_value_type(atom, X) :- atom(X), !. +check_value_type(string, X) :- string(X), !. +check_value_type(atomic, X) :- atomic(X), !. +check_value_type(compound, X) :- compound(X), !. +check_value_type(callable, X) :- callable(X), !. +check_value_type(ground, X) :- ground(X), !. + +check_value_type(ClassName, Instance) :- + is_class(ClassName), + is_instance(Instance, ClassName), + !. + +check_value_type(ClassName, Instance) :- + is_child_of(ClassName, ChildClass), + is_instance(Instance, ChildClass), + !. + +check_value_type(ClassName, Instance) :- + is_child_of(ClassName, ChildClass), + check_value_type(ChildClass, Instance). +#+end_src diff --git a/Prolog/oop.pl b/Prolog/oop.pl index 7bcd4c6..97f659f 100644 --- a/Prolog/oop.pl +++ b/Prolog/oop.pl @@ -133,6 +133,7 @@ add_part(ClassName, [field(Name, Value, Type) | OtherParts]) :- %% Se è già definito questo member cancellalo retractall(is_member(field(Name, _, _), ClassName)), + check_value_type(Type, Value), asserta(is_member(field(Name, Value, Type), ClassName)), add_part(ClassName, OtherParts). @@ -144,6 +145,7 @@ add_part(ClassName, [field(Name, Value, Type) | OtherParts]) :- %% Se è già definito questo member cancellalo retractall(is_member(field(Name, _, _), ClassName)), + check_value_type(Type, Value), asserta(is_member(field(Name, Value, Type), ClassName)), add_part(ClassName, OtherParts). @@ -173,6 +175,8 @@ set_fields_for(InstanceName, ClassName, [=(Name, Value) | OtherFields]) :- field(InstanceName, Name, OldValue), + is_member(field(Name, _Value, Type), ClassName), + check_value_type(Type, Value), retractall(field(InstanceName, Name, OldValue)), !, asserta(field(InstanceName, Name, Value)), @@ -206,3 +210,31 @@ call_method(Instance, ClassName, Body) :- define_this(Instance), call(Body), retractall(field(this, _, _)). + +check_value_type(nil, _X) :- !. +check_value_type(var, X) :- var(X), !. +check_value_type(nonvar, X) :- nonvar(X), !. +check_value_type(integer, X) :- integer(X), !. +check_value_type(float, X) :- float(X), !. +check_value_type(rational, X) :- rational(X), !. +check_value_type(number, X) :- number(X), !. +check_value_type(atom, X) :- atom(X), !. +check_value_type(string, X) :- string(X), !. +check_value_type(atomic, X) :- atomic(X), !. +check_value_type(compound, X) :- compound(X), !. +check_value_type(callable, X) :- callable(X), !. +check_value_type(ground, X) :- ground(X), !. + +check_value_type(ClassName, Instance) :- + is_class(ClassName), + is_instance(Instance, ClassName), + !. + +check_value_type(ClassName, Instance) :- + is_child_of(ClassName, ChildClass), + is_instance(Instance, ChildClass), + !. + +check_value_type(ClassName, Instance) :- + is_child_of(ClassName, ChildClass), + check_value_type(ChildClass, Instance).