liveBook · Manning

Value constructor

As explained in Functional Design and Architecture: Examples in Haskell

A value constructor is a constructor that creates a value of an algebraic data type and may expose part of its representation.

A value constructor creates a value of an algebraic data type. Its visibility determines how much of the representation client code can depend on: public constructors permit pattern matching over internal structure, while opaque modules hide that structure. section 6.3.2 section 3.2.4

Value constructors play distinct roles in the examples:

  • Free stores an action and its continuation; Pure terminates the chain while holding the resulting value. section 6.3.2
  • BoolValue, IntValue, FloatValue, and StringValue construct the alternatives of Value. section 3.2.4
  • Smart constructors wrap construction behind functions, allowing the type and its representation to remain opaque. section 3.2.4

Wrapping an operation in Free

A manually constructed recipe uses Free with a Pure placeholder: `Free (StartNewSandwich Toast Tomato Pure)`. Monadic binding later replaces that placeholder with the continuation, producing one nested computation chain. section 6.3.2

data Free t a
  = Pure a               #1
  | Free                 #2
     (t                  #3
        (Free t a)       #4
     )

Inspect the recursive definition showing Pure as the value-holding terminator and Free as the constructor carrying an action and continuation. section 6.3.2

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Explains constructor roles, continuation nesting, representation exposure, and the shift to smart constructors and opaque modules.
Examples
Uses Free, Pure, sandwich operations, and the BoolValue, IntValue, FloatValue, and StringValue alternatives.
What it assumes
Assumes algebraic data types, monadic chaining, pattern matching, and modules; distinguishes value constructors from smart constructors.
What you can do after
Evaluate/understand when constructor visibility exposes representation and when smart constructors preserve an abstract type.
How it connects
Connects value constructors to algebraic data types, free monads, smart constructors, modules, encapsulation, and pattern matching.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Design and Architecture: Examples in Haskell. The section links point to the parts of the book it draws on.