FlatCurryGoodies

The function foldr is a versatile function for list processing:

foldr cons nil [] = nil
foldr cons nil (x:xs) = cons x (foldr cons nil xs)

It replaces every (:) with cons and [] with nil. If you apply this idea to FlatCurry dataterms instead of lists, you can define selectors, test and update operations and useful auxiliary functions based on one primitive for each datatype inspired by the foldr idea. You get over 800 lines of boring code and you never need to rewrite it for your program transformations. Just watch FlatCurryGoodies and use the versatile predefined functions to do exactly what you need.

As an example consider a transformation function for type expressions:

trTypeExpr tv tc ft (TVar n) = tv n

trTypeExpr tv tc ft (TCons name args) = tc name (map (trTypeExpr tv tc ft) args)

trTypeExpr tv tc ft (FuncType from to) = ft (tr from) (tr to)
 where
  tr = trTypeExpr tv tc ft

Now you can specialize this function to rename all indices of type variables with a rename function rename:

rnmAllVarsInTypeExpr rename 
  = trTypeExpr (TVar . rename) TCons FuncType

Many other typical transformations can be written using such tr… operations. Hopefully, these goodies can help you writing clear and brief code!

/srv/dokuwiki/currywiki/data/pages/libraries/flatcurry.txt · Last modified: 2014-06-13 12:35 (external edit)
Back to top
CC Attribution-Noncommercial-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0