diff --git a/prolog/metta_lang/metta_typed_functions.pl b/prolog/metta_lang/metta_typed_functions.pl index 4bbf7c8205..d62ab7b1e4 100755 --- a/prolog/metta_lang/metta_typed_functions.pl +++ b/prolog/metta_lang/metta_typed_functions.pl @@ -57,7 +57,6 @@ %********************************************************************************************* - :- discontiguous default_isa/2. :- discontiguous desc_aka/2. :- discontiguous explicit_isa/2. @@ -139,10 +138,10 @@ explicit_isa('FailureOriginal', 'OutOfClausesEnum'). -% predicate_behavior(Predicate, Len, NoMatchBehavior, EvaluationOrder, SuccessBehavior, FailureBehavior, OutOfClausesBehavior) -predicate_behavior(Predicate, Len, MisMatchBehavior, NoMatchBehavior, EvaluationOrder, SuccessBehavior, FailureBehavior, OutOfClausesBehavior) :- - predicate_behavior_impl(Predicate, Len, MisMatchBehavior, NoMatchBehavior, EvaluationOrder, SuccessBehavior, FailureBehavior, OutOfClausesBehavior) - *-> true ; predicate_behavior_fallback(Predicate, Len, MisMatchBehavior, NoMatchBehavior, EvaluationOrder, SuccessBehavior, FailureBehavior, OutOfClausesBehavior). +% predicate_behavior(Op, Len, NoMatchBehavior, EvaluationOrder, SuccessBehavior, FailureBehavior, OutOfClausesBehavior) +predicate_behavior(Op, Len, MisMatchBehavior, NoMatchBehavior, EvaluationOrder, SuccessBehavior, FailureBehavior, OutOfClausesBehavior) :- + predicate_behavior_impl(Op, Len, MisMatchBehavior, NoMatchBehavior, EvaluationOrder, SuccessBehavior, FailureBehavior, OutOfClausesBehavior) + *-> true ; predicate_behavior_fallback(Op, Len, MisMatchBehavior, NoMatchBehavior, EvaluationOrder, SuccessBehavior, FailureBehavior, OutOfClausesBehavior). % default predicate_behavior_fallback(_, _, 'MismatchOriginal', 'NoMatchOriginal', 'OrderClause', 'Nondeterministic', 'ClauseFailNonDet', 'FailureOriginal'). @@ -152,6 +151,24 @@ predicate_behavior_impl('match', 4, 'MismatchFail', 'NoMatchFail', 'OrderClause', 'Nondeterministic', 'ClauseFailNonDet', 'FailureEmpty'). +get_ftype(Eq,RetType,Depth,Self,Val,TypeO):- + if_or_else(get_ftype_decl(Eq,RetType,Depth,Self,Val,TypeO),get_ftype_fallback(Eq,RetType,Depth,Self,Val,TypeO)). + +get_ftype_decl(_Eq,_RetType,Depth,Self,Val,TypeO):- + get_type(Depth,Self,Val,TypeO),is_list(TypeO),[Type|_]=TypeO,Type=='->'. + +get_ftype_fallback(_Eq,_Type,_Depth,Self,[Op|Args],TypeO):- nonvar(Op), len_or_unbound(Args,Len),!,get_operator_ftypedef(Self, Op, Len, TypeO). +get_ftype_fallback(_Eq,_Type,_Depth,Self,Op,TypeO):- get_operator_ftypedef(Self, Op, _Len, TypeO). + +op_farity(Op,Len):- no_repeats_var(Len), if_or_else(op_farity_decl(Op,Len),op_farity_fallback(Op,Len)). + +op_farity_decl(Op,Len):- metta_defn(_Self, [Op | Args], _Body), len_or_unbound(Args,Len). +op_farity_decl(Op,Len):- metta_type(_Self, Op, [Ar,_|Types]), Ar=='->', len_or_unbound(Types,Len). +op_farity_fallback(_Op,Len):- between(0,8,Len). + +get_operator_ftypedef(Self, Op, Len, TypeO):- (var(Len)->op_farity(Op,Len);true), + get_operator_typedef(Self, Op, Len, ParamTypes, RetType), append(['->'|ParamTypes],[RetType],TypeO). + /* metta_defn('&self', ['double-it', 'Z'], 'Z'). metta_defn('&self', ['double-it', ['S', X], ['S', ['S', ['double-it', X]]]). @@ -164,52 +181,108 @@ */ -function_declaration(Predicate, Len, Parameters, ParamTypes, RetType, [let, ReturnVal, Body, ReturnVal], ReturnVal) :- +fake_body([Op | Parameters], [Op | Parameters]). + + +metta_defn_return(Self, Original, Body, DeclBody, ReturnVal):- + if_or_else(metta_defn_decl(Self,Original, Body, DeclBody, ReturnVal),metta_defn_fallback(Self, Original, Body, DeclBody, ReturnVal)). + +metta_defn_decl(Self, [Op | Parameters], Body, [let, ReturnVal, Body, ReturnVal], ReturnVal):- metta_defn(Self, [Op | Parameters], Body). +metta_defn_fallback(_Self, [Op | Parameters], Body, Body, ReturnVal):- + Body = [let, [quote,ReturnVal], [quote,[Op | Parameters]], ReturnVal]. + + +function_declaration(Op, Len, Parameters, ParamTypes, RetType, DeclBody, ReturnVal) :- Self = '&self', len_or_unbound(Parameters, Len), - NR = ([Predicate | Parameters] + Body), + metta_defn_return(Self, [Op | Parameters], Body, DeclBody, ReturnVal), + len_or_unbound(Parameters, Len), + NR = ([Op | Parameters] + Body), copy_term(NR, NRR), no_repeats_var(NRR), - metta_defn(Self, [Predicate | Parameters], Body), - get_operator_typedef(Self, Predicate, Len, ParamTypes, RetType), + get_operator_typedef_near(Self, Op, Len, ParamTypes, RetType, [Op | Parameters], Body), NR = NRR, - write_src_nl(metta_defn(Self, [Predicate | Parameters], Body)). - -info_about(Predicate, Len) :- + nop(write_src_nl(metta_defn(Self, [Op | Parameters], Body))). + +get_operator_typedef_near(Self, Op, Len, ParamTypes, RetType, Head, Body):- + src_data_ordinal([=,Head,Body],FileLineClause), + length(ParamTypes,Len), + findall(FileLineType-pr(ParamTypes, RetType),(src_data_ordinal([:,Op,[Ar|Type]],FileLineType),Ar=='->',append(ParamTypes,[RetType],Type)),LocTypes), + get_operator_typedef_near(Self, Op, Len, ParamTypes, RetType, Head, Body, FileLineClause,LocTypes). + +src_data_ordinal(Data,Ordinal):- + user:metta_file_buffer(0,Ordinal,_TypeNameCompound,Data,_NamedVarsListC,_Context,_Range). + +get_operator_typedef_near(_Self, _Op, _Len, ParamTypes, RetType, _Head, _Body, FileLineClause,LocTypes):- + select(FileLineType-pr(ParamTypes, RetType),LocTypes,Rest), + Space is FileLineType-FileLineClause, + Space<0, + \+ (member(FileLineTypeOther-_,Rest), + SpaceOther is FileLineTypeOther-FileLineClause, + SpaceOther<0, SpaceOther>Space),!. + +get_operator_typedef_near(_Self, _Op, _Len, ParamTypes, RetType, _Head, _Body, FileLineClause,LocTypes):- + select(FileLineType-pr(ParamTypes, RetType),LocTypes,Rest), + Space is FileLineType-FileLineClause, + Space>0, + \+ (member(FileLineTypeOther-_,Rest), + SpaceOther is FileLineTypeOther-FileLineClause, + SpaceOther>0, SpaceOther ! ; true) % vs Nondeterministic ; (FailureBehavior == 'ClauseFailDet' -> % vs ClauseFailNonDet - (OutOfClausesBehavior == 'FailureOriginal' -> throw(metta_notreducable([Predicate| ReducedParams])) ; (!, fail)); + (OutOfClausesBehavior == 'FailureOriginal' -> throw(metta_notreducable([Op| ReducedParams])) ; (!, fail)); fail))) *-> true ; (OutOfClausesBehavior == 'FailureOriginal' -> throw(metta_notreducable(Original)) ; (!, fail))).