Compile time testfor 'atoms'










0















Completely new to prolog. Interesting journey so far in trying to change how I think, so appreciate any help here.



I am trying to assert facts for a pre-defined set of names. For example, assume I have a a set of people [alice, bob, ...] in one file. I would like to assert facts about these folks in other files, but want to make sure that these folks exist and that is checked when the facts are loaded/compiled(?).



For example, assume I don't have 'chuck' in the list and I make an assertion:



user: swipl app.pl



?- full_name(chuck, "Charlie Steel").


should result in an error.



What is the best way I can do this?










share|improve this question
























  • The trick here is probably to open the file and use read/2 to obtain Prolog terms from the file, then examine them; if they pass, use assertz/1 to add them to the store, if they do not pass, then report an error. I'm going to try and gin something up but take a crack at it yourself and maybe you'll get there first.

    – Daniel Lyons
    Nov 16 '18 at 5:40















0















Completely new to prolog. Interesting journey so far in trying to change how I think, so appreciate any help here.



I am trying to assert facts for a pre-defined set of names. For example, assume I have a a set of people [alice, bob, ...] in one file. I would like to assert facts about these folks in other files, but want to make sure that these folks exist and that is checked when the facts are loaded/compiled(?).



For example, assume I don't have 'chuck' in the list and I make an assertion:



user: swipl app.pl



?- full_name(chuck, "Charlie Steel").


should result in an error.



What is the best way I can do this?










share|improve this question
























  • The trick here is probably to open the file and use read/2 to obtain Prolog terms from the file, then examine them; if they pass, use assertz/1 to add them to the store, if they do not pass, then report an error. I'm going to try and gin something up but take a crack at it yourself and maybe you'll get there first.

    – Daniel Lyons
    Nov 16 '18 at 5:40













0












0








0








Completely new to prolog. Interesting journey so far in trying to change how I think, so appreciate any help here.



I am trying to assert facts for a pre-defined set of names. For example, assume I have a a set of people [alice, bob, ...] in one file. I would like to assert facts about these folks in other files, but want to make sure that these folks exist and that is checked when the facts are loaded/compiled(?).



For example, assume I don't have 'chuck' in the list and I make an assertion:



user: swipl app.pl



?- full_name(chuck, "Charlie Steel").


should result in an error.



What is the best way I can do this?










share|improve this question
















Completely new to prolog. Interesting journey so far in trying to change how I think, so appreciate any help here.



I am trying to assert facts for a pre-defined set of names. For example, assume I have a a set of people [alice, bob, ...] in one file. I would like to assert facts about these folks in other files, but want to make sure that these folks exist and that is checked when the facts are loaded/compiled(?).



For example, assume I don't have 'chuck' in the list and I make an assertion:



user: swipl app.pl



?- full_name(chuck, "Charlie Steel").


should result in an error.



What is the best way I can do this?







prolog swi-prolog






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 14:35









lurker

45.1k74575




45.1k74575










asked Nov 16 '18 at 4:31









stan_plogicstan_plogic

225




225












  • The trick here is probably to open the file and use read/2 to obtain Prolog terms from the file, then examine them; if they pass, use assertz/1 to add them to the store, if they do not pass, then report an error. I'm going to try and gin something up but take a crack at it yourself and maybe you'll get there first.

    – Daniel Lyons
    Nov 16 '18 at 5:40

















  • The trick here is probably to open the file and use read/2 to obtain Prolog terms from the file, then examine them; if they pass, use assertz/1 to add them to the store, if they do not pass, then report an error. I'm going to try and gin something up but take a crack at it yourself and maybe you'll get there first.

    – Daniel Lyons
    Nov 16 '18 at 5:40
















The trick here is probably to open the file and use read/2 to obtain Prolog terms from the file, then examine them; if they pass, use assertz/1 to add them to the store, if they do not pass, then report an error. I'm going to try and gin something up but take a crack at it yourself and maybe you'll get there first.

– Daniel Lyons
Nov 16 '18 at 5:40





The trick here is probably to open the file and use read/2 to obtain Prolog terms from the file, then examine them; if they pass, use assertz/1 to add them to the store, if they do not pass, then report an error. I'm going to try and gin something up but take a crack at it yourself and maybe you'll get there first.

– Daniel Lyons
Nov 16 '18 at 5:40












1 Answer
1






active

oldest

votes


















1














So, here's the code I came up with:



person(deborah).
person(tony).

read_my_file(Filename) :-
open(Filename, read, In),
read_my_file1(In),
close(In).

read_my_file1(In) :-
read(In, Term),
( Term == end_of_file
-> true
; assert_or_abort(Term),
read_my_file1(In)
).

assert_or_abort(Term) :-
( full_name(Person, Name) = Term
-> ( person(Person)
-> assertz(full_name(Person, Name))
; format(user, '~w is not a person I recognize~n', [Person])
)
; format(user, '~w is not a term I know how to parse~n', [Term])
).


The trick here is using read/2 to obtain a Prolog term from the stream, and then doing some deterministic tests of it, hence the nested conditional structure inside assert_or_abort/1. Supposing you have an input file that looks like this:



full_name(deborah, 'Deborah Ismyname').
full_name(chuck, 'Charlie Steel').
full_name(this, has, too, many, arguments).
squant.


You get this output:



?- read_my_file('foo.txt').
chuck is not a person I recognize
full_name(this,has,too,many,arguments) is not a term I know how to parse
squant is not a term I know how to parse
true.

