Posted By: Charles | Nov 19th, 2009 @ 10:55 AM | 53,115 Views | 18 Comments
We've kicked off C9 Lectures with a journey into the world of Functional Programming with functional language purist and high priest of the lambda calculus, Dr. Erik Meijer (you can thank Erik for many of the functional constructs that have shown up in languages like C# and VB.NET. When you use LINQ, thank Erik in addition to Anders). 

We will release a new chapter in this series every Thursday.

In Chapter 8, Functional Parsers, it's all about parsing and parsers. A parser is a program that analyses a piece of text to determine its syntactic structure. In a functional language such as Haskell, parsers can naturally be viewed as functions.

  type Parser = String -> Tree

A parser is a function that takes a string and returns some form of tree.


You should watch these in sequence (or skip around depending on your curent level of knowledge in this domain):

Get the presentation slides here

Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Rating:
8
0

Hope you get the time to do an introduction on QuickCheck. This is one of the things in Haskell that seems the most interesting to me, and I think it is a pretty unique to functional programming languages, if I understand it correctly.

Excelent lecture as always. Keep up the good work. Smiley

 

In the book Hutton didn't explain why he chose List instead of the Maybe type. I'm glad you touch that subject in the lecture.

exoteric
exoteric
embarassingly sequential

Erik, could you make the generalization offered by lists over optional types more statically correct with dependent typing? Saying that Maybe is just a list of zero or one element?

 

In the ideal world we want to have lists as a generalization of Maybe but we don't want to loose the type safety that Maybe offers.

 

This problem is complete Deja Vu for me: I encountered the same tension between the option type and the list type in an attempt to implement a lazy functional L-system library in haXe back in 2007.

 

Thanks for the "epic fail" parser, haha.

 

Deja Vu again! Brian Beckman repeated this so many times it was burned into my memory: the state monad is a function from state to state contents pair. This looks exactly as what we see here: you start with a string and get back a string and a tree.

 

Erik, why do you not like the layout rule in this case and use explicit delineation. I don't think you mention this.

 

This whole parsing business is reminiscent of unfolding. Expansion of some input into a more complex form. Composition, application and unfolding, hmm...

While parser combinators can be expressed functionally on .NET, even in C#, after having written a few such combinator libraries I've found that Pratt parsers strike a better balance between simplicity and performance on the CLR. Pratt parsers also naturally handle operator precedence, while parser combinator libraries require more complicated structures to express these relationships. The extensible parser in the above link will be available in an upcoming release of my open source Sasa class library, so check it out if you're interested in this domain.

exoteric
exoteric
embarassingly sequential

I've seen your blog before naasking and will check this out, thanks!

Excellent as always, Erik.

 

Perhaps like exoteric, I felt a little dissatisfied with the return type of Parser being "list of..." rather than "Maybe...". I've come to admire the expressive power of Haskell types and this seems like a bit of a hack. Considering, as you pointed out, that both the list and Maybe types comprise Monads, I am wondering where the advantage exists for using the list type. Monads allow for map, filter, lift operations, no?

The list type is generally used for parser combinators to return multiple possible parses.

 

I don't own Hutton's book, but I can think of a few reasons for not using Maybe. It would require introducing a new datatype to the reader, while the reader is already familiar with the list type. Also, it takes more characters to write when pattern matching on a Maybe value Tongue Out.

 

Now, in a real project you'd use the appropiate data type. If you know that at most only a single answer will be produced, use Maybe.

exoteric
exoteric
embarassingly sequential

In general, this tension between structural abstraction and type-safety is conceptually dissatisfying.

 

There must be a way to use the same list abstraction, only constrained to apply to [n,m] elements. Such value-restrictions upheld by a type system appear to be what dependent typing does. This provides a new expressive dimension which is missing in order to type programs correctly without also loosing the abstraction provided by lists over options. The ideal must be a way to fluently move from intensional to extensional semantics by explicitly coercing a type into the extensional domain of structural compatibility.

 

It also reminds me of template metaprogramming. Someone built a compile-time regexp module in D and there's also a compile-time raytracer in D. In the last case you may discuss the applicability of that but at least it shows the expressive power.

 

http://www.digitalmars.com/d/2.0/templates-revisited.html

 

Now time to play with parsers in C# using LINQ...

Microsoft Communities