الاثنين، 31 أكتوبر 2011

للديموقراطية صور كثيرة

رئيس الولايات المتحدة يأتي بالانتخاب، وكذلك أعضاء الكونجرس. دستورهم شهير في هذا الجانب حتى صارت كلمة "دستور جفرسوني" Jeffersonian Constitution تطلق على الدساتير المصنوعة على النمط الأمريكي. لكن الصورة الآن ليست كما نتخيل...

سيطرة رجال الأعمال على الدولة حتى أن القوانين تُفصّل لهم خصيصاً، تفتيش الناس في المطارات، معتقلات جوانتانامو، ضرب المتظاهرين بالرصاص المطاطي في مظاهرات Occupy wall street. طبعاً مازال لديهم نقاط إيجابية كثيرة (ليس هدف مقالي الهجوم عليهم) لكني أريد أن أتساءل: كيف تتآكل الديموقراطية في الأمم؟ وكيف يمكن تحصينها لجعل تآكلها أصعب؟ هذه بالطبع اسئلة لها معنى في الجو السياسي الحالي، جو رسم الصورة المستقبلية للدول العربية، ومن هنا هذا المقال.

تعال نضرب أمثلة على استخدام كلمة "ديموقراطية" في نسق غير سياسي، سنعرف لماذا بعد قليل:
  • الفيسبوك ويوتيوب - ومن قبلهم المدونات - صنعوا ديموقراطية في الإعلام، وصار يمكن لأي شخص أن يصنع بذرة قناة إخبارية (مثل شبكة رصد وغيرهم). وهناك أشخاص مثلي لم يكن ليُسمع لهم صوت بدون هذه الأدوات.
  • المدونات بالذات جعلت إنشاء موقع على الإنترنت بسهولة ملء صندوق نصي. قبل ذلك كان يمكن لمتخصص أن يصنع موقعاً للإنترنت بلغة HTML ويبحث لها عن host، لكن الآن خالتي تستطيع عمل مدونة إن أرادت.
  • لغة Visual basic وبرنامج Microsoft Access صنعوا في عصرهم ديموقراطية برمجية، بحيث يمكن لصيدلي مثلاً أو صاحب سوبر ماركت أن يصنع برنامجاً لمحله دون الحاجة لمبرمج متخصص.
ما الذي يجعلني اطلق كلمة "ديموقراطية" على كل من هذه المواقف؟ الشيء المشترك بين كل من هؤلاء هو فكرة نزع القوة من القلة وإعطاؤها للأكثرية. وللقوة صور عديدة...اقتصادية وفكرية وعلمية وتكنولوجية وسياسية وإعلامية...كل قوة منهم لو تركزت في يد قلة فإن هذه القلة قد تسعى لاستثمار قواها الحالية في اكتساب قوى جديدة. وبهذا تبدأ الديموقراطية في التآكل.

وهذا ما حدث في أمريكا: جزء كبير من مشاكلهم سببها تركز الإعلام في يد فئات محددة توجه الشعب كيفما تشاء، إن أهملت تلك الفئات أي تجاوز لم يغضب من الشعب أحد، وإن ركزت على أشياء أخرى لم يتحدث أحد إلا عنها: لقد صاروا يحددون للناس ما هو المهم وما هو غير المهم. [فكر في ما حدث في مصر أيام مباراة الجزائر أيضاً]

