The Lowly Mathematician

May 18, 2007

Programming Languages Part I: Syntactic Similarity

Filed under: FP, Functional, Imperative, Programming — jfredett @ 4:09 am
I like languages. when I was younger, I remember reading The Hobbit and spending more time reading the first two preface pages about the Moon Runes, A gussied up version of the Futhark, than actually reading the book. For a good bit of time after that, I wanted to be a Linguist, and not a mathematician.

But Alas, over time my interests went from Natural Language to Formal, from Formal Language to Abstract Language, and from there to the wide world of Algebra and Logic. A good transition, I think. Nevertheless I still love Languages, and that love has now been turned to specifically Programming languages. I like these languages because they are first and foremost utilitarian. They are designed from the start to do one thing, get a point across to an idiot. Lets face it, Programmers and Computers alike are pretty damn dumb. The language is the smart thing. A Programmer has no capability to tell a computer what to do without the help of a good language, and a Computer can’t do anything it isn’t told to do. So the most fundamental piece of Computer Technology is that of the Language, the layer that binds Programmer with Programmee.

I love languages, but I often hate them too. Take, for instance, Java. Java is an exceptionally pretty language, but it’s also ugly as your Aunt Anita Maykovar (May - ko - var). Java effectively boils down to one syntactic structure, the class. Every Java file revolves around it, and in some ways, this is really good. The fundamental commonality this structure brings allows Java code to be easier to learn to read. Your brain is good at processing things that are similar, the pathways it has to draw are all about the same– so it’s easier to optimize up there. The issue I have with Java actually is that, sometimes its too good at looking the same. To the point where I forget where I am. I get lost in my own neural pathways while I try to figure out whether I’m looking at an Abstract class or an Interface, or if I’m looking at a dirty hack of a databearing class or if I’m looking at something more legitimate. C/C++ is great at making this distinction, but it’s also, IMO, ugly as shit, uglier even. I like C often for its ability to compartmentalize things, but I think it takes it to far, nothing looks alike, even if it should. One of my peeves with C vs Java is they’re taking extreme views on something which should be easily decided. I’d like to sum it up as a fundamental rule I want to see in all programming languages (though that will probably never happen). Here it is:

Syntacticly similar things should be Semantically similar things, and vice versa, according to the proportion of similarity.

That is, If I want to create an object called “List” which implements a linked list, and then I want to create an interface (which is really just a Type Class, I’ve come to realize, but thats a story for another day.) called “Listable” which, when implemented, forces a object to have list-like properties. These things should have some similar structure. However, this is not to say we should copy Java. Java takes this too far, I think. In that, Java follows the rule: “If things are Semantically similar, they are Structurally almost Identictal.” This is bad, Interfaces should look different than Classes, but only in a minor way. I’d like Java Interfaces, heck, Java in general if I could specify type signatures ala Haskell. I think Haskell has got it right when it comes to how types should work syntactically. The brilliance of this comes in when you try to write a Java Method with this Type Sig ala Haskell type syntax, here’s Fibonacci:

fib :: public, static :: int -> int

fib(nth) {
(nth == 1) ? 1 : fib(nth-1) + fib(nth-2);
}

(I think that’ll work, but it’s been a while, so I might have it wrong.)

Granted, there are issues with this. Notably, Java has things like side effects, but these could be built into the type signatures. I think that the ultimate benefit of this kind of type signaturing is a separation of concerns syntactually. I think that overall, this would make the language as a whole a lot cleaner. As interfaces would no longer have stubs like:

public static int fib(int nth);

which, though nice, doesn’t carry the same amount of information that could be held in something like:

fib :: public, static :: int -> int

Syntactually, the latter structure is more extensible, it could allow for the incorporation things like side effect tracking, or thread signatures, which might look like:

fib :: public, static, threaded :: int -> (int, int, …) -> int

which says that effectively fib is a method with takes a int to a unspecified number of threads, to an integer.

I’m really just spitballing at the end here, with some neat ideas I think that a small change in Java’s syntactic structure could bring.

Just my thoughts.

