<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Phantom type problems</title>
	<atom:link href="http://therning.org/magnus/archives/322/feed" rel="self" type="application/rss+xml" />
	<link>http://therning.org/magnus/archives/322?&amp;owa_from=feed&amp;owa_sid=</link>
	<description>Incoherent mumblings</description>
	<pubDate>Mon, 08 Sep 2008 11:55:34 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
		<item>
		<title>By: Magnus</title>
		<link>http://therning.org/magnus/archives/322#comment-82446</link>
		<dc:creator>Magnus</dc:creator>
		<pubDate>Wed, 17 Oct 2007 22:58:53 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/archives/322#comment-82446</guid>
		<description>&lt;p&gt;DN, yes, XML (and HTML) escaping are special in so much as I wouldn't call them &lt;em&gt;encodings&lt;/em&gt;.  The library I've been implementing is for data encodings and for that I think we agree that &lt;code&gt;[Word8] -&#62; String&lt;/code&gt; is the type that fits best.  Escaping, where &lt;code&gt;String -&#62; String&lt;/code&gt; does fit, should arguably be in another module.  (I'm not sure what URL encoding should be classed as, I suppose it falls somewhere in between encoding and escaping.)&lt;/p&gt;

&lt;p&gt;Typewise there's no problem with layered encodings.  All I was trying to say was that I'm not sure what advantages it offers &lt;em&gt;for data encodings&lt;/em&gt; since it isn't possible to look at an encoded string and "see past" the first layer.  Even for "multiply-escaped" strings I'm not sure if it's possible to look at the string and see what type to give it in order to do the correct sequence of de-escaping.&lt;/p&gt;

&lt;p&gt;I think it might be worth taking your point into consideration when comming up with the API for the string escaping library, but for data encodings I'm not convinced that a "layered type" is worth it in practice.  Hell, I'm not even sure using a phantom type to record one level of encoding is worth it in practice. :-)&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>DN, yes, XML (and HTML) escaping are special in so much as I wouldn&#8217;t call them <em>encodings</em>.  The library I&#8217;ve been implementing is for data encodings and for that I think we agree that <code>[Word8] -&gt; String</code> is the type that fits best.  Escaping, where <code>String -&gt; String</code> does fit, should arguably be in another module.  (I&#8217;m not sure what URL encoding should be classed as, I suppose it falls somewhere in between encoding and escaping.)</p>
<p>Typewise there&#8217;s no problem with layered encodings.  All I was trying to say was that I&#8217;m not sure what advantages it offers <em>for data encodings</em> since it isn&#8217;t possible to look at an encoded string and &#8220;see past&#8221; the first layer.  Even for &#8220;multiply-escaped&#8221; strings I&#8217;m not sure if it&#8217;s possible to look at the string and see what type to give it in order to do the correct sequence of de-escaping.</p>
<p>I think it might be worth taking your point into consideration when comming up with the API for the string escaping library, but for data encodings I&#8217;m not convinced that a &#8220;layered type&#8221; is worth it in practice.  Hell, I&#8217;m not even sure using a phantom type to record one level of encoding is worth it in practice. <img src='http://therning.org/magnus/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DN</title>
		<link>http://therning.org/magnus/archives/322#comment-82437</link>
		<dc:creator>DN</dc:creator>
		<pubDate>Wed, 17 Oct 2007 22:09:23 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/archives/322#comment-82437</guid>
		<description>&lt;p&gt;We're talking two different kinds of encoding here: something like UTF-8 is typed [Word8] -&#62; String but XML escaping is typed String -&#62; String.  So you've got to decide what you intend there.&lt;/p&gt;

