Xref: utzoo comp.lang.functional:167 comp.lang.prolog:2774 Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!comp.vuw.ac.nz!brian From: brian@comp.vuw.ac.nz (Brian Boutel) Newsgroups: comp.lang.functional,comp.lang.prolog Subject: Re: Pattern matching considered harmful Message-ID: <1990Jun07.020319.27609@comp.vuw.ac.nz> Date: 7 Jun 90 02:03:19 GMT References: <1990Jun5.175706.415@newcastle.ac.uk> <2584@skye.ed.ac.uk> <3077@goanna.cs.rmit.oz.au> <2790@syma.sussex.ac.uk> Sender: news@comp.vuw.ac.nz (News Admin) Reply-To: brian@comp.vuw.ac.nz (Brian Boutel) Organization: Dept. of Comp. Sci., Victoria Uni. of Wellington, New Zealand. Lines: 52 In article <1990Jun5.175706.415@newcastle.ac.uk>, Chris.Holt@newcastle.ac.uk (Chris Holt) writes: > In article rjc@uk.ac.ed.cstr (Richard Caley) writes: > > > >One can change the implementation of a function without changing its > >calling sequence, one can not change the structure of a data object > >without changing the pattern used to match it. > > > >It would be nice this was possible. declare a dtat type ad declare a > >`picture' of it which is what the outside world sees. > > The picture is surely just those functions that can take the data type > as an argument, and those that can return the data type as a result. Abstract data types, whose constructors are not visible, solve the problem of changing the implementation without having to change the use, but cannot be combined with pattern matching, where the constructors need to be visible. This does not make pattern matching "harmful". Pattern matching is what makes data-driven case analysis possible, and this is a good thing. Phil Wadler proposed the use of "views" to avoid this difficulty. A view is an alternative definition of a datatype, with a new set of constructors and with functions for mapping between the view and the real representation. An abstract data type defines a view whose constructors can be used for pattern matching, but the real representation is hidden and can be changed, without invalidating the user's code, if the mapping functions are also changed. The mapping functions are not explicitly used by the user, but inserted by the compiler where necessary. Simple example of views might be polar representation of complex numbers if the implementation is cartesian, or conventional (nil, cons) lists where the real representation uses a tree (this makes append very efficient). Views were considered for inclusion in Haskell, but dropped when we found some awkward consequences that we did not want to deal with without first gaining more experience with their use. Reference: P Wadler, Views, a way for pattern matching to cohabit with data abstraction. In Proc 14th ACM POPL pp307-312 Jan 1987 --brian Internet: brian@comp.vuw.ac.nz Postal: Brian Boutel, Computer Science Dept, Victoria University of Wellington, PO Box 600, Wellington, New Zealand Phone: +64 4 721000 Fax: +64 4 712070