3

Im pretty new to prolog, and got stuck. I need to store a variable with some string during calculations, I decided to do this by adding a "single fact" to the class I'm working with. Everything works well, the string is being stored, but when I try to add the code to access it later on, compiler returns an error "The expression has type 'dataBL::dataBL@objectDB', which is incompatible with the type '::symbol'".
I dont think it is the valid way to store a variable, so, can anyone help me with that? I tried searching online for the answers, and found nothing.

I'm trying to access the fact like this:

    getString(U) :-
    U = stringStorage(_).
coder
  • 12,832
  • 5
  • 39
  • 53
Nikita Vasin
  • 125
  • 1
  • 11
  • Well, I googled a little bit more, checked tutorials that come with the system, and looks like this solution works: `getString(U) :- stringStorage(U).` But I'd really like some explanation on how it actually works... – Nikita Vasin Dec 16 '17 at 16:40
  • 1
    Forget what you know about how other languages work. Prolog is different. It has *predicates*, not *functions*. *predicates* do not return values. The can accept arguments, but only either succeed, fail, or do not terminate (which is a problem if that happens). It attempts to instantiate any variables you give it that will lead to a successful query. So when you say `getString(U) :- stringStorage(U).` Prolog will look at facts and predicates called `stringStorage(Something)` and when it finds them, it looks for a value for your `U` which will cause it to succeed. – lurker Dec 16 '17 at 18:14
  • 1
    The `=/2` in Prolog is not an assignment operator. It's a *unification* operator. It attempts to *unify* its arguments. So when you say, `U = stringStorage(_)` it looks at the variables, in this case, `U` and `_` (an anonymous variable) and looks for ways to make this unification succeed. It actually does succeed by binding the term `stringStorage(_)` to `U`. So the result of the `getString(U)` query will be something like `U = stringStorage(_)`, just like you asked for. :) – lurker Dec 16 '17 at 18:16
  • 1
    Looks like you need find a good book or tutorial on Prolog. – lurker Dec 16 '17 at 18:16

1 Answers1

1

If I get you right you need to store a value associated with some variable ID (key) as a fact. The (abstract) solution for your task canas the storing your values as a facts:

bind( Key, Value ).

Example of implementation (SWI Prolog)

Storing

recordz('var1', "String value1"),

recordz('var2', "String value2")

Querying value of var2

current_key(-Key), 

Key = 'var2'

recorded(Key, Value)
Anton Danilov
  • 1,246
  • 11
  • 26
  • Yeah, thank you, I felt like an idiot when I reread my question. Forgot about binding and the fact that prolog is different. – Nikita Vasin Jan 03 '18 at 08:14