Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use higher-order predicates for more convenience #1

Open
triska opened this issue Jun 9, 2016 · 4 comments
Open

Use higher-order predicates for more convenience #1

triska opened this issue Jun 9, 2016 · 4 comments

Comments

@triska
Copy link
Contributor

triska commented Jun 9, 2016

The following two higher-order predicates may turn out very helpful, also in future extensions:

  • maplist/3, for example in: maplist(pretty_type, Ls0, Ls), avoiding an auxiliary predicate
  • foldl/4 to avoid or shorten replace_vars_/6

I hope this helps to make the code a bit shorter and more readable.

@alehander92
Copy link
Owner

Oh, those are great, I'll refactor it tomorrow!

Also, do you know if this free var manipulation (detecting free vars and replacing them with atoms) is something which is normal in Prolog or is there a more idiomatic way to do it?

@triska
Copy link
Contributor Author

triska commented Jun 10, 2016

The best way to reason about variables within your program is to choose a good representation for your data. In an initial conversion step, you will likely need impure checks like var/1 to detect variables, but you can convert this to a cleaner representation by distinguishing variables with a suitable wrapper. You can for example use v(Var), var(Var) or variable(Var) to represent variables within your data, and a(Atom) or atom(Atom) to represent atoms.

Throughout the remainder of your program, you can simply distinguish the cases symbolically:

some_predicate(a(Atom)) :- ...
some_predicate(v(Var))  :- ...

@alehander92
Copy link
Owner

That's a pretty cool trick, another thing I wondered is how prolog-y is using dicts (I used assoc lists, because I wanted to support SWI 6, so I guess dicts are a recent addition, but on the other side dict data structures are very popular in most other languages lately, including lisps: Clojure)

@triska
Copy link
Contributor Author

triska commented Jun 10, 2016

Association lists are a fine and also very portable way to do such lookups, and their performance is often completely acceptable.

Dicts may be useful if you do a let of web programming and JSON interactions, but they will chain you to a specific Prolog system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants