Gofer "atom" function 
Author Message
 Gofer "atom" function

I am in the process of learning gofer/Haskell and am having *great*
difficulty with the following (I thought simple) function:

I am trying to code a restricted form of Common Lisp's atom where:

atom [] = True
atom "string" = True
atom ["string","string", ...] = False

basically strings are atoms but lists of strings are not. A nil list is
treated as true.

All help/ideas to get me in the right direction are appreciated

Thank you

Lee Leitner



Tue, 12 Aug 1997 23:22:22 GMT  
 Gofer "atom" function

Quote:
>I am in the process of learning gofer/Haskell and am having *great*
>difficulty with the following (I thought simple) function:

>I am trying to code a restricted form of Common Lisp's atom where:

>atom [] = True
>atom "string" = True
>atom ["string","string", ...] = False

>basically strings are atoms but lists of strings are not. A nil list is
>treated as true.

Basically the problem is that gofer/Haskell is strongly typed, and
Common Lisp is not.  Type-test functions such as "atom" don't make much
sense in a strongly-typed system.

I think the appropriate question to ask at this point is "what real-world
problem are you trying to solve?"

--

all [L] (programming_language(L), L \= "Mercury") => better("Mercury", L) ;-)



Thu, 14 Aug 1997 01:35:40 GMT  
 Gofer "atom" function

Quote:
>>I am in the process of learning gofer/Haskell and am having *great*
>>difficulty with the following (I thought simple) function:

>>I am trying to code a restricted form of Common Lisp's atom where:

>>atom [] = True
>>atom "string" = True
>>atom ["string","string", ...] = False

>>basically strings are atoms but lists of strings are not. A nil list is
>>treated as true.

Do you really mean this

atom [] = True
atom [a] = True
atom (x:xs) = False

Not knowing what youa re really tring to solve this is the best I can
assume.  (I know Zero lisp BTW)
--
Darren J Moffat                               20 Southpark Ave

URL:   http://www.dcs.gla.ac.uk/~moffatd      G12 8HZ  Tel: 041-334-5902



Sat, 16 Aug 1997 22:28:21 GMT  
 Gofer "atom" function

|> I am trying to code a restricted form of Common Lisp's atom where:
|>
|> atom [] = True
|> atom "string" = True
|> atom ["string","string", ...] = False
|>
|> basically strings are atoms but lists of strings are not. A nil list is
|> treated as true.

You have hit on a difficult problem with Haskell's type classes there.  I
originally thought that overloading would do the trick, using something like:

  class CharTestable a where
    char :: a -> Bool

  class AtomTestable a where
    atom :: a -> Bool

  instance CharTestable a => AtomTestable [a] where
    atom []     = True
    atom (x:xs) = ischar x

  instance CharTestable Char where
    char x      = True

  instance CharTestable [a] where
    char xs     = False

Running that through Gofer, I got a type error for the case (atom []):

  atom []
  ERROR: Unresolved overloading
  *** type        : AtomTestable [a] => Bool
  *** translation : atom []

This is a problem inherent in Haskell's type class concept. Basically, the
system insists on a specification of what the element type of [] should be.
As a workaround, you could tag all empty lists with type [Char]. I.e.

  atom ([]::[Char])
  True

  atom [[]::[Char]]
  False

would do what you want.

Let me also note that there are overloading type systems that avoid Haskell's
ambiguity problems -- at the price of some restrictions such as no overloaded
constants.  The earliest one is probably by Stefan Kaes, described in his 1988
ESOP paper. I have recently worked with Phil Wadler and Martin Wehr on a
simplification and generalisation of Stefan's system. You can find a report on
this at http://wwwipd.ira.uka.de/~odersky/papers.html#Types.

A corresponding paper will appear in FPCA'95. Please note that the currently
available report is still preliminary -- we expect that there will be some
changes in the published version.

Cheers

-- Martin



Tue, 19 Aug 1997 21:19:40 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Problem: "My atoms are too big!!"

2. Miranda/Gofer "Courseware"

3. string.join(["Tk 4.2p2", "Python 1.4", "Win32", "free"], "for")

4. Function names and "filter" function

5. BEGIN{want[]={"s1o", "s2o", "s2q", "s3q"}

6. Gofer "ERROR "c:\wingofer\kat\eval.gs": Compiled code too complex"

7. Parsing ""D""?

8. "Fifth", "Forth", zai nar?

9. Ruby "finalize", "__del__"

10. beginners "let"/"random" question

11. ANNOUNCE: new "plus"- and "dash"-patches available for Tcl7.5a2/Tk4.1a2

12. Looking for "stdin", "stdout"

 

 
Powered by phpBB® Forum Software