فكيف نحافظ على الديموقراطية إذاً؟ أشياء كثيرة، لكن من ضمنها فكرة "توزيع القوة على الأكثرية":
  • المعلومات قوة، لذلك لابد أن يكون هناك شفافية ويستطيع المواطن أن يعرف كل صغيرة وكبيرة تحدث في إداريات وحكم الدولة. لو تركزت هذه المعلومات في يد قلة فسيكون من الصعب محسابتهم أو وقف الفساد.
  • التكنولوجيا قوة، لذلك لابد أن يكون استخدام الإنترنت مثلاً متاحاً للجميع بلا قيود، وبسعر رخيص وفي كل مكان، وإلا فسيكون السبيل الوحيد للحصول على المعلومات هو الوسائل التقليدية وهي سهل السيطرة عليها. كل تكنولوجيا يملكها قلة ولا يملكها كثرة هي نقطة يمكن إساءة استغلالها.
  • الفكر قوة، ولو لم يكن الشعب كله على مستوى فكري عالٍ فسيمكن في أية لحظة أن يأتي من يسمون أنفسهم النخبة ويدّعوا - ولو زيفاً - أن الجميع لابد أن يستمع لهم لأن التفكير حكراً عليهم.
  • المال قوة، وفي غياب العدالة الاجتماعية وتكافؤ الفرص والحراك الاجتماعي - أي وجود فرصة للفقير لكي يعمل ويرفع مستواه - ستجد دولة رجال الأعمال.
  • الوظائف قوة، وأرى أنه لابد من تسهيل عملية أن يفتح من يريد شركته أو عمله الخاص ونشر تلك الثقافة، لكي يزيد استقلال كل شخص اقتصادياً ولا يجد نفسه معتمداً على الحكومة أو المؤسسات الكبيرة يعينونه متى شاووا ويتخلصوا منه متى شاؤوا.
  • اللغة قوة، ولو تعلم الأكثرية فن الخطابة والتفاوض والإقناع لكان ذلك ايسر للحوار القومي بدلاً من أن يحتكر ذوو الكلام المزخرف التأثير على الآخرين.
وهكذا وهكذا..إن كنت تريد ترسيخ الديموقراطية في المجتمع عليك أن تسأل نفسك: ما هي صور القوة؟ وكيف يمكن كسر احتكارها؟

بالمناسبة، هل تذكر خطة النهضة التي ذكرتها من قبل على تلك المدونة؟ اسلوبها هو "رفع المستوى الفكري، تغيير القيم الاجتماعية" ومحاورها "العمل بالاسلام، طبقة وسطى قوية، علم مستشري".

الخطة في اساسها اجتماعية، لكن مثل كل شيء لها جوانب سياسية أيضاً!

السبت، 29 أكتوبر 2011

Kalimat-related open source project ideas


Do you like open source and want to create your own project? Do you like Kalimat? Do you want to contribute something to the future of programming in the Arab world?

Here are some new projects that I would love to see for the Kalimat programming language, and which can be done independently of the main Kalimat package of you want to 'own' your project:

Kalimat for .Net

If you write a .Net implementation for SmallVM (the virtual machine of Kalimat & Kick) then the rest would be easy: Just recreate the compiler that translates from Kalimat to SmallVM, or even use the original compiler.

Why Kalimat for .net? It would make Kalimat both usable as an educational language and a professional one; opening the door for libraries that wrap ASP.net, ADO.net, ...etc into an Arabised API and make it available for Arab programmers; kids and adults alike.

Automatic code documentation

The CoffeeScript language has a tool called Docco, that takes commented source code and generates documentation that puts the code and the explanation side by side like this

I really want a tool that works the same way; it would make it so much easier for educators to write Kalimat tutorials with heavy source code and notes, which is why the language was created in the first place!

Kalimat environment on the web

This would make trying the language much easier without needing to download anything and make Linux users happier (since currently Kalimat for Linux has an Ubuntu-only ready made package and other Linux users have to compile from source).

Again, the secret is in SmallVM: If you write it using JavaScript and implement the run-time library (Graphics, Sprites, ...etc) with HTML5 then the IDE itself would probably not be too hard.

More platforms

In addition to .Net, Kalimat could be ported to other platforms. If done right we could have some sort of a "Kalimat everywhere" ecosystem:
  • SmallVM could be translated to, or interpreted by, ActionScript bytecode (ABC) to enable running Kalimat programs as Flash applications (suggested by Issam Elbaytam in the comments)
  • Could similarly be translated to Java, to use the Java platform or to create Android programs
  • Or perhaps translated to Objective-C, to create iOS applications ;)

الاثنين، 24 أكتوبر 2011

حكاية التلميذ الملول، والعملاق، والشراب المنوم

سنة 1991 كنت طالبا بالمرحلة الابتدائية في مدرسة قطر انترناشونال، وهي مدرسة على النظام البريطاني، المدرسون فيها اجانب، والتلامذة خليط من الأجانب والعرب.

ووضع المدرّس أمامنا مجموعة من الصور كما يلي:
1- عملاق اسطوري يحطم القرية
2- شاب يحمل زجاجة شراب منوم
3- العملاق يأكل عشاءه
4- العملاق نائم
5- سكان القرية يحتفلون

