From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id NAA27293; Tue, 20 Nov 2001 13:25:49 +0100 (MET) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id NAA26713 for ; Tue, 20 Nov 2001 13:25:48 +0100 (MET) Received: from mail.mimuw.edu.pl (paf87.warszawa.sdi.tpnet.pl [217.96.225.87]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id fAKCPeT12385 for ; Tue, 20 Nov 2001 13:25:43 +0100 (MET) Received: (from news@localhost) by mail.mimuw.edu.pl (PLD/8.9.3) id NAA18606 for caml-list@inria.fr; Tue, 20 Nov 2001 13:26:29 +0100 X-Authentication-Warning: qrnik.zagroda: news set sender to Marcin 'Qrczak' Kowalczyk using -f From: "Marcin 'Qrczak' Kowalczyk" Subject: Re: [Caml-list] functors with style? Date: Tue, 20 Nov 2001 12:26:26 +0000 (UTC) Organization: Klub Nieszkodliwych =?iso-8859-2?Q?Manjak=F3w?= Message-ID: References: <9td4tb$csv$1@qrnik.zagroda> X-Trace: qrnik.zagroda 1006259186 17491 192.168.0.1 (20 Nov 2001 12:26:26 GMT) X-Complaints-To: abuse@localhost NNTP-Posting-Date: Tue, 20 Nov 2001 12:26:26 +0000 (UTC) User-Agent: slrn/0.9.7.2 (Linux) To: caml-list@inria.fr Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Mon, 19 Nov 2001 14:10:25 -0800, Shivkumar Chandrasekaran pisze: > For example, in Clean (which has type classes not functors of course), > instants of basic types be prevented from using dictionary passing. Not > sure what happens in haskell though... GHC tries to specialize code which uses overloaded functions during optimization. Generally dictionaries are optimized out when the type is statically known *or* the overloaded function is small enough to be inlined or explicitly marked as inline (so they will be optimized out at call site if types are known there). You can also request generation of specializations of functions or instances for particular types: {-# SPECIALISE maximum :: [Int] -> Int #-} {-# SPECIALISE minimum :: [Int] -> Int #-} maximum, minimum :: (Ord a) => [a] -> a maximum [] = errorEmptyList "maximum" maximum xs = foldl1 max xs minimum [] = errorEmptyList "minimum" minimum xs = foldl1 min xs You can even substitute arbitrary definitions used for specialized types; the compiler doesn't check if they have the same semantics. They are used when the compiler statically knows the type (perhaps after inlining at call site). For example: fromIntegral :: (Integral a, Num b) => a -> b fromIntegral = fromInteger . toInteger {-# RULES "fromIntegral/Word8->Int16" fromIntegral = \(W8# x#) -> I16# (word2Int# x#) "fromIntegral/Int8->Int16" fromIntegral = \(I8# x#) -> I16# x# "fromIntegral/Int16->Int16" fromIntegral = id :: Int16 -> Int16 "fromIntegral/a->Int16" fromIntegral = \x -> case fromIntegral x of I# x# -> I16# (narrow16Int# x#) "fromIntegral/Int16->a" fromIntegral = \(I16# x#) -> fromIntegral (I# x#) #-} (RULES are even more powerful: the lhs doesn't have to be an identifier). nhc and Hugs do less optimizations and I would guess they don't optimize out class dictionaries at all. hbc did some optimizations and had SPECIALISE pragmas too. -- __("< Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/ \__/ ^^ QRCZAK ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr