mbazs

Coq bits

Aug 10th, 2020 (edited)
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ----- Original Coq source -----
  2. Require Extraction.
  3. Extraction Language Haskell.
  4. Set Extraction Optimize.
  5.  
  6. Module Bits.
  7.  
  8. Inductive bit: Type :=
  9. | B0
  10. | B1.
  11.  
  12. Inductive nibble: Type :=
  13. | bits (b0 b1 b2 b3: bit).
  14.  
  15. Definition nibble_zero (n : nibble): bool :=
  16.   match n with
  17.     | (bits B0 B0 B0 B0) => true
  18.     | (bits _ _ _ _) => false
  19.   end.
  20.  
  21. Definition nibble_odd (n: nibble): bool :=
  22.   match n with
  23.     | (bits _ _ _ B1) => true
  24.     | (bits _ _ _ _) => false
  25.   end.
  26.  
  27. Definition nibble_even (n: nibble): bool :=
  28.   negb (nibble_odd n).
  29.  
  30. Definition nibble_chess (n: nibble): bool :=
  31.   match n with
  32.     | (bits B0 B1 B0 B1) => true
  33.     | (bits B1 B0 B1 B0) => true
  34.     | _ => false
  35.   end.
  36.  
  37. Theorem test_nibble_zeroity: (nibble_zero (bits B0 B0 B0 B0)) = true.
  38.   reflexivity.
  39. Qed.
  40.  
  41. Theorem test_nibble_oddity: (nibble_odd (bits B1 B1 B0 B1)) = true.
  42.   reflexivity.
  43. Qed.
  44.  
  45. Theorem test_nibble_evenity: (nibble_even (bits B1 B0 B1 B0)) = true.
  46.   reflexivity.
  47. Qed.
  48.  
  49. Theorem test_nibble_chessity: (nibble_chess (bits B1 B0 B1 B0)) = true.
  50.   reflexivity.
  51. Qed.
  52.  
  53. End Bits.
  54.  
  55. Recursive Extraction Bits.
  56.  
  57. ----- Converted to Haskell -----
  58.  
  59. module Main where
  60.  
  61. import qualified Prelude
  62.  
  63. data Bool =
  64.    True
  65.  | False
  66.  
  67. negb :: Bool -> Bool
  68. negb b =
  69.   case b of {
  70.    True -> False;
  71.    False -> True}
  72.  
  73. data Bit =
  74.    B0
  75.  | B1
  76.  
  77. bit_rect :: a1 -> a1 -> Bit -> a1
  78. bit_rect f f0 b =
  79.   case b of {
  80.    B0 -> f;
  81.    B1 -> f0}
  82.  
  83. bit_rec :: a1 -> a1 -> Bit -> a1
  84. bit_rec =
  85.   bit_rect
  86.  
  87. data Nibble =
  88.    Bits Bit Bit Bit Bit
  89.  
  90. nibble_rect :: (Bit -> Bit -> Bit -> Bit -> a1) -> Nibble -> a1
  91. nibble_rect f n =
  92.   case n of {
  93.    Bits x x0 x1 x2 -> f x x0 x1 x2}
  94.  
  95. nibble_rec :: (Bit -> Bit -> Bit -> Bit -> a1) -> Nibble -> a1
  96. nibble_rec =
  97.   nibble_rect
  98.  
  99. nibble_zero :: Nibble -> Bool
  100. nibble_zero n =
  101.   case n of {
  102.    Bits b0 b1 b2 b3 ->
  103.     case b0 of {
  104.      B0 ->
  105.       case b1 of {
  106.        B0 ->
  107.         case b2 of {
  108.          B0 -> case b3 of {
  109.                 B0 -> True;
  110.                 B1 -> False};
  111.          B1 -> False};
  112.        B1 -> False};
  113.      B1 -> False}}
  114.  
  115. nibble_odd :: Nibble -> Bool
  116. nibble_odd n =
  117.   case n of {
  118.    Bits _ _ _ b3 -> case b3 of {
  119.                      B0 -> False;
  120.                      B1 -> True}}
  121.  
  122. nibble_even :: Nibble -> Bool
  123. nibble_even n =
  124.   negb (nibble_odd n)
  125.  
  126. nibble_chess :: Nibble -> Bool
  127. nibble_chess n =
  128.   case n of {
  129.    Bits b0 b1 b2 b3 ->
  130.     case b0 of {
  131.      B0 ->
  132.       case b1 of {
  133.        B0 -> False;
  134.        B1 ->
  135.         case b2 of {
  136.          B0 -> case b3 of {
  137.                 B0 -> False;
  138.                 B1 -> True};
  139.          B1 -> False}};
  140.      B1 ->
  141.       case b1 of {
  142.        B0 ->
  143.         case b2 of {
  144.          B0 -> False;
  145.          B1 -> case b3 of {
  146.                 B0 -> True;
  147.                 B1 -> False}};
  148.        B1 -> False}}}
  149.  
Add Comment
Please, Sign In to add comment