# Using Attribute Variables for custom type checking (at runtime)

**URL:** <https://swi-prolog.discourse.group/t/using-attribute-variables-for-custom-type-checking-at-runtime/3688>\
**Category:** Help!\
**Created:** [March 5, 2021, 11:42am UTC](https://swi-prolog.discourse.group/t/using-attribute-variables-for-custom-type-checking-at-runtime/3688 "2021-03-05T11:42:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [March 5, 2021, 11:42am UTC](https://swi-prolog.discourse.group/t/using-attribute-variables-for-custom-type-checking-at-runtime/3688/1 "2021-03-05T11:42:57Z")

</div>

Consider the assoc library.

One aspect of it is that it has an “initializer” for an empty assoc: **empty\_assoc** (?Assoc)

What if i were to wrap this predicate to add an attribute variable indicating the intended type of the assoc – say, its of type ‘a-\>b’

```prolog
new_teacher_student_mapping(Assoc) :-
    empty_assoc(Assoc)
   add_type('teacher->student', Assoc). % adds the type symbol as attribute variable to Assoc

get_students(Assoc, Students) :-
     must_be_of_type('teacher->student', Assoc), % raise exception, if wrong type
    assoc_to_values(Assoc, Students).

                      

```

Btw, this can be done without attribute variables. For example, by returning a pair instead of a vanilla assoc, and then checking for that pair.

How do these two approaches compare – e.g. performance wise.

All predicates that subsequently expect an assoc as argument could have a type check, by retrieving the assoc and checking for the expected type.

Is this something that could be done with attribute variables?

thanks,

Dan

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [March 5, 2021, 12:14pm UTC](https://swi-prolog.discourse.group/t/using-attribute-variables-for-custom-type-checking-at-runtime/3688/2 "2021-03-05T12:14:02Z")

</div>

The easiest way to add this kind of information to Prolog terms is to wrap them inside a compound.

```prolog
new_teacher_student_mapping('teacher->student'(Assoc)) :-
    empty_assoc(Assoc).

```

You can now use syntactic unification in the head of the predicate that uses this “kind” of mapping. It will by default fail if you give it a different kind of a mapping, but I don’t want to go into that topic again.

---

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [March 5, 2021, 12:18pm UTC](https://swi-prolog.discourse.group/t/using-attribute-variables-for-custom-type-checking-at-runtime/3688/3 "2021-03-05T12:18:04Z")

</div>

Thanks Boris.

Sometimes, the simplest solution doesn’t occur to me 🙂

Although, i think using compounds as arguments might make it harder to remove type information and testing via the term expansion approach I am now using – its a bit too built in as compound – i think.

Edit:

Also, i wonder if having compound terms passed around has a negative effect on indexing … if i recall correctly, O’Keefe in his Crafts book wasn’t too fond of using compound terms to type arguments.

Dan
