Advertisement
tinyevil

Untitled

Feb 25th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. class Expr{
  2. public:
  3. Expr typecheck(Context&);
  4. Expr nf(Context&);
  5. Expr subst(int index, Expr);
  6. bool depends(int index);
  7. };
  8.  
  9. class LambdaExpr: public Expr{
  10. public:
  11.  
  12. Expr typecheck(Context&){
  13. Expr bodyType = c.withBinding(paramType, [](Context& c){
  14. return body.typecheck(c);
  15. });
  16. return ArrowExpr(paramType, bodyType);
  17. }
  18.  
  19. Expr nf(Context& c){
  20. Expr paramNF = paramType.nf(c);
  21. return LambdaExpr(paramNF, c.withBinding(paramNF, [](Context& c){
  22. return body.nf(c)
  23. });
  24. }
  25.  
  26. Expr subst(int index, Expr e){
  27. return LambdaExpr(paramType.subst(index, e), body.subst(index + 1, e));
  28. }
  29.  
  30. bool depends(int index){
  31. return paramType.depends(index) || body.depends(index + 1)
  32. }
  33.  
  34. private:
  35. Expr paramType;
  36. Expr body;
  37. };
  38.  
  39.  
  40. class GlobalRefExpr: public Expr{
  41. public:
  42.  
  43. Expr typecheck(Context&){
  44. return decl->type;
  45. }
  46.  
  47. Expr nf(Context& c){
  48. return decl->expr.nf(c);
  49. }
  50.  
  51. Expr subst(int index, Expr e){
  52. return decl->expr;
  53. }
  54.  
  55. bool depends(int index){
  56. return false;
  57. }
  58.  
  59. private:
  60. Decl* decl;
  61. };
  62.  
  63. class TypeExpr: public Expr{
  64. public:
  65. Expr typecheck(Context&){
  66. return TypeExpr(level + 1);
  67. }
  68.  
  69. Expr nf(Context& c){
  70. return this;
  71. }
  72.  
  73. Expr subst(int index, Expr e){
  74. return this;
  75. }
  76.  
  77. bool depends(int index){
  78. return false;
  79. }
  80.  
  81. private:
  82. int level;
  83. };
  84.  
  85.  
  86. class BuiltinNat: public Expr{
  87. public:
  88.  
  89. Expr typecheck(Context&){
  90. return TypeExpr(0);
  91. }
  92.  
  93. Expr nf(Context& c){
  94. return this;
  95. }
  96.  
  97. Expr subst(int index, Expr e){
  98. return this;
  99. }
  100.  
  101. bool depends(int index){
  102. return false;
  103. }
  104. };
  105.  
  106. class NatValue: public Expr{
  107. public:
  108.  
  109. Expr typecheck(Context&){
  110. return BuiltinNat();
  111. }
  112.  
  113. Expr nf(Context& c){
  114. return this;
  115. }
  116.  
  117. Expr subst(int index, Expr e){
  118. return this;
  119. }
  120.  
  121. bool depends(int index){
  122. return false;
  123. }
  124.  
  125. private:
  126. int val;
  127. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement