الخميس، 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 :(