Issue2524.agda 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. -- Andreas, 2017-03-30, issue #2524
  2. -- compile abstract definitions to arbitrary Haskell code
  3. module Issue2524 where
  4. open import Agda.Builtin.Nat
  5. open import Agda.Builtin.Unit
  6. open import Agda.Builtin.String
  7. {-# FOREIGN GHC import qualified Data.Text #-}
  8. postulate
  9. NativeIO : Set → Set
  10. nativeReturn : {A : Set} → A → NativeIO A
  11. _native>>=_ : {A B : Set} → NativeIO A → (A → NativeIO B) → NativeIO B
  12. {-# BUILTIN IO NativeIO #-}
  13. {-# COMPILE GHC NativeIO = type IO #-}
  14. {-# COMPILE GHC nativeReturn = (\_ -> return :: a -> IO a) #-}
  15. {-# COMPILE GHC _native>>=_ = (\_ _ -> (>>=) :: IO a -> (a -> IO b) -> IO b) #-}
  16. {-# COMPILE JS nativeReturn =
  17. function(u0) { return function(u1) { return function(x) { return function(cb) { cb(x); }; }; }; } #-}
  18. {-# COMPILE JS _native>>=_ =
  19. function(u0) { return function(u1) { return function(u2) { return function(u3) {
  20. return function(x) { return function(y) { return function(cb) {
  21. x(function (xx) { y(xx)(cb); });
  22. }; }; };
  23. }; }; }; }
  24. #-}
  25. postulate
  26. nativePutStrLn : String → NativeIO ⊤
  27. primShowNat : Nat → String
  28. {-# COMPILE GHC nativePutStrLn = (\ s -> putStrLn (Data.Text.unpack s)) #-}
  29. {-# COMPILE GHC primShowNat = Data.Text.pack . show #-}
  30. {-# COMPILE JS nativePutStrLn = function (x) { return function(cb) { process.stdout.write(x + '\n'); cb(0); }; } #-}
  31. {-# COMPILE JS primShowNat = agdaRTS.primShowInteger #-}
  32. abstract
  33. record Pointer : Set where
  34. constructor mkPointer
  35. field thePointer : Nat
  36. otherInfo : Nat
  37. newPointer : Pointer
  38. newPointer = mkPointer 5 17
  39. showPointer : Pointer → String
  40. showPointer p = primShowNat (Pointer.thePointer p)
  41. {-# COMPILE GHC Pointer = type Integer #-}
  42. {-# COMPILE GHC newPointer = 5 #-}
  43. {-# COMPILE GHC showPointer = Data.Text.pack . show #-}
  44. {-# COMPILE JS newPointer = 5 #-}
  45. {-# COMPILE JS showPointer = function(x) { return x; } #-}
  46. main : NativeIO ⊤
  47. main = nativePutStrLn (showPointer newPointer)