[mercury-users] polymorphism
Tomas By
T.By at dcs.shef.ac.uk
Thu Sep 25 13:55:50 AEST 1997
Hi all,
Thanks for the replies Tyson, Fergus. I'm not really sure
you answered my question, because I'm not sure what I'm
trying to ask. :-)
So I'll explain the problem. Any feedback appreciated. At
this point I'm interested in elegant design rather than
efficiency.
I'm writing an XML library. The interface consists of a
document type and procedures to read/write/validate etc.
The problem with XML documents is that apps might want to
treat them either as a stream of elements or as a tree
structure, and that you sometimes want to preserve
whitespace and sometimes not.
So you need four different types:
stream with whitespace [ stream(byte) ]
stream without whitespace [ stream(word) ]
tree with whitespace [tree(byte) ]
tree without whitespace [ tree(word) ]
My solution is to have 'list(item(V,T))' as the document
type, declared as follows.
--------------------------------------------------------
:- type item(V,T) ---> v(V) ; token(T) ; spec(spec) .
:- type s(_) ---> start_tag(string,list(attribute))
; end_tag(string)
.
:- type t(T) ---> element(string,list(attribute),tree(T))
.
:- type bs_item == item(s(byte),byte).
:- type stream(T) == list(item(s(T),T)).
:- type tree(T) == list(item(t(T),T)).
:- type byte == char.
:- type word ---> word(string)
; number(string)
; sym(char)
.
:- type attribute ---> string - string.
:- type spec ---> ref(ref)
; empty_tag(string,list(attribute))
; comment(string)
; procinstr(string,string)
; cdata(string)
; doctype(string)
.
:- type ref ---> amp
; lt
; gt
; apos
; quot
; char(int,int) % Base, Number
; def(string)
.
--------------------------------------------------------
"Fergus Henderson" <fjh at cs.mu.oz.au> wrote:
> Why don't you want p/1 to handle things of other types?
I want a procedure 'dump' for example that can handle all the
four types above. The reason I don't want it to handle other
things is I don't want to write the code. :-)
In general, if I have a procedure with several modes, eg:
:- pred stream_tree(stream(T),tree(T)).
:- mode stream_tree(in,out) is det.
:- mode stream_tree(out,in) is det.
but I can't use the same code for both modes, how do I write
the "top-level"?
/Tomas
More information about the users
mailing list