PS: I don’t know if the Syntactic/Semantic Similarity rule is well known, but either way, its a damn good idea.

May 1, 2007

Haskell: The Good, Bad, and Ugliness of Types

Filed under: FP, Functional, Imperative, Programming — jfredett @ 9:14 pm
I’ve started to learn Haskell, for those who don’t know, Haskell is a wonderful little language which is based on Lazy Evaluation, Pure Functional Programming, and Type Calculus.

Effectively, this means that, like Erlang and other sister languages, If I write a function foo in Haskell, and evaluate it at line 10 in my program. Then I evaluate it again at Line 10000, or 10000000, or any other point in my code. It will– given the same input– always return the same value. Furthermore, if I write a function to generate an arbitrary list of one’s, like this:

listOfOnes = 1 : listOfOnes

Haskell just accepts it as valid. No Questions asked. Schemer’s and ML’ers of the world are probably cowering in fear. Recursive data types are scary in an Eager language, But Haskell is lazy. Where the equivalent definition in scheme:

(define list-of-ones
(cons 1 list-of-ones))

would explode and probably crash your computer, (that is, if the interpreter didn’t catch it first.) in Haskell, its not evaluated till its needed, so until I ask Haskell to start working on the listOfOnes structure, it won’t. I like Languages like that, IMO, if a language is at least as lazy as I am, its good.

The third really neat thing about Haskell, and what really drew me to it in the first place, is the Type Checker. I’ve used Scheme for a while now, and I love it to death. Sometimes, though- Scheme annoys me. For instance, I was working on a function like this once:

;count-when-true : [bool] x [num] -> num
;supposed to be a helper for filter, I want do a conditional sum. So I pass in (filter foo some-list-of-numbers) and some-list-of-numbers,
; and I should get out a sum of the elements
(define (count-when-true list-of-bools list-of-numbers)
;or-list : ([a] -> bool) x [[a]] -> bool
;applys a filter across a list of lists and ors the results
(cond [(or-list nil? (list-of-bools list-of-numbers)) 0]
[(car x) (+ (car list-of-number) (count-when-true (cdr list-of-bools) (cdr list-of-numbers)))]
[else (count-when-true (cdr list-of-bools) (cdr list-of-numbers))]))

This probably has bugs in it, doesn’t work right, etc. but the idea is to return a conditional sum, now. I want to use this on lists, thats how its defined, but sometimes the calling function would try to call it on atoms, instead of lists. Big problem? not really, pain in the ass to find, you bet. The issue was, when I was trying to figure out what was wrong, Scheme didn’t realize that the type of the inputs were wrong. This would have made the error obvious, but Scheme doesn’t care about types, thats it’s principle strength, until it starts making bugs hard to find. I HATE it when its hard to find bugs.

Lets face it, as programmers, we suck, we write lots of buggy functions, things are generally done wrong the first (two or three… thousand) times. Programming is a recursive process, we write some code, run it, check for bugs, fix bugs, run it, check, fix, etc. Until we get tired of finding bugs/the program doesn’t come up with any. IMO, languages should not be designed to force programmers to write bug-free code, which seems to be the consensus today. At least, thats what I gather from the interweb and such. The goal should be to make all bugs so blatently obvious, that when the programmer sits down and trys to debug his program, he can’t help but to smack himself in the face and proclaim, “!@#$, I missed that!” This is where Haskell Shines.

When I write Scheme, I typically don’t want to be burdened by knowing which types go where. Scheme is great at this, however, it takes things to far, I think, in that it forces you to never have types. Sure, typed schemes exist, but most of them suck, because scheme isn’t designed for types. Don’t get me wrong, typed schemes are wicked cool, I’ve used types in CL too, and they’re great, especially when you want to compile. So to solve the problem of not having types, we invented contracts, which are cool. For the unenlightened: a contract is a specification of what the given datastructure or function does in terms of its arguments. eg:

+ : num * num -> num
toASCII : string -> num
toCHAR : num ->; string

etc.

These can be read as follows:

literally:

+ is num cross num to num
etc

in english

+ is a function which takes two numbers and returns another number.