وكان "الواجب" ذلك اليوم هو ان نكتب قصة مستوحاة من تلك الصور. لكني لم أرض أن أكتب تلك القصة..

إنها مباشرة جداً ومملة..في الواقع لماذا ينبغي كتابتها أصلا؟ ان الصور وحدها تكفي لوصف الأحداث للقاريء، وأي إضافة عليها هي من باب التكرار بلا طائل. اضف إلى هذا ان المدرس السابق كان يحب الإبداع ويأتي كل يوم بجديد. كنت معتاداً على عمل أشياء أفضل. لكن هناك واجب ينبغي عمله. بدأت الكتابة...

"كان هناك عالِم يعيش في المستقبل البعيد، وكان هناك مسابقة للأشياء الغريبة، وفكر العالم أن يعود إلى الماضي لكي يحضر شيئا من هناك يشارك به في المسابقة..."

...وهكذا أكملت الحكاية عن العالم الذي ساعد أهل الماضي على التخلص من العملاق ومقابل ذلك أحضروا له شيئا غير موجود في المستقبل ليشارك به في مسابقته، طبعا كانت القصة صفحتين منها حوالى ثلاثة اسطر تصف الأحداث المطلوبة أصلاً في الواجب المدرسي :)

وهذا ما اكتشفه المدرس طبعاً، لذلك وضع دائرة حول الثلاثة اسطر إياها وكتب بخط كبير: "The real story only begins here"

وحين رأى أبي ذلك لم يعجبه الأمر. لم يكن أبي خبيراً باللغة الانجليزية، لذلك كان استنتاجه كالآتي: المعلم كتب بخط كبير، إذاً المعلم غير راض [في الواقع هذا الجزء صحيح].

من الناحية "التقنية"، أنا فعلاً لم اقم بعمل الواجب كما ينبغي، لكن من اهتم بالطفل الذي شعر أن الواجب مستواه اقل مما يحب، وكتب بدلاً منه قصة للخيال العلمي؟

الأربعاء، 12 أكتوبر 2011

A pony for Amina



يوم في حياة اعضاء بمركز التفكير الحوسبي

نحن في فترة ما في المستقبل القريب، وقد تم إقامة مركز التفكير الحوسبي. تهانينا؛ لقد اجتزت الinterview وتم قبولك للعمل فيه. اليوم ستأخذ جولة لترى شكله من الداخل.

المركز في صورته الحالية مقسم إلى ثلاثة انشطة: التدريب، البحث العلمي، التواصل الاجتماعي. كل نشاط منهم يدعم النشاطين الآخرين في صورة متكاملة:
  • تخيل ان البحث العلمي، بعد سنوات، وصل لنتائج هائلة: هل سنأخذ سنوات اخرى في إقناع المجتمع بها أم نقدمها في مجتمع مستعد للتفكير الحوسبي اصلاً؟ دور "التواصل الاجتماعي" هو الترويج لفكرة التفكير الحوسبي وأهميته من الآن.
  • البحث العلمي قسمان: تطبيق علوم الحاسب على كل العلوم الأخرى (هذا تعريف التفكير الحوسبي) وتطوير اساليب تعليم البرمجة وعلومها.
  • لكي تقوم ببحث علمي تحتاج للقيام بتجارب، ولكي تقوم بتجارب تحتاج لأشخاص حقيقيين تعلمهم، هذا دور "التدريب".
توجد دورتان تدريبيان شغالتان حالياً، واحدة لتعليم البرمجة بلغة كلمات، والأخرى بلغة مطابقة لها ولكن مترجمة للانجليزية. هذه إجابة تجريبية للسؤال الآتي: "ما مقدار الفرق بين تعليم البرمجة لأشخاص بلغتهم الأصلية وتعليمهم بلغة اجنبية عليهم؟" وهي إجابة تتبنى منهج البحث العلمي ولا تعتمد فقط على الآراء الشخصية.

في الغرفة المجاورة يوجد فريق عمل صغير يطور كتاب للأطفال عن تجارب الفيزياء بالكمبيوتر. جدول محتوياته قريب من هذا:
  1. الحركة والسرعات
  2. القوى، الجاذبية، التصادم
  3. البصريات
  4. الدوائر الكهربائية
