A question of reliability – 2.0

Lately I had an interesting debate with a friend of mine, he works for a small company which works in the IT field. For obvious reason I will not write the name of the company nor who my friend is, anyway, what they do is the classical technical service and maintenance, servers machine deployment, system administration and so on. These days everyone needs some kind of IT Infrastructure, everyone needs an ERP system, etc etc; obviously they have quite a lot of work with small/medium business companies. Close to every machine they sell (except the classic desktop PCs) have a RAID; obviously I second that having a RAID is a must-have in every machine used for enterprise tasks but I also argue that there is RAID and RAID. Many of the HDDs they use are going into NAS or low level server machine, to keep the price low and don’t have to add an external RAID controller the HDDs need to be SATA. Nothing wrong with using SATA drives, but similarly as what can be said for RAIDs we can say that there is disk and disk. For instance, if looking at the Western Digital HDDs lineup, it’s clear that they have two completely different series: the desktop one and the enterprise one. These drives have different firmwares, different warranty policies and different error handling capabilities; so, another time, if the drive is going to be part of a RAID it’s very advisable to pick one of the enterprise series (Red, RAID edition or Velociraptor) because they are meant to be capable of working non-stop 24/7, they have 5 years warranty, they have a better error handling and on top they support TLER. So, how people who are supposed to know what they are doing sell NAS with Caviar Blue HDDs instead of Caviar Red/RE? Why they don’t know what TLER is? Why they keep using RAID-5 when everyone know it’s crap? My friend said they have more than 250 machines build with consumer desktop drives configured in RAID and so far they are having no problem; what they are failing to realize is that sooner or later the crap they are selling will explode under their chairs. The whole internet is full of people complaining about consumer disks with not TREL support being dropped from RAIDs, same can be said for RAID-5 in which a second disk fails during the array rebuilding. They remind me of the retards which use 4 HDDs in RAID-1 thinking that this way their data are safe, probably they don’t know that RAID is not a backup and that a broken controller or a much simpler file system corruption could possibly make their 4 disks mirror completely useless. Well, guess sooner or later they will learn it the hard way… …

Posted on

Pi-greco - Chudnovsky :: C

While being a big fan of Python I am also in the continuous pursuit of better performance. Like it or not, but when seeking for better performance in many cases you have to put away high level framework/programming-languages and go back to basic; many times basic means good old C. After having developed some different Python scripts to calculate the Pi-greco up to a given number of digits (later I will post the most advanced version I came up with) I decided it was time to make a step forward and write it in C. Because I am still away from home this time the test machine instead of being the usual FX-8120 is my Lenovo Thinkpad equipped with a 2.1 GHz 2C4T 3 MB of L3 cache i3-2310M. On Windows the most known C (it also has a C++ wrapper) library for arbitrary precision math is MPIR. One of the most important “features” of the library is that it is written in both C and Assembly, they also provide different versions of the Assembly code, so you can use the one that is specifically written for the CPU architecture of the machine you will run your program on. The full source code of the library can be downloaded, modified and compiled by the user to better suit his needs. The only downside is that the super optimized Assembly code included in the latest stable release of the library ins’t really up to date, in fact there still is no support for Sandy Bridge and AMD Bulldozer CPUs. Anyway I think no one should complain about it, MPIR is an open source project so we can wait for an official release with the added features or get our hands dirty and write the code we need by ourselves. Since I am lazy I will wait for the official team to realease the updated Assembly; since then I can live with the current stuff. Talking about “current stuff” I have noticed that AMD K10 library’s version is much faster than P4, C2D and Nehalem ones even if I am using an Intel Sandy Bridge CPU; and I am not talking about 2-3% faster but 15-20%. Anyway, while the Python - I have the feeling that there still something left here and there, tho I don’t think I can make it as fast as the C implementation is - script (which uses GMPY2) requires 30+ seconds to compute one million digits the program written in C takes only 1.4 seconds to complete the same task. Both programs use the same algorithm: Chudnovsky brothers formula …

Posted on

Execution time :: C & Win

Once in a while I happen to have to know how much time a certain function needs to be executed. While many programming languages have built in functions (time in Python, System.nanoTime() in Java and so on) to get this information, C doesn’t appear to have one. There is timec.h but it appears to be quite inaccurate, so if extremely high accuracy is needed we have to rely on something else; the downside is that these much more reliable libraries are not platform independent. For Windows platform Microsoft suggests to use QueryPerformanceFrequency method and so I did. Digging through Google I found a post in which a guy posted some C++ code he used to query system time, I’ve adapted it and used it in my program. For future reference I’m going to post it here. …

Posted on

Nerd gonna nerd

Today officially started my summer holiday, 5 days ago I had my last exam at university and now finally I’ve the time to do all the things I can’t find the time to do the rest of the year. The majority of the people on earth awaits the summer holiday to completely stop doing what they do the entire year, pack things up and go away from home for as much as they can. On the contrary, I like what I do the entire year and I don’t feel the need of doing something else; I just want to have the time to play with computer in a different way of what I have to do for university. While everyone is thinking about sea, mountain, sports, etc etc I think what kind of program I may develop, what system I would like to build and so on. This year my summer holiday is two weeks in Livigno; as usual I could not resist to get with me my laptop (actually there are two, mine and my father one’s) and a bunch of other hi-tech stuff. So, this is what my setup will look like for the next two weeks: …

Posted on

Major upgrade

OK, not exactly an upgrade, but still an improvement over my precedent configuration. You have to know that I used for a long time – years – a dual monitor configuration, something like 6 months ago one of the two monitors went nut and I had to replace it. Obviously it was already discontinued by some time so I had to replace both my monitors, I decided to buy a single Dell U2412M (here is the post I wrote). Nothing to complain about the monitor itself, also, I’ve no problem to say that so far it is one of the best purchases I ever did; the problem is that one monitor can be as big as you want but two smaller monitor are always better than one. Explain why two smaller monitors are always better than a bigger one isn’t simple at all, but trust me, everyone who have used at least one time a multiple monitor configuration will be of the same thinking. So today, after had used for 6 months or so a single monitor I bought another one; Below you can see a photo. …

Posted on

My daily dose of anti-apple

Today I had the bad luck to have to deal with a damn Macbook Pro on which I need to install a retarded software (IBM Rational Software Architect). Despite RSA becoming one of the worst piece of crapware I’ve ever seen this time it wasn’t the problem. The Macbook was one of the latest model, a 13″ one equipped with a Core i5 Sandy Bridge CPU and an awesome a certain amount of GB 5400 RPM hard disk drive…everything sold at the fair price of 1200 or so €. As long as you use it just for what close to every Apple user use it – going on Facebook, synchronizing music tracks on an IPod and iCloud – everything is fine (tho, you can do these things with every PC/MAC/whatever-you-want not older than 6 or 7 years). Anyway, if you happen to be one of those bunch of unlucky people on earth who pretend to use it for something different you will quickly face with the complete inability of this, I repeat, 1200 € worth machine, to provide, at least decent performances. Starting from the point that many programs (like RSA) don’t exist for OSX you have to virtualize a Windows or Linux OS, and here comes the problem. The 5400 RPM HDD is so crappy that when you fire up the virtual machine everything slow down to a level similar to a 10 years old PC with a P4 Willamette and a 20 GB PATA HDD. I really don’t get why people keep buying those completely overpriced piece of garbage apple is selling; this Macbook Pro hardware wise is very similar to my Thinkpad but it costs much more (something like +80/90% over the price of the Thinkpad), is way slower, have an higher weight and miss some important features (like external battery, UMTS module, matte display, trackpad, etc etc). Keep up the good work apple… …

Posted on

Clausify :: Prolog

