Skip to content

Commit 92dfc51

Browse files
committed
metta_typed_functions
1 parent 8b86f98 commit 92dfc51

File tree

3 files changed

+87
-36
lines changed

3 files changed

+87
-36
lines changed

prolog/metta_lang/metta_printer.pl

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@
748748
must_det_lls((
749749
pre_guess_varnames(V,II),call(II=V),
750750
guess_varnames(II,I),
751-
nop(ignore(numbervars(I,10000,_,[singleton(true),attvar(skip)]))),
751+
nop(ignore(numbervars(I,400,_,[singleton(true),attvar(skip)]))),
752752
materialize_vns(I))).
753753
pre_guess_varnames(V,I):- \+ compound(V),!,I=V.
754754
pre_guess_varnames(V,I):- copy_term_nat(V,VC),compound_name_arity(V,F,A),compound_name_arity(II,F,A), metta_file_buffer(_, _, _, II, Vs, _,_), Vs\==[], copy_term_nat(II,IIC), VC=@=IIC, II=I,maybe_name_vars(Vs),!.
@@ -758,22 +758,48 @@
758758

759759
write_w_attvars(Term):- \+ \+ write_w_attvars0(Term).
760760
write_w_attvars0(Term):-
761+
number_src_vars(Term,PrintSV,Goals),
762+
once_writeq_nl_now(PrintSV),
763+
if_t(Goals\==[], once_writeq_nl_now(yellow,goals={Goals})),!.
764+
765+
number_src_vars(Term,PrintSV,Goals):-
761766
src_vars(Term,PrintSV),
762767
copy_term(Term,TermC,Goals), PrintSV = TermC,
763768
materialize_vns(PrintSV),
764-
ignore(numbervars(PrintSV,20000,_,[singleton(true),attvar(skip)])),
769+
ignore(numbervars(PrintSV,260,_,[singleton(true),attvar(skip)])),
765770
ignore(numbervars(PrintSV,26,_,[singleton(true),attvar(bind)])),
766-
must(PrintSV = Term),
767-
once_writeq_nl_now(green,Term),
768-
if_t(Goals\==[], once_writeq_nl_now(yellow,{Goals})),!.
771+
must(PrintSV = Term),!.
769772

770-
once_writeq_nl_now(Color,P) :-
773+
once_writeq_nl_now(P) :-
771774
% Standardize variable names in `P` and print it using `ansi_format`.
772775
% Use `nb_setval` to store the printed term in `$once_writeq_ln`.
773776
\+ \+ (must_det_ll((
774777
src_vars(P,PP),
775-
with_output_to(user_error,ansi_format([fg(Color)], '~N~q.~n', [PP]))))).
778+
format('~N~q.~n', [PP])))).
779+
780+
% Standardize variable names in `P` and print it using `ansi_format`.
781+
% Use `nb_setval` to store the printed term in `$once_writeq_ln`.
782+
once_writeq_nl_now(Color,P) :- w_color(Color,once_writeq_nl_now(P)).
776783

784+
%! write_src_nl(+Src) is det.
785+
%
786+
% Prints a source line followed by a newline.
787+
%
788+
% @arg Src The source line to print.
789+
%
790+
write_src_nl(Src) :-
791+
% Print a newline, the source line, and another newline.
792+
\+ \+ (must_det_ll((
793+
number_src_vars(Src, SrcSrc, Goals),
794+
(format('~N'), write_src(SrcSrc),
795+
if_t(Goals\==[],once_writeq_nl_now(yellow,goals=Goals)),
796+
format('~N'))))).
797+
798+
799+
w_color(Color,Goal):-
800+
\+ \+ (must_det_ll((
801+
wots(Text,Goal),
802+
with_output_to(user_error,ansi_format([fg(Color)], '~w', [Text]))))).
777803

778804
materialize_vns(Term):- term_variables(Term,List), maplist(materialize_vn,List).
779805
materialize_vn(Var):- \+ attvar(Var),!.

prolog/metta_lang/metta_space.pl

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,15 +1664,6 @@
16641664
% Print each line of the source code.
16651665
maplist(write_src_nl, SrcL).
16661666

1667-
%! write_src_nl(+Src) is det.
1668-
%
1669-
% Prints a source line followed by a newline.
1670-
%
1671-
% @arg Src The source line to print.
1672-
%
1673-
write_src_nl(Src) :-
1674-
% Print a newline, the source line, and another newline.
1675-
format('~N'), write_src(Src), format('~N').
16761667

16771668
%'get-metta-src'(Pred,[Len|SrcL]):-
16781669
% findall(['AtomDef',Src], 'get-metta-src1'(Pred,Src), SrcL),

prolog/metta_lang/metta_typed_functions.pl

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -203,33 +203,51 @@
203203

204204

