Page 1

Application Programming with GF

GF Tutorial 2012

Copenhagen, 15 September 2012

Thomas Hallgren

Page 2

Outline

Page 3

The Portable Grammar Format

Page 4

Haskell Applications

PGF run-time library

Page 5
Haskell Applications → PGF run-time library

To install it:

cabal install gf

Page 6
Haskell Applications

Example 1: a simple translator

Translator.hs

Page 7
Haskell Applications

Example 2: a question-answering system

Page 8
Haskell Applications → Example 2: a question-answering system

Abstract syntax in GF

abstract Arithmetic = {
    
flags startcat=Question ;

cat Answer; Object; Question;

fun Even, Odd, Prime : Object -> Question;
    Number : Int -> Object;

    Yes, No : Answer;
}
Page 9
Haskell Applications → Example 2: a question-answering system

Concrete syntax for English

concrete ArithmeticEng of Arithmetic = {

lincat
    Answer, Object, Question = Str;

lin Even object = is "even" object;
    Odd object = is "odd" object;
    Prime object = is "prime" object;
    Number int = int.s;
    No  = "No.";
    Yes  = "Yes.";

oper is : Str -> Str -> Str  = \ pred,obj -> "is"++obj++pred++"?";
}
Page 10
Haskell Applications → Example 2: a question-answering system

Haskell code

Similar to the translator example, but

Page 11
Haskell Applications → Example 2: a question-answering system → Haskell code
Page 12
Haskell Applications → Example 2: a question-answering system

Exporting GF abstract syntax to Haskell

Page 13
Haskell Applications → Example 2: a question-answering system

Generated Haskell Code

Page 14
Haskell Applications → Example 2: a question-answering system

Transfer function

import PGF
import Arithmetic

transfer :: Tree -> Tree
transfer = gf . answer . fg

answer :: GQuestion -> GAnswer
answer (GEven  x) = test even  x
answer (GOdd   x) = test odd   x
answer (GPrime x) = test prime x

test :: (Int->Bool) -> GObject -> GAnswer    
test p (GNumber (GInt x)) = if p x then GYes else GNo

prime n = and [n `mod` d /= 0 | d <-[2..n-1]]
Page 15
Haskell Applications → Example 2: a question-answering system → Transfer function
Page 16
Haskell Applications

Example 3

A larger query language

Page 17

Web applications

Some examples

Page 18

Implementing web applications using GF

Page 19

Server-side GF applications

Page 20
Server-side GF applications

Example: the question-answering system

Page 21
Server-side GF applications → Example: the question-answering system

Implementation

A simple CGI wrapper answer.cgi:

#!/bin/bash echo "Content-Type: text/plain" echo "" /usr/local/bin/qparse "$QUERY_STRING" query | ./Answer

Page 22
Server-side GF applications → Example: the question-answering system → Implementation

Some HMTL+JavaScript to submit questions and embed the answer in the same web page

Page 23

PGF web service API

Page 24

GF JavaScript library

PGF service access

Page 25
GF JavaScript library

User interface

Page 26

Running the PGF service on your own computer

GF server mode

Page 27
Running the PGF service on your own computer

Example

Question answering with guided input:

Page 28
Running the PGF service on your own computer

FastCGI with Lighttpd / Apache httpd

Page 29

Web applications without a web server

Translating grammars to JavaScript, example:

Page 30
Web applications without a web server

Minibar offline experiment (November 2010)

Page 31

Applications in other languages

Page 32

The End

Questions?

More info