Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. module FromInteger
  2.  
  3. import Data.Vect
  4.  
  5. -- This is inspired by the fromInteger definition for Fin.
  6.  
  7. IntegerToElem :
  8. DecEq a => (i : Integer) -> (x : a) -> (xs : Vect n a)
  9. -> Maybe (Elem x xs)
  10. IntegerToElem i x [] = Nothing
  11. IntegerToElem i x (y :: xs) with (decEq x y)
  12. IntegerToElem i x (x :: xs) | (Yes Refl) =
  13. if i == 0
  14. then Just Here
  15. else if i < 0
  16. then Nothing
  17. else map There (IntegerToElem (i - 1) x xs)
  18. IntegerToElem i x (y :: xs) | (No contra) =
  19. if i <= 0
  20. then Nothing
  21. else map There (IntegerToElem (i - 1) x xs)
  22.  
  23. fromInteger :
  24. DecEq a => {x : a} -> {xs : Vect n a} -> (i : Integer)
  25. -> {default ItIsJust prf : IsJust (IntegerToElem i x xs)} -> Elem x xs
  26. fromInteger {x} {xs} i {prf} with (IntegerToElem i x xs)
  27. fromInteger {x = x} {xs = xs} i {prf = ItIsJust} | Nothing impossible
  28. fromInteger {x = x} {xs = xs} i {prf = ItIsJust} | (Just y) = y
  29.  
  30. -- All of these work:
  31.  
  32. t1 : Elem True [True]
  33. t1 = 0
  34.  
  35. t2 : Elem True [False, False, True]
  36. t2 = 2
  37.  
  38. t3 : Elem True [True, True]
  39. t3 = 1
  40.  
  41. t4 : Elem () [(), ()]
  42. t4 = 0
  43.  
  44. t5 : Elem () [(), ()]
  45. t5 = 1
  46.  
  47. t6 : Elem (the (Fin 8) 0) (the (Vect 8 (Fin 8)) Data.Vect.range)
  48. t6 = 0
  49.  
  50. t7 : Elem (the (Fin 8) 7) (the (Vect 8 (Fin 8)) Data.Vect.range)
  51. t7 = 7
  52.  
  53. t8 : Elem (the Nat 0) (the (Vect 3 Nat) [0,1,2])
  54. t8 = 0
  55.  
  56. -- None of these work, but I don't know why ... :
  57.  
  58. {-
  59. f1 : Elem (the Integer 0) (the (Vect 3 Integer) [0,1,2])
  60. f1 = 0
  61.  
  62. f2 : Elem 'x' ['x']
  63. f2 = 0
  64. -}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement