PreviousUpNext

5.7.15  Mythryl Package Syntax

C calls a record a struct, so to avoid misleading C intuition, where SML uses the keyword structure, Mythryl uses the keyword package.

Named packages are declared using a conventional, compact, Posix-flavored syntax parallel to the API syntax. Here is a package definition matching the preceding API declaration:

    package my_package: My_Api {
        Color = RED | GREEN | BLUE;
        Point = TWO_D (Float, Float)
              | THREE_D (Float, Float, Float);   
        
        fun my_function ints
            =
            sum (ints, 0)
            where
                fun sum ([],       result) => result;
                    sum ((i ! is), result) => sum (is, i + result);
                end;
            end;
    };

Unlike SML, which uses plain colon for weak package sealing for historical reasons, Mythryl uses plain colon above to represent strong sealing, in order to encourage good programming practices by giving the shortest, most convenient form to the preferred construct.

The Mythryl syntax for weak sealing in the same case is

    package my_package: (weak) My_Api {
        Color = RED | GREEN | BLUE;
        Point = TWO_D (Float, Float)
              | THREE_D (Float, Float, Float);   
        
        fun my_function ints
            =
            sum (ints, 0)
            where
                fun sum ([],       result) => result;
                    sum ((i ! is), result) => sum (is, i + result);
                end;
            end;
    }

(The above weak is not a reserved identifier, by the way.)

This syntax has better C intuition than SML’s :> operator. Also, recent research has revealed forms of module sealing other than conventional strong and weak sealing; the above (weak) syntax will extend naturally to accommodate other forms of sealing should the need arise.

As with Mythryl API syntax, anonymous package declarations (often useful as arguments to generics) are written by replacing the package name with an underbar wildcard in the above syntax:

    package my_package
        =
        some_g (
            package {
                Color = RED | GREEN | BLUE;
                Point = TWO_D (Float, Float)
                      | THREE_D (Float, Float, Float);   

                fun my_function ints
                    =
                    sum (ints, 0)
                    where
                        fun sum ([],       result) => result;
                            sum ((i ! is), result) => sum (is, i + result);
                        end;
                    end;
            }
        );

Anonymous package syntax is also used when a package is sealed with an API trailing where modifiers. This form parallels normal SML syntax in such cases:

    package my_package
        :
        My_Api
        where
            Int == Integer
        =
        package {
            Color = RED | GREEN | BLUE;
            Point = TWO_D (Float, Float)
                  | THREE_D (Float, Float, Float);   

            fun my_function ints
                =
                sum (ints, 0)
                where
                    fun sum ([],       result) => result;
                        sum ((i ! is), result) => sum (is, i + result);
                    end;
                end;
        }

A Mythryl file defining a package customarily uses the .pkg file extension.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext