43d29f448ecdc2a0dda0a520960c8f369114e4a5
[coq-hetmet.git] / examples / IFLDemos.hs
1 {-# OPTIONS_GHC -XModalTypes -dcore-lint -XScopedTypeVariables -ddump-types -XTypeFamilies -XNoMonomorphismRestriction #-}
2 module Demo (demo, demo2) where
3
4 demo  z   = <[ \y -> ~~z ]>
5
6 demo2 :: <[ (a,b) ~~> c ]>@d -> <[ () ~~> a ]>@d -> <[ b ~~>c ]>@d
7 demo2 x y = <[ ~~x ~~y ]>
8
9 swap :: <[ (a,(b,c)) ~~> d ]>@e -> <[ (b,(a,c)) ~~> d ]>@e
10 swap f = <[ \x -> \y -> ~~f y x ]>
11
12 -- bad = <[ \f -> \x -> f x ]>
13
14 demo3 x y z q = <[ ~~q (~~x ~~y ~~z) ]>
15
16
17
18 class BitSerialHardwarePrimitives g where
19   type Wire
20
21   <[ not ]>    :: <[             (Wire,())    ~~> Wire ]>@g
22   <[ xor ]>    :: <[       (Wire,(Wire,()))   ~~> Wire ]>@g
23   <[ or  ]>    :: <[       (Wire,(Wire,()))   ~~> Wire ]>@g
24   <[ and ]>    :: <[       (Wire,(Wire,()))   ~~> Wire ]>@g
25   <[ mux2 ]>   :: <[ (Wire,(Wire,(Wire,())))  ~~> Wire ]>@g
26   <[ maj3 ]>   :: <[ (Wire,(Wire,(Wire,())))  ~~> Wire ]>@g
27   <[ reg ]>    :: <[             (Wire,())    ~~> Wire ]>@g
28   <[ zero ]>   :: <[             ()    ~~> Wire ]>@g
29   <[ one ]>    :: <[             ()    ~~> Wire ]>@g
30
31   loop   :: [Bool] -> <[ () ~~> Wire ]>@g
32   <[ lfsr ]>   :: Int ->  [ <[ Wire ]>@g ]
33   <[ adder ]>  :: <[  (Wire,(Wire,())) ~~> Wire ]>@g
34   fifo         :: Int -> <[  (Wire,()) ~~> Wire ]>@g
35
36   <[ probe ]>  :: Int -> <[ (Wire,())  ~~> Wire ]>@g
37   <[ oracle ]> :: Int -> <[                Wire ]>@g
38
39 xor3 :: forall g . BitSerialHardwarePrimitives g => <[ (Wire,(Wire,(Wire,()))) ~~> Wire ]>@g
40 xor3 = <[ \x -> \y -> \z -> xor (xor x y) z ]>
41
42 adder =
43    <[ \in1 ->
44       \in2 ->
45       let firstBitMarker = ~~(loop [ i/=0 | i <- [0..31] ])
46           carry_out      = reg (mux2 firstBitMarker zero carry_in)
47           carry_in       = maj3 carry_out in1 in2
48       in  ~~xor3 carry_out in1 in2
49     ]>
50
51
52 rotRight n =
53   <[ \input ->
54      let sel   = ~~(loop [ i >= 32-n | i<-[0..31] ])
55          fifo1 = ~~(fifo (32-n)) input
56          fifo2 = ~~(fifo  32   ) fifo1
57      in  mux2 sel fifo1 fifo2
58    ]>
59
60 sha256round =
61   <[ \load ->
62      \input ->
63      \k_plus_w ->
64      let a    = ~~(fifo 32) (mux2 load a_in input)
65          b    = ~~(fifo 32) a
66          c    = ~~(fifo 32) b
67          d    = ~~(fifo 32) c
68          e    = ~~(fifo 32) (mux2 load e_in d)
69          f    = ~~(fifo 32) e
70          g    = ~~(fifo 32) f
71          h    = ~~(fifo 32) g
72          s0   = ~~xor3
73                     (~~(rotRight  2) a_in)
74                     (~~(rotRight 13) a_in)
75                     (~~(rotRight 22) a_in)
76          s1   = ~~xor3
77                     (~~(rotRight  6) e_in)
78                     (~~(rotRight 11) e_in)
79                     (~~(rotRight 25) e_in)
80          a_in = adder t1 t2
81          e_in = adder t1 d
82          t1   = adder
83                    (adder h s1)
84                    (adder (mux2 e g f)
85                           k_plus_w)
86          t2   = adder s0 (maj3 a b c)
87      in h
88    ]>