For the same exam in which I had to do the LISP project I also had to write a Clausify program in Prolog. Long story short: the goal was to translate a well formed form (WFF or FBF in italian) of a first order logic language in a conjunctive normal form (CNF). congiunzione(and). disgiunzione(or). negazione(not). implicazione(implies). esiste(exist). per_ogni(every). fbf2cnf(FBF, CNFFBF):- atomic(FBF), CNFFBF = FBF, !. fbf2cnf(FBF, CNFFBF):- FBF =.. [X|ListaArgs], semplifica([X|ListaArgs], [], CNFFBF_3_p), semplifica_2(CNFFBF_3_p, [], CNFFBF_4_p), controlla_lista(CNFFBF_4_p, [], CNFFBF_2_p), CNFFBF_2_p =.. [M|Ms], controlla_lista([M|Ms], [], CNFFBF_2), CNFFBF_2 =.. [Y|Ys], semplifica([Y|Ys], [], CNFFBF_3), semplifica_2(CNFFBF_3, [], CNFFBF_4), CNFFBF_R_P =.. CNFFBF_4, not(confronta(CNFFBF_2_p, CNFFBF_2)), fbf2cnf(CNFFBF_R_P, CNFFBF), !. fbf2cnf(FBF, CNFFBF):- FBF =.. [X|ListaArgs], semplifica([X|ListaArgs], [], CNFFBF_3_p), semplifica_2(CNFFBF_3_p, [], CNFFBF_4_p), controlla_lista(CNFFBF_4_p, [], CNFFBF_2_p), CNFFBF_2_p =.. [M|Ms], controlla_lista([M|Ms], [], CNFFBF_2), CNFFBF_2 =.. [Y|Ys], semplifica([Y|Ys], [], CNFFBF_3), semplifica_2(CNFFBF_3, [], CNFFBF_4), CNFFBF =.. CNFFBF_4, !. confronta(CNFFBF, CNFFBF). semplifica([], CNFFBF_R, CNFFBF_R). semplifica([Y|Ys], CNFFBF, CNFFBF_R):- var(Y), append(CNFFBF, [Y], CNFFBF_1), semplifica(Ys, CNFFBF_1, CNFFBF_R), !. semplifica([Y|Ys], CNFFBF, CNFFBF_R):- congiunzione(Y), togli_and(Ys, CNFFBF, CNFFBF_1), append([Y], CNFFBF_1, CNFFBF_R). semplifica([Y|Ys], CNFFBF, CNFFBF_R):- compound(Y), Y =.. [Z|Zs], semplifica([Z|Zs], [], CNFFBF_1), semplifica(Ys, [], CNFFBF_2), R1 =.. CNFFBF_1, append(CNFFBF, [R1], CNFFBF_3), append(CNFFBF_3, CNFFBF_2, CNFFBF_R), !. semplifica([Y|Ys], CNFFBF, CNFFBF_R):- append(CNFFBF, [Y], CNFFBF_1), semplifica(Ys, [], CNFFBF_2), append(CNFFBF_1, CNFFBF_2, CNFFBF_R), !. semplifica_2([], CNFFBF_R, CNFFBF_R). semplifica_2([Y|Ys], CNFFBF, CNFFBF_R):- var(Y), append(CNFFBF, [Y], CNFFBF_1), semplifica_2(Ys, CNFFBF_1, CNFFBF_R), !. semplifica_2([Y|Ys], CNFFBF, CNFFBF_R):- disgiunzione(Y), togli_or(Ys, CNFFBF, CNFFBF_1), append([Y], CNFFBF_1, CNFFBF_R). semplifica_2([Y|Ys], CNFFBF, CNFFBF_R):- compound(Y), Y =.. [Z|Zs], semplifica_2([Z|Zs], [], CNFFBF_1), semplifica_2(Ys, [], CNFFBF_2), R1 =.. CNFFBF_1, append(CNFFBF, [R1], CNFFBF_3), append(CNFFBF_3, CNFFBF_2, CNFFBF_R), !. semplifica_2([Y|Ys], CNFFBF, CNFFBF_R):- append(CNFFBF, [Y], CNFFBF_1), semplifica_2(Ys, [], CNFFBF_2), append(CNFFBF_1, CNFFBF_2, CNFFBF_R), !. togli_and([], CNFFBF_R, CNFFBF_R). togli_and([Y|Ys], CNFFBF, CNFFBF_R):- Y =.. [Z|Zs], congiunzione(Z), togli_and(Zs, CNFFBF, CNFFBF_1), togli_and(Ys, CNFFBF_1, CNFFBF_R), !. togli_and([Y|Ys], CNFFBF, CNFFBF_R):- append(CNFFBF, [Y], CNFFBF_1), togli_and(Ys, CNFFBF_1, CNFFBF_R), !. togli_or([], CNFFBF_R, CNFFBF_R). togli_or([Y|Ys], CNFFBF, CNFFBF_R):- Y =.. [Z|Zs], disgiunzione(Z), togli_or(Zs, CNFFBF, CNFFBF_1), togli_or(Ys, CNFFBF_1, CNFFBF_R), !. togli_or([Y|Ys], CNFFBF, CNFFBF_R):- append(CNFFBF, [Y], CNFFBF_1), togli_or(Ys, CNFFBF_1, CNFFBF_R), !. %% lista vuota controlla_lista([], CNFFBF_R, CNFFBF_R). %% trova and controlla_lista([X|ListaArgs], CNFFBF, CNFFBF_R):- congiunzione(X), count_elements(ListaArgs, N), N >= 2, costruisci_and(ListaArgs, CNFFBF, CNFFBF_1), CNFFBF_R =.. [X|CNFFBF_1], !. %% controllare se or (... and) controlla_lista([X|ListaArgs], CNFFBF, CNFFBF_R):- disgiunzione(X), count_elements(ListaArgs, N), N >= 2, cerca_and(ListaArgs), estrai_parametri(ListaArgs, [], Lista_AND), costruisci_or_and(Lista_AND, [], CNFFBF_R1), append(CNFFBF, CNFFBF_R1, CNFFBF_R2), CNFFBF_R =.. ['and'|CNFFBF_R2], !. %% trova or controlla_lista([X|ListaArgs], CNFFBF, CNFFBF_R):- disgiunzione(X), count_elements(ListaArgs, N), N >= 2, costruisci_or(ListaArgs, [], CNFFBF_1), append(CNFFBF, CNFFBF_1, CNFFBF_2), CNFFBF_R =.. [X|CNFFBF_2], !. %% trova not di not controlla_lista([X,X1|ListaArgs], CNFFBF, CNFFBF_R):- negazione(X), X1 =.. [Y,Y1|Ys], count_elements(Ys, M), M = 0, count_elements(ListaArgs, N), N = 0, negazione(Y), costruisci_not(Y1, CNFFBF, CNFFBF_R), !. %% trova not di atomo controlla_lista([X,X1|ListaArgs], CNFFBF, CNFFBF_R):- negazione(X), count_elements(ListaArgs, N), N = 0, atomic(X1), CNFFBF_1 =.. [X|[X1]], append(CNFFBF, CNFFBF_1, CNFFBF_R), !. %% CASO #1 : not (and (p, q)) = or (not (p), not (q)) controlla_lista([X,X1|ListaArgs], CNFFBF, CNFFBF_R):- negazione(X), count_elements(ListaArgs, N), N = 0, compound(X1), X1 =.. [Y|Ys], congiunzione(Y), costruisci_not_and(Ys, CNFFBF, CNFFBF_1), CNFFBF_R =.. ['or'|CNFFBF_1], !. %% CASO #2 : not (or (p, q)) = and (not (p), not (q)) controlla_lista([X,X1|ListaArgs], CNFFBF, CNFFBF_R):- negazione(X), count_elements(ListaArgs, N), N = 0, compound(X1), X1 =.. [Y|Ys], disgiunzione(Y), costruisci_not_or(Ys, CNFFBF, CNFFBF_1), CNFFBF_R =.. ['and'|CNFFBF_1], !. %% not(every) # not(exist(X, p(X))) = every(X, not(P(X))) controlla_lista([X,X1|ListaArgs], CNFFBF, CNFFBF_R):- negazione(X), count_elements(ListaArgs, N), N = 0, X1 =.. [Y,V,F|Ys], count_elements(Ys, M), M = 0, esiste(Y), CNFFBF_F =.. ['not'|[F]], append([V], [CNFFBF_F], CNFFBF_1), CNFFBF_2 =.. ['every'|CNFFBF_1], CNFFBF_2 =.. [Z|Zs], controlla_lista([Z|Zs], [], CNFFBF_R), !. %% not(every) # not(every(X, p(X))) = exist(X, not(P(X))) controlla_lista([X,X1|ListaArgs], CNFFBF, CNFFBF_R):- negazione(X), count_elements(ListaArgs, N), N = 0, X1 =.. [Y,V,F|Ys], count_elements(Ys, M), M = 0, per_ogni(Y), CNFFBF_F =.. ['not'|[F]], append([V], [CNFFBF_F], CNFFBF_1), CNFFBF_2 =.. ['exist'|CNFFBF_1], CNFFBF_2 =.. [Z|Zs], controlla_lista([Z|Zs], [], CNFFBF_R), !. %% trova un implies # implies(P, Q) = or(not(P), Q) controlla_lista([X,P,Q|ListaArgs], CNFFBF, CNFFBF_R):- implicazione(X), count_elements(ListaArgs, N), N = 0, costruisci_implicazione(P, Q, CNFFBF_R), !. %% trova un exist # exist(Y, every(Y,p(X,Y))) = every(Y, p(sf666(Y), Y)) controlla_lista([X,V,F|ListaArgs], CNFFBF, CNFFBF_R):- esiste(X), count_elements(ListaArgs, N), N = 0, var(V), F =.. [Y,V1,F1|Ys], count_elements(Ys, M), M = 0, per_ogni(Y), skolem_function([V],SF). %% trova un exist # exist(X, p(X)) = p(skv+numero) controlla_lista([X,V,F|ListaArgs], CNFFBF, CNFFBF_R):- esiste(X), count_elements(ListaArgs, N), N = 0, var(V), skolem_function(SK, V), F =.. [Y|Ys], controlla_lista([Y|Ys], [], CNFFBF_R), !. %% trova un every # every(X, p(X)) = p(skv+numero) controlla_lista([X,V,F|ListaArgs], CNFFBF, CNFFBF_R):- per_ogni(X), count_elements(ListaArgs, N), N = 0, var(V), skolem_function(SK, V), F =.. [Y|Ys], controlla_lista([Y|Ys], [], CNFFBF_R), !. %% trova un predicato controlla_lista([X|Xs], CNFFBF, CNFFBF_R):- count_elements(Xs, N), N >= 1, controlla_predicato([X|Xs], CNFFBF, [Y|Ys]), CNFFBF_R =.. [Y|Ys], !. %% trovato un predicato, controlla i suoi elementi controlla_predicato([], CNFFBF_R, CNFFBF_R). controlla_predicato([X|Xs], CNFFBF, CNFFBF_R):- atomic(X), append(CNFFBF, [X], CNFFBF_1), controlla_predicato(Xs, CNFFBF_1, CNFFBF_R), !. controlla_predicato([X|Xs], CNFFBF, CNFFBF_R):- compound(X), X =.. [Y|Ys], controlla_lista([Y|Ys], [], CNFFBF_1), append(CNFFBF, [CNFFBF_1], CNFFBF_2), controlla_predicato(Xs, CNFFBF_2, CNFFBF_R), !. %% trova un and, nel caso gli elementi siano atomi fa l'append a CNFFBF_R %% , altrimenti richiama controlla_lista %% elementi, lista_a_cui_aggiungere, lista_risultato costruisci_and([], CNFFBF_R, CNFFBF_R). costruisci_and([X|Xs], CNFFBF, CNFFBF_R):- atomic(X), append(CNFFBF, [X], CNFFBF_1), costruisci_and(Xs, CNFFBF_1, CNFFBF_R), !. costruisci_and([X|Xs], CNFFBF, CNFFBF_R):- compound(X), X =.. [Y|Ys], controlla_lista([Y|Ys], [], CNFFBF_R1), append(CNFFBF, [CNFFBF_R1], CNFFBF_R2), costruisci_and(Xs, CNFFBF_R2, CNFFBF_R), !. %% trova un or costruisci_or([], CNFFBF_R, CNFFBF_R). costruisci_or([X|Xs], CNFFBF, CNFFBF_R):- atomic(X), append(CNFFBF, [X], CNFFBF_1), costruisci_or(Xs, CNFFBF_1, CNFFBF_R), !. costruisci_or([X|Xs], CNFFBF, CNFFBF_R):- compound(X), X =.. [Y|Ys], controlla_lista([Y|Ys], [], CNFFBF_R1), costruisci_or(Xs, [], CNFFBF_R2), append(CNFFBF, [CNFFBF_R1], CNFFBF_2), append(CNFFBF_2, CNFFBF_R2, CNFFBF_R3), CNFFBF_R3 =.. [Z|Zs], controlla_lista([Z|Zs], [], CNFFBF_R), !. %% trovo un or con dentro almeno un and, allora costruisco le coppie %% Lista_parametri_da_accoppiare, CNFFBF_parziale, CNFFBF_finale costruisci_or_and([], CNFFBF_R, CNFFBF_R). costruisci_or_and([X|Xs], CNFFBF, CNFFBF_R):- accoppia(X, Xs, CNFFBF, CNFFBF_R1), costruisci_or_and(Xs, CNFFBF_R1, CNFFBF_R), !. accoppia(Elem, [], CNFFBF_R, CNFFBF_R). accoppia([], _, CNFFBF_R, CNFFBF_R). accoppia([E|Es], [[Y|Ys]|Xs], CNFFBF, CNFFBF_R):- accoppia_and(E, [Y|Ys], CNFFBF, CNFFBF_1), accoppia_and(E, Xs, CNFFBF_1, CNFFBF_2), accoppia(Es, [[Y|Ys]|Xs], CNFFBF_2, CNFFBF_R), !. accoppia([E|Es], [Y|Ys], CNFFBF, CNFFBF_R):- accoppia_and(E, [Y|Ys], CNFFBF, CNFFBF_1), elementi_lista(Es, [Y|Ys], CNFFBF_1, CNFFBF_R). accoppia(Elem, [[Y|Ys]|Xs], CNFFBF, CNFFBF_R):- append([Elem], [Y], CNFFBF_1), CNFFBF_2 =.. ['or'|CNFFBF_1], append(CNFFBF, [CNFFBF_2], CNFFBF_3), accoppia(Elem, [Ys|Xs], CNFFBF_3, CNFFBF_R), !. accoppia(Elem, [X|Xs], CNFFBF, CNFFBF_R):- accoppia(Elem, Xs, CNFFBF, CNFFBF_R), !. accoppia_and(_, [], CNFFBF_R, CNFFBF_R). accoppia_and(Elem, [[Y|Ys]|Xs], CNFFBF, CNFFBF_R):- accoppia_and(Elem, [Y|Ys], CNFFBF, CNFFBF_1), accoppia_and(Elem, Xs, CNFFBF_1, CNFFBF_R). accoppia_and(Elem, [Y|Ys], CNFFBF, CNFFBF_R):- append([Elem], [Y], CNFFBF_1), CNFFBF_2 =.. ['or'|CNFFBF_1], append(CNFFBF, [CNFFBF_2], CNFFBF_3), accoppia_and(Elem, Ys, CNFFBF_3, CNFFBF_R), !. elementi_lista([], _, CNFFBF_R, CNFFBF_R). elementi_lista([E|Es], [Y|Ys], CNFFBF, CNFFBF_R):- accoppia_and(E, [Y|Ys], CNFFBF, CNFFBF_1), elementi_lista(Es, [Y|Ys], CNFFBF_1, CNFFBF_R). %% estrae i parametri dell'or estrai_parametri([], Lista_R, Lista_R). estrai_parametri([X|Xs], Lista_1, Lista_R):- atomic(X), append(Lista_1, [X], Lista_2), estrai_parametri(Xs, Lista_2, Lista_R), !. estrai_parametri([X|Xs], Lista_1, Lista_R):- compound(X), X =.. [Y|Ys], congiunzione(Y), controlla_lista([Y|Ys], [], Lista_X), Lista_X =.. [Z|Zs], estrai_parametri(Xs, [], Lista_Xs), append(Lista_1, [Zs], Lista_R1), append(Lista_R1, Lista_Xs, Lista_R), !. estrai_parametri([X|Xs], Lista_1, Lista_R):- compound(X), X =.. [Y|Ys], controlla_lista([Y|Ys], [], Lista_X), estrai_parametri(Xs, [], Lista_Xs), append(Lista_1, [Lista_X], Lista_R1), append(Lista_R1, Lista_Xs, Lista_R), !. %% cerca se c'è un and cerca_and([X|Xs]):- atomic(X), cerca_and(Xs), !. cerca_and([X|Xs]):- compound(X), X =.. [Y|Ys], count_elements(Ys, N), N >= 2, congiunzione(Y), !. cerca_and([X|Xs]):- compound(X), X =.. [Y|Ys], cerca_and(Xs), !. %% not è stato trovato, guarda cosa ha come parametro costruisci_not(Y1, CNFFBF, CNFFBF_R):- atomic(Y1), append(CNFFBF, Y1, CNFFBF_R), !. costruisci_not(Y1, CNFFBF, CNFFBF_R):- compound(Y1), Y1 =.. [Z|Zs], controlla_lista([Z|Zs], CNFFBF, CNFFBF_R), !. costruisci_not(Y1, CNFFBF, CNFFBF_R):- Y1 =.. [X|Xs], negazione(X), controlla_lista([X|Xs], CNFFBF, CNFFBF_R), !. %% trova un not che ha come parametro un and costruisci_not_and([], CNFFBF_R, CNFFBF_R). costruisci_not_and([X|Xs], CNFFBF, CNFFBF_R):- atomic(X), CNFFBF_1 =.. ['not'|[X]], append(CNFFBF, [CNFFBF_1], CNFFBF_2), costruisci_not_and(Xs, CNFFBF_2, CNFFBF_R), !. costruisci_not_and([X|Xs], CNFFBF, CNFFBF_R):- compound(X), X =.. [Y|Ys], controlla_lista([Y|Ys], CNFFBF_1, CNFFBF_2), CNFFBF_3 =.. ['not'|[CNFFBF_2]], append(CNFFBF, [CNFFBF_3], CNFFBF_4), costruisci_not_and(Xs, CNFFBF_4, CNFFBF_R), !. %% trova un not che ha come parametro un or costruisci_not_or([], CNFFBF_R, CNFFBF_R). costruisci_not_or([X|Xs], CNFFBF, CNFFBF_R):- atomic(X), CNFFBF_1 =.. ['not'|[X]], append(CNFFBF, [CNFFBF_1], CNFFBF_2), costruisci_not_or(Xs, CNFFBF_2, CNFFBF_R), !. costruisci_not_or([X|Xs], CNFFBF, CNFFBF_R):- compound(X), X =.. [Y|Ys], controlla_lista([Y|Ys], CNFFBF_1, CNFFBF_2), CNFFBF_3 =.. ['not'|[CNFFBF_2]], append(CNFFBF, [CNFFBF_3], CNFFBF_4), costruisci_not_or(Xs, CNFFBF_4, CNFFBF_R), !. %% trova un implies # implies(P, Q) = or(not(P), Q) costruisci_implicazione(P, Q, CNFFBF_R):- atomic(P), atomic(Q), CNFFBF_P =.. ['not'|[P]], append([CNFFBF_P], [Q], CNFFBF_1), CNFFBF_R =.. ['or'|CNFFBF_1]. costruisci_implicazione(P, Q, CNFFBF_R):- atomic(P), compound(Q), CNFFBF_P =.. ['not'|[P]], Q =.. [Y|Ys], controlla_lista([Y|Ys], [], CNFFBF_Q_1), append([CNFFBF_P], [CNFFBF_Q_1], CNFFBF_1), CNFFBF_R =.. ['or'|CNFFBF_1]. costruisci_implicazione(P, Q, CNFFBF_R):- compound(P), atomic(Q), CNFFBF_P =.. ['not'|[P]], CNFFBF_P =.. [X|Xs], controlla_lista([X|Xs], [], CNFFBF_P_1), append([CNFFBF_P_1], [Q], CNFFBF_1), CNFFBF_R =.. ['or'|CNFFBF_1]. costruisci_implicazione(P, Q, CNFFBF_R):- CNFFBF_P =.. ['not'|[P]], CNFFBF_P =.. [X|Xs], controlla_lista([X|Xs], [], CNFFBF_P_1), Q =.. [Y|Ys], controlla_lista([Y|Ys], [], CNFFBF_Q_1), append([CNFFBF_P_1], [CNFFBF_Q_1], CNFFBF_1), CNFFBF_R =.. ['or'|CNFFBF_1]. skolem_variable(V, SK):- var(V), gensym(skv, SK). skolem_function([], SF):- skolem_variable(_, SF). skolem_function([A | ARGS], SF) :- gensym(skf, SF_op), SF =.. [SF_op, A | ARGS]. %% conta gli elementi della lista count_elements([],0). count_elements([X|Xs],N):- count_elements(Xs,M), N is M + 1. %%%% controlla se la FBF è clausola di Horn horn(FBF):- atomic(FBF). horn(FBF):- fbf2cnf(FBF, CNF), CNF =.. [X|Xs], negazione(X). horn(FBF):- fbf2cnf(FBF, CNF), CNF =.. [X|Xs], conta(Xs, 0, N), N < 2. %%%% conta i letterali non negativi conta([], N, N). conta([X|Xs], N, N):- negazione(X). conta([X|Xs], N1, N):- compound(X), X =.. [Y|Ys], negazione(Y), conta(Xs, 0, M), N is N1 + M, !. conta([X|Xs], N1, N):- compound(X), X =.. [Y|Ys], conta(Ys, 0, M0), conta(Xs, 0, M1), M2 is M0 + N1, N is M2 + M1, !. conta([X|Xs], N1, N):- atomic(X), M1 is N1 + 1, conta(Xs, 0, M), N is M1 + M, !. confronta_liste([],[]). confronta_liste([X|Xs],[X|Ys]):- confronta_liste(Xs, Ys), !. I think that’s all. Cheers. …

Posted on

My new love: Python

Past Friday’s afternoon, around 3 pm, I was at university, specifically I was in one of the libraries and I was reading a book titled Concurrency; it’s about engineering concurrent systems using the modelling software LTSA and then write the actual program in Java. While reading I was also talking with two friends of mine about a problem another friend found on a book; to make a long story short, the problem was about balancing a predefined non-balanced random function. One of these two friends showed us how he solved the problem, the algorithm he wrote was written in Python. When some hours later I was back home I did thought it was finally the time to install a Python interpreter and start playing with it. So I did it. I got myself the latest release of Python interpreter and the Pydev plugin for Eclipse IDE, installation and configuration on Windows is incredibly straight forward, just a matter of pressing install and next 2 or 3 times. I don’t have any Python book then as first programming guide I used the well known (at least here in Italy) html.it site, if you are not Italian just google Python and you will find a huge amount of interesting PDFs and guides of any kind. After had digged trough the html.it Python guide in a bunch of hours (guess not more than 2 or 3) I felt myself just like the guy in this comics: …

Posted on