Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Modified languages example.

  • Loading branch information...
commit e3086944b9ac368b247e0aaf81731292dbbc43d9 1 parent f035b68
Lee Pike authored January 12, 2012
47  Examples/Languages.hs
@@ -2,7 +2,7 @@
2 2
 -- an external variable.  Assume the input doesn't given tokens outside the
3 3
 -- alphabet, and the result is always delayed by one w.r.t. the input stream.
4 4
 
5  
--- I think Copilot can express polynomial-time algorithms.
  5
+-- I think Copilot can recognize context-sensitive grammars.
6 6
 
7 7
 {-# LANGUAGE RebindableSyntax #-}
8 8
 
@@ -113,11 +113,50 @@ csAccept n = do
113 113
 -- Context-sensitive grammars
114 114
 
115 115
 {-
116  
-I don't think the "copy language" <xx | x \in {0,1}*> can be recognized by a
117  
-Copilot program.  It requires non-constant space.  Note: some context-sensitive
118  
-grammars have PSACE recognizers.
  116
+This Copilot program recognizes the "copy language" <xx | x \in {0,1}*>.
  117
+
  118
+Note: the "trick" is to encode the history of streams in a bitvector.  Thus, we
  119
+can only recognize arbitrarily long words if we have arbitrarily long
  120
+bitvectors.  There is nothing in Copilot preventing this, but the largest base
  121
+type is currently a Word64.  
  122
+
  123
+Without this encoding, we couldn't build a recognizers, because we can't
  124
+generate new streams on the fly or look back arbitrarily far in the history of a
  125
+stream; both are fixed at compile time.
  126
+
119 127
 -}
120 128
 
  129
+
  130
+copyAccept :: Spec
  131
+copyAccept = do
  132
+  observer "accept" accept
  133
+  observer "hist" hist
  134
+  observer "string" string
  135
+  observer "cnt" cnt
  136
+  where
  137
+
  138
+  accept :: Stream Bool
  139
+  accept = if cnt `mod` 2 == 1 then false else bottom == top 
  140
+    where
  141
+    halfCnt  = cnt `div` 2
  142
+    zeroBot  = (complement $ (2^halfCnt) - 1) .&. hist
  143
+    top      = zeroBot .>>. halfCnt
  144
+    bottom   = hist - zeroBot
  145
+
  146
+  hist :: Stream Word64
  147
+  hist = [0] ++ ((2^cnt) * cast string) + hist
  148
+
  149
+  cnt :: Stream Word64
  150
+  cnt = [0] ++ cnt + 1
  151
+
  152
+  -- Input tokens.
  153
+  string :: Stream Word8
  154
+  string = let x = [1,0,0,1,0,1] in 
  155
+           x P.++ x
  156
+             ++ 0 -- don't care about part of
  157
+                  -- stream after ++
  158
+
  159
+
121 160
 ---------------------------------------------------------------------------------
122 161
 -- -- Recognize the language of arbitrarily long sequence of prime numbers.
123 162
 
2  copilot.cabal
... ...
@@ -1,5 +1,5 @@
1 1
 name:                copilot
2  
-version:             2.0.3
  2
+version:             2.0.4
3 3
 cabal-version:       >= 1.10
4 4
 license:             BSD3
5 5
 license-file:        LICENSE

0 notes on commit e308694

Please sign in to comment.
Something went wrong with that request. Please try again.