Skip to content

Commit

Permalink
Refactor type constraint handling in metta_types.pl
Browse files Browse the repository at this point in the history
- Replaced `metta_type` with `cns` for clarity.
- Added `prevent_type_violations` for stricter constraint enforcement.
- Enhanced `get_type_each` for variable constraint handling.
  • Loading branch information
TeamSPoon committed Jan 6, 2025
1 parent fe29cb2 commit 687ca1f
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions prolog/metta_lang/metta_types.pl
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,8 @@
% get_type(Depth, Self, Val, Type) :- is_debugging(eval),
% ftrace(get_type_each(Depth, Self, Val, Type)),
% fail.
get_type_each(Depth, Self, Var, Type) :- var(Var), get_attr(Var,cns, _ = Set),member(Type,Set).

get_type_each(Depth, Self, Expr, ['StateMonad', Type]) :-
% Handle state monad expressions.
notrace(is_valid_nb_state(Expr)), !,
Expand All @@ -606,7 +608,7 @@
get_type_each(_Dpth, Self, Var, Type) :-
% Retrieve type from variable attributes.
var(Var), !,
get_attr(Var, metta_type, Self = TypeList),
get_attr(Var, cns, Self = TypeList),
member(Type, TypeList).
get_type_each(_Dpth, _Slf, Expr, 'hyperon::space::DynSpace') :-
% Check if the expression is a dynamic space.
Expand Down Expand Up @@ -969,7 +971,6 @@
% ?- get_value_type(_, _, X, Type).
% Type = '%Undefined%'.
%
get_value_type(_Dpth,_Slf,Var,'%Undefined%'):- var(Var),!.
get_value_type(_Dpth,_Slf,Val,'Number'):- number(Val),!.
get_value_type(_Dpth,_Slf,Val,'String'):- string(Val),!.
get_value_type(Depth,Self,Val,T):- get_type(Depth,Self,Val,T), T\==[], T\=='%Undefined%',!.
Expand Down Expand Up @@ -1445,7 +1446,7 @@
%
into_typed_arg(_Dpth, Self, T, M, Y) :-
% If the value is a variable, assign the type attribute and unify it.
var(M), !, Y = M, nop(put_attr(M, metta_type, Self = T)).
var(M), !, Y = M, nop(put_attr(M, cns, Self = T)).
into_typed_arg(Depth, Self, T, M, Y) :-
% Use into_typed_arg0 for further evaluation or fallback to direct unification.
into_typed_arg0(Depth, Self, T, M, Y) *-> true ; M = Y.
Expand Down Expand Up @@ -1490,21 +1491,27 @@
nonvar(T), is_pro_eval_kind(T), !.
wants_eval_kind(_) :- true.

%! metta_type:attr_unify_hook(+Input, +NewValue) is det.
%! cns:attr_unify_hook(+Input, +NewValue) is det.
%
% Handles the unification of a value with a `metta_type` attribute.
% Handles the unification of a value with a `cns` attribute.
%
% @arg Input The context or structure being evaluated.
% @arg NewValue The value being unified with the attribute.
%
metta_type:attr_unify_hook(Self = TypeList, NewValue) :-
prevent_type_violations(BecomingValue,RequireType):- non_arg_violation(_Self, RequireType, BecomingValue).
% TODO make sure it is inclusive rather than exclusive
cns:attr_unify_hook(_=TypeRequirements,BecomingValue):- \+ maplist(prevent_type_violations(BecomingValue),TypeRequirements), !, fail.
cns:attr_unify_hook(Self = TypeList, NewValue) :-
% If the new value is an attributed variable, assign the same attribute.
attvar(NewValue), !, put_attr(NewValue, metta_type, Self = TypeList).
metta_type:attr_unify_hook(Self = TypeList, NewValue) :-
attvar(NewValue), !, put_attr(NewValue, cns, Self = TypeList).
cns:attr_unify_hook(Self = TypeList, NewValue) :-
% Retrieve the type of the new value and check if it can be assigned.
get_type(20, Self, NewValue, Was),
can_assign(Was, Type).




%! set_type(+Depth, +Self, +Var, +Type) is det.
%
% Sets the type of a variable (Var) based on the given type (Type).
Expand Down Expand Up @@ -1545,7 +1552,7 @@
add_type(_Depth, Self, Var, TypeL, Type) :- var(Var), !,
% Add the new type to the list and set it as an attribute.
append([Type], TypeL, TypeList),
put_attr(Var, metta_type, Self = TypeList).
put_attr(Var, cns, Self = TypeList).
add_type(_Depth, _Self, Var, TypeL, Type) :-
ignore(append(_,[Type|_], TypeL)),!.
% If the variable is not bound, do nothing.
Expand Down

0 comments on commit 687ca1f

Please sign in to comment.