# Pattern matching dicts in head terms?

**URL:** <https://swi-prolog.discourse.group/t/pattern-matching-dicts-in-head-terms/2487>\
**Category:** General\
**Created:** [June 9, 2020, 6:25pm UTC](https://swi-prolog.discourse.group/t/pattern-matching-dicts-in-head-terms/2487 "2020-06-09T18:25:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stuz5000](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/stuz5000/32/940_2.png) [@stuz5000](https://swi-prolog.discourse.group/u/stuz5000)\
**Post date:** [June 9, 2020, 6:25pm UTC](https://swi-prolog.discourse.group/t/pattern-matching-dicts-in-head-terms/2487/1 "2020-06-09T18:25:35Z")

</div>

Can dicts be used effectively match on the heads of rules?

Suppose I have a number of pattern transformations of structured data:

```prolog
A ---> B:-
    % token(tokendata{tag:tagdata{kind:str, ... } }, [<token>] )
    A = token(AData, AChildren),
    AData.tag.kind = 'number',
    ... 

A ---> B:- ...
A ---> B:- ...
A ---> B:- ...
... many

```

… which I expect will have a very negative impact on performance with more rules. (I assume no indexing happening on the contents of A or B, or does the prolog JIT look inside the rule?).

Is there a way to benefit from indexing here?

Perhaps something like this:

```prolog
token(tokendata{tag:tagdata{kind:number | TagDatas } | TokenDatas }, Children ) --> B :-

```

where TagDatas means “the remaining key-value pairs”.

I understand that I could flatten `tok` and `tag` to be plain-old-structure, but ideally, for my sanity, I’d like to preserve the naming of attributes that `dict` provides. (If it helps, the dicts for have a fixed set of keys and are easily mapped to structures).

---

<div class="post-metadata">

**Author:** ![stuz5000](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/stuz5000/32/940_2.png) [@stuz5000](https://swi-prolog.discourse.group/u/stuz5000)\
**Post date:** [June 10, 2020, 4:08am UTC](https://swi-prolog.discourse.group/t/pattern-matching-dicts-in-head-terms/2487/2 "2020-06-10T04:08:03Z")

</div>

Related: [Missing a way for partial dict unification in clause header?](https://swi-prolog.discourse.group/t/missing-a-way-for-partial-dict-unification-in-clause-header/824)

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [June 10, 2020, 8:09am UTC](https://swi-prolog.discourse.group/t/pattern-matching-dicts-in-head-terms/2487/3 "2020-06-10T08:09:02Z")

</div>

If all dicts have the same set of keys the system may use _deep indexing_. Run the code and use jiti\_list/1 to see what indexes it created.

(I really love the auto-linking as it avoids the need to lookup the exact name and arity in the manual 🙂 Thanks @jamesnvc)

In other cases, you may use term\_expansion/2 to extract the indexable keys from the dict and (also) pass it as an additional argument.
