Tangible Value
From HaskellWiki
m (→Sorting examples) |
m (→Sorting examples) |
||
| Line 158: | Line 158: | ||
=== Sorting examples === | === Sorting examples === | ||
| - | Here's a sorting TV (see [http://darcs.haskell.org/packages/TV/doc/html/Interface-TV.html#v% | + | 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>: |
<blockquote> | <blockquote> | ||
Revision as of 01:05, 11 February 2007
Contents |
1 Abstract
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.
TV is for creating software that is ready to use and ready to reuse.
Beside this page, here are some ways to explore 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.
2 Tangible values
2.1 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>
2.2 Outputs
What I've been calling an "interface" is a value of typetotal :: Show a => COutput a total = oTitle "total" showOut
2.3 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
2.4 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
2.5 The general story
TVs, outputs and inputs are not restricted to GUIs and IO. In general, theyre parameterized by an arrow.
data Output (~>) a data Input (~>) a type TV (~>) a
type KIO = Kleisli IO
runUI :: TV UI a -> IO () runUI = runTV runIO :: TV KIO a -> IO () runIO = runTV
2.6 Common Ins and Outs
The examplestype Common f a = forall (~>). CommonInsOuts (~>) => f (~>) a
type CInput a = Common Input a type COutput a = Common Output a type CTV a = Common TV a
2.7 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]))
2.8 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![]()
*Examples> runIO sortWordsT sentence in: The night Max wore his wolf suit sentence out: Max The his night suit wolf wore
2.9 Arrow-specific interfaces
While some interfaces can be implemented for different kinds of interfaces, others are more specialized.
2.9.1 GUIs
Here are inputs for our shopping example above that specifically work with Phooey's UI arrow.
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 (+)![]()
2.9.2 IO
We can useinteractOut :: Output KIO (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 KIO (String -> String) testO = oLambda (fileIn "test.txt") defaultOut
wrapF :: (c->d) -> (a->b) -> ((b->c) -> (a->d)) wrapF after before f = after . f . before
onLines, onWords :: ([String] -> [String]) -> (String -> String) onLines = wrapF unlines lines onWords = wrapF unwords words
And 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.
Categories: Interfaces | User Interfaces | IO | Arrow | Libraries | Packages









