-
Notifications
You must be signed in to change notification settings - Fork 157
Reif cookbook #3100
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
Open
dougransom
wants to merge
25
commits into
mthom:master
Choose a base branch
from
dougransom:reif_cookbook
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+521
−1
Open
Reif cookbook #3100
Changes from 2 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
6b13523
added examples.
a298614
move file
a1897a5
some edits
0dedc6f
Merge branch 'mthom:master' into reif_cookbook
dougransom 7195310
updated based on pr feedback
ec7eb43
Merge branch 'reif_cookbook' of https://github.com/dougransom/scryer-…
9939763
Merge branch 'mthom:master' into reif_cookbook
dougransom a479373
incorporation some feedback.
6b550ba
Merge branch 'reif_cookbook' of https://github.com/dougransom/scryer-…
2bf4f5e
incorporating feedback
0d5dd00
edits
f5db942
edits
c23bbf0
edits
42bc692
edits
2e1c180
edits
f127d7a
removed ->
d3b9aa6
added motivation
6a73e11
typo
f3556f7
Add triska's fizzbuzz
ed6d281
Apply suggestions from code review
dougransom d6027f3
feedback from code review
25ad120
Merge branch 'reif_cookbook' of https://github.com/dougransom/scryer-…
f737028
tweaked some language, added a missing module load in fizzbuss.
01c27aa
called partial goals partial goals.
ecbc0b8
Tweak of examples around partial goals and #> #<
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,42 +6,67 @@ Load lambda, dif, clpz, and reif into your toplevel to evaluate the examples in | |
| If you have an improvement (such as a better example or an explanation) to this tutorial, please [submit a PR](https://github.com/mthom/scryer-prolog/pulls) or | ||
| [github issue](https://github.com/mthom/scryer-prolog/issues). | ||
|
|
||
| ## Introduction and meta_predicate Review | ||
| ## Introduction | ||
|
|
||
| Review the summary at 25:45 [Power of Prolog meta_predicate declarations](https://www.youtube.com/watch?v=m3cbgebcKng). You will need some understanding of meta_predicate declarations. | ||
| reif predicates will have as an argument a predicate requiring at least one additional argument. That predicate's last argument will be the reified value of the predicate. | ||
|
|
||
| Typically, reif predicates provided as arguments typically require the last additional argument will be a reified binary value. | ||
| For example, the argument to tfilter is a predicate taking two additional arguments. Inspecting the source, you see the parameters for the first argument | ||
| is named C_2; the naming of parameters ending in _N is used to convey the paremeter will be a predicate taking N additional arguments (in reif, the last of which is expected | ||
| to be a reifed boolean value). | ||
|
|
||
| In this case, the first argument of tfilter is required to be a predicate taking two additional arguments. | ||
|
|
||
| ``` | ||
| :- meta_predicate(tfilter(2, ?, ?)). | ||
|
|
||
| tfilter(_, [], []). | ||
| tfilter(C_2, [E|Es], Fs0) :- | ||
| … | ||
| ``` | ||
|
|
||
| C_2 can be a partial goal requring two more arguments, or a 2-ary predicate. In either case the last argument is the reified | ||
| truth value. | ||
| For example, using dif/3 from reif, and applying one argument 3, gives a partially applied goal X_2 requiring two more arguments. | ||
|
|
||
| ``` | ||
| ?- X_2=dif(3), call(X_2,9,T). | ||
| X_2 = dif(3), T = true. | ||
| ?- | ||
| ``` | ||
| or more succinctly: | ||
|
|
||
| You will probably need to inspect reif.pl to fully understand how to use the module, and to learn | ||
| some prolog skills you may not already have. | ||
| ``` | ||
| ?- call(dif(3),9,T). | ||
| T = true. | ||
| ?- | ||
| ``` | ||
|
|
||
| You will probably need to inspect reif.pl to fully understand how to use the module . By studying the implementation of predicates, you may also learn | ||
| some new prolog skills | ||
|
|
||
| There is some code that you may find confusing, which shows a predicate expecting 2 arguments, but only one is provided: | ||
|
|
||
| ``` | ||
| if_(call(C_2, E), Fs0 = [E|Fs], Fs0 = Fs), | ||
| ``` | ||
| Fortunatley, you don't need to understand that mechanism to use the reif module. Below there examples of how to use if_. | ||
| Fortunatley, you don't need to understand that mechanism to use the reif module. The C_2 parameter name gives you enough information about the predicate | ||
dougransom marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| you must supply as an argument. | ||
|
|
||
| You may be surprised when something like this works: | ||
|
|
||
| Also, don't be confused when something like this works: | ||
| ``` | ||
| ?- call( tfilter,=(3),[3],X). | ||
|
||
| X = [3]. | ||
| ?- | ||
| ``` | ||
|
|
||
| but this breaks. | ||
|
|
||
| ``` | ||
| ?- call( tfilter,#>(3),[3],X). | ||
|
||
| error(existence_error(procedure,(#>)/3),(#>)/3). | ||
| ?- | ||
| ``` | ||
|
|
||
| What is happening is that reif has provided =/3, and that is what is being used in tfilter (not the usual =/2). | ||
dougransom marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| But (in my system at the time of writing) #>/2 exists, but there is no #>/3. | ||
|
|
@@ -69,7 +94,6 @@ so that P1(A1, A2, ...AN) holds when P2(A1, A2, ..., AN, T) holds and T is the r | |
| [Probably not](https://github.com/mthom/scryer-prolog/discussions/2557), but you can probably learn something about writing reified versions of predicates by reading that discussion. | ||
|
|
||
| You can look at predicates like dif/3 or =/3 in [reif](/reif.html) to get ideas about writing predicates that include a reifed boolean value. | ||
| ``` | ||
|
|
||
| ## memberd_t/3 | ||
|
|
||
|
|
@@ -134,25 +158,12 @@ reif introdces =/3, which holds if and only if the first two arguments are equal | |
|
|
||
| You may find it useful to write predicates where the last argument is a boolean variable containing the result of whether some relation holds, for use with other reif predicates. | ||
|
|
||
|
|
||
|
|
||
| ## if_/3 | ||
|
|
||
| if_1 calls predicate If_1, adding on the last argument the predicate expects, which is a boolean value, T. | ||
| if T then calls the predicate Then_0 | ||
| otherwise, calls the predicate Else 0. | ||
|
|
||
| It is worth looking at the source. | ||
| ``` | ||
| if_(If_1, Then_0, Else_0) :- | ||
| call(If_1, T), | ||
| ( T == true -> call(Then_0) | ||
| ; T == false -> call(Else_0) | ||
| ; nonvar(T) -> throw(error(type_error(boolean, T), _)) | ||
| ; throw(error(instantiation_error, _)) | ||
| ). | ||
| ``` | ||
|
|
||
| ``` | ||
| ?- if_( (X=3),(Y=4),(Z=5)), (X=4;X=3). | ||
| X = 3, Y = 4 | ||
|
|
@@ -166,8 +177,8 @@ In the above example, only Y or Z are instantiated variables, but not both. | |
| Example with a differrent comparison predicate. | ||
|
|
||
| ``` | ||
| greater_than_two(Y,T) :- Y #> 2 ,T=true. | ||
| greater_than_two(Y,T) :- Y #< 2 ,T=false. | ||
| greater_than_two(Y,true) :- Y #> 2. | ||
| greater_than_two(Y,false) :- Y #=< 2. | ||
| ?- if_(greater_than_two(3),X="works", X="fails"). | ||
| X = "works". | ||
| ?- | ||
|
|
@@ -312,6 +323,8 @@ Find Z, for which #<(Z) partions [1,2,4,5] into [4,5], and [1,2]: | |
|
|
||
| [if_/3](https://www.metalevel.at/prolog/metapredicates) "correctly commits to one of two alternatives when admissable …". | ||
|
|
||
|
|
||
|
|
||
| The first argument is a predicate requiring one more argument, which is the reified truth value of a comparsion . | ||
|
|
||
| The prolog system provides some help here. The prolog system uses =/3 in this example, because =/3 wouldn't match. | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.