caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Marcin 'Qrczak' Kowalczyk" <qrczak@knm.org.pl>
To: caml-list@inria.fr
Subject: Re: [Caml-list] functors with style?
Date: Tue, 20 Nov 2001 12:26:26 +0000 (UTC)	[thread overview]
Message-ID: <slrn9vkivi.h2i.qrczak@qrnik.zagroda> (raw)
In-Reply-To: <9td4tb$csv$1@qrnik.zagroda>

Mon, 19 Nov 2001 14:10:25 -0800, Shivkumar Chandrasekaran <shiv@ece.ucsb.edu> 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


       reply	other threads:[~2001-11-20 12:25 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <9td4tb$csv$1@qrnik.zagroda>
2001-11-20 12:26 ` Marcin 'Qrczak' Kowalczyk [this message]
2001-11-19 18:32 Krishnaswami, Neel
2001-11-19 22:10 ` Shivkumar Chandrasekaran
2001-11-20 16:48   ` Brian Rogoff
  -- strict thread matches above, loose matches on Subject: below --
2001-11-19 16:32 William Harold Newman
2001-11-19 19:31 ` Francisco Valverde Albacete

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=slrn9vkivi.h2i.qrczak@qrnik.zagroda \
    --to=qrczak@knm.org.pl \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).