In Scheme, these contracts are basically comments, so Type checking is left to the programmer. This is all well and good, but I find it often leads to the practice of what I like to call single-typing. In which the programmer attempts to force all of his data to have the same type, or lists of the same type, or lists of lists, or etc. Typically, this results in convoluted datastructures which give FP in general a bad name. I’ve seen some horrible code written by single-typers, its bad, horrific even, It makes me want to gauge out my eyes with a pencil and tear my brain out… Okay, maybe its not that bad. Still, single-typing is most often bad. So how does Haskell fix it?

By not changing a thing.

Contracts are a wonderful Idea, they work, they just don’t work in Scheme. Because it was designed that way. Haskell has type inference, you don’t ever need to touch the Type Calculus capabilities of Haskell, You can– more or less– literally translate Scheme to Haskell with minimal difficulty. (Though, it may be easier just to write scheme in haskell.) But the brilliance of haskell is this:

Heres the Standard Factorial function in Scheme:

;Fac : int -> int

(define (fac x)
(cond [(= 0 x) 1]
[else (* n (fac (-n 1)))]))

Here it is in Haskell:

fac :: Int -> Int
fac(x)
| (x == 0) 1
| otherwise x * fac(x - 1)

(I used a ML style to make things look the same.)

The only real difference (besides some syntax changes) is the lack of the semicolon in front of the contract.

But what does all this do? Well, the difference comes during evaluation, watch this:

In Scheme:

(fac 1.414)

we have an infinite recursion, because:

(fac 1.414) -> 1 * fac(0.414) -> 1 * 0.414 * (fac -0.586) …

In Haskell:

fac 1.414

is a type error, and the whole thing kersplodes. Over, Evaluation Done, Haskell has Denied your function the right to evaluate.

In short, you have been rejected.Enough about the wonderfulness of the Type system. My title says the Good -> Bad -> Ugliness, obviously we’ve seen the good. How about the Bad?

Type Errors in Haskell:

Type errors in haskell suck, easy as that. They’re hard to understand, and in general, not very helpful. Further, alot of the differences between types are very subtle. For instance, consider the factorial function again, (just the type contracts for succinctness)

fac0 :: Int -> Int
fac1 :: Num -> Num

The look equivalent, right? Wrong. Num != Int, it includes Reals too.* So no lovely type errors here. These things are unfortunate, yes, but nothings really perfect. I could deal with this, but what I can’t deal with is exactly the problem I hoped to solve with Haskell, My bugs are hard to find. Not only that, they’re not hard to locate, I know exactly where they are, I just can’t decipher the cryptic text from the Haskell error stream to know exactly what the bug is. So I have to resort to piecing through the code bit by bit, trying to figure it out.

Silly.

Type Signatures are Ugly:

I Like Contracts, but Haskell doesn’t technically use them. Haskell has type signatures. Which are differ
ent.

So far, I’ve written contracts like this:

F : S * T * U * … -> D

I could also have:

F : S * T * … -> (D1, D2, …)

or if I wanted HOF’s

F : (G : X -> Y) * … -> (D, …)

these are all pretty easy to understand, (if you know how to read the shorthand). We know exactly what the arguments should be, elements of the set of elements of type S, or T etc. We also know exactly what the return types are, elements of the typed-set D, or ordered k-tuples of elements of typesets D1 through Dn, etc. Equivalent signatures in Haskell are:

(assuming f = F, and any capital letter is a valid type, and that …’s would be replaced with types in the end result.)**

f :: S -> T -> U -> … -> D
f :: S -> T -> … -> (D1, D2, …)
f :: (X -> Y) -> … -> (D, …)

Now, I understand that, since Haskell is Lazily evaluated, we want the type signatures to be heavily curried, hence the load of arrows. Honestly though, how hard is it to convert all that to a form Haskell can use? I’m not saying get rid of the arrow version, maybe just add an option to provide a “normal form” version, I shouldn’t have to add these in my code as comments, solely so I can understand whats going on. I understand that the implication method more accurately reflects what the compiler is doing, but as a programmer, I don’t really give a rats ass what the compiler is doing. As a mathematician,