الكتاب يجعلك تكوّد كل قانون فيزيائي في برنامج أو مكتبة، البرامج تتنوع بين تجارب، محاكاة لظواهر (مثل المجموعة الشمسية وكيف تتفاعل الجاذبية بين الشمس والكواكب) والعاب. برنامجي المفضل هو برنامج يجعلك ترص مجموعة من العدسات والمرايا ثم تدخل صورة ليريك البرنامج كيف تبدو من خلال الرصة التي وضعتها.

ماذا عنك أنت؟ أول مهمة سنكلفك بها هي موقع برمجة على الانترنت، يسمح للطفل - أو أي شخص - أن يكتب الكود ويراها تنفّذ فوراً. سوف يكون مكتوبا بتكنولوجيا HTML5، ويحتاج لحيل كثيرة بالـ Ajax ليقوم بدوره. أيضاً يمكنك إن أردت أن تبدأ القراءة عن MindStorms و Adruino، لأن مهمتك التالية ستكون دراسة فكرة عمل لعب الكترونية صغيرة يمكن برمجتها.

نشجعك على ان تكتب عن عملك في المدونة الخاصة بالمركز، فكل معلومة ننشرها للمجتمع لها دور. الاسبوع القادم سيكون هناك ندوة في إحدى كليات الحاسبات عن التفكير الحوسبي ودوره في المجتمع؛ نبدأ بالمبرمجين انفسهم ثم "نسرسب" الموضوع إلى المجتمع ككل.

يحتاج العمل هنا إلى خبرة في مجالات كثيرة، ونحن نعلم أنك لست ملماً بها كلها..لذلك يوجد مجلس علم اسبوعي نتحدث فيه عن مواضيع متنوعة. احيانا عن الdistributed computing، احيانا عن النحو او الزخارف الإسلامية، احيانا عن علم النفس الإدراكي وعلاقته بتعليم الأطفال. من حين لآخر نأتي بضيوف من الخارج (من خارج المركز أو من خارج مصر) ليقدموا ندوات في مجالاتهم المختلفة.

طبعاً هناك علوم متعمقة لا يكفي مجلس العلم لها، لذلك ندرس حاليا إمكانية لإرسال بعض اعضاء المركز ليحضروا ماجستير أو دكتوراه في جامعات خارجية في مواضيع متعلقة باهتمامات المركز. ما رأيك بالمناسبة بعلم لغات البرمجة كأحد مواضيع الدراسة؟

(هذه الرؤية لم تتحقق بعد. تريد لها أن تتحقق؟ لماذا لا نفتح حواراً عن تمويل المركز؟ لو لم تراها بعد، هذه رؤية للمركز من زاوية أخرى)

الخميس، 15 سبتمبر 2011

أهمية علم لغات البرمجة

My area of interest is programming language theory (PLT). This is an area separate from compilers, but related.

It discusses things like programming paradigms (imperative, logic, functional, object oriented,...), language semantics, type systems, programming language features, and other things.

The problem is; most programmers think 'languages' are a solved problem. They think that the best possible languages are the familiar ones like C++ or Java, and that effort should now focus on e.g the libraries. That is completely not true!

So, let's discuss some lesser-known languages and how they offer completely new ways to programming.

There is a new language called OPA - derived from the ML family - that makes web application development significantly faster. Projects that take months to develop can be made in OPA in weeks.

A large part of the power of this language comes from its type system, Which lets you describe only once the shape of your data and then generates client-side (running on the web browser) and server-side code from the same description, thus eliminating many causes for error.

Or how about Google's Go language, which focuses on speed of compilation, safety, simplicity and concurrency?

Then we have Subtext, a language (part of a series) that attempts to simplify the reading and understanding of programs?

Or Lisp, where you can define new syntax for the language, having components written with "mini-languages" inside a larger program?

Or...we know that C is fast and powerful, but very unsafe and hard-to-debug. How about a language that has the same speed and low level capabilities, but much more safe and expressive? Enter BitC.

Speaking of low-level; Mozilla (the creators of Firefox) are working on a new language for systems programming called Rust. It should be useful for the same type of programs that are written in C++ but with features like this (from their site):
  • Memory safe. No null pointers, wild pointers, etc. Automatic storage management.
  • Dynamic execution safety: task failure / unwinding, trapping, logging.RAII / dtors.
  • Typestate system: ability to define complex invariants that hold over data structures.
  • Very lightweight tasks (coroutines). Cheap to spawn thousands-to-millions.
