definition 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. A calculus of definitions, with normalisation as evaluation
  2. -------------------------
  3. We present a small calculus with constructors and functions
  4. defined by pattern-matching. The difficulty is to do computations
  5. on open terms and to give the result in the expected form.
  6. Example: f 0 = 0, f (S x) = f x
  7. We may want to evaluate f (S (S X)), X variable. The expected form
  8. for the result is f X.
  9. We give a simple way to implement this language, using the idea
  10. of normalisation as evaluation. We describe a notion of values for the
  11. language, and the normal form of an expression will be its semantics.
  12. More example: we want to be able to define (all definitions are recursive)
  13. f 0 = 0
  14. f (S n) = g n
  15. g 0 = S 0
  16. g (S n) = f n
  17. h 0 = g (f 0)
  18. h (S n) = g (h n)
  19. and then to evaluate symbolically, for instance
  20. h (S (S x))
  21. the result should be g (g (h x))
  22. We want also to have definitions inside a definition. For instance
  23. f x = let
  24. h 0 = x
  25. h (S n) = f (h n)
  26. in h x
  27. If we evaluate symbolically f (S y), the result will be f ((f y).h y)
  28. If we rewrite this naively we get f (h y).
  29. Notice that the name h is local to the function f. We represent this
  30. by writing (f y).h instead of h. We indicate that this is the function
  31. h, which occurs in the definition of f, with the argument y.
  32. 1. Basic language
  33. --------------
  34. Language: syntax of terms
  35. M ::= x | M M | \x M | c M1 ... Mk
  36. The name x can be a variable bound by an abstraction, or by a definition.
  37. The definitions are
  38. E ::= \ x E | D E | H
  39. H ::= M | F
  40. F ::= <c1 E1,...,ck Ek>
  41. D ::= x1=E1,...,xn=En
  42. The definitions are recursive.
  43. V ::= Lam f | N | c V1 ... Vk
  44. N ::= P | N V | P N
  45. P ::= () | P.x | P V
  46. We define [M]r as usual.
  47. [x]r = r(x)
  48. [M1 M2]r = [M1]r ([M2]r)
  49. [\x M]r = Lam f where f V = [M](r,x=V)
  50. The crucial point is the computation of [D](P,r)
  51. [D](P,r) is x1 = V1,...,xn = Vn
  52. where
  53. Vi = [Ei](P.xi,r') with r' = r,x1=V1,...,xn=Vn
  54. [\x E](P,r) = Lam f where f V = [E](P V,(r,x=V))
  55. [D E] (P,r) = [E](P,r') where r' = r + [D](P,r)
  56. [M](P,r) = [M]r
  57. [<c1 E1,...,ck Ek>](P,r) = Lam f
  58. where f (ci w) = [Ei](P,r) w
  59. f V = P V if V is not of the form ci w
  60. 2. Addition of infinite objects
  61. -----------------------------
  62. To see how we can represent infinite objects, we can add records
  63. (that may be infinite)
  64. M ::= x | M M | \x M | c M1 ... Mk | M.x
  65. E ::= \ x E | D E | H
  66. H ::= M | F | R
  67. R ::= (x1=M1,...,xn=Mn)
  68. The new values are
  69. V ::= Lam f | N | c V1 ... Vk | (P,W)
  70. N ::= P | N V | P N | N.x
  71. P ::= () | P.x | P V
  72. W ::= (x1 = V1,...,xn=Vn)
  73. We have added "infinite values" (P,W) that have
  74. two components.
  75. If R = (x1=M1,...,xn=Mn) we define [R](P,r) as
  76. (P,(x1 = [M1]r,...,xn = [Mn]r))
  77. Examples:
  78. f = (u = f,v = 0)
  79. This is an infinite object. If we compute [f] we get
  80. (f,(u=[f],v=0))
  81. If we compute [f.u] we get the same as [f] and if we
  82. compute [f.v] we get 0.
  83. Another example is
  84. f = \ x (u = f 0,v = f (f x))
  85. We can then form
  86. f (S 0)
  87. the value is [f (S 0)] = (f (S 0),V) where
  88. V = (u = [f 0],v = [f (f (S 0))])
  89. Thus if we take (f (S 0)).u we get something of the form
  90. (f 0,...).
  91. 3. Package
  92. -------
  93. We can package the definitions arbitrarily, with parameters. This is actually
  94. definable as soon as we have records.
  95. p = \ x1...\ xk D1...Dn (y1 = y1,...,ym = ym)
  96. where y1,...,ym are the definitions among D1,...,Dn that we want to make
  97. accessible for outside.
  98. For instance
  99. p = \ x
  100. let
  101. w = (u = x,v = f 0)
  102. f 0 = x
  103. f (S n) = (u = f n,v = w)
  104. in
  105. (w=w,f = f)
  106. We can then consider
  107. (p 0).f (S 0)
  108. which has for values
  109. (p 0).f (S 0), (u = 0,v = (p 0).w)
  110. Thus if we compute ((p 0).f (S 0)).v we get (p 0).w