&lt;p&gt;But once past that, there's no problem with nested encodings.  Something UUEncoded then XMLEscaped would be of type Encoded XMLEsc (Encoded UUEnc String).  You'll have to tweak your definition of encode/decode to wrap or unwrap just one layer of encoding.  It might be useful to define a NullEncoding that does nothing to get you from raw data to encoded data; then all your functions can work on encoded data.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>We&#8217;re talking two different kinds of encoding here: something like UTF-8 is typed [Word8] -&gt; String but XML escaping is typed String -&gt; String.  So you&#8217;ve got to decide what you intend there.</p>
<p>But once past that, there&#8217;s no problem with nested encodings.  Something UUEncoded then XMLEscaped would be of type Encoded XMLEsc (Encoded UUEnc String).  You&#8217;ll have to tweak your definition of encode/decode to wrap or unwrap just one layer of encoding.  It might be useful to define a NullEncoding that does nothing to get you from raw data to encoded data; then all your functions can work on encoded data.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Magnus</title>
		<link>http://therning.org/magnus/archives/322#comment-82383</link>
		<dc:creator>Magnus</dc:creator>
		<pubDate>Wed, 17 Oct 2007 09:34:02 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/archives/322#comment-82383</guid>
		<description>&lt;p&gt;Adam, that is a good observation.  I'm not sure whether it's an argument against using phantom types, but it certainly is a limitation worth noting.  AFAICS it's impossible in the general case to determine layered encodings so I'm afraid every API requires several iterations of detecting what encoding was used and then decoding.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Adam, that is a good observation.  I&#8217;m not sure whether it&#8217;s an argument against using phantom types, but it certainly is a limitation worth noting.  AFAICS it&#8217;s impossible in the general case to determine layered encodings so I&#8217;m afraid every API requires several iterations of detecting what encoding was used and then decoding.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adam Turoff</title>
		<link>http://therning.org/magnus/archives/322#comment-82372</link>
		<dc:creator>Adam Turoff</dc:creator>
		<pubDate>Tue, 16 Oct 2007 14:44:37 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/archives/322#comment-82372</guid>
		<description>&lt;p&gt;This is very interesting, but the module(s) seem to presume that a string will be encoded once.  How would you represent a string that's both UUEncoded and XML encoded (&#38; =&#62; &#38;amp;, etc.)?  RSS/Atom items also periodically deal with content that's doubly XML encoded as well, which is a nasty and bothersome problem.&lt;/p&gt;

&lt;p&gt;It looks like the approach here would be to incarcerate a string into one encoding, decode, and then incarcerate it into a different encoding....&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>This is very interesting, but the module(s) seem to presume that a string will be encoded once.  How would you represent a string that&#8217;s both UUEncoded and XML encoded (&amp; =&gt; &amp;amp;, etc.)?  RSS/Atom items also periodically deal with content that&#8217;s doubly XML encoded as well, which is a nasty and bothersome problem.</p>
<p>It looks like the approach here would be to incarcerate a string into one encoding, decode, and then incarcerate it into a different encoding&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Magnus</title>
		<link>http://therning.org/magnus/archives/322#comment-82364</link>
		<dc:creator>Magnus</dc:creator>
		<pubDate>Tue, 16 Oct 2007 09:51:46 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/archives/322#comment-82364</guid>
		<description>&lt;p&gt;DN, thanks.  I'll have to take a closer look at it.  From my cursory look it seems to be exactly what I was aiming for.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>DN, thanks.  I&#8217;ll have to take a closer look at it.  From my cursory look it seems to be exactly what I was aiming for.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DN</title>
		<link>http://therning.org/magnus/archives/322#comment-82362</link>
		<dc:creator>DN</dc:creator>
		<pubDate>Tue, 16 Oct 2007 08:28:16 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/archives/322#comment-82362</guid>
		<description>&lt;p&gt;Try this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module Main where

import Data.Word

{-
'encoding' is a phantom type. It's not expressed in the structure of
the data (so no overhead), but the compiler knows about it and can use
it to typecheck.  'a' is a real type.  'Encoded encoding a' means an
'a', but wrapped in 'Encoded' so we know it's encoded, not raw data.
The compiler knows what kind of encoding you're using (the phantom
type), but once the code is compiled and known to work correctly, that
information is discarded and vanishes.
-}
data Encoded encoding a = Encoded a

{-
Here we define what it means to be a DataEncoding.
-}
class DataEncoding encoding where
    encode :: [Word8] -&#62; Encoded encoding String
    decode :: Encoded encoding String -&#62; [Word8]
    chop :: Int -&#62; Encoded encoding String -&#62; Encoded encoding [String]
    unchop :: Encoded encoding [String] -&#62; Encoded encoding String
    liberate :: Encoded encoding a -&#62; a
    incarcerate :: a -&#62; Encoded encoding a

{-
Here we define one of the encodings.  The encoding doesn't have to
carry any information; it's just a tag to tell the compiler which
implementation of encode/decode/chop/etc. to use.  In fact, since it's
a phantom type which gets thrown out after compilation, there wouldn't
be any point to having it store data.
-}

data B16 = B16

{-
Here we define how we want to implement the functions for B16.
-}
instance DataEncoding B16 where
    encode ws = Encoded (b16Encode ws)
    decode (Encoded str) = b16Decode str
    chop n (Encoded str) = Encoded (b16Chop n str)
    unchop (Encoded strs) = Encoded (b16Upchop strs)
    liberate (Encoded whatever) = whatever
    incarcerate whatever = Encoded whatever

{- 
Here are a bunch of dummy implementations of your functions.  They're
here just to provide the right type signatures.
-}

b16Encode :: [Word8] -&#62; String
b16Encode = error "b16Encode"

b16Decode :: String -&#62; [Word8]
b16Decode = error "b16Decode"

b16Chop :: Int -&#62; String -&#62; [String]
b16Chop = error "b16Chop"

b16Unchop :: [String] -&#62; String
b16Unchop = error "b16Unchop"
&lt;/code&gt;&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<p>Try this:</p>
<pre><code>module Main where

import Data.Word

{-
'encoding' is a phantom type. It's not expressed in the structure of
the data (so no overhead), but the compiler knows about it and can use
it to typecheck.  'a' is a real type.  'Encoded encoding a' means an
'a', but wrapped in 'Encoded' so we know it's encoded, not raw data.
The compiler knows what kind of encoding you're using (the phantom
type), but once the code is compiled and known to work correctly, that
information is discarded and vanishes.
-}
data Encoded encoding a = Encoded a

{-
Here we define what it means to be a DataEncoding.
-}
class DataEncoding encoding where
    encode :: [Word8] -&gt; Encoded encoding String
    decode :: Encoded encoding String -&gt; [Word8]
    chop :: Int -&gt; Encoded encoding String -&gt; Encoded encoding [String]
    unchop :: Encoded encoding [String] -&gt; Encoded encoding String
    liberate :: Encoded encoding a -&gt; a
    incarcerate :: a -&gt; Encoded encoding a

{-
Here we define one of the encodings.  The encoding doesn't have to
carry any information; it's just a tag to tell the compiler which
implementation of encode/decode/chop/etc. to use.  In fact, since it's
a phantom type which gets thrown out after compilation, there wouldn't
be any point to having it store data.
-}

data B16 = B16

{-
Here we define how we want to implement the functions for B16.
-}
instance DataEncoding B16 where
    encode ws = Encoded (b16Encode ws)
    decode (Encoded str) = b16Decode str
    chop n (Encoded str) = Encoded (b16Chop n str)
    unchop (Encoded strs) = Encoded (b16Upchop strs)
    liberate (Encoded whatever) = whatever
    incarcerate whatever = Encoded whatever

{-
Here are a bunch of dummy implementations of your functions.  They're
here just to provide the right type signatures.
-}

b16Encode :: [Word8] -&gt; String
b16Encode = error "b16Encode"

b16Decode :: String -&gt; [Word8]
b16Decode = error "b16Decode"

b16Chop :: Int -&gt; String -&gt; [String]
b16Chop = error "b16Chop"

b16Unchop :: [String] -&gt; String
b16Unchop = error "b16Unchop"
</code></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Magnus</title>
		<link>http://therning.org/magnus/archives/322#comment-82338</link>
		<dc:creator>Magnus</dc:creator>
		<pubDate>Mon, 15 Oct 2007 10:48:29 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/archives/322#comment-82338</guid>
		<description>&lt;p&gt;Antoine, exactly!  That's what's puzzling me as well.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Antoine, exactly!  That&#8217;s what&#8217;s puzzling me as well.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Antoine</title>
		<link>http://therning.org/magnus/archives/322#comment-82317</link>
		<dc:creator>Antoine</dc:creator>
		<pubDate>Mon, 15 Oct 2007 01:57:55 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/archives/322#comment-82317</guid>
		<description>&lt;p&gt;I'm not really clear on why liberate and incarcerate can't be polymorphic for all types a, seeing as their operation doesn't depend on the type it's operating on.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>I&#8217;m not really clear on why liberate and incarcerate can&#8217;t be polymorphic for all types a, seeing as their operation doesn&#8217;t depend on the type it&#8217;s operating on.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
