Tangible Value
From HaskellWiki
m (→Abstract: mention talk page) |
(updating for 0.3) |
||
| Line 1: | Line 1: | ||
| + | [[Category:Interfaces]] | ||
| + | [[Category:IO]] | ||
| + | [[Category:Arrow]] | ||
| + | [[Category:Libraries]] | ||
| + | [[Category:Packages]] | ||
| + | |||
== Abstract == | == Abstract == | ||
| - | ''' | + | ''Warning'': The Haddock docs are out of date. I'm trying to get a working haddock 2.0 running (on my windows machine). |
| - | TV is for | + | '''TV''' is a library for composing ''tangible values'' ("TVs"), i.e., values that carry along external interfaces. In particular, TVs can be composed to create new TVs, ''and'' they can be directly executed with a friendly GUI, a process that reads and writes character streams, or many other kinds interfaces. Values and interfaces are ''combined'' for direct use, and ''separable'' for composition. This combination makes for software that is ''ready to use and ready to reuse''. |
| - | + | TV can be thought of as a simple functional formulation of the Model-View-Controller pattern. (My thanks to an anonymous ICFP referee for pointing out this connection.) The value part of a TV is the ''model'', and the "interface" part, or "output" as it is called below, is the ''viewer''. Outputs are built up compositionally from other outputs and from inputs (the ''controllers''), as described below. | |
| + | |||
| + | Besides this wiki page, here are more ways to learn about TV: | ||
* [http://darcs.haskell.org/packages/TV/doc/html Read the Haddock docs] (with source code, additional examples, and Comment/Talk links) | * [http://darcs.haskell.org/packages/TV/doc/html Read the Haddock docs] (with source code, additional examples, and Comment/Talk links) | ||
* Get the code repository: '''<tt>darcs get http://darcs.haskell.org/packages/TV</tt>''' | * Get the code repository: '''<tt>darcs get http://darcs.haskell.org/packages/TV</tt>''' | ||
* Or grab a [http://darcs.haskell.org/packages/TV/dist distribution tarball]. | * Or grab a [http://darcs.haskell.org/packages/TV/dist distribution tarball]. | ||
| + | * See the [[TV/Versions| version history]]. | ||
| + | * See the use of TV in [[Eros]]. | ||
As of version 0.2, I have moved the GUI functionality out of TV and into a small new package [[GuiTV]]. I moved it out to eliminate the dependency of core TV on [[Phooey]] and hence on [[wxHaskell]], as the latter can be difficult to install. The GUI examples below require [[GuiTV]]. | As of version 0.2, I have moved the GUI functionality out of TV and into a small new package [[GuiTV]]. I moved it out to eliminate the dependency of core TV on [[Phooey]] and hence on [[wxHaskell]], as the latter can be difficult to install. The GUI examples below require [[GuiTV]]. | ||
| Line 14: | Line 24: | ||
I'd love to hear your comments at the [[Talk:TV]] page. | I'd love to hear your comments at the [[Talk:TV]] page. | ||
| - | + | == First Example == | |
| - | + | ||
| - | + | ||
Here is a tangible reverse function: | Here is a tangible reverse function: | ||
| Line 45: | Line 53: | ||
We'll see [[#The_general_story|later]] that "<hask>runUI</hask>" and "<hask>runIO</hask>" are both type-specialized synonyms for a more general function. | We'll see [[#The_general_story|later]] that "<hask>runUI</hask>" and "<hask>runIO</hask>" are both type-specialized synonyms for a more general function. | ||
| - | + | == Outputs == | |
What I've been calling an "interface" is a value of type <hask>COutput a</hask> for a type <hask>a</hask>. For instance, for <hask>reverseT</hask>, <hask>a</hask> is <hask>String->String</hask>. The reason for the <hask>C</hask> prefix is explained below. At the heart of TV is a small algebra for constructing these outputs. Weve already seen one output function, <hask>oTitle</hask>. Another one is <hask>showOut</hask>, which is an output for all <hask>Show</hask> types. For instance, | What I've been calling an "interface" is a value of type <hask>COutput a</hask> for a type <hask>a</hask>. For instance, for <hask>reverseT</hask>, <hask>a</hask> is <hask>String->String</hask>. The reason for the <hask>C</hask> prefix is explained below. At the heart of TV is a small algebra for constructing these outputs. Weve already seen one output function, <hask>oTitle</hask>. Another one is <hask>showOut</hask>, which is an output for all <hask>Show</hask> types. For instance, | ||
| Line 54: | Line 62: | ||
</haskell> | </haskell> | ||
| - | + | == Inputs and function-valued outputs == | |
Just as an output is a way to ''deliver'' a value, an "input" is a way to ''obtain'' a value. For example, here are two inputs, each specifying an initial value and a value range, and each given a title. | Just as an output is a way to ''deliver'' a value, an "input" is a way to ''obtain'' a value. For example, here are two inputs, each specifying an initial value and a value range, and each given a title. | ||
| Line 91: | Line 99: | ||
</blockquote> | </blockquote> | ||
| - | + | == A variation == | |
Here is an uncurried variation: | Here is an uncurried variation: | ||
| Line 119: | Line 127: | ||
</blockquote> | </blockquote> | ||
| - | + | == The general story == | |
| - | TVs, outputs and inputs are not restricted to GUIs and IO. In general, they are parameterized by | + | TVs, outputs, and inputs are not restricted to GUIs and IO. In general, they are parameterized by the mechanics of "transmitting values", i.e., delivering ("sinking") output and gathering ("sourcing") input. |
<haskell> | <haskell> | ||
| - | data | + | data Input src a |
| - | data | + | data Output src snk a |
| - | type TV | + | type TV src snk a |
</haskell> | </haskell> | ||
| - | + | The "sources" will be [[applicative functor]]s (AFs), and the "sinks" will be cofunctors. | |
| - | < | + | In the examples above, we've used two different mechanisms, namely [[Phooey]]'s <hask>UI</hask> AF and <hask>IO</hask>. The sinks are counterparts <hask>IU</hask> and <hask>OI</hask>. |
| - | + | ||
| - | </ | + | |
| - | + | ||
| - | + | ||
The functions <hask>runUI</hask> and <hask>runIO</hask> used in examples above are simply type-specialized synonyms for [http://darcs.haskell.org/packages/TV/doc/html/Interface-TV.html#v%3ArunTV <hask>runTV</hask>]. | The functions <hask>runUI</hask> and <hask>runIO</hask> used in examples above are simply type-specialized synonyms for [http://darcs.haskell.org/packages/TV/doc/html/Interface-TV.html#v%3ArunTV <hask>runTV</hask>]. | ||
<haskell> | <haskell> | ||
| - | runUI :: TV UI a -> IO () | + | runUI :: TV UI IU a -> IO () |
runUI = runTV | runUI = runTV | ||
| - | runIO :: TV | + | runIO :: TV IO OI a -> IO () |
runIO = runTV | runIO = runTV | ||
</haskell> | </haskell> | ||
| - | + | == Common Ins and Outs == | |
| - | The examples <hask>reverseT</hask> and <hask>shoppingT</hask> above used not only the generic <hask>Output</hask> and <hask>Input</hask> operations, but also some operations that apply to | + | The examples <hask>reverseT</hask> and <hask>shoppingT</hask> above used not only the generic <hask>Output</hask> and <hask>Input</hask> operations, but also some operations that apply to AFs having a few methods for sourcing and sinking a few common types (strings, readables, showables, and booleans). The type constructors <hask>CInput</hask>, <hask>COutput</hask>, and <hask>CTV</hask> are universally quantified over sources and sinks having the required methods. |
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
<haskell> | <haskell> | ||
| - | type CInput | + | type CInput a = forall src. |
| - | type COutput a = | + | (CommonIns src) => Input src a |
| - | type CTV | + | type COutput a = forall src snk. |
| + | (CommonIns src, CommonOuts snk) => Output src snk a | ||
| + | type CTV a = forall src snk. | ||
| + | (CommonIns src, CommonOuts snk) => TV src snk a | ||
</haskell> | </haskell> | ||
| - | + | == Sorting examples == | |
Here's a sorting TV (see [http://darcs.haskell.org/packages/TV/doc/html/Interface-TV.html#v%3AinteractLineRS <hask>interactLinesRS</hask>]), tested with <hask>runUI</hask>: | Here's a sorting TV (see [http://darcs.haskell.org/packages/TV/doc/html/Interface-TV.html#v%3AinteractLineRS <hask>interactLinesRS</hask>]), tested with <hask>runUI</hask>: | ||
| Line 180: | Line 183: | ||
: <hask>runUI (sortT :: CTV ([String] -> [String]))</hask> | : <hask>runUI (sortT :: CTV ([String] -> [String]))</hask> | ||
| - | + | == Composition of TVs == | |
So far, we done a little composition of interfaces and combined them with values to construct TVs. Now let's look at composition of TVs. | So far, we done a little composition of interfaces and combined them with values to construct TVs. Now let's look at composition of TVs. | ||
| Line 237: | Line 240: | ||
The operator "[http://darcs.haskell.org/packages/DeepArrow/doc/html/Control-Arrow-DeepArrow.html#v%3A-%3E%7C <hask>->|</hask>]" is part of a general approach to value composition from [[DeepArrow]]. | The operator "[http://darcs.haskell.org/packages/DeepArrow/doc/html/Control-Arrow-DeepArrow.html#v%3A-%3E%7C <hask>->|</hask>]" is part of a general approach to value composition from [[DeepArrow]]. | ||
| - | == | + | == Transmission-specific interfaces == |
| - | While some interfaces can be implemented for different | + | While some interfaces can be implemented for different means of transmission, others are more specialized. |
| - | + | === GUIs === | |
| - | Here are inputs for our shopping example above that specifically work with [[Phooey]]'s UI | + | Here are inputs for our shopping example above that specifically work with [[Phooey]]'s UI applicative functor. |
<haskell> | <haskell> | ||
applesU, bananasU :: Input UI Int | applesU, bananasU :: Input UI Int | ||
| Line 268: | Line 271: | ||
'''Note''': We could define other type classes, besides <hask>CommonInsOuts</hask>. For instance, <hask>islider</hask> could be made a method of a <hask>GuiArrow</hask> class, allowing it to be rendered in different ways with different GUI toolkits or even using HTML and Javascript. | '''Note''': We could define other type classes, besides <hask>CommonInsOuts</hask>. For instance, <hask>islider</hask> could be made a method of a <hask>GuiArrow</hask> class, allowing it to be rendered in different ways with different GUI toolkits or even using HTML and Javascript. | ||
| - | + | === IO === | |
| - | We can use <hask>IO</hask> operations in TV interfaces | + | We can use <hask>IO</hask> operations in TV interfaces. The corresponding sink is <hask>OI</hask>, defined in [[TypeCompose]]. TV provides a few functions in its [http://darcs.haskell.org/packages/TV/doc/html/Interface-TV-IO.html <hask>IO</hask> module], including a close counterpart to the standard <hask>interact</hask> function. |
<haskell> | <haskell> | ||
| - | interactOut :: Output | + | interactOut :: Output IO OI (String -> String) |
interactOut = oLambda contentsIn stringOut | interactOut = oLambda contentsIn stringOut | ||
</haskell> | </haskell> | ||
| Line 278: | Line 281: | ||
Assuming we have a file <tt>"test.txt"</tt> containing some lines of text, we can use it to test string transformations. | Assuming we have a file <tt>"test.txt"</tt> containing some lines of text, we can use it to test string transformations. | ||
<haskell> | <haskell> | ||
| - | testO :: Output | + | testO :: Output IO OI (String -> String) |
testO = oLambda (fileIn "test.txt") defaultOut | testO = oLambda (fileIn "test.txt") defaultOut | ||
</haskell> | </haskell> | ||
| - | First, | + | First, let's define higher-order functions that apply another function to the lines or on the words of a string. |
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
<haskell> | <haskell> | ||
onLines, onWords :: ([String] -> [String]) -> (String -> String) | onLines, onWords :: ([String] -> [String]) -> (String -> String) | ||
| - | onLines = | + | onLines f = unlines . f . lines |
| - | onWords = | + | onWords f = unwords . f . words |
</haskell> | </haskell> | ||
| - | + | Next, specializations that operate on ''each'' line or word: | |
<haskell> | <haskell> | ||
perLine,perWord :: (String -> String) -> (String -> String) | perLine,perWord :: (String -> String) -> (String -> String) | ||
| Line 351: | Line 348: | ||
There are more examples [http://darcs.haskell.org/packages/TV/src/Examples.hs in the TV repository] and in the [http://darcs.haskell.org/packages/GuiTV/src/Examples.hs in the GuiTV repository]. See also "[http://journal.conal.net/#%5B%5Bseparating%20IO%20from%20logic%20--%20example%5D%5D separating IO from logic -- example]". | There are more examples [http://darcs.haskell.org/packages/TV/src/Examples.hs in the TV repository] and in the [http://darcs.haskell.org/packages/GuiTV/src/Examples.hs in the GuiTV repository]. See also "[http://journal.conal.net/#%5B%5Bseparating%20IO%20from%20logic%20--%20example%5D%5D separating IO from logic -- example]". | ||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
Revision as of 21:58, 11 September 2007
Contents |
1 Abstract
Warning: The Haddock docs are out of date. I'm trying to get a working haddock 2.0 running (on my windows machine).
TV is a library for composing tangible values ("TVs"), i.e., values that carry along external interfaces. In particular, TVs can be composed to create new TVs, and they can be directly executed with a friendly GUI, a process that reads and writes character streams, or many other kinds interfaces. Values and interfaces are combined for direct use, and separable for composition. This combination makes for software that is ready to use and ready to reuse.
TV can be thought of as a simple functional formulation of the Model-View-Controller pattern. (My thanks to an anonymous ICFP referee for pointing out this connection.) The value part of a TV is the model, and the "interface" part, or "output" as it is called below, is the viewer. Outputs are built up compositionally from other outputs and from inputs (the controllers), as described below.
Besides this wiki page, here are more ways to learn about TV:
- Read the Haddock docs (with source code, additional examples, and Comment/Talk links)
- Get the code repository: darcs get http://darcs.haskell.org/packages/TV
- Or grab a distribution tarball.
- See the version history.
- See the use of TV in Eros.
As of version 0.2, I have moved the GUI functionality out of TV and into a small new package GuiTV. I moved it out to eliminate the dependency of core TV on Phooey and hence on wxHaskell, as the latter can be difficult to install. The GUI examples below require GuiTV.
I'd love to hear your comments at the Talk:TV page.
2 First Example
Here is a tangible reverse function:
reverseT :: CTV (String -> String) reverseT = tv (oTitle "reverse" defaultOut) reverse
Running:
We'll see later that "
runUI reverseT runIO reverseT![]()
*Examples> runIO reverseT reverse: Hello, reversible world. .dlrow elbisrever ,olleH *Examples>
3 Outputs
What I've been calling an "interface" is a value of typetotal :: Show a => COutput a total = oTitle "total" showOut
4 Inputs and function-valued outputs
Just as an output is a way to deliver a value, an "input" is a way to obtain a value. For example, here are two inputs, each specifying an initial value and a value range, and each given a title.
apples, bananas :: CInput Int apples = iTitle "apples" defaultIn bananas = iTitle "bananas" defaultIn
shoppingO :: COutput (Int -> Int -> Int) shoppingO = oTitle "shopping list" $ oLambda apples (oLambda bananas total)
And a TV:
shopping :: CTV (Int -> Int -> Int) shopping = tv shoppingO (+)
Running:
runUI shopping runIO shopping![]()
shopping list: apples: 8 bananas: 5 total: 13
5 A variation
Here is an uncurried variation:
shoppingPr :: CTV ((Int,Int) -> Int) shoppingPr = tv ( oTitle "shopping list -- uncurried" $ oLambda (iPair apples bananas) total ) (uncurry (+))
shoppingPr = uncurryA $$ shopping
Running:
runUI shoppingPr runIO shoppingPr![]()
shopping list: apples: 8 bananas: 5 total: 13
6 The general story
TVs, outputs, and inputs are not restricted to GUIs and IO. In general, they are parameterized by the mechanics of "transmitting values", i.e., delivering ("sinking") output and gathering ("sourcing") input.
data Input src a data Output src snk a type TV src snk a
The "sources" will be applicative functors (AFs), and the "sinks" will be cofunctors.
In the examples above, we've used two different mechanisms, namely Phooey'srunUI :: TV UI IU a -> IO () runUI = runTV runIO :: TV IO OI a -> IO () runIO = runTV
7 Common Ins and Outs
The examplestype CInput a = forall src. (CommonIns src) => Input src a type COutput a = forall src snk. (CommonIns src, CommonOuts snk) => Output src snk a type CTV a = forall src snk. (CommonIns src, CommonOuts snk) => TV src snk a
8 Sorting examples
Here's a sorting TV (see <div class="inline-code">Note that
sortT :: (Read a, Show a, Ord a) => CTV ([a] -> [a]) sortT = tv (oTitle "sort" $ interactLinesRS []) sort![]()
- runUI (sortT :: CTV ([String] -> [String]))
9 Composition of TVs
So far, we done a little composition of interfaces and combined them with values to construct TVs. Now let's look at composition of TVs.
First, wrap up the
unwordsT :: CTV ([String] -> String) unwordsT = tv ( oTitle "function: unwords" $ oLambda (iTitle "words in" defaultIn) (oTitle "sentence out" defaultOut)) unwords![]()
Finally, compose
unwordsT :: CTV ([String] -> String) unwordsT = tv ( oTitle "function: unwords" $ oLambda (iTitle "words in" defaultIn) (oTitle "sentence out" defaultOut)) unwords![]()
sortWordsT :: CTV (String -> String) sortWordsT = wordsT ->| sortT ->| unwordsT
Running:
The operator "<div class="inline-code">
runUI sortWordsT runIO sortWordsT![]()
sentence in: The night Max wore his wolf suit sentence out: Max The his night suit wolf wore
10 Transmission-specific interfaces
While some interfaces can be implemented for different means of transmission, others are more specialized.
10.1 GUIs
Here are inputs for our shopping example above that specifically work with Phooey's UI applicative functor.
applesU, bananasU :: Input UI Int applesU = iTitle "apples" (islider 3 (0,10)) bananasU = iTitle "bananas" (islider 7 (0,10)) shoppingUO :: Output UI (Int -> Int -> Int) shoppingUO = oTitle "shopping list" $ oLambda applesU (oLambda bananasU total)
We can then make curried and uncurried TVs:
Note: We could define other type classes, besides
code runUI rendering tv shoppingUO (+)![]()
uncurryA $$ tv shoppingUO (+)![]()
10.2 IO
We can useinteractOut :: Output IO OI (String -> String) interactOut = oLambda contentsIn stringOut
Assuming we have a file "test.txt" containing some lines of text, we can use it to test string transformations.
testO :: Output IO OI (String -> String) testO = oLambda (fileIn "test.txt") defaultOut
First, let's define higher-order functions that apply another function to the lines or on the words of a string.
onLines, onWords :: ([String] -> [String]) -> (String -> String) onLines f = unlines . f . lines onWords f = unwords . f . words
Next, specializations that operate on each line or word:
perLine,perWord :: (String -> String) -> (String -> String) perLine f = onLines (map f) perWord f = onWords (map f)
Some examples:
string function f runIO (tv test0 f) idTo see a World in a Grain of Sand And a Heaven in a Wild Flower, Hold Infinity in the palm of your hand And Eternity in an hour. - William Blake reverse ekalB mailliW - .ruoh na ni ytinretE dnA dnah ruoy fo mlap eht ni ytinifnI dloH ,rewolF dliW a ni nevaeH a dnA dnaS fo niarG a ni dlroW a ees oT onLines reverse - William Blake And Eternity in an hour. Hold Infinity in the palm of your hand And a Heaven in a Wild Flower, To see a World in a Grain of Sand perLine reverse dnaS fo niarG a ni dlroW a ees oT ,rewolF dliW a ni nevaeH a dnA dnah ruoy fo mlap eht ni ytinifnI dloH .ruoh na ni ytinretE dnA ekalB mailliW - perLine (perWord reverse) oT ees a dlroW ni a niarG fo dnaS dnA a nevaeH ni a dliW ,rewolF dloH ytinifnI ni eht mlap fo ruoy dnah dnA ytinretE ni na .ruoh - mailliW ekalB
There are more examples in the TV repository and in the in the GuiTV repository. See also "separating IO from logic -- example".
Categories: Interfaces | IO | Arrow | Libraries | Packages









