DCGs as algebraic types
Stassa Patsantzis <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <[email protected]> |
Hi list,
When I look at a grammar rule like the following:
list --> [].
list --> [H|T], { list(T,[]) }.
- I can't help but think that it looks a lot like an algebraic data type. Say
for instance, the following example from wikipedia:
data List a = Nil | Cons a (List a)
-which is almost identical to the above, except for the fact that 'a' is a typed
variable. We could however write something like this:
int(A) --> [A], { integer(A) }.
- or even define our own types, such as:
type --> [t] | [y] | [p] | [e].
So that the following:
list --> [].
list --> [H|T], { type([H], []), list(T, []) }.
- would only be true if the input H was one of t,y,p or e, or a list of such,
therefore conforming to what looks a lot like a type constraint, to me at least.
So does that all sound too naive? Or did I just reinvent someone's wheel? Is
there any good reason why not to use DCG's to do type checking like above?
- Stassa.
-------------- next part --------------
HTML attachment scrubbed and removed