Finally, how about F# from Microsoft, a functional programming language that is now part of Visual Studio, described on its site as "...It is a simple and pragmatic language, and has particular strengths in data-oriented programming, parallel I/O programming, parallel CPU programming, scripting and algorithmic development"

In summary, programming language theory is not simply about academic research: real companies like Google, Mozilla, and Microsoft are working on serious projects that help them in making money or beating their competition.

I think this field is very worthy of study, even if so few people pay attention to it...

الجمعة، 9 سبتمبر 2011

يوم في حياة أطفال مبرمجين

نحن في عام 2018. محسن لديه ثلاثة أبناء: مروة (13 عاماً)، منى (8 سنوات) ومصطفى (5 سنوات).

سوف تأخذ مروة اختها منى اليوم لأحد مراكز التفكير الحوسبي. منى جديدة على عالم البرمجة بينما مروة مبرمجة عتيدة. دخلا من بوابة المركز في إلى الحديقة المرتبطة به حيث يلعب بعض الأطفال. وصلت مروة إلى "المشروع" الذي كانت تعمل فيه منذ أيام: روبوت صغير موجه الكترونياً يسير في متاهة. هناك مشكلة في خوارزمية السير عبر المتاهات لأن الآلي احياناً كان يعود إلى نفس الممرات التي زارها من قبل، وأحياناً يدور في حلقة مفرغة بدون أن يستكشف طرقاً جديدة للخروج مما هو فيه..

بينما كانت مروة منهمكة في حل المشكلة كانت أختها الصغرى تشعر بالملل، لذلك ذهبت بها مروة إلى أحد المرشدين في المركز ليقترح لها شيئا تفعله، وعادت هي إلى مشكلة الروبوت والمتاهة.

أخذ المرشد منى إلى ساحة مثل ساحة العربات المتصادمة في الملاهي، لكن أمامها شاشة كمبيوتر. فتح لها برنامجاً مثل الpaintbrush لكن يحمل خريطة الساحة أمامها، وقال لها أن ترسم الطريق الذي تريد أن تسير فيه السيارة.

رسمت شكلاً لولبياً ثم ضغطت زراً يدل على انهائها من الرسم، ثم ركبت السيارة الصغيرة لتتحرك بها كما في الرسمة بالضبط. بينما كانت السيارة تتحرك كانت يُرسم على الأرض مساراً ضوئياً من أثر حركتها؛ اثر هو بالضبط الرسمة على الشاشة. نزلت منى من السيارة وأعطاها المرشد صورة أُخذت من الأعلى للساحة وبها الرسم الضوئي.

مع الوقت لن تكتفي منى بالرسم على الpaintbrush، بل سوف تكتب برامج لرسم مربعات ونجوم وزخارف كالزخارف الإسلامية، وفي كل مرة سوف تسير السيارة بها لتنفذ ما قد خُطّط لها.

لكن اليوم سوف تلهو قليلاً، بينما أختها الكبرى تتقدم قليلاً في مشروع الروبوت ثم تذهب لأخذ محاضرة في خوارزمية Depth first search.

أما في البيت، فإن مصطفى يحب أن يحكي الحكايات. أمسك بالقلم الإلكتروني في يده اليسرى ووضع جهاز أوراق على حجره: لابد أولاً أن يرسم شخصيات الحكاية ثم بعد ذلك ينفّذ الأحداث. رسم قطة ورجلاً ثم ضغط على زر "تسجيل الأحداث".

وضع يداً فوق صورة الرجل على شاشة اللمس، ووضع اليد الأخرى على القطة، وهو يعلم أن الكمبيوتر يسجل كل حركة يحركها للشخصيات، كما يعلم أيضاً أن الكمبيوتر يسجّل صوته.

قال بصوت رفيع: آه! أنا تائهة! أريد أن أعود إلى أمي!

ثم غير صوته لصوت أكثر غلظة (على الأقل بالنسبة لطفل في الخامسة) وحرك صورة الرجل قليلاً وقال: لا تخافي أيتها القطة الصغيرة! أنا أعرف أين هي أمك! تعالي معي!

