All entries
Efficient type-level division for Haskell
23 May 2010 (haskell programming math) (1 comment)Another approach I wanted to explore to Encsé's 9-digit problem was creating a type-level solution in Haskell. First I'll describe what that would entail, and then present a solution to one of its sub-problems: testing special cases of divisibility in a more efficient way than the naïve implementation of repeating subtractions.
Type-level programming
Basically, a type-level Haskell program is to regular Haskell programs what C++ template metaprograms are to C++ proper. Conrad Parker wrote a great introduction to type-level programming using functional dependencies in issue 6 of The Monad.Reader journal, titled Type-Level Instant Insanity. You encode values as types, and then construct an expression whose type, as inferred by the Haskell compiler, encodes the output of your computation. To actually implement the equivalent of a function, typeclasses are used.
For example, take the following (value-level) Haskell definition of length: (deliberately not using any builtin types, and using unary representation for simplicity):
module ValueLength where
data Nat = Z | S Nat
data List a = Nil | a ::: (List a)
infixr 6 :::
length :: List a -> Nat
length Nil = Z
length (x ::: xs) = S (length xs)
(note that due to technical limitations, both : and :: are reserved names in Haskell, and so we have to use ::: for our cons)
To lift this definition to the level of types, we first need a way to represent actual numbers and lists as types. This is pretty straightforward using algebraic datatypes. Since we are ultimately only going to be interested in the type of expressions and not their value, no constructors are defined.
{-# LANGUAGE EmptyDataDecls, TypeOperators,
FunctionalDependencies,
MultiParamTypeClasses, UndecidableInstances #-}
module TypeLength where
data Z
data S n
data Nil
data x ::: xs
infixr 6 :::
-- Continued below
Compiling Haskell into C++ template metaprograms
16 May 2010 (haskell programming language)After Lőri posted his C++ template meta-program solution to the 9-digit problem, Encsé asked me to help him understand it, being the first real-world metaprogram he's encountered. To help both him and myself, I rewrote, function-by-function, his code into Haskell.
This rewriting process was surprisingly straightforward, and the resulting code, while being in a very strict one-to-one correspondence with the original C++ code, was immensely more readable. So this, naturally, lead to the idea of a simple functional programming language that could be compiled into C++ compile-time metaprograms.
A couple of days later, I had a small accident and had to stay in bed for a week, which gave me plenty of time to throw together a proof-of-concept compiler from a Haskell-like language into C++ TMP.
And so MetaFun was born.
MetaFun is structured into three separate modules, one for parsing and typechecking the input language called Kiff (for Keep It Fun & Functional), one for unparsing C++ metaprograms, and the third one is, of course, the compiler itself. I plan to re-use the Kiff module whenever I come up with an idea for another new throwaway functional compiler.
Unary arithmetics is even slower than you'd expect
19 April 2010 (haskell programming math agda correctness)My coworker Encsé posted a challange use the 9-digit number problem to demonstrate interesting programming techniques. So I sat down and wrote my first practical program using dependant types, in Agda. I've been playing around with Agda previously, but this seemed like a good opportunity to try to write an actual, self-contained, provably correct program with it.
I'm not going to get into details now about the resulting Agda code, because I'm planning to present it in detail in a later post. In its current form, my program is formally proven to produce only good nine-digit numbers; the only property that still needs proving is that it finds all of them.
But the sad surprise came when I tried to actually run it. It was unbearably slow to just get to all possible combinations for the first three digits. I'm talking about 12 minutes just to list them from 123 to 423 (at which point I killed the process). For comparison, the following Haskell program, which is an implementation of the same naïve algorithm, finds the (unique) solution in 4 milliseconds:
import Control.Monad.List
fromDigits = foldr shift 0
where shift d s = 10 * s + d
p `divides` q = q `mod` p == 0
encse :: [Int]
encse = map fromDigits $ encse' 0 []
where encse' 9 ds = return ds
encse' n ds = do d <- [1..9]
let ds' = d:ds
n' = n + 1
guard $ not (d `elem` ds)
guard $ n' `divides` fromDigits ds'
encse' n' ds'
So where's that slowdown coming from?
The first intuition would be that the code generated by Agda is slow because in parallel to the actual computation, it is also evaluating all kinds of proofs. But the proofs exist only in the world of types, so they shouldn't matter once the program is compiled.
The real answer is that calculating in unary representation is slow. Very, very slow. Even slower than you'd imagine.
Continue reading »Neked aztán lehet
15 March 2010 (personal ELTE) (1 comment)Ezt Viki mesélte, de szerintem érdemes ide is.
Az az alaphelyzet, hogy a suli mellett az ELTÉn dolgozik operátorként. Keveset operál, annál többet kell segítenie random ELTÉs oktatóknak akiknek "nem megy a levelezés". Szóval endless fun. Most viszont becsúszott egy jó sztori.
Bölcsészmérnök felhívja telefonon az operátori szolgálatot valami fájással, mondják neki hogy azonosítás végett küldjön egy levelet a benti címéről (ugyanis az SMTP autentikált). Persze, hogy leírja a levélben a jelszavát is. Válaszolnak neki hogy jó, akkor most mielőtt bármi egyéb történik, letiltották a jelszavát mert kiadta, itt meg itt tud ETR-en keresztül újat választani (remélhetőleg az ETR-es hozzáférését még nem adta ki senkinek).
Mivel ez már bőven túl bonyolult volt neki (amúgy én azt is el tudom képzelni, hogy azt hitte, szivatásból tiltották le), bement személyesen az irodába. Ott Viki elmondta neki az alábbi 3 (három) információt:
- Ne legyen a jelszóban ékezetes betű
- Nem praktikus, ha van benne y vagy z betű
- Soha senkinek, az operátori szolgálatnak se adja ki a jelszavát. Sosenem.
Különösebben hosszú gondolkodási idő nélkül vágta erre rá az alábbi, azóta nálunk szállóigévé vált mondatot:
Akkor lehet a jelszavam az, hogy "prüntyőke"?
Internet-celebritás lettem...
3 March 2010 (meta) (1 comment)... pedig nem is tudtam róla.
Ez a mostani sztori kb. egy hete kezdődött, amikor ránéztem a Google Analytics oldalon a honlapom napi látogatóinak számára, és az alábbi grafikont láttam (a mellbevágóbb hatás kedvéért itt most fél évre visszamenőleg látszanak az adatok):

Adja magát a kérdés, hogy mégis mi az isten történt január végén-február elején, amitől, hirtelen, a hatszorosára ugrott a látogatottságom, és utána is csak lassan csörgedezik vissza a megszokott szintre. Első blikkre csak annyi látszott, hogy a Google képkereső a fő forrása a forgalomnak, konkrétan pedig a domb szóra rákeresve talált rá ez a hirtelen jött sokaság erre a fotóra.
Ennél több akkor, egy hete nem is derült ki. Elkönyveltem magamban azzal, hogy hát a "domb" egy elég gyakori, egyszerű szó, és valóban ez a kép a második-harmadik a keresési eredmények között, úgyhogy ha sokan keresnek fotókat dombokról, miért is ne néznék meg az első néhány találatot.
Aztán tegnap kaptam egy emailt egy ismeretlen embertől:
Continue reading »Older entries:
- 22 February 2010: The B Method for Programmers (Part 2)
- 16 February 2010: The B Method for Programmers (Part 1)
- 8 February 2010: The downfall of Soviet microprocessor technology and Lisp
- 1 December 2009: Adventi Menger-szivacs (4 comments)
- 20 July 2009: Ember a Holdon! (7 comments)
- 29 May 2009: Grand Slam (8 comments)
- 7 May 2009: Relativisztikus hiperszámítógépek (2 comments)
- 24 March 2009: Titanic 2009 itiner
- 20 March 2009: Odüsszeisz-fázis
- 17 March 2009: Eliminating magic strings from ELisp's (interactive)
- 3 March 2009: Composite pattern a gyakorlatban (3 comments)
- 25 February 2009: λ: A tiltott kalkulus (1 comment)
- 22 January 2009: Type inference for CLazy (1 comment)
- 12 January 2009: CLazy interpreter and compiler (2 comments)
- 18 December 2008: Pierre Basieux: Top 7 (1 comment)
- 17 October 2008: Generikus programozás (5 comments)
- 21 September 2008: Hacktivity 2008 (7 comments)
- 15 September 2008: Viki in a nutshell (5 comments)
- 4 September 2008: Programozás visszavezetéssel (4 comments)
- 17 August 2008: Geekparty és egy breaking news (3 comments)
- 7 August 2008: Konzolok vs. HDTV-k (2 comments)
- 15 July 2008: Hiányzó iWiW feature (9 comments)
- 6 July 2008: A titkosügynökök magányossága (2 comments)
- 17 June 2008: Mark all as read (4 comments)
- 2 June 2008: Incompetent fuckwits (3 comments)
- 26 May 2008: Indi János és az ajkai kristályszuvenírek (9 comments)
- 25 May 2008: Tooltips for SLIME
- 18 May 2008: Nyomkodom a "Műköggyé" gombot (3 comments)
- 11 May 2008: A hétvége rendeltetésszerű használata (4 comments)
- 23 April 2008: Iskola az őrület határán
- 21 April 2008: Munkamorál (2 comments)
- 16 April 2008: Structure and Interpretation of Computer Programs
- 13 April 2008: Titanic: Szamócás süti
- 11 April 2008: Zuboly-koncert
- 11 April 2008: Cruisin' with me main man (1 comment)
- 8 April 2008: Titanic: Fél Nelson
- 4 April 2008: Titanic: Lesipuskás
- 28 March 2008: An important milestone (3 comments)
- 21 March 2008: Így lopjunk evőeszközt (3 comments)
- 6 February 2008: Look at me, I'm Dr. Zoidberg, home-owner! (1 comment)
- 1 February 2008: Alternatív WRT firmware-ek (3 comments)
- 9 January 2008: Egy nem PC asszociáció
- 28 December 2007: Egy eseménymentes hét
- 20 December 2007: Reál balfasz (3 comments)
- 9 December 2007: Azt hiszem, jól vagyok tartva (1 comment)
- 6 December 2007: Mikulás (4 comments)
- 14 November 2007: Not worth the paper it's printed on
- 10 November 2007: Diviánszky Péter for prezident! (1 comment)
- 30 October 2007: Nintendo a seggem (9 comments)
- 27 October 2007: Sapkakiválasztási axióma (14 comments)
- 15 October 2007: Jönnek a letölthető Wii demók? (3 comments)
- 12 October 2007: K stands for Kwality (6 comments)
- 7 October 2007: Egy jó doboz bármit elad
- 3 October 2007: Valós halál (10 comments)
- 15 September 2007: Élelmiszertúladagolás
- 5 September 2007: Folytatásos könyvek a bábeli könyvtárban (1 comment)
- 3 August 2007: Pókmalac, pókmalac... (3 comments)
- 2 August 2007: A kindergarten of pancakes
- 18 July 2007: Quote és backquote fia vagyok én (2 comments)
- 12 July 2007: Autósmozi (1 comment)
- 8 July 2007: Goblin
- 14 June 2007: Foglalkozása: aranytestű rock-isten (4 comments)
- 28 May 2007: Orvosi segítségre van szükségem
- 18 May 2007: Encsé, az élet császára (1 comment)
- 11 May 2007: Merre megy a matematika? (1 comment)
- 26 April 2007: Ég a fater^H^Hörzs! (1 comment)
- 11 April 2007: Kampány
- 29 March 2007: Félelem és reszketés Hamburgban (6 comments)
- 17 March 2007: Interactivity and Games as an Artform (2 comments)
- 16 March 2007: Encsé leverte a köcsögöket (3 comments)
- 26 February 2007: Az áltudós az becsap árvíz aszály tájfun lecsap!! (2 comments)
- 15 February 2007: Statisztikai vizualizáció rizsszemekkel
- 11 February 2007: Wii buli Warioval és Raymannel
- 9 February 2007: Szóval a Wii-ről
- 2 February 2007: Random sirám (1 comment)
- 21 January 2007: Ami a Wii használati utasításából kimaradt
- 29 December 2006: Playstation retrospektív: Gran Turismo (6 comments)
- 27 December 2006: Kis- és nagykarácsonyok
- 11 December 2006: Kamera által, homályosan (5 comments)
- 28 November 2006: Sziget a Google Earth-on (1 comment)
- 22 November 2006: Commenting on blog entries (2 comments)
- 8 November 2006: Ruby on Rails a seggem
- 6 November 2006: Újra itthon...
- 4 November 2006: Utazás Absztinenciába
- 2 November 2006: Képernyőregények
- 17 October 2006: A hallgató, akinek semmi sem kerülte el a figyelmét
- 13 October 2006: Activity log
- 11 October 2006
- 8 October 2006: Registering Windows file types with NSIS (7 comments)
- 2 October 2006: RSS feeds to ease transition from Advogato (1 comment)
- 22 September 2006: Queueing timeouts in JavaScript
- 12 September 2006: Tombol a kompetencia
- 10 September 2006: VPG 20 (2 comments)
- 9 September 2006: Luggage space extravaganza (1 comment)
- 27 August 2006: Mad Maestro
- 21 August 2006: Tilos 15 (1 comment)
- 13 August 2006: Sziget, bazmeg
- 8 August 2006: Papírmentes iroda
- 19 July 2006: The following takes place between 10:00 a.m. and 6:00 p.m. (1 comment)
- 24 June 2006: Programming by Google
- 16 June 2006: Éjjeli áramszünet
- 14 June 2006: Microsoft Natural Keyboard 4000
- 12 May 2006: Legközelebb eladok neki egy toronyórát, lánccal
- 8 May 2006: π DVD (5 comments)
- 7 May 2006: Mi ez itten ez?
- 18 May 2005: Dension customer support: A+
- 2 May 2005: Chrome rings (1 comment)
- 24 April 2005
- 25 March 2005
- 11 March 2005
- 8 March 2005: I think I've found it