Indexing clauses that use dict?

From running jiti_list/0, it appears that a predicate with dicts in the head doesn’t create any indexes. Is this the case? - and is there a way to index a predicate that has dicts in the head? (Caveat: from looking at the output from profile/1, it’s unclear to me how much this would speed up my code – the profiler seems to have a black hole into which ~50% of the processing time disappears.)

The code looks something like this (the node are generated by a Python program that outputs JSON, which is why I’m using dicts rather than Prolog terms):

node('AnnAssignStmt'{left_annotation: LeftAnnotation, expr: Right, left: Left},
       [stmt(annassign(Left, LeftAnnotation, Right))]) --> !,
    assign_normalized(Left, LeftAnnotation),
    assign_normalized(Left, Right).
node('ArgumentNode'{name: NameAstn, arg: Arg},
       [arg(Name, ArgType)]) --> !,
    { node_astn(NameAstn, _, _, Name) },
    node(Arg, ArgType).

% ... another ~100 similar clauses

node(X, Ty) -->
    { goal_failed(node(X,Ty)) }.
3 Likes

Dicts are just compound terms. This means they might get indexed, but given the variation it is not very likely.

It appears that having instantiated tags on dicts doesn’t help (according to jiti_list/0), presumably because a dict is a compound term using the functor dict, with the tag as the first argument, and deep indexing doesn’t get to the tag first argument?

1 Like

I was able to index my predicate by this trick, using the dict’s tag to index an auxilliary predicate:

%! node(+Node:dict, -Type) is det.
node(Node, Type) --> 
    { is_dict(Node, Tag) },
    node(Tag, Node, Type).

%! node(+Tag:atom, Node:dict, -Type) is det.
node('AnnAssignStmt',
     'AnnAssignStmt'{left_annotation: LeftAnnotation, expr: Right, left: Left},
     [stmt(annassign(Left, LeftAnnotation, Right))]) --> !,
    assign_normalized(Left, LeftAnnotation),
    assign_normalized(Left, Right).
  % ... etc

jiti_list/0 confirms that this creates an index, although in this case it didn’t result in any significant overall speedup (I’m still trying to figure out where the black hold in the profiling output is).