205205
metta_defn_return(Self, Original, Body, WrappedBody, ReturnVal):-
206-
if_or_else(metta_defn_decl(Self, Original, Body, WrappedBody, ReturnVal), metta_defn_fallback(Self, Original, Body, WrappedBody, ReturnVal)).
206+
if_or_else(metta_defn_decl(Self, Original, Body, WrappedBody, ReturnVal),
207+
metta_defn_fallback(Self, Original, Body, WrappedBody, ReturnVal)).
207208

208209
metta_defn_decl(Self, [Op | Parameters], Body, [let, ReturnVal, Body, ReturnVal], ReturnVal):- metta_defn(Self, [Op | Parameters], Body).
210+
211+
must_length(List,Len):- is_list(List),!,must_det_lls(length(List,Len)).
212+
must_length(List,Len):- integer(Len),!,must_det_lls(length(List,Len)).
213+
must_length(List,Len):- trace,length(List,Len).
214+
215+
metta_defn_fallback(_Self, [Op | Parameters], [let, ReturnVal, Body, ReturnVal], Body, ReturnVal):-
216+
must_length(Parameters, Len),
217+
format(atom(Fn),'mc_~w__~w',[Len,Op]),
218+
current_predicate(Fn/_),
219+
Body = ['call-fn',Fn|Parameters],!.
220+
209221
metta_defn_fallback(_Self, [Op | Parameters], Body, Body, ReturnVal):-
210222
Body = [let, [quote, ReturnVal], [quote, [Op | Parameters]], ReturnVal].
211223

212224

213225
metta_typed_defn(Self, ParamTypes, RetType, Head, WrappedBody, ReturnVal):- Head = [Op | Parameters],
214226
function_declaration(Self, Op, _Len, Parameters, ParamTypes, RetType, WrappedBody, ReturnVal).
215227

216-
function_declaration(Self, Op, Len, Parameters, ParamTypes, RetType, WrappedBody, ReturnVal) :-
228+
function_declaration(_, Op, Len, Parameters, ParamTypes, RetType, WrappedBody, ReturnVal) :-
229+
must_det_lls((
217230
%Self = '&self',
218231
len_or_unbound(Parameters, Len),
232+
%(var(Len)->op_farity(Op, Len);true),
233+
%len_or_unbound(Parameters, Len),
234+
nop(no_repeats_var(NRR)),
219235
metta_defn_return(Self, [Op | Parameters], Body, WrappedBody, ReturnVal),
220-
len_or_unbound(Parameters, Len),
236+
must_length(Parameters, Len),
221237
NR = ([Op | Parameters] + Body),
222-
copy_term(NR, NRR),
223-
no_repeats_var(NRR),
224238
head_body_typedef(Self, Op, Len, ParamTypes, RetType, [Op | Parameters], Body),
225-
NR = NRR. %nop(write_src_nl(metta_defn(Self, [Op | Parameters], Body))).
239+
NR = NRR)). %nop(write_src_nl(metta_defn(Self, [Op | Parameters], Body))).
226240

227241
head_body_typedef(Self, Op, Len, ParamTypes, RetType, Head, Body):-
228-
(src_data_ordinal(Self, [=, Head, Body], ClauseOrdinal)*->true; (ClauseOrdinal = -1)),
242+
(src_data_ordinal(Self, [=, Head, Body], ClauseOrdinal)*->true;
243+
(src_data_ordinal(Self, [=, Head, _], ClauseOrdinal)*->true;
244+
(ClauseOrdinal = -1))),
229245
length(ParamTypes, Len),
230246
SrcObject = pr(ParamTypes, RetType),
231247
findall(TypeDeclLoc-SrcObject, (src_data_ordinal(Self, [:, Op, [Ar|Type]], TypeDeclLoc), Ar=='->', append(ParamTypes, [RetType], Type)), SrcObjectList),
232-
nearest_src_object(SrcObject, ClauseOrdinal, SrcObjectList).
248+
nearest_src_object(SrcObject, ClauseOrdinal, SrcObjectList),!.
249+
head_body_typedef(Self, Op, Len, ParamTypes, RetType, _Head, _Body):-
250+
get_operator_typedef(Self, Op, Len, ParamTypes, RetType).
233251

234252
src_data_ordinal(_Self, Data, Ordinal):-
235253
user:metta_file_buffer(0, Ordinal, _TypeNameCompound, Data, _NamedVarsListC, _Context, _Range).
@@ -250,7 +268,8 @@
250268
DistanceOther is LocOther-ClauseOrdinal,
251269
DistanceOther>0, DistanceOther<Distance), !.
252270

253-
nearest_src_object(SrcObject, _, SrcObjectList):- last(_ - SrcObject, SrcObjectList).
271+
nearest_src_object(SrcObject, _, SrcObjectList):- last(_ - SrcObject, SrcObjectList),!.
272+
254273

