Maybe type

21 views
Skip to first unread message

John J. Camilleri

unread,
May 9, 2013, 9:09:05 AM5/9/13
to gf-...@googlegroups.com
Hi all
While writing a resource grammar, I've found it's quite common to have possibly non-existent forms. In English for example, some adjectives have a morphological comparative ("happy" -> "happier") while others do not ("expensive" -> "more expensive"). The normal way to encode this is as a string combined with a boolean, so that if the boolean is false then we ignore the string.

But I realised this is the same as the Maybe type in Haskell, so I wrote a GF resource module to model this type. The function names and type signatures are made to mirror the Haskell implementation. Maybe is a higher-order generic type, and MaybeS is Maybe parametrised with the Str type, since that is a common case. 

The Maybe.gf module is attached - below is a small example of its use. I'm surprised this hasn't been written before. Comments are welcome - if you like it, I suggest we include this module in the RGL in the Prelude folder.

Abstract

abstract Test = {
  cat
    A ; N ; Phr ;
  fun
    caviar_N, pizza_N : N ;
    expensive_A, good_A : A ;
    more : N -> A -> N -> Phr ;
}

Concrete

concrete TestEng of Test = open Prelude, Maybe in {
  lincat
    A = {
      posit : Str ;
      comp : MaybeS
      };
    N   = SS ;
    Phr = SS ;
  lin
    caviar_N = ss "caviar" ;
    pizza_N = ss "pizza" ;
    expensive_A = {
      posit = "expensive" ;
      comp = NothingS ;
      } ;
    good_A = {
      posit = "good" ;
      comp = JustS "better" ;
      } ;
    more = \n1,a,n2 ->
      let
        cmp : Str = fromMaybeS ("more" ++ a.posit) a.comp
      in
      ss (n1.s ++ "is" ++ cmp ++ "than" ++ n2.s) ;
}

Testing on shell

Test> l more caviar_N good_A  pizza_N
caviar is better than pizza

Test> l more caviar_N expensive_A  pizza_N
caviar is more expensive than pizza

Maybe.gf
Reply all
Reply to author
Forward
0 new messages