foo :: Int -> String -> Num -> Bool

looks ugly, do I know what it means? Yes. Do I like the way it looks? No. I grasp that, as a Haskell Compiler, reading these type of signatures in makes things easier, and further, that these definitions make things easier to prove correct*** but damnit Haskell, I’m a mathematician, not a miracle worker, I want to be able to read those definitions intuitively, and not have to muddle around trying to figure out exactly what that signature represents. It’s ugly, fix it.

On that note, I am beginning to work on some Haskell Code which will convert a Type Signature of the form:

f :: S^n1 * T^n2 * … -> (D1,D2, … Dn)

to the form:

f :: S -> S -> .. n1 times .. -> T -> T -> ..n2 times.. -> (D1, D2, … Dn)

and hopefully, given some user input, the latter to the former as well. (This is not harder, sortof, but I can’t know what the normal form of the type signature should be without some user input about the in-arity (arity) and out-arity (ority) of the function.

Anywho, Haskell is awesome, go play with it.

~~Joe

*= Aside: I’m quite glad Haskell calls them Reals and not
something silly like Float (though that is allowed) or Double. Us
Mathematicians have had these names for years, IEEE can call the format
Double precision floating point of w/e the hell they want, they’re
reals, not doubles. Silly computer scientists…

Edit: Note that in fact I understand that floats != reals, but its about state of mind. I know I’m working on a computer, and so I’m not going to treat things as reals, but I want to be thinking as if I’m not limited, so that when I work with my code, I’m not tuning the algorithm to work with the computer, I’m tuning the computer to work with my algorithm. In this way, the problem becomes a problem of making the compiler better, rather than hacking my algorithm to work.

**= Haskell doesn’t really like capitalized function names.

***= Proofs of correctness are done through the Curry-Howard Isomorphism, which effectively states that if the contract of a given function is a valid statement of Logic, then the function is correct, otherwise its not. Note that this requires the Signature to be correctly written, ie:

concatString :: String -> String -. String as a signature for a function which zipped two strings together would be “correct” but only in the sense that the contract would be satisfied. A Proof of correctness means that the function of that type can exist, there are other methods related to this Isomorphism which allow for a better proof of the semantic correctness, as opposed to the more syntactual flare of Curry-Howard

April 18, 2007

The Second Stupidest thing on the planet.

Filed under: Uncategorized — jfredett @ 4:54 pm
This is only marginally brighter than TimeCube



The Earth Is Not Moving



Somehow, despite all scientific evidence, this man seems to believe that the earth is in fact the center of the universe, I’m not entirely sure why, but he says that the Bible, of all things, supports him.



Now, don’t get me wrong, if you want to believe in the bible, thats your right, I won’t judge. Hell, I believe in the bible, to an extent. But Come on, Can we really deny the evidence around us like this?



My Sister says that technically, I’m not agnostic, I’m a “comtemplative” (con - TEM - plah - tihv). I’m not sure what that means, but my belief is that- I, like the rest of humanity, am fallible. I’m fundamentally capable of making mistakes. And as such, I choose to put complete stock in nothing, I choose instead to believe that all things are innocent till proven guilty. Right until proven wrong, by a contradiction of pure fact, pure logic, or otherwise. I’m sure that everyone can agree- thats not a radical point of view, thats just common sense. As such, since I cannot prove that Evolution or Creation or any such nonsense is true, then they are both equally correct.



Hold on, aren’t they mutually exclusive? Isn’t it true that you can’t believe in both? Bullshit. Of course you can believe in both. The only important thing is that these things must make sense. They must fit the theory. Here’s my theory.



God Created some rules, laws of math. From these he derived the laws of physics, and then he sat his ass down, and watched the 100 billion year show. We Evolved, isn’t it amazing? That simple rules that God came up with led to such complex beauty as Sentience? Look at us, how did we get here by chance? Against all odds we evolved from nothing, doesn’t that sound like a concept that God would want us to learn from, Perseverence? Why the hell does it matter where we came from anyway? Why can’t we just agree to disagree that we got here- and so now we should figure out where we’re going? It’s ridiculous, we fight, we kill, maim, murder, yell, cheat, steal. Whats the Point? We’re supposed to be enlightened, but we behave like animals. I don’t seem to understand, why can’t we realize that we have different Ideas, that people are different, and that they’re entitled to be different. Maybe I’m getting a little Laodecian, but maybe thats what we need, some Lukewarm politics to get the status quo thrown the fuck up.



Just remember folks. Apparently, The Earth Is Not Moving







March 31, 2007

Compilers and Orchestra

Filed under: Programming — jfredett @ 5:38 am
When I started this silly blog thing, I had hoped to be able to post it in on a regular basis about current events and happenings in my many (yah, right) mathematical (sometimes) travels (sittings) .



Needless to say, it didn’t work out that way.



Heres what I’m working on, and what I’m not working on anymore:



Orchestra, A CAMaCS:



Orchestra is designed to be a Composer Assistant, something that helps you get out of a rut by learning from previous compositions and applying rules you supply it to suggest the next few notes of whatever your working on. I hope to have the Paper I’m writing about it as well as the actual source up on my site soon. It’ll be pretty bare, I don’t think I’m going to get to the GUI any time soon, but it’ll be workable, more or less, if you don’t mind hand editing code to make it work… :)



