-
Notifications
You must be signed in to change notification settings - Fork 4
/
Intro3.hs
66 lines (49 loc) · 1.66 KB
/
Intro3.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
{-# LANGUAGE NoMonomorphismRestriction #-}
module Intro3 where
-- * Tagless Typed Interpreters, introduction
-- * Initial and Final, first-class
import qualified Intro1 as I
import qualified Intro2 as F
import Intro1 (Exp(..))
import Intro2 (ExpSYM(..))
-- Remind what is initial Exp, and what is a final ExpSYM.
-- Discuss `initial' vs `final' from the categorical view.
-- A bit informally, I would be calling ExpSYM `final'
-- (waving hands over the existence of the final co-algebra)
-- We can put expressions represented as a data type in the same
-- list
til1 = [Lit 1, Add (Lit 1) (Lit 3), I.ti1]
-- And we can evaluate them uniformly
til1_eval = map I.eval til1
-- [1,4,5]
til1_view = map I.view til1
-- ["1","(1 + 3)","(8 + (-(1 + 2)))"]
-- * //
-- Can we do the same for expressions represented in the final form?
tfl1 = [lit 1, add (lit 1) (lit 3), F.tf1]
stringId = F.view
tfl1_eval = map F.eval tfl1
-- [1,4,5]
tfl1_view = map F.view tfl1
-- ["1","(1 + 3)","(8 + (-(1 + 2)))"]
-- See the type of tfl1...
-- *Main> :t tfl1
-- tfl1 :: (ExpSYM repr) => [repr]
-- * //
-- Further questions about the final representation, in particular with
-- respect to pattern-match and expressing operations that are not fold
-- * How to pattern-match on terms?
-- * How to process expressions unfold-like
-- * How to compare terms for equality?
-- * What if our language is higher-order?
-- * What if the language is typed?
-- (Incidentally, it is this question that has prompted our research.
-- That's where the label `tagless typed' comes from.)
{-
main = do
print til1_eval
print til1_view
print tfl1_eval
print tfl1_view
-}
-- LocalWords: ExpSYM