-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interpret.hs
120 lines (97 loc) · 2.92 KB
/
Interpret.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE FlexibleContexts #-}
module Interpret where
-- import TransducerAlgOpr
import Transducer
-- import Nondeterministic
import Nat
import Log
-- import FuncType
import Pointed
import Arrow
import MyShow
import Control.Monad.Writer.Class
import Control.Monad.State.Class
-- for wires of transducer
type VarName = String
type Flow = (Maybe VarName, Nat)
type TdGoI m x = Td m x Flow Flow
runGoI :: (Monad m, Pointed x) => TdGoI m x -> Nat -> m (Nat, x)
runGoI = runTd . hideVar
hideVar :: (Monad m) => TdGoI m x -> Td m x Nat Nat
hideVar = mapTd before after
where
before n = (Nothing, n)
after (Nothing, n) = n
after (Just var, n) = error "Oops >_<"
-- give name, and watch boundaries
watchBounds :: (MonadWriter Log m, MyShow x) =>
String -> TdGoI m x -> TdGoI m x
watchBounds name t = Td $ \ a -> do
x0 <- get
tell $ Log -- $ if bad a then [] else
[ LogStrLn $ "---- " ++ show (snd a) ++ " --->"
, LogStrLn $ showIn (fst a) ++ ":[ " ++ name ++ " @ " ++ myShow x0 ++ " ]"
, Indent
]
b <- getTd t a
x1 <- get
tell $ Log -- $ if bad b then [] else
[ Unindent
, LogStrLn $ "[ " ++ name ++ " @ " ++ myShow x1 ++ " ]:" ++ showOut (fst b)
, LogStrLn $ "---- " ++ show (snd b) ++ " --->"
]
return b
where
showIn, showOut :: Maybe VarName -> String
showIn Nothing = "Query"
showIn (Just var) = "Answer " ++ var
showOut Nothing = "Answer"
showOut (Just var) = "Query " ++ var
-- bad (Nothing, n) = case psi n of
-- Left _ -> True
-- Right k -> case psi k of
-- Left _ -> True
-- Right _ -> False
-- bad (Just _, _) = False
{- functions h, k_i, sum -}
hh :: Either Nat Nat -> Either Nat Nat
hh = either
(either
(Left . phi . Right . phi . Left)
(either
(Left . phi . Left)
(Right)
. psi)
. psi)
(Left . phi . Right . phi . Right)
kk :: Nat -> Nat -> Nat
kk i = uu . (id *** const i) . vv
data Trit = First | Second | Third
deriving Eq
summ :: (Trit, Nat) -> (Trit, Nat)
summ (First, n) = (Second, n)
summ (Second, n) = (Third, uu (n, 0))
summ (Third, i) = let
(j, l) = vv i
(n, m) = vv j
in (First, uu (n, m + l))
{- functions using u, v, phi, psi -}
ee' :: Nat -> Nat
ee' = snd . vv
ee :: Nat -> Nat
ee = uu . (\ n -> (0, n)) -- why (1, n) in the paper?
cc' :: Nat -> Either Nat Nat
cc' = (uu +++ uu) . distribute . (psi *** id) . vv
cc :: Either Nat Nat -> Nat
cc = uu . (phi *** id) . distribute' . (vv +++ vv)
distribute :: (Either a b, c) -> Either (a, c) (b, c)
distribute (Left a, c) = Left (a, c)
distribute (Right b, c) = Right (b, c)
distribute' :: Either (a, c) (b, c) -> (Either a b, c)
distribute' (Left (a, c)) = (Left a, c)
distribute' (Right (b, c)) = (Right b, c)
dd' :: Nat -> Nat
dd' = uu . (id *** uu) . (\ ((a, b), c) -> (a, (b, c))) . (vv *** id) . vv
dd :: Nat -> Nat
dd = uu . (uu *** id) . (\ (a, (b, c)) -> ((a, b), c)) . (id *** vv) . vv