Monday, February 6, 2012

Library Support

I devoted the last weeks mainly to advance the development of the standard libraries.

Data.List, Data.Maybe

The release 3.19.9 that is ready for download now contains a Data.List  module almost compatible with the Haskell 2010 report and the Prelude has the same list functions as the Haskell Prelude; and, in addition, makes available all that is in Haskell's Data.Maybe.
The remaining incompatibilities have to do with the fact that String is not [Char] in Frege, hence list functions do not work on Strings.
There is an easy way to make lists of characters from Strings, and it works the same for any other type whose values that can be viewed as lists. Technically, this is achieved through a type class ListSource with method toList that is supposed to turn its argument into a list lazily. Currently, [a], String and Maybe a are instances of ListSource, arrays will be added later and of course you can make instances for your own types.
The ListSource type class is employed in list comprehension generators: one can write any expression that has a type which is an instance of ListSource on the right hand side of an arrow.
The functions nullhead, ++ and tail are also class operations for ListLike types, and they work on Strings and normal lists. This makes is possible to write functions that work on lists, Strings and every other instantiated type without conversion of the value to a list.

Monadic functions and type classes

The prelude has now a hierarchy of monad related type classes that adapts the one proposed in The Other Prelude. However, the traditional names like fmap, <*>, etc. have been kept. And, because pure is a keyword in Frege, the corresponding operation lives under the name  return in  Applicative.

We can instantiate a whole class hierarchy with a single instance declaration in Frege, hence there is no need to have explicit separate Functor, Applicative and Monad instances when one needs a MonadZero, for example.

The functions and classes provided by the Frege Prelude are a superset of what is in the Haskell 2010 Prelude `union`  Control.Monad.

Other libraries

Thanks to Daniel Gronaus efforts (who also advocated and proof-of-concepted the monadic code, see above) we now have the Floating class (import frege.prelude.Floating to use it) and the modules Control.Monoid, Control.NonEmpty and Control.Foldable.

Haskellish Module Names

A Frege module name is at the same time the fully qualified Java name of the class the compiler creates as result of the compilation of the module. The package part of that name should consist of all lowercase identifiers according to Java customs.
But as seen in the paragraphs above, we can now use module names like Data.Maybe.
For compatibility reasons, the compiler will accept such names in package clauses as well as import clauses, but transform them a bit to arrive nevertheless at a more or less javaish correct class name:

{--
    When a package name consists of more than just a 'CONID' 
    and the first character is an uppercase letter, then this 
    letter is replaced by its lowercase equivalent and the
    string @"frege."@ prepended to form the real package name.
    
    > magicPack "Data.List" = "frege.data.List"
    > magicPack "List"      = "List"  
    -}
magicPack (nm@´\.´)
    | fst.isUpperCase = "frege." ++ fst.toLowerCase.display ++ tail nm
    where 
        fst = String.charAt nm 0
magicPack nm = nm

Note that such classes will always live in some Java package whose name starts with frege. Hence this  feature is intended for use with very well known standard libraries. Please follow Java conventions for naming your own packages.

fregIDE
The fregIDE now has an outline view. This makes navigating in a bigger-than-a-screenful source file much easier.

No comments:

Post a Comment

Comments will be censored by me as I see fit, most likely if they contain insults or propaganda for ideologies I do not like. Comments that are on topic will not be censored. If I leave a comment uncensored this does not imply that I agree with the opinions expressed therein.