Joe, a mini java compiler:

I’m taking CS41 something or other, a Senior level CS course,

I’m a Sophmore level Math major.

I suck.

I don’t think I’m going to be able to finish it, fortunately, I only took this course as an elective, I don’t need it to graduate, so I should be fine when I fail it miserably.



The material is interesting, I just don’t have the code-writing skill to handle the requirements.





Anywho, I intend to take PLT next year (what I should have taken this year) which I do want for my “Major” (which is Math + Algebra + AI + Functional Programming Languages + Logic = Math w/ concentration in Logic and Algebra, and a healthy smattering of AI and Formal Languages) I hope to end up working with Automated Theorem Proving/Proof Assistant systems, (hence my inspiration for Orchestra, and Automated Music Writing system, more or less.) with all that, but who knows.



Anyway,





~~Joe

January 8, 2007

Of Compilers and Virtual Things

Filed under: Programming — jfredett @ 4:28 am
C++ is ugly and it sucks ass.



Java is pretty.



I’m writing a compiler/virtual machine I’m considering just saying “fuck it” and doing it in java. Because I know java, and java is pretty.



But I dont know, it might end up slow, and that would make me sad, theres nothing worse than a slow VM/Compiler.



I’m just so annoyed with the state of computer languages in respect to math these days. The only really worthwhile languages are all about computers, I mean– come on… Who wants a computer language to primarily do computer stuff? But seriously, I need a language that will have all the beauty and awesome power of languages like Lisp and Perl. But still be efficient and portable enough to be useful. So I finally said, “screw it, I’ll do it myself”, I think my current method will be to write the VM is C++, for speed purposes. But maybe I’ll write the final compiler in Java, because it will be a significantly more complex bit of code. In C++, debugging anything bigger than a few lines hither and thither is a bitch- but for the speed it offers– I dont know- maybe I can find a decent cross compiler and then just go back and comb out the resulting code… I suppose I should get back to learning about templates in C++, ugly little bastards. I hate pointers. Hate them hate them hate them…



~~meh.

September 5, 2006

Chess

Filed under: Uncategorized — jfredett @ 4:06 pm

This Site is an absolutely wonderful resource on chess and the like. Starts from the basics and moves into notation and all such wonderful things- I’m really rather enjoying it.

So besides the chess site- hopefully I’ll have a nice looking Formal Grammar to post in a few days which is (intended to be a) description of the wonderful craft of Crocheting. Its really a terribly neat concept- I think.

Anyway- Terribly interesting stuff

~~QED

September 3, 2006

The Grand Disparate Notion:

Filed under: Blog — jfredett @ 6:18 pm

This is a Blog. I Caved. I don’t mind though- I’m weak. Anyway, I’ll post plenty of random shit for noone to ever read, but it will make me happy. So there.

Enjoy the disparity of my thoughts.

« Newer Posts

Powered by WordPress