Morgan-Stanley / morphir-elm / Morphir.IR.Type

This module contains the building blocks of types in the Morphir IR.

Type Expression


type Type a
    = Variable a Morphir.IR.Name.Name
    | Reference a Morphir.IR.FQName.FQName (List (Type a))
    | Tuple a (List (Type a))
    | Record a (List (Field a))
    | ExtensibleRecord a Morphir.IR.Name.Name (List (Field a))
    | Function a (Type a) (Type a)
    | Unit a

An opaque representation of a type. Check out the docs for each building blocks for more details:

Creation

variable : a -> Morphir.IR.Name.Name -> Type a

Creates a type variable.

toIR a == variable [ "a" ] ()

toIR fooBar == variable [ "foo", "bar" ] ()

reference : a -> Morphir.IR.FQName.FQName -> List (Type a) -> Type a

Creates a fully-qualified reference to a type.

toIR (List Int)
    == reference SDK.List.listType [ intType ]

toIR Foo.Bar
    == reference
        ( [ [ "my" ], [ "lib" ] ], [ [ "foo" ] ], [ "bar" ] )
        []

tuple : a -> List (Type a) -> Type a

Creates a tuple type.

toIR ( Int, Bool )
    == tuple [ basic intType, basic boolType ]

record : a -> List (Field a) -> Type a

Creates a record type.

toIR {} == record []

toIR { foo = Int }
    == record
        [ field [ "foo" ] SDK.Basics.intType
        ]

toIR { foo = Int, bar = Bool }
    == record
        [ field [ "foo" ] SDK.Basics.intType
        , field [ "bar" ] SDK.Basics.boolType
        ]

extensibleRecord : a -> Morphir.IR.Name.Name -> List (Field a) -> Type a

Creates an extensible record type.

toIR { e | foo = Int }
    == extensibleRecord (variable [ "e" ])
        [ field [ "foo" ] intType
        ]

toIR { f | foo = Int, bar = Bool }
    == extensibleRecord (variable [ "f" ])
        [ field [ "foo" ] intType
        , field [ "bar" ] boolType
        ]

function : a -> Type a -> Type a -> Type a

Creates a function type. Use currying to create functions with more than one argument.

toIR (Int -> Bool) ==
    function
        SDK.Basics.intType
        SDK.Basics.boolType

toIR (Int -> Bool -> Char) ==
    function
        intType
        (function
            SDK.Basics.boolType
            SDK.Basics.charType
        )

unit : a -> Type a

Creates a unit type.

toIR () == unit

Record Field


type alias Field a =
{ name : Morphir.IR.Name.Name
, tpe : Type a 
}

An opaque representation of a field. It's made up of a name and a type.

mapFieldName : (Morphir.IR.Name.Name -> Morphir.IR.Name.Name) -> Field a -> Field a

Map the name of the field to get a new field.

mapFieldType : (Type a -> Type b) -> Field a -> Field b

Map the type of the field to get a new field.

Specification


type Specification a
    = TypeAliasSpecification (List Morphir.IR.Name.Name) (Type a)
    | OpaqueTypeSpecification (List Morphir.IR.Name.Name)
    | CustomTypeSpecification (List Morphir.IR.Name.Name) (Constructors a)

typeAliasSpecification : List Morphir.IR.Name.Name -> Type a -> Specification a

opaqueTypeSpecification : List Morphir.IR.Name.Name -> Specification a

customTypeSpecification : List Morphir.IR.Name.Name -> Constructors a -> Specification a

Definition


type Definition a
    = TypeAliasDefinition (List Morphir.IR.Name.Name) (Type a)
    | CustomTypeDefinition (List Morphir.IR.Name.Name) (Morphir.IR.AccessControlled.AccessControlled (Constructors a))

This syntax represents a type definition. For example:

In the definition, the List Name refers to type parameters on the LHS and Type extra refers to the RHS

typeAliasDefinition : List Morphir.IR.Name.Name -> Type a -> Definition a

customTypeDefinition : List Morphir.IR.Name.Name -> Morphir.IR.AccessControlled.AccessControlled (Constructors a) -> Definition a

definitionToSpecification : Definition a -> Specification a

Constructors


type alias Constructors a =
List (Constructor a)


type Constructor a
    = Constructor Morphir.IR.Name.Name (List ( Morphir.IR.Name.Name, Type a ))

Mapping

mapTypeAttributes : (a -> b) -> Type a -> Type b

mapSpecificationAttributes : (a -> b) -> Specification a -> Specification b

mapDefinitionAttributes : (a -> b) -> Definition a -> Definition b

mapDefinition : (Type a -> Result e (Type b)) -> Definition a -> Result (List e) (Definition b)

eraseAttributes : Definition a -> Definition ()