?- full_name(X,Y).
X = deborah,
Y = 'Deborah Ismyname'.





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331484%2fcompile-time-testfor-atoms%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    So, here's the code I came up with:



    person(deborah).
    person(tony).

    read_my_file(Filename) :-
    open(Filename, read, In),
    read_my_file1(In),
    close(In).

    read_my_file1(In) :-
    read(In, Term),
    ( Term == end_of_file
    -> true
    ; assert_or_abort(Term),
    read_my_file1(In)
    ).

    assert_or_abort(Term) :-
    ( full_name(Person, Name) = Term
    -> ( person(Person)
    -> assertz(full_name(Person, Name))
    ; format(user, '~w is not a person I recognize~n', [Person])
    )
    ; format(user, '~w is not a term I know how to parse~n', [Term])
    ).


    The trick here is using read/2 to obtain a Prolog term from the stream, and then doing some deterministic tests of it, hence the nested conditional structure inside assert_or_abort/1. Supposing you have an input file that looks like this:



    full_name(deborah, 'Deborah Ismyname').
    full_name(chuck, 'Charlie Steel').
    full_name(this, has, too, many, arguments).
    squant.


    You get this output:



    ?- read_my_file('foo.txt').
    chuck is not a person I recognize
    full_name(this,has,too,many,arguments) is not a term I know how to parse
    squant is not a term I know how to parse
    true.

    ?- full_name(X,Y).
    X = deborah,
    Y = 'Deborah Ismyname'.





    share|improve this answer



























      1














      So, here's the code I came up with:



      person(deborah).
      person(tony).

      read_my_file(Filename) :-
      open(Filename, read, In),
      read_my_file1(In),
      close(In).

      read_my_file1(In) :-
      read(In, Term),
      ( Term == end_of_file
      -> true
      ; assert_or_abort(Term),
      read_my_file1(In)
      ).

      assert_or_abort(Term) :-
      ( full_name(Person, Name) = Term
      -> ( person(Person)
      -> assertz(full_name(Person, Name))
      ; format(user, '~w is not a person I recognize~n', [Person])
      )
      ; format(user, '~w is not a term I know how to parse~n', [Term])
      ).


      The trick here is using read/2 to obtain a Prolog term from the stream, and then doing some deterministic tests of it, hence the nested conditional structure inside assert_or_abort/1. Supposing you have an input file that looks like this:



      full_name(deborah, 'Deborah Ismyname').
      full_name(chuck, 'Charlie Steel').
      full_name(this, has, too, many, arguments).
      squant.


      You get this output:



      ?- read_my_file('foo.txt').
      chuck is not a person I recognize
      full_name(this,has,too,many,arguments) is not a term I know how to parse
      squant is not a term I know how to parse
      true.

      ?- full_name(X,Y).
      X = deborah,
      Y = 'Deborah Ismyname'.





      share|improve this answer

























        1












        1








        1







        So, here's the code I came up with:



        person(deborah).
        person(tony).

        read_my_file(Filename) :-
        open(Filename, read, In),
        read_my_file1(In),
        close(In).

        read_my_file1(In) :-
        read(In, Term),
        ( Term == end_of_file
        -> true
        ; assert_or_abort(Term),
        read_my_file1(In)
        ).

        assert_or_abort(Term) :-
        ( full_name(Person, Name) = Term
        -> ( person(Person)
        -> assertz(full_name(Person, Name))
        ; format(user, '~w is not a person I recognize~n', [Person])
        )
        ; format(user, '~w is not a term I know how to parse~n', [Term])
        ).


        The trick here is using read/2 to obtain a Prolog term from the stream, and then doing some deterministic tests of it, hence the nested conditional structure inside assert_or_abort/1. Supposing you have an input file that looks like this:



        full_name(deborah, 'Deborah Ismyname').
        full_name(chuck, 'Charlie Steel').
        full_name(this, has, too, many, arguments).
        squant.


        You get this output:



        ?- read_my_file('foo.txt').
        chuck is not a person I recognize
        full_name(this,has,too,many,arguments) is not a term I know how to parse
        squant is not a term I know how to parse
        true.

        ?- full_name(X,Y).
        X = deborah,
        Y = 'Deborah Ismyname'.





        share|improve this answer













        So, here's the code I came up with:



        person(deborah).
        person(tony).

        read_my_file(Filename) :-
        open(Filename, read, In),
        read_my_file1(In),
        close(In).

        read_my_file1(In) :-
        read(In, Term),
        ( Term == end_of_file
        -> true
        ; assert_or_abort(Term),
        read_my_file1(In)
        ).

        assert_or_abort(Term) :-
        ( full_name(Person, Name) = Term
        -> ( person(Person)
        -> assertz(full_name(Person, Name))
        ; format(user, '~w is not a person I recognize~n', [Person])
        )
        ; format(user, '~w is not a term I know how to parse~n', [Term])
        ).


        The trick here is using read/2 to obtain a Prolog term from the stream, and then doing some deterministic tests of it, hence the nested conditional structure inside assert_or_abort/1. Supposing you have an input file that looks like this:



        full_name(deborah, 'Deborah Ismyname').
        full_name(chuck, 'Charlie Steel').
        full_name(this, has, too, many, arguments).
        squant.


        You get this output:



        ?- read_my_file('foo.txt').
        chuck is not a person I recognize
        full_name(this,has,too,many,arguments) is not a term I know how to parse
        squant is not a term I know how to parse
        true.

        ?- full_name(X,Y).
        X = deborah,
        Y = 'Deborah Ismyname'.






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 6:09









        Daniel LyonsDaniel Lyons

        18k23964




        18k23964





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331484%2fcompile-time-testfor-atoms%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            ReactJS Fetched API data displays live - need Data displayed static