ثم بصوت القطة: أصحيح هذا؟؟ حسناً أيها الرجل الطيب! سوف آتي معك.

ثم حرك صورتي الرجل والقطة معاً عبر المشهد الذي رسمه حتى خرجا من الشاشة. هنا غير مصطفى صوته إلى صوت ثالث أشبه بالمذيعين وقال: "ولكن القطة لا تعلم أن الرجل هو عضو في عصابة تخطف القطط! ماذا سيحدث بعد ذلك؟؟ أن أن أن....."

قال محسن لابنه بعد أن رأى الحكاية: عظيم عظيم! أنا متشوق لرؤية الجزء الثاني! ثم ذهب ليسأل زوجته عن نوعية البرامج التي تدع الأطفال يشاهدوها في التلفزيون.

ليس ما يفعله مصطفى بالضبط برمجة، لكن بينهما علاقة: إنه يعتاد من الآن أن يجعل الكمبيوتر يفعل ما يريده، فإن أدرك مدى قوة وتأثير هذا الأمر فهناك وسائل تعليمية أخرى بعد ذلك.

كانت زوجة محسن تعمل معلّمة، وكانت مهمتها حالياً تحضير درس النحو الجديد، وقد تم تعديل منهج النحو ليستفيد من الطرق الحوسبية؛ مثلاً الإعراب يُكتب الآن في صورة شجرة إعرابية parse tree. بدايات صغيرة لكنها تنمو مع الوقت.

لقد بدأ التفكير الحوسبي يؤثر على المجتمع. بدأ أحد الأساتذة في كلية التجارة في تجربة لتدريس بعض طرق المحاسبة بطريقة خوارزمية. هناك شركتان جديدتان في مجال الهندسة الطبية، لكن نصف نشاطهم في الواقع يبدو مثل شركة تطوير برامج. إن بعض من هذه الأجهزة بها كود أكثر من جهاز كمبيوتر من الأجهزة القديمة. هناك شاعر يكتب قصيدة، ويساعده برنامج يراجع من ورائه الأوزان، الجديد هنا أن الشاعر قد كتب ذلك البرنامج بنفسه. ثم بعد ذلك هناك حركة لترجمة كتب علوم الحاسب المختلفة وتعريب مصطلحاتها. هناك أيضاً حركة تأليف متزايدة في النموّ.

كانت نهاية القصة سعيدة، إذ قبض الشرطي على لص القطط وأعاد القطة الصغيرة لأمها، بينما عادت مروة ومنى من المركز. قال الأب لمنى: هل كنت سعيدة اليوم؟

قالت منى: نعم!

قال لها: وهل تريدين الذهاب غداً؟

قاطعته مروة: ولكنها سوف تعطلني عما أفعل يا أبي!

قال محسن: لا تقلقي، سوف أذهب معها أنا.

الا تريد لهذا أن يصبح حقيقة؟ يمكننا أن نبدأ بحوار عن الفكرة (بريدي الاكتروني هو samy2004 على gmail.com )، ولديّ خطة أعمال مبدئية هنا.

الثلاثاء، 6 سبتمبر 2011

Concept: The Whisper programming language

I'm thinking up of a new programming language, heavily derived from Smalltalk and called 'Whisper'. This is an outline of my current thoughts -- I don't know when or if I'd actually implement the language.

Syntax

Like Smalltalk, almost everything is a message send. There are two kinds of message send in Whisper: binary operators and keyword messages. Binary operators follow the usual Smalltalk tradition:

12 + 8 * 2 -- returns 40

Keyword messages follow syntax like this:

myWindow : drawCircleAt(100, 100) withRadius(50)

Some notes:
  • A colon after the object indicates the start of a message send
  • A keyword can have zero, one or more values acting as positional arguments
  • Normal C-like positional arguments are a special case of this syntax
  • The syntax is heavily inspired by the Grace educational programming language, itself inspired - again- by Smalltalk
We have the traditional ST blocks:

myFunc = { x, y | x + y }


When sending a keyword message, and a block is the only argument between parenthesis, the parens can be omitted like so

(x > 5) ifTrue { out : print("yes") } else { out : print("no") }

IDE & Image

As an experiment, the main IDE for Whisper would be completely browser-based with IDE logic running from a web server. Local applications can be written that access local files...etc but the application's UI would still be browser based.

