Showing posts with label metaprogramming. Show all posts
Showing posts with label metaprogramming. Show all posts

Friday, 22 October 2010

Metaprogramming Ruby

I explained here (in greek) a bit about metaprogramming and here about an application in a python/c++ finite element code but this book is everything you need about metaprogramming in Ruby. Written by Paolo Perrotta in a way that two colleagues discuss and solve every day problems. The book is full of example code and assumes some basic knowledge of the Ruby language. The first part of the book deals with general metaprogramming concepts in Ruby from objects and methods to blocks and classes to finally code that writes code. The second part dives in the metaprogramming techniques of Rails, with ActiveRecods being the most important part. The last chapter deals how to use metaprogramming safely. As Yukihiro "matz" Matsumoto says in the foreword "It's a bit like magic", but be ware because there is white magic and black magic.

Wednesday, 6 October 2010

Metaprogramming and finite elements

The idea of metaprogramming is great; you write code that writes code that writes code... If I am not wrong the first language that introduced this concept was LISP (at the moment I am learning the Scheme dialect) So, the idea behind metaprogramming is to write a program in a high level language which will then create source code in a lower language (or even the same) in order to automate some procedure. From Lisp many languages have been influenced, such as Ruby and python, that also incorporate metaprogramming aspects.

One day someone said why don't we apply this concept to finite element code as well. Finite element code can be really annoying (as every code is) so by writing some code that writes finite element code would make our calculations easier. The result of this idea is the FEnics project. Thank you for the great job guys!!! Finite elements are usually used to solve partial differential equations. So find a PDE you like and just solve it.

The program can be easily installed in Ubuntu by writing
sudo apt-get install fenics
Of course there are many other packages for many distributions. Have fun!

Tuesday, 15 September 2009

Μεταπρογραμματισμός

Μεταγλώσσα, μεταλογική και μεταπρογραμματισμός


Ήθελα να προγραμματίσω στην python την εντολή struct του matlab, η οποία λειτουργεί ως εξής:

a = struct('x', 1, 'y', [1,2], 'z', zeros(2))


και δημιουρργεί μια τέτοια δομή:

a =


x: 1

y: [1 2]

z: [2x2 double]


και μετά είναι δυνατό να γράψουμε:

a.id = 1


Μετά από μια χρονοβόρα αναζήτηση σε εγχειρίδια της python, σε online documentation και στο διαδίκτυο γενικότερα, οδηγήθηκα στη λέξη μεταπρογραμματισμός, και συνειρμικά φυσικά θυμήθηκα τις λέξεις μεταγλώσσα και μεταλογική. Στη γλώσσα χρησιμοποιούμε προτάσεις 'ένα συν ένα κάνει δύο' αλλά και 'η πρόταση "ένα συν ένα κάνει δύο" είναι αληθής'. Στην πραγματικότητα οι δύο αυτοί τύποι προτάσεων ανήκουν σε δύο διαφορετικές γλώσσες. Η μία είναι η γλώσσα που περιέχει ένα σύνολο προτάσεων---δυνατών συντακτικά και γραμματικά---και η μεταγλώσσα, η οποία περιέχει τις προτάσεις που εκφέρουν την αλήθεια ή ψευδος των προτάσεων της γλώσσας. Η μεταγλώσσα είναι κι αυτή φυσικά μια γλώσσα, η οποία στη συγκεκριμένη περίπτωση χρησιμοποιεί τα ίδια σύμβολα με τη γλώσσα. Στο παραπάνω παράδειγμα η πρόταση 'ένα συν ένα κάνει δύο' ανήκει στη γλώσσα, ενώ η 'η πρόταση "ένα συν ένα κάνει δύο" είναι αληθής' ανήκει στη μεταγλώσσα.

Μια μεταγλώσσα με τη σειρά της μπορεί να έχει μια τρίτη μεταγλώσσα η οποία περιγραφει την αλήθεια των προτάσεώ της. Για παράδειγμα <η πρόταση 'η πρόταση "ένα συν ένα κάνει δύο" είναι αληθής' είναι αληθής> ανήκει στη μεταγλώσσα της μεταγλώσσας.

Ο κλάδος της λογικής (ή των μαθηματικών) που ασχολείται με αυτά τα προβλήματα είναι η μεταλογική (ή τα μεταμαθηματικά).


Κατ' αναλογία, υπάρχει κάτι αντίστοιχο και στον προγραμματισμό. Όχι με έκφραση αλήθειας βέβαια. Στον αντικειμενοστραφή προγραμματισμό είναι γνωστές οι έννοιες της κληρονομικότητας (inheritance), πολυμορφίας (polymorphism), encapsulation (δε γνωρίζω την ελληνική έννοια), κ.α. Δημιουργούμε classes, υπερφορτώνουμε τελεστές, κρύβουμε δεδομένα, αλλά οι δυνατότητες της python επεκτείνονται και στον μεταπρογραμματισμό.


def struct(*args):

class mystruct: pass

for i in range(len(args)/2):

setattr(mystruct, args[2*i], args[2*i+1])

return mystruct