Looking into Guide Types

I don’t know if this concept has another name, because it is something that I thought of myself, and I call it ‘guide types’. But I am sure I am not the first to think of it, so do let me know if you have another name for it.

A ‘guide type’ is a type, usually higher order, that aims to structure a whole program or at least a larger piece of a program, not just an individual function or data model or module. They are very often higher order, because they describe a way in which two pieces of software can be connected together, and both sides split up their respective responsibilities by describing them with function signatures.

Here is the most obvious example in Elm, Browser.Dom.sandbox:

sandbox :
    { init : model
    , view : model -> Html msg
    , update : msg -> model -> model
    }
    -> Program () model msg

This connects together the Elm runtime, and a user supplied program. The user supplied program has its responsibilities described as the view and update functions (init is a constant this time…). These are effectively call-backs that the user fills in to customize his or her program to do some cool stuff, that the Elm runtime can simply think of as a model/view/update cycle.

I’ll post up some examples of guide types that I have written to help structure some of my programs. I have found this concept useful when figuring out how to divide up responsibilities between larger components of a program.

3 Likes

Very interesting! While we encounter these all over the place (in Elm and also in e.g. Haskell and potentially also in other, less typed, places although they are less noticeable).

This is however the first time that I’ve seen the concept been given a concrete name. Some of them have been called ‘config types’, but I think that might be a more general term. I think this is very useful to shed more light on how we can use guide types as a tool in our source code.

Other notable examples I can think of from the top of my head are:

Interestingly, I think that guide types have a sort of similar use-case to when one would use a typeclass in Haskell.

Yes, those are examples of it. They are examples that are towards the smaller end of what I am thinking of. To explain, List.map is a higher order function but not a guide type, since it is used on a small scale - usually just for convenience within code that we write. What I am thinking of is bigger types, in the sense that they structure larger components or a whole program.

Here is an example:

{-| Defines the type of a template. A template takes a device definition, and can be
    dynamic or static. Dynamic templates take the model as argument and can produce
   HTML with effects. Static templates are just fixed HTML with no effects.
-}
type Template msg model
    = Dynamic (DeviceStyles -> model -> Html msg)
    | Static (DeviceStyles -> Html Never)


{-| Defines the type of a layout. A layout is a higher level template; it takes a
template as input and produces a template as output.
-}
type alias Layout msg model =
    Template msg model -> Template msg model

I am writing a style guide, which is a demo page for trying out CSS styles that I want to use as a branded theme across applications that I write. Most of it is just styling static HTML, like showing ‘Lorem ipsum …’, or rendering a collection of buttons and so on. There are a few dynamic pages too. I also want to try out various layouts, like having different headers and footers. So the overall type that I came up with to help me structure this application is ‘Layout’.

Of course, I don’t really need these types to write this application. But I wrote the types before writing the application, and doing so helped me figure out how to structure it better. Layouts and templates are self contained pieces of view code, and they connect together with the application top-level in its Main module which plugs together the current layout and template. The overall type helped me to figure out how best to divvy up the responsibilities of various parts of the code into modules.

2 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.