Wednesday, August 13, 2008

Using Difference Lists

I posted my enthusiasm about difference lists for Haskell earlier this month, but then became sidetracked by combinators and reading lists, so, let's get back on track.

Difference lists are a (very) useful data structure in Prolog, particularly when it comes to parsing with definite clause grammars (DCGs) and when it comes to list construction (again, using DCGs). Why? Well, to answer that question, let's examine the structure of difference lists. The Prolog (or relational) representation of a difference list is of the form:
X = [1,2,3|Y] - Y
What that relation describes is that X is the difference of the pair of lists of (first) [1,2,3] appended with the list Y minus (second) the list Y.

Well, what is the value of the list Y? To a Prologian, this question is not important (for Y, being a logic variable, can be any list or, even, not a list at all because the variable is still uninstantiated (a "free" variable)). Its importance is that it gives us an "instant" index into the list after the elements we've already processed. The significance of that index for parsing is that we now have a stream over the parsed (list) data, which gives us the ability to translate BNF grammars directly into working parsers. The significance for list construction is that we "simply" walk "backward" through the DCG to construct a list from a (parsed) internal representation, without paying the linear cost of either (a) traversing the list each time to add a new element (which results in exponential time list construction) or (b) building the list in reverse (adding new elements to the head of the list) and then applying reverse to the end result.

"Big deal!" you scoff, "zippers [also called 'finger trees'] already give us indices into data structures. Who needs a funny-looking specialized zipper just for lists?"

"Good point!" is my response. Zippers are powerful and flexible data structures that not only provide that index over any data structure, but, even better than difference list's index that only goes forward (with any dignity) in constant time, the zipper's index can go both forward and backward through that (generic) structure in constant time. So, if you are not familiar with zippers, I request that you run, don't walk, to a good overview to learn more about their utility.

With all their flexibility and power, in certain situations, a zipper is not the correct choice for some list processing tasks; the difference list's simplicity shines in these situations. First, for the unidirectional index of the difference list (it moves forward through the list well, but backwards ... not so much), this, for list construction purposes, is hardly ever an issue, and if it becomes one, then backtracking is easily installed with some MonadPlus magic. Next, you, the coder, are responsible for constructing a zipper list data type each time you need one, but Don Stewart has provided Data.DList; no construction necessary! Last, an example structure of a zipper list, for the list a++b is (reverse a,b), so if we wish to reconstruct the original list, for an a of large magnitude, we still pay the linear (reverse) cost in reconstruction as well as the linear cost of appending b to that result.

You heard it here first, folks — difference lists: [zipper lists] can't touch this!

Okay, I've finished throwing it down for now. Let's look at difference lists from the Haskell perspective and then use this data structure in a list construction example.
> import Data.DList
For my examples, I'm going to be looking at the subset of difference lists that I can compare and see:
> instance Eq a => Eq (DList a) where
> x == y = toList x == toList y

