@@ -10,7 +10,7 @@ Cell : Nat -> Type
1010Cell n = Maybe (Fin n)
1111
1212data Board : Nat -> Type where
13− MkBoard : {n : Nat} -> Vect (Vect (Cell n) n) n -> Board n
13+ MkBoard : {n : Nat} -> Vect n (Vect n (Cell n)) -> Board n
1414
1515emptyBoard : Board n
1616emptyBoard {n=n} = MkBoard (replicate n (replicate n Nothing))
@@ -19,10 +19,11 @@ showElt : Cell n -> String
1919showElt Nothing = "."
2020showElt (Just x) = show (1 + (the Int (fromInteger (cast x))))
2121
22−showRow : Vect (Cell n) n -> String
23−showRow xs = unwords (toList (map showElt xs))
22+-- FIXME: Inline type decl should not be necessary here
23+showRow : Vect n (Cell n) -> String
24+showRow {n=n} xs = unwords (toList (the (Vect n String) (map showElt xs)))
2425
25−unlines : Vect String n -> String
26+unlines : Vect n String -> String
2627unlines Nil = ""
2728unlines (l::Nil) = l
2829unlines (l::ls) = pack (foldl addLine (unpack l) (map unpack ls))
@@ -33,8 +34,8 @@ unlines (l::ls) = pack (foldl addLine (unpack l) (map unpack ls))
3334instance Show (Board n) where
3435 show (MkBoard rs) = unlines (map showRow rs)
3536
36−updateAt : Fin n -> Vect a n -> (a -> a) -> Vect a n
37−updateAt fO (x::xs) f = f x :: xs
37+updateAt : Fin n -> Vect n a -> (a -> a) -> Vect n a
38+updateAt fZ (x::xs) f = f x :: xs
3839updateAt (fS i) (x::xs) f = x :: updateAt i xs f
3940
4041setCell : Board n -> (Fin n, Fin n) -> Fin n -> Board n
@@ -43,17 +44,17 @@ setCell (MkBoard b) (x, y) value = MkBoard (updateAt y b (\row => updateAt x row
4344getCell : Board n -> (Fin n, Fin n) -> Cell n
4445getCell (MkBoard b) (x, y) = index x (index y b)
4546
46−anyElim : {xs : Vect a n} -> {P : a -> Type} -> (Any P xs -> b) -> (P x -> b) -> Any P (x :: xs) -> b
47+anyElim : {xs : Vect n a} -> {P : a -> Type} -> (Any P xs -> b) -> (P x -> b) -> Any P (x :: xs) -> b
4748anyElim _ f (Here p) = f p
4849anyElim f _ (There p) = f p
4950
50−getRow : Fin n -> Board n -> Vect (Cell n) n
51+getRow : Fin n -> Board n -> Vect n (Cell n)
5152getRow i (MkBoard b) = index i b
5253
53−getCol : Fin n -> Board n -> Vect (Cell n) n
54+getCol : Fin n -> Board n -> Vect n (Cell n)
5455getCol i (MkBoard b) = helper i b
5556 where
56− helper : Fin n -> Vect (Vect a n) m -> Vect a m
57+ helper : Fin n -> Vect m (Vect n a) -> Vect m a
5758 helper _ Nil = Nil
5859 helper i (xs::xss) = index i xs :: helper i xss
5960
@@ -115,8 +116,8 @@ FullBoard (MkBoard b) = All (All Filled) b
115116fullBoard : (b : Board n) -> Dec (FullBoard b)
116117fullBoard (MkBoard b) = all (all filled) b
117118
118−fins : Vect (Fin n) n
119−fins {n=O} = Nil
119+fins : Vect n (Fin n)
120+fins {n=Z} = Nil
120121fins {n=(S m)} = last :: map weaken fins
121122
122123data LegalBoard : Board n -> Type where
@@ -126,21 +127,21 @@ data LegalBoard : Board n -> Type where
126127CompleteBoard : Board n -> Type
127128CompleteBoard b = (LegalBoard b, FullBoard b)
128129
129−indexStep : {i : Fin n} -> {xs : Vect a n} -> {x : a} -> index i xs = index (fS i) (x::xs)
130+indexStep : {i : Fin n} -> {xs : Vect n a} -> {x : a} -> index i xs = index (fS i) (x::xs)
130131indexStep = refl
131132
132−find : {P : a -> Type} -> ((x : a) -> Dec (P x)) -> (xs : Vect a n)
133+find : {P : a -> Type} -> ((x : a) -> Dec (P x)) -> (xs : Vect n a)
133134 -> Either (All (\x => Not (P x)) xs) (y : a ** (P y, (i : Fin n ** y = index i xs)))
134135find _ Nil = Left Nil
135136find d (x::xs) with (d x)
136− | Yes prf = Right (x ** (prf, (fO ** refl)))
137+ | Yes prf = Right (x ** (prf, (fZ ** refl)))
137138 | No prf =
138139 case find d xs of
139140 Right (y ** (prf', (i ** prf''))) =>
140141 Right (y ** (prf', (fS i ** replace {P=(\x => y = x)} (indexStep {x=x}) prf'')))
141142 Left prf' => Left (prf::prf')
142143
143−findEmptyInRow : (xs : Vect (Cell n) n) -> Either (All Filled xs) (i : Fin n ** Empty (index i xs))
144+findEmptyInRow : (xs : Vect n (Cell n)) -> Either (All Filled xs) (i : Fin n ** Empty (index i xs))
144145findEmptyInRow xs =
145146 case find {P=Empty} empty xs of
146147 Right (_ ** (pempty, (i ** pidx))) => Right (i ** trans pempty pidx)
@@ -152,12 +153,12 @@ emptyCell (MkBoard rs) =
152153 Left p => Left p
153154 Right (ri ** (ci ** pf)) => Right ((ci, ri) ** pf)
154155 where
155− helper : (rs : Vect (Vect (Cell n) n) m)
156+ helper : (rs : Vect m (Vect n (Cell n)))
156157 -> Either (All (All Filled) rs) (r : Fin m ** (c : Fin n ** Empty (index c (index r rs))))
157158 helper Nil = Left Nil
158159 helper (r::rs) =
159160 case findEmptyInRow r of
160− Right (ci ** pf) => Right (fO ** (ci ** pf))
161+ Right (ci ** pf) => Right (fZ ** (ci ** pf))
161162 Left prf =>
162163 case helper rs of
163164 Left prf' => Left (prf::prf')
@@ -171,12 +172,12 @@ tryValue {b=b} l c _ v =
171172 No prf => Left prf
172173 Yes prf => Right (_ ** Step prf l)
173174
174−nullBoardFull : (b : Board O) -> FullBoard b
175+nullBoardFull : (b : Board Z) -> FullBoard b
175176nullBoardFull (MkBoard Nil) = Nil
176177
177178-- TODO: Prove complete by induction on illegal values wrt. some base state, e.g. every value is illegal for 123\21_\3_2
178179fillBoard : (b : Board n) -> LegalBoard b -> Maybe (b' : Board n ** CompleteBoard b')
179−fillBoard {n=O} b l = Just (b ** (l, nullBoardFull b))
180+fillBoard {n=Z} b l = Just (b ** (l, nullBoardFull b))
180181fillBoard {n=(S n)} b l with (emptyCell b)
181182 | Left full = Just (b ** (l, full))
182183 | Right (coords ** p) = recurse last
@@ -189,14 +190,14 @@ fillBoard {n=(S n)} b l with (emptyCell b)
189190 Left _ => -- TODO: Prove unsolvable
190191 case v of
191192 fS k => tryAll (weaken k)
192− fO => (v, Nothing)
193+ fZ => (v, Nothing)
193194
194195 %assert_total
195196 recurse : Fin (S n) -> Maybe (b' : Board (S n) ** CompleteBoard b')
196197 recurse start =
197198 case tryAll start of
198199 (_, Nothing) => Nothing
199− (fO, Just (b' ** l')) => fillBoard b' l'
200+ (fZ, Just (b' ** l')) => fillBoard b' l'
200201 (fS next, Just (b' ** l')) =>
201202 case fillBoard b' l' of
202203 Just solution => Just solution