fromInteger for Lists

There's nothing better than making a data type an instance of Num. In particular, fromInteger is a joy. But how about lists? For example, if I have data Foo a = F [a] I can create a fromInteger such as fromInteger i = F [fromInteger i] and then a 19::(Foo Int), could become F [19]. Is it possible to do something similar for lists? So could [1,2,3]::(Foo Int) become something slightly different, say, F [1,2,3] Paul

Paul,
There's nothing better than making a data type an instance of Num. In particular, fromInteger is a joy. But how about lists?
Do you mean something like Blargh below, only useful? John dorsey@elwood:~/src/scratch$ cat list.hs class Blargh f where fromList :: [a] -> f a data Foo a = Foo [a] deriving (Show) data Bar a = Bar [a] deriving (Show) instance Blargh Foo where fromList = Foo instance Blargh Bar where fromList l = Bar (reverse l) dorsey@elwood:~/src/scratch$ ghci list.hs GHCi, version 6.8.3: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. [1 of 1] Compiling Main ( list.hs, interpreted ) Ok, modules loaded: Main. *Main> fromList [1,2,3] :: Foo Int Foo [1,2,3] *Main> fromList [1,2,3] :: Bar Int Bar [3,2,1] *Main>

I wish for a language extension "OverloadedLiterals" that includes all of these:
1 => fromInteger 1
"hello" => fromString "hello"
['a', 'b', 'c'] => cons 'a' (cons 'b' (cons 'c' nil))
[ ord x | x <- ['a', 'b', 'c'], ok x ]
=>
cons 'a' (cons 'b' (cons 'c' nil)) >>= \x -> guard (ok x) >> return (ord x)
Maybe something similar for pair notation too!
GHC already has:
class Num a where ... fromInteger :: Integer -> a
class IsString where fromString :: String -> a
What is the right interface for cons/nil? John's fromList is one
suggestion. I'd suggest either:
class ListLike c where
type Element c
fromList :: [Element c] -> c
or
class Nil c where
nil :: c
class Container c e where
cons :: e -> c -> c
This would allow something like:
import qualified Data.Map as M
data KeyVal k v = k :> v
instance Ord k => ListLike (M.Map k v) where
type Element (M.Map k v) = k :> v
fromList = foldr insertElem M.empty where
insertElem (k :> v) m = M.insert k v m
which lets you have "first-class" looking maps:
M.lookup "Bob" ["Bob" :> 1, "Alice" :> 2, "John" :> 15]
-- ryan
On Fri, May 1, 2009 at 4:06 PM, John Dorsey
Paul,
There's nothing better than making a data type an instance of Num. In particular, fromInteger is a joy. But how about lists?
Do you mean something like Blargh below, only useful?
John
dorsey@elwood:~/src/scratch$ cat list.hs
class Blargh f where fromList :: [a] -> f a
data Foo a = Foo [a] deriving (Show) data Bar a = Bar [a] deriving (Show)
instance Blargh Foo where fromList = Foo
instance Blargh Bar where fromList l = Bar (reverse l)
dorsey@elwood:~/src/scratch$ ghci list.hs GHCi, version 6.8.3: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. [1 of 1] Compiling Main ( list.hs, interpreted ) Ok, modules loaded: Main. *Main> fromList [1,2,3] :: Foo Int Foo [1,2,3] *Main> fromList [1,2,3] :: Bar Int Bar [3,2,1] *Main>
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

2009/5/1 Paul Keir
There's nothing better than making a data type an instance of Num. In particular, fromInteger is a joy. But how about lists?
For example, if I have
data Foo a = F [a]
I can create a fromInteger such as fromInteger i = F [fromInteger i]
and then a 19::(Foo Int), could become F [19].
Is it possible to do something similar for lists? So could [1,2,3]::(Foo Int) become something slightly different, say,
F [1,2,3]
Paul
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
If you mean what I think you're referring to, you can't. The only reason it works for integer literals is that the compiler replaces occurrences of, say, 19 with (fromInteger 19). There's no function that's automatically applied to list literals, so ([1,2,3] :: Foo Int) isn't able to do anything useful, unfortunately. However, there's an extension in GHC, OverloadedStrings, which lets you use the method fromString of class Data.String.IsString to overload literals. (That's not what you asked, though, I know. :) )

Thanks Andy, et al. I can stop hacking for now then. I'm using a simple fromList function already which seems like a reasonable, and at least semi-standard solution (http://www.haskell.org/hoogle/?hoogle=fromList)
Paul
-----Original Message-----
From: sploink88@gmail.com on behalf of andy morris
Sent: Sat 02/05/2009 00:13
To: Paul Keir
Cc: haskell-cafe@haskell.org
Subject: Re: [Haskell-cafe] fromInteger for Lists[MESSAGE NOT SCANNED]
2009/5/1 Paul Keir
There's nothing better than making a data type an instance of Num. In particular, fromInteger is a joy. But how about lists?
For example, if I have
data Foo a = F [a]
I can create a fromInteger such as fromInteger i = F [fromInteger i]
and then a 19::(Foo Int), could become F [19].
Is it possible to do something similar for lists? So could [1,2,3]::(Foo Int) become something slightly different, say,
F [1,2,3]
Paul
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
If you mean what I think you're referring to, you can't. The only reason it works for integer literals is that the compiler replaces occurrences of, say, 19 with (fromInteger 19). There's no function that's automatically applied to list literals, so ([1,2,3] :: Foo Int) isn't able to do anything useful, unfortunately. However, there's an extension in GHC, OverloadedStrings, which lets you use the method fromString of class Data.String.IsString to overload literals. (That's not what you asked, though, I know. :) )

Paul Keir schrieb:
There's nothing better than making a data type an instance of Num. In particular, fromInteger is a joy. But how about lists?
http://www.haskell.org/haskellwiki/Num_instance_for_functions http://www.haskell.org/haskellwiki/Type_classes_are_for_reusability

Henning Thielemann
Paul Keir schrieb:
There's nothing better than making a data type an instance of Num. In particular, fromInteger is a joy. But how about lists?
http://www.haskell.org/haskellwiki/Num_instance_for_functions http://www.haskell.org/haskellwiki/Type_classes_are_for_reusability
Hear, hear! -- Jón Fairbairn Jon.Fairbairn@cl.cam.ac.uk http://www.chaos.org.uk/~jf/Stuff-I-dont-want.html (updated 2009-01-31)
participants (6)
-
andy morris
-
Henning Thielemann
-
John Dorsey
-
Jon Fairbairn
-
Paul Keir
-
Ryan Ingram