Another, for me much harder experiment is to make everything persistent all the time on an Sqlite database: All program state changes would be by writing to slots in objects, even function activation records or global variables (an idea taken from Self and others), and all slot writes would be trapped and written in the DB.

This would have a very high performance cost, so a lot of design thought should be put into this; by thinking about how to use memory for cache, possibly marking special object as transient, or some other clever method. I'm sure the Lispers, Smalltalkers, Selfers...etc have probably already "killed this problem from research", as the Arabic saying goes, and that I could find an ideal solution in some paper published in 1989..we'll see!

Gradual typing

Fields, method arguments and return values can have optional type declarations, this would allow the JIT compiler to better optimize things and (more importantly for me) allow autocomplete to work. It would also make refactoring safer, for example renaming a method would change the identifiers in all known calls to that method.

This should not change the dynamic nature of the language: The IDE and program are still 'live' and programs are still assembled piecemeal without an edit/compile cycle. There will be situations when calling a method with wrong argument types throws a runtime exception instead of a compiler error, and that's fine for me.

Other goodies

Since I'm now in the dreaming phase and not serious work phase, let's steal some ideas from Lisp, while we're at it:

The first is multiple dispatch. I think this will be useful in certain types of applications like compilers, allow us to think about libraries in new ways (I can imagine a GUI library inspired by the 'lenses' concept from MIT's Haystack project) and allows niceties like the return of traditional syntax:

if(x > 5) do { out: print("yes") } else { out: print("no") }

We can almost fool users into thinking it's Java or C#!

Also, since the syntax is nicely minimal, we could think about adding some metaprogramming...

Since Smalltalk tries to make as much of the program be represented as objects, we can do the same with the program syntax tree itself; and have the AST of each method be a public property of that method.

This would allow us to write code in our own DSL inside a method, code which looks like gibberish to the interpreter but useful for us, since we can write procedures to read the tree from the method and process it in whatever way we like. Possibly layer Common Lisp-style macros on top of this feature.

Looks like a nice language, right? I wish it were already available so that I didn't have to develop it :(

الأربعاء، 31 أغسطس 2011

Graduation project themes and ideas - 2010

In 2010 I suggested the idea of having multiple graduation projects with a common topic or 'theme'. My hope from this was to:
  • Give students the feelings that their projects are part of a larger goal
  • Give opportunity for different teams to cooperate and share e.g research papers they found on the topic
  • Possibly plant the seed of 'research departments' in FCIS, where multiple MS.c or Ph.D researchers work on common topics instead of each doing an independent topic.
That year I gave a presentation outlining two themes: Arabic natural language processing and Pen computing , and giving a lot of ideas for each theme (and some outside the themes).

Here's the presentation slides, in case they might be useful: Themes for Graduation projects - 2010

الأحد، 21 أغسطس 2011

How Kalimat produces EXE files


Everyone tells me Kalimat is a toy language since it doesn't produce .exe files.

At first, I didn't pay attention since my main goal was teaching children programming. I mean, does Smallbasic produce exe's? Does Scratch?

But gradually, I changed my mind
  • Many children would feel patronized if they have a perception of being taught with a "kiddy" language, even if the language is actually powerful - if it seems kiddy, that's bad.
  • Being an Arabic-based language means it's under more scrutiny, since a lot of people will have the "Arabs can't make a real language" point of view, and will find any reason to say so
  • There is a real technical need for making .exe files from programs, so that users - kid or adult - can distribute their programs to others.
So started the journey of making executables. I began considering my options:
  1. Generate assembly code or machine code from Kalimat, perhaps using something like LLVM or C--
  2. Generate code in another language like C++ or Go, and use e.g a C++ compiler to create the .exe
  3. Cheat
Cheating sounds good, right? What does that mean exactly? Well, in early versions of Visual Basic (far before VB6 or .Net) the IDE could create exe files, but not exactly the way you know: The file contained a bytecode version of your program, and you had to include a DLL that came with VB and contained an interpreter for this bytecode. All your exe had to do was to load the DLL and tell it: "Here, take this program and run it for me, will you?".

This is also how py2exe works: It bundles your python program and a python interpreter into one package, and that is your executable.

Kalimat has already taken a lot of ideas from Basic and Python, so I decided to go this route and quickly add that feature, and in the long term consider adding the capability of making real, 'respectful' .exe files.

(I do mean 'quickly', it was done in ~ 3 days).

Step 1: Separate SmallVM into its own DLL

The Kalimat IDE and SmallVM (the virtual machine that runs Kalimat programs) were very tightly coupled in the source code. I had to spend some times moving all runtime code from the IDE to the VM, making small changes as I go, and export some VM functions.

Now I have an independent smallvm.dll which exports a function that your programs can send code to execute.

Even better: smallvm.dll does not take Kalimat code, but takes code in the form of its own assembly. That means if you're creating your own programming language you can use it.

Step 2: Generate the "driver" program

Now suppose the user typed this program and wants to create an .exe from it:
اطبع 12
First the Kalimat IDE will generate this assembly:

.method main
pushv 12
callex print
ret
.endmethod
This is good. Now we need a program that does something like this:
#include "smallvm.h"

int main( )
{
char *program =".method main\npushv 12\n\callex print\nret\n.endmethod";
SmallVMRunCode(program);
}
If this program is then made to .exe, then we're done!

Notice that I've simplified a lot of details here. For example the char *program is actually not a direct representation of the program but a base64 encoding of it. Also notice that I could've used a technique called 'binary blobs' to bypass the need for repeatedly compiling C++ code and just use a linker to combine object files.

So far so good, but that means I need to include a C or C++ compiler (or a linker) with Kalimat. On the Linux version of Kalimat that's easy: Just add gcc or g++ as a dependency and the package manager would take care of the job.

On Windows I'd have to manually bundle a compiler. The standard Open Source C++ compiler on Windows is MinGW. Its a little more than 120 megabytes...

Ouch. Remember that Kalimat's download is currently about 5.4 megabytes.

I tried to take only the necessary files in MinGW and include them, but failed. I don't know what little stuff depends on other little stuff.. It might be possible, even easy, but I don't want to keep trying out things aimlessly, and I don't want to study - right now- the structure of the GNU toolchain. Let's find another way.

No problem, I thought: I'll use Google's Go language. The compiler and linker (8g.exe, 8l.exe) are 1.8 megabytes together, and they don't need anything else to work. Excellent! All I need to do is generate a small Go program that calls a function from a C dll.

To do this, I think you use a tool called cgo that's bundled with Go. I tried for some time to use cgo but failed. I didn't spend a long time doing that; maybe I'm too lazy, maybe if I spent a little more time I'd have figured it out, but anyway...

What other languages produce native .exe's these days? I know: Free Pascal.

I'll spoil the surprise for you: this is the current solution. Yup! Good ole' Pascal :)

