[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: question on assert and retract
From: |
David Bennett |
Subject: |
Re: question on assert and retract |
Date: |
Tue, 27 Aug 2002 13:56:09 -0600 |
RE: Checking for ground.
Yesterday, I needed a portable check for grounded terms. So I created this
code that you're welcome to have, tweak or change as needed.
call the predicate is_ground/1 with your term to be checked; e.g.
is_ground( a(c,d,[x,G,y]))
-David
%
% is_ground(Term): checks to see if Term contains uninstantiated variables.
Fails if it does, succeeds if not.
% By: David Bennett
is_ground(Var):- var(Var), !, fail. %YEP, Found an uninstantiated variable,
so we aren't ground, fail.
is_ground(Atom):- atom(Atom), !. %Single Atoms are ground
is_ground(Number):- number(Number), !. %Unique numbers are ground
is_ground(Term):-
Term =.. List, %A complex term (that is not a list) can be converted to a
list
is_ground_list(List), %and check the list for unground variables.
!.
is_ground(List):-
!,
is_ground_list(List).
is_ground_list([]):-!. %End of list, we are ground!
is_ground_list([H|T]):-
is_ground(H), %recursive
!,
is_ground_list(T).
is_ground_list(_):-!, fail. %Item in list is not ground, so stop here.
----- Original Message -----
From: <address@hidden>
To: <address@hidden>
Cc: "bruno patin" <address@hidden>
Sent: Tuesday, August 27, 2002 1:24 PM
Subject: Re: question on assert and retract
>
> On Tuesday, August 27, 2002, at 02:14 PM, address@hidden wrote:
>
> >
> > On Tuesday, August 27, 2002, at 01:31 PM, bruno patin wrote:
> >
> >> But now, as listing is not an iso-prolog directive how not to add the
> >> same facts two times ?
> >
> > Perhaps my_assertz:
> >
> > my_assertz(Term) :-
> > (var(Term)
> > ;
> > copy_term(Term, TermCopy),
> > my_clause(TermCopy) )
> > -> true
> > ;
> > assertz(Term).
> >
> > my_clause( Term ) :-
> > Term =.. [ (:-), Head, Body]
> > -> clause(Head, Body)
> > ;
> > clause(Term, true).
> >
> > This only assertz's Term if Term is ground (i.e. contains no unbound
> > variables) and is not unifiable with a clause already in the clause
> > database.
>
> Oops. I changed the code but not the comment--I changed it to use var/1
> instead of checking for groundness, since ground/1 isn't ISO and I'm too
> lazy to go find an implementation of ground/1 right now.
> -lindsey
>
>
>
> _______________________________________________
> Users-prolog mailing list
> address@hidden
> http://mail.gnu.org/mailman/listinfo/users-prolog
>
- question on assert and retract, bruno patin, 2002/08/26
- Re: question on assert and retract, Vic Bancroft, 2002/08/26
- Re: question on assert and retract, Timo Karjalainen, 2002/08/27
- Re: question on assert and retract, Vic Bancroft, 2002/08/27
- Re: question on assert and retract, bruno patin, 2002/08/27
- Re: question on assert and retract, spratt, 2002/08/27
- Re: question on assert and retract, spratt, 2002/08/27
- Re: question on assert and retract,
David Bennett <=
- Re: question on assert and retract--ground testing., spratt, 2002/08/27
- Re: question on assert and retract--ground testing., David Bennett, 2002/08/27
- Re: question on assert and retract, Vic Bancroft, 2002/08/27