> instance (Eq a, Show a) => Show (DList a) where
> show x | x == empty = "\\x -> [x]"
> | otherwise = "\\x -> [" ++ show (head x)
> ++ doShow (toList (tail x)) ++ ":x]"
> where doShow [] = ""
> doShow (a:b) = ", " ++ show a ++ doShow b
With the above instance declarations, we see that
(singleton 1) `snoc` 2 `snoc` 3
gives us the representation
λ x . [1, 2, 3:x]
... snoc appends an element to the end of a list (so it's the reverse of cons).

Prologians are always smug with their ability to create an infinite stream of 1s with X = [1|X]. And, no doubt about it, Prolog is a programming language that makes list construction easy ... *ahem* almost as easy as list construction in Haskell ("Your list comprehensions outshine even Prolog for sure..."):
repeat 1
... and now with difference lists ...
let x = unDL (singleton 1) x in x
But no self-respecting Prologian would freeze their interpreter with a such a rash proof; Haskellians have no such concern, thanks to weak head normal form:
take 5 (repeat 1)
So there!

Let's examine an application of difference lists. I have a little scanner that I use to help me in my oration. The meat of the algorithm (before difference lists) was as follows:
> lettersIn word = lettersIn' word []

> lettersIn' [] ans = reverse ans
> lettersIn' (h:t) ans | isLetter h = lettersIn' t (h:ans)
> | otherwise = reverse ans
Ugh, yes, reverses galore! This is an oft-recurring pattern in Haskell code that is very easily fixed with some difference list magic:
> lettersIn word = lettersIn' word empty

> lettersIn' [] ans = toList ans
> lettersIn' (h:t) ans | isLetter h = lettersIn' t (ans `snoc` h)
> | otherwise = toList ans
A very simply example, I grant you, for if we take a list whose elements are all in the set of the alphabet, the above code is a very complicated way of reinventing the id function. Or, put another way, who's going to teach me about scanl? Hm, would I be required to use the continuation Monad to escape the scan, I wonder? But you get the general idea for list construction, right? Instead of building the list by adding to the head ("cons"ing) and then applying reverse to the result, the different list allows one to "snoc" each element and return the correctly ordered result. For a single list construction, the benefit may not be all that obvious, but when one builds, e.g., an XML generator for a complex internal representation, reversing the reverse of the reversed reversed child element becomes an odious labour — difference lists eliminate all the internal juggling, replacing that complication with a straightforward constant-time list and element in-place append.

So, next time you need to do some list construction, instead of falling back on the reverse standby, flex your new difference list muscles; you'll be pleasantly surprised.


David R. MacIver said...

"zippers [also called 'finger trees']"

Um. No they're not. A finger tree is a totally different data structure to a zipper. A zipper is a view into another data structure with a "special point" in that structure. A finger tree is an tree designed for sequences with fast concat, append and prepend.


Finger tree:

meteficha said...

I wonder why you didn't write

lettersIn (x:xs) | isLetter x = x : lettersIn xs
lettersIn _ = []

meteficha said...

Or even better:

lettersIn = takeWhile isLetter

Jim Apple said...

David R. MacIver:

Yes, finger trees aren't the same as zippers, but zippers are really just fingers. Finger trees could have been called zipper trees if Kaplan & Tarjan hadn't gotten there first. Their fingers are actually special points in trees, along with a path from those points to the root, just like a zipper!

Jim Apple said...

I like difference lists. However, I think anything you can do with a difference list you can do with constant-time-append deques, which provide other operations as well:

Kaplan & Tarjan's simpler real-time version
Okasaki's amortized-time version

Chris Rathman said...

Might be worth noting that the book Concepts, Techniques, and Models of Computer Programming has a nice explanation of Difference Lists using Oz in section 3.4.4. I translated the code to Alice ML, where futures can used to get the same effect.

Chris Rathman said...
This comment has been removed by the author.
Pseudonym said...

Prologians are always smug with their ability to create an infinite stream of 1s with X = [1|X].

Yes, because breaking the logical interpretation of unification is a virtue in logic programming.

(Actually, I think that Prologians are secretly happy every time they come up with a way to do anything at all in Prolog. I digress.)

What I actually wanted to say was that I found it amusing that you used "show" rather than "shows" in an article about difference lists.

حسام داود said...

شركة تنظيف بالرياض
شركة تنظيف فلل بالرياض
شركة تنظيف شقق بالرياض
شركة تنظيف مجالس بالرياض
شركة تنظيف خزنات بالرياض
شركة مكافحة حشرلت بالرياض
شركة تخزين اثاث بالرياض
شركة تسليك مجارى بالرياض
شركة رش مبيدات بالرياض

jualobatkuatcialisasli45. said...

Vimax Obat Pembesar Penis Permanen
Ciri-Ciri Vimax Asli Canada
Jual Vimax Asli
Vimax Original
Vigrx Plus Asli
Cobra Oil Super
Minyak Lintah Papua
Vakum Pembesar Penis
Obat Kuat Procomil
Obat Kuat Cialis
Obat Kuat Nangen Zengzhangsu
Obat Perangsang Potenzol Asli
Obat Perangsang Sex Drop
Obat Perangsang Viagra Cair
Dildo Dua Kepala
Vibrator Peluru
Kondom Duri Sungut Lele
Ring Getar Butterfly
Boneka Full Body Blonde Semi Realistis
Boneka Sex Pantat Doggy Style
Vagina Getar Klasik 16 Cm
Vagina Getar Suara
FleshLight Vagina Getar
Vibrator Kupu-Kupu Triple Spot
Dildo Vibrator Sakky Anti Air
Vibrator Fairy Magic
Vibrator Kapsul Love Egg
Dildo Getar Goyang Tempel

Jual Obat Perangsang said...

Obat Perangsang Wanita

Juliana Kho said...


Jenny Wijaya said...

login cbet

joker123 live chat

Juliana Kho said...




Juliana Kho said...




Juliana Kho said...

Juliana Kho said...



Juliana Kho said...

Juliana Kho said...

poker idn play

LG Indonesia said...

Service Center Sony said...

Dubaiboy45 said...

Thank you for watching Gclub online

lnwMashow45 said...

Very good bro โบนัส 50% Gclub

lnwMashow45 said...

Thank you for watching สมัคร SBOBET said...

SahabatQQ Agen Domino 99 Domino QQ dan Poker Online aman dan terpercaya, serta tersedia 8 Games Terpopuler dengan hanya minimal Deposit & Withdraw Rp 20.000,- dan dengan mendaftarkan 1 akun anda sudah dapat menikmati 8 games yang tersedia.

Serta nikmati Promo Bonus-bonus yang menggiurkan
- Bonus Rollingan (cashback) sebesar 0,3% dibagikan setiap 5 hari
- Bonus Refferal (member get member) sebesar 15% seumur hidup
- Bonus Extra Refferal dibagikan setiap tanggal 1 (berlaku bagi anda yang memiliki refferal)

Dan SahabatQQ memiliki 8 jenis bank lokal
Senin-Jumat : Offline pukul 21.00 - 01.00 WIB
Sabtu : Offline pukul 00.00 - 05.00 WIB
Minggu Online 24 jam
Senin-Minggu : Offline pukul 23.00 - 04.00 WIB
Senin-Minggu : Online 24 jam (kecuali gangguan)
Senin-Minggu : Offline pukul 23.00 - 04.00 WIB
- Danamon
Senin-Minggu : Online 24 jam (kecuali gangguan)
Senin-Minggu : Online 24 jam (kecuali gangguan)
Senin-Minggu : Online 24 jam (kecuali gangguan)
Senin-Minggu : Online 24 jam (kecuali gangguan)

Contact Us

Website SahabatQQ
WA 1 : +85515769793
WA 2 : +855972076840
Telegram : +85515769793
Kami Siap Melayani anda 24 jam Nonstop
daftar sahabatqq

Terima Kasih Salam ALL IN ^^

JannethManalu said...

Very interesting blog. Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definately interested in this one. Just thought that I would post and let you know

Agen Poker IDN

Patricia Putri said...

Kasir4D, Agen Togel, Bandar Togel, Casino Online Terpercaya

Menghadirkan TOGEL & CASINO Online Dengan Diskon Dan Bonus Terbesar.
Keamanan dan Kenyamanan Member Adalah Prioritas Kami

Nikmati Promo dan Bonus di Kasir4D :
Bonus Deposit Harian 10%
Bonus Cashback 5% Up 15%
Bonus Refferal 2% Setiap harinya
Bonus Bulanan Turn Over
Dan Masih Banyak Bonus Menarik Lainnya Menanti Anda

Jadwal Tutup Pasaran Togel :
Pasaran Sydney
Tutup : 12.20 WIB & Buka : 14.00 WIB
Pasaran Singapore
Tutup : 17.20 WIB & Buka : 17.45 WIB
Pasaran Hongkong
Tutup : 22.10 WIB & Buka : 23.00 WIB

Kunjungi :
Webiste : Kasir4D
What's App : +855974247219
TELEGRAM : +6281393706188
LINE : kasir4d
Facebook : Kasir4D
Twitter : Kasir4D
Link Alternatif : Kasir4D

Agen Togel Online
Casino Online