/ReductionSemantics.lhs

http://sauce-code.googlecode.com/ · Haskell · 114 lines · 106 code · 8 blank · 0 comment · 29 complexity · 4b970a0ad08f2cd1d83e24116e24539f MD5 · raw file

  1. \begin{code}
  2. module ReductionSemantics where
  3. import qualified AbstractSyntax as S
  4. import qualified EvaluationContext as E
  5. import qualified StructuralOperationalSemantics as S
  6. import qualified IntegerArithmetic as I
  7. makeEvalContext :: S.Term -> Maybe (S.Term, E.Context)
  8. makeEvalContext t = case t of
  9. S.App (S.Abs x tau11 t12) t2
  10. | S.isValue t2 -> Just (t, E.Hole)
  11. S.App t1 t2
  12. | S.isValue t1 -> case makeEvalContext t2 of
  13. Just (t2', c2) -> Just (t2', (E.AppV t1 c2))
  14. _ -> Nothing
  15. | otherwise -> case makeEvalContext t1 of
  16. Just(t1', c1) -> Just (t1', (E.AppT c1 t2))
  17. _ -> Nothing
  18. S.If (S.Tru) t2 t3 -> Just (t, E.Hole)
  19. S.If (S.Fls) t2 t3 -> Just (t, E.Hole)
  20. S.If t1 t2 t3 -> case makeEvalContext t1 of
  21. Just(t1', c1) -> Just (t1', (E.If c1 t2 t3))
  22. _ -> Nothing
  23. S.IntAdd t1 t2
  24. | S.isValue t1 -> case makeEvalContext t2 of
  25. Just(t2', c2) -> Just(t2', (E.IntAddV t1 c2))
  26. Nothing -> Just(t, E.Hole)
  27. | otherwise -> case makeEvalContext t1 of
  28. Just(t1', c1) -> Just(t1', (E.IntAddT c1 t2))
  29. _ -> Nothing
  30. S.IntSub t1 t2
  31. | S.isValue t1 -> case makeEvalContext t2 of
  32. Just(t2', c2) -> Just(t2', (E.IntSubV t1 c2))
  33. Nothing -> Just(t, E.Hole)
  34. | otherwise -> case makeEvalContext t1 of
  35. Just(t1', c1) -> Just(t1', (E.IntSubT c1 t2))
  36. _ -> Nothing
  37. S.IntMul t1 t2
  38. | S.isValue t1 -> case makeEvalContext t2 of
  39. Just(t2', c2) -> Just(t2', (E.IntMulV t1 c2))
  40. Nothing -> Just(t, E.Hole)
  41. | otherwise -> case makeEvalContext t1 of
  42. Just(t1', c1) -> Just(t1', (E.IntMulT c1 t2))
  43. _ -> Nothing
  44. S.IntDiv t1 t2
  45. | S.isValue t1 -> case makeEvalContext t2 of
  46. Just(t2', c2) -> Just(t2', (E.IntDivV t1 c2))
  47. Nothing -> Just(t, E.Hole)
  48. | otherwise -> case makeEvalContext t1 of
  49. Just(t1', c1) -> Just(t1', (E.IntDivT c1 t2))
  50. _ -> Nothing
  51. S.IntNand t1 t2
  52. | S.isValue t1 -> case makeEvalContext t2 of
  53. Just(t2', c2) -> Just(t2', (E.IntNandV t1 c2))
  54. Nothing -> Just(t, E.Hole)
  55. | otherwise -> case makeEvalContext t1 of
  56. Just(t1', c1) -> Just(t1', (E.IntNandT c1 t2))
  57. _ -> Nothing
  58. S.IntEq t1 t2
  59. | S.isValue t1 -> case makeEvalContext t2 of
  60. Just(t2', c2) -> Just(t2', (E.IntEqV t1 c2))
  61. Nothing -> Just(t, E.Hole)
  62. | otherwise -> case makeEvalContext t1 of
  63. Just(t1', c1) -> Just(t1', (E.IntEqT c1 t2))
  64. _ -> Nothing
  65. S.IntLt t1 t2
  66. | S.isValue t1 -> case makeEvalContext t2 of
  67. Just(t2', c2) -> Just(t2', (E.IntLtV t1 c2))
  68. Nothing -> Just(t, E.Hole)
  69. | otherwise -> case makeEvalContext t1 of
  70. Just(t1', c1) -> Just(t1', (E.IntLtT c1 t2))
  71. _ -> Nothing
  72. S.Let x t1 t2
  73. | S.isValue t1 -> Just (t, E.Hole)
  74. | otherwise -> case makeEvalContext t1 of
  75. Just(t1', c1) -> Just(t1', (E.Let x c1 t2))
  76. _ -> Nothing
  77. S.Fix (S.Abs x tau1 t2) -> Just (t, E.Hole)
  78. S.Fix t -> case makeEvalContext t of
  79. Just(t', c) -> Just(t', E.Fix c)
  80. _ -> Nothing
  81. _ -> Nothing
  82. makeContractum :: S.Term -> S.Term
  83. makeContractum t = case t of
  84. S.App (S.Abs x tau11 t12) t2 -> S.subst x t2 t12
  85. S.If (S.Tru) t2 t3 -> t2
  86. S.If (S.Fls) t2 t3 -> t3
  87. S.IntAdd (S.IntConst n1) (S.IntConst n2) -> S.IntConst (I.intAdd n1 n2)
  88. S.IntSub (S.IntConst n1) (S.IntConst n2) -> S.IntConst (I.intSub n1 n2)
  89. S.IntMul (S.IntConst n1) (S.IntConst n2) -> S.IntConst (I.intMul n1 n2)
  90. S.IntDiv (S.IntConst n1) (S.IntConst n2) -> S.IntConst (I.intDiv n1 n2)
  91. S.IntNand (S.IntConst n1) (S.IntConst n2) -> S.IntConst (I.intNand n1 n2)
  92. S.IntEq (S.IntConst n1) (S.IntConst n2) -> if I.intEq n1 n2 then S.Tru else S.Fls
  93. S.IntLt (S.IntConst n1) (S.IntConst n2) -> if I.intLt n1 n2 then S.Tru else S.Fls
  94. S.Let x t1 t2 -> S.subst x t1 t2
  95. S.Fix (S.Abs x tau1 t2) -> S.subst x (S.Fix (S.Abs x tau1 t2)) t2
  96. textualMachineStep :: S.Term -> Maybe S.Term
  97. textualMachineStep t =
  98. case makeEvalContext t of
  99. Just(t1, c) -> Just (E.fillWithTerm c (makeContractum t1))
  100. Nothing -> Nothing
  101. textualMachineEval :: S.Term -> S.Term
  102. textualMachineEval t =
  103. case textualMachineStep t of
  104. Just t' -> textualMachineEval t'
  105. Nothing -> t
  106. \end{code}