At first, the generated .pas file looked something like this:
program RunSmallVM;

procedure RunSmallVMCodeBase64(A:PChar;B:PChar);
stdcall ;external 'smallvm.dll';
begin
RunSmallVMCodeBase64('','2e6d657468');
end.
This is good as long as your encoded program is small. Once it gets a little large you find out that traditional Pascal string can't go more than 255 characters. What?

Ok, you can add a compiler directive to make the language use another type of string (AnsiStrings), but string literals maintain the 255 character limit. Sigh :(

No problem: I made the code generator make a series of string concatenations to form the final program form. This would slow down the time from loading the .exe to running the program, but it now works. I can speed things up later, by embedding binary blobs in the .exe or something.

Also I had trouble with base64 encoding of programs: Parts of the SmallVM assembly program are themselves encoded in base64. It seems the encoding in this case messes up. This is what happened with me:

programHeader = encode64(stuff)
program = programHeader + restOfCode
stringToSend = encode64(program)

originalProgram = decode64(stringToSend)

To my surprise, the string originalProgram is not equal to programHeader+restOfCode as expectd, but instead it is equal to stuff+restOfCode. It seems the base64 decoder is too eager to decode anything that seems like base64 characters :(

As a hack I used different functions in the .DLL to send different parts of the program. I'll figure out a proper solution later.

There is a lesson to be learned here: It pays to diversify your knowledge! In order to create an actual useful product, I went through a journey of old and new technology: VB, py2exe, MinGW, Go, and even Pascal. You don't know what knowledge will finally solve the problem.

So it's buggy, it's hacky, it's unstable, but it's there! Kalimat can now generate .exe files! And with a few iterations I hope it works well enough for day to day usage.