1

I try to assert some rules automatically in SWI-Prolog:

generate_rule_len(FG,SG):- 
    length(FG,L),length(SG,L0), 
    Head = input_len(FG,SG,FS,SS,X),
    Body = (length(FG,L1),L1 is L, length(SG,L2), L2 is L0, X = SS),
    % Rule = (Head :- Body), \+Rule,
    assertz(Head :- Body),
    append('rulesDB.pl'), 
    writeq(Head :- Body),write('.'),nl,
    told.

This works pretty fine, but the asserted result in the rulesDB.pl do not use the names of the variables FG,SG,FS,SS,Xthey are replaced with their memory position (?), this looks like:

input_len(_3078,_3080,_3082,_3084,_3086):-
   length(_3078,_3098),_3098 is 2,length(_3080,_3122),_3122 is 2,_3086=_3084. 

Is it possible to use the names of the variables instead?

Futhermore I want to prevent the duplication of generated rules. Therefore I tried Rule = (Head :- Body), \+Rule, but this give me an Undefined procedure: (:-)/2. Can anybody tell me what´s wrong with my code?

Thanks in advance!

lurker
  • 56,987
  • 9
  • 69
  • 103
74ck
  • 65
  • 2
  • 9
  • 3
    See [this](https://stackoverflow.com/questions/7947910/converting-terms-to-atoms-preserving-variable-names-in-yap-prolog/7948525#794852) for variable names. Also, rather use `portray_clause(( Head :- Body )).` in place of `writeq ... nl` it gives nicer names. Then, look at `clause/2` – false Jan 03 '18 at 12:29
  • 1
    They are not memory addresses, just sequence numbers. See [this question](https://stackoverflow.com/questions/9358623/variable-names-in-swi-prolog) for example. The error is because you try to call the rule, which is not how it works (you call just the head). You should use an if-then-else so that the whole thing does not fail. – Tomas By Jan 03 '18 at 12:30
  • 1
    ... also note that `length(FG, 2)` is more compact than `length(FG, L1), L1 is 2` – false Jan 03 '18 at 12:30
  • To check if the rule exists, you can use [`callable/1`](http://www.swi-prolog.org/pldoc/man?predicate=callable%2f1) (I think). – Tomas By Jan 03 '18 at 12:34
  • Thank you both, I will test it – 74ck Jan 03 '18 at 12:44
  • @false `portray_clause(( Head :- Body ))` solves the variable name issue also. And `\+(clause(Head,Body))` my other Problem, thanks! – 74ck Jan 03 '18 at 12:54
  • @J4ck: Note that `portray_clause/1` generates the names anew! – false Jan 03 '18 at 12:56
  • @false you re right, but in my case it´s not a problem, i just needed a better representation of the clauses, as with the adresses. – 74ck Jan 03 '18 at 13:07

0 Answers0