Sunday, July 15, 2012

A Frege Servlet

I was playing around with Frege to create a small Servlet. There is no web framework for Frege available yet so it is going to be mostly interfacing with native Java classes. Here is the code snippet in Frege.

First, we need to create native declarations in Frege for the Java classes and methods that we are interested in. For this example, we will be defining data types for HttpServletRequest, HttpServletResponse, HttpServlet classes and an implementation for Servlet's doGet method.

In HttpServletRequest class, we will be encoding getParameter method. The type signature for that method,

Since this method could return null in case of missing parameters from the request, the return type is Maybe String. Frege automatically handles null if the return type is "Maybe".

Now let us declare a getWriter function to it's native counterpart in HttpServletResponse class.

Since HttpServletResponse.getWriter method can throw IOException and since it is not a pure method, the return type is IO (Exception PrintWriter).

The definitions for ServletContext and HttpServlet are not of much interest since we are not going to use any of the methods from those classes for this example.

The definition of doGet function is where we are actually writing to response. From the signature of the function, it takes a servlet, a request and a response instance and returns IO () since it is a side-effect function which doesn't return any value. Another important point is that the parameter names are prefixed by bang character which makes the parameters strict which would otherwise be all lazy since Frege is by default lazy.

The last missing piece is how do we specify a servlet class in web.xml? I don't really know yet in Frege how to create a named class that extends a Java class. So I created the servlet in Scala which will forward the request to our doGet function defined in Frege.

Here FregeServlet.doGet(this, request, response) calls the doGet function defined in FregeServlet module and returns an IO lambda. Then we call that IO action by passing an arbitrary integer representing the real world. Here the Scala's support for 'apply' method is helpful.
FregeServlet.doGet(this, request, response)(realWorld)
is actually
FregeServlet.doGet(this, request, response).apply(realWorld)
where the doGet function defined in Frege module returns frege.rt.Lambda which has an apply method. The final method call _e is to evaluate the lazy value which is where the IO action is actually executed and our response is sent to the client. We can add this class as servlet in web.xml and point to the servlet URL to see the Frege/Scala servlet in action.

Saturday, March 3, 2012

Brush your code!

Lately I have been playing around with SyntaxHighlighter to highlight code syntax on my blog for the new programming language, Frege. Here are some simple steps on that process which would be useful for anyone wanting to create new/enable SyntaxHighlighter.

1) Copy and paste the following just before '</head>'in the blog template

You don't need to include all the 'shBrushxxx.js' scripts. Just include the brushes you need for your blogs. The scripts in the format are inbuilt brushes. The last "<script>" is the one I created for the language 'Frege'. Yon can refer this guide on creating new brushes: http://alexgorbatchev.com/SyntaxHighlighter/manual/brushes/custom.html

2) After step 1 (You have included all the css and scripts for brushes), you can use it in your blogs like this ('Edit html' for your blog), or You can use "script" method: The difference is that when you use "script", your contents will be automatically html-escaped whereas in "pre" method, you need to escape those special characters.

3) If you want to customize some styles, you can add it before "]]></skin>" like this, The last style "syntaxhighlighter" is to disable scrollbars in chrome which adds scrollbars even when there is nothing to scroll. IE and firefox correctly render without scrollbars.
Thats all you need to enable SyntaxHighlighter for your blog.

Wednesday, February 29, 2012

Hello World Frege!

Ever since I started learning Scala, the whole functional programming thing is absolutely fascinating to me. As "Scala is a gateway drug to Haskell",  then I was attracted by Haskell's gravity. Haskell changed the way I think about programs and made me enjoy programming more than ever. While learning Haskell, as a Java programmer, I used to think how good would it be to code Haskell in the ubiquitous JVM and then I found this language, Frege more interesting for JVM and Haskell-like.

As I am practicing Frege along with Haskell and Frege is still relatively new, I thought it would be a good idea to share my experience with Frege.


Getting started:

FregIDE Eclipse plugin is still at basic level but is really usable and a good way to start with Frege. The plugin installation details can be found here: http://fregepl.blogspot.com/p/fregide-tutorial.html. Frege requires JRE 7 so even after following the instructions on the page, if you still do not see Frege Builder/Preferences enabled in Eclipse, try starting your Eclipse with "-vm" argument pointing to your JRE 7. Now, the "Hello World"
module Main where
main _ = println "Hello World"

Yes, I know that is not much interesting. How about implementing a function from Haskell?
In Haskell, there is a function "getLine" which reads a line from standard input. Now lets implement that function in Frege using Java's IO classes.

Haskell's 'getLine' using Java's methods:
import frege.IO

--Reads a line from standard input device
getLine :: IO String
getLine = do
  isin  <- stdin
  isrin <- IO.InputStreamReader.new isin
  brin  <- IO.BufferedReader.fromISR isrin
  line <- brin.readLine
  return $ fromExceptionMaybe line

fromExceptionMaybe :: Exception (Maybe a) -> a
fromExceptionMaybe (Right (Just r)) = r
fromExceptionMaybe (Right _) = error "get on Nothing"
fromExceptionMaybe (Left l) = error l.getMessage

--Native reference to Java's parseInt
pure native parseInt java.lang.Integer.parseInt :: String -> Int

main _ = do
  line <- getLine
  println $ parseInt line

Here as you can see, we have defined 'getLine' using Java's Classes, InputStreamReader, BufferedReader, Exception along with Haskell goodies Maybe and Either (through Exception). The parseInt function is declared with native reference to Java's 'parseInt' method from 'Integer' class and since it is a pure method, the types are straight-forward otherwise we would be using ST monad for those native declarations.

Happy Haskelling on JVM!