255274
finfo([Op|Args]):- is_list(Args), !, length(Args, Len), finfo(Op, Len).
256275
finfo(Op):- atomic(Op), !, finfo(Op, _, _).
@@ -259,28 +278,43 @@
259278
finfo(Op, Len, Head) :-
260279
% length(Parameters, Len),
261280
op_farity(Op, Len),
281+
succ(Len,LenP1),
262282
call_showing(predicate_behavior(Self, Op, Len, [List|_]), predicate_behavior(Self, Op, Len, List)),
263283
length(Parameters, Len),
264284
length(ParamTypes, Len),
265285
[Op|Parameters] = Head,
266-
call_showing(get_ftype('=', _RetType1, 20, Self, Head, TypeO), get_ftype(Self, Head, TypeO)),
286+
call_showing(get_ftype('=', _RetType1, 20, Self, Head, TypeO), [['get-ftype', Self, Head],==>,TypeO]),
267287
%call_showing(metta_atom(_, [iz, Op, _])),
268288
%call_showing(metta_atom(_, [':', Op, _])),
269-
% ReturnVal = '$VAR'('ReturnVal'),
270-
call_showing(function_declaration_scores(Self, Op, Len, Parameters, ParamTypes, _RetType, _Body, _ReturnVal, _Scores)),
271-
call_showing((metta_atom(KB, [A, B|Out]), sub_var(Op, [A, B])), ist(KB, [A, B|Out])),
289+
ReturnVal = '$VAR'('_returnVal'),
290+
call_showing(transpiler_predicate_store(Op, LenP1, _, _)),
291+
call_showing(transpiler_clause_store(Op, LenP1, _, _, _, _, _, _, _)),
292+
format(atom(Fn),'mc_~w__~w',[Len,Op]), % forall(current_predicate(Fn/LenP1),listing(Fn/LenP1)),
293+
call_showing(Fn/LenP1),
294+
call_showing(function_declaration_scores(Self, Op, Len, Parameters, ParamTypes, _RetType, Body, ReturnVal,_)),
295+
if_t(\+ metta_defn(Self, [Op | Parameters], Body), call_showing(metta_defn_return(Self, [Op | Parameters], Body, _, ReturnVal),[=,[Op | Parameters],Body])),
296+
call_showing((metta_atom(KB, [A, B|Out]), sub_var(Op, [A, B])), [ist,KB, [A, B|Out]]),
272297
true.
273298

274-
call_showing(Var):- \+ callable(Var), !.
275-
call_showing(Atom):- atom(Atom), !, current_predicate(Atom/_, SHOWP), !, call_showing(SHOWP, SHOWP).
276-
call_showing(Op/Len):- !, current_predicate(Op/Len, SHOWP), !, call_showing(SHOWP, SHOWP).
277-
call_showing(SHOWP):- call_showing(SHOWP, SHOWP), !.
299+
call_showing(Var):- \+ callable(Var), !, write_src_nl(not(callable(Var))).
300+
call_showing(Atom):- atom(Atom), \+ current_predicate(Atom/_, _), !, write_src_nl(unknown(Atom)).
301+
call_showing(Atom):- atom(Atom), !, forall(current_predicate(Atom/N),call_showing(Atom/N)).
302+
call_showing(Op/Len):- \+ current_predicate(Op/Len), !, write_src_nl(unknown(Op/Len)).
303+
call_showing(Op/Len):- !, forall(current_predicate(Op/Len, SHOWP), call_showing(clause(SHOWP,Body), (SHOWP:-Body))).
304+
call_showing(SHOWP):- \+ current_predicate(_, SHOWP), !, write_src_nl(unknown(SHOWP)).
305+
call_showing(SHOWP):- call_showing(SHOWP, SHOWP).
278306

279-
call_showing(SHOWP, Template):- current_predicate(_, SHOWP), !,
307+
call_showing(SHOWP, Template):-
280308
no_repeats_var(TemplateNR),
281309
findall(Template, (SHOWP, TemplateNR=Template), ScoredBodies),
282-
maplist(write_src_nl, ScoredBodies), !.
283-
call_showing(SHOWP, _Template):- write_src_nl(unknown(SHOWP)).
310+
maplist(output_showing, ScoredBodies),
311+
ignore((ScoredBodies==[], functor(Template,F,A),output_showing(missing(F/A)))).
312+
313+
output_showing(List):- is_list(List),!,write_src_nl(List).
314+
output_showing(Info):- w_color(white,write_w_attvars(Info)),nl, !.
315+
output_showing(Info):- write_src_nl(Info).
316+
317+
284318

285319
function_declaration_scores(Self, Op, Len, Parameters, ParamTypes, RetType, Body, ReturnVal, Score + HScore):-
286320
function_declaration(Self, Op, Len, Parameters, ParamTypes, RetType, Body, ReturnVal),

0 commit comments

Comments
 (0)