Debian Codegraph @

doc/README/Foreign/Haskell.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- A simple example of a program using the foreign function interface
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --guardedness #-}
8
9 module README.Foreign.Haskell where
10
11 -- In order to be considered safe by Agda, the standard library cannot
12 -- add COMPILE pragmas binding the inductive types it defines to concrete
13 -- Haskell types.
14
15 -- To work around this limitation, we have defined FFI-friendly versions
16 -- of these types together with a zero-cost coercion `coerce`.
17
18 open import Level using (Level)
19 open import Agda.Builtin.Int
20 open import Agda.Builtin.Nat
21 open import Data.Bool.Base using (Bool; if_then_else_)
22 open import Data.Char as Char
23 open import Data.List.Base as List using (List; _∷_; []; takeWhile; dropWhile)
24 open import Data.Maybe.Base using (Maybe; just; nothing)
25 open import Data.Product.Base using (_×_; _,_)
26 open import Function
27 open import Relation.Nullary.Decidable
28
29 import Foreign.Haskell as FFI
30 open import Foreign.Haskell.Coerce
31
32 private
33 variable
34 a : Level
35 A : Set a
36
37 -- Here we use the FFI version of Pair.
38
39 postulate
40 primUncons : List A → Maybe (FFI.Pair A (List A))
41 primCatMaybes : List (Maybe A) → List A
42 primTestChar : Char → Bool
43 primIntEq : Int → Int → Bool
44
45 {-# COMPILE GHC primUncons = \ _ _ xs -> case xs of
46 { [] -> Nothing
47 ; (x : xs) -> Just (x, xs)
48 }
49 #-}
50
51 {-# FOREIGN GHC import Data.Maybe #-}
52 {-# COMPILE GHC primCatMaybes = \ _ _ -> catMaybes #-}
53
54 {-# COMPILE GHC primTestChar = ('-' /=) #-}
55
56 {-# COMPILE GHC primIntEq = (==) #-}
57
58 -- We however want to use the notion of Pair internal to the standard library.
59 -- For this we use `coerce` to take use back to the types we are used to.
60
61 -- The typeclass mechanism uses the coercion rules for Pair, as well as the
62 -- knowledge that natural numbers are represented as integers.
63 -- We additionally benefit from the congruence rules for List, Maybe, Char,
64 -- Bool, and a reflexivity principle for variable A.
65
66 uncons : List A → Maybe (A × List A)
67 uncons = coerce primUncons
68
69 catMaybes : List (Maybe A) → List A
70 catMaybes = primCatMaybes
71
72 testChar : Char → Bool
73 testChar = coerce primTestChar
74 -- note that coerce is useless here but the proof could come from
75 -- either `coerce-fun coerce-refl coerce-refl` or `coerce-refl` alone
76 -- We (and Agda) do not care which proof we got.
77
78 eqNat : Nat → Nat → Bool
79 eqNat = coerce primIntEq
80 -- We can coerce `Nat` to `Int` but not `Int` to `Nat`. This fundamentally
81 -- relies on the fact that `Coercible` understands that functions are
82 -- contravariant.
83
84 open import IO
85 open import Codata.Musical.Notation
86 open import Data.String.Base using (toList; fromList; unlines; _++_)
87 open import Relation.Nullary.Negation
88
89 -- example program using uncons, catMaybes, and testChar
90
91 main = run $ do
92 content ← readFiniteFile "README/Foreign/Haskell.agda"
93 let chars = toList content
94 let cleanup = catMaybes ∘ List.map (λ c → if testChar c then just c else nothing)
95 let cleaned = dropWhile ('\n' ≟_) $ cleanup chars
96 case uncons cleaned of λ where
97 nothing → putStrLn "I cannot believe this file is filed with dashes only!"
98 (just (c , cs)) → putStrLn $ unlines
99 $ ("First (non dash) character: " ++ Char.show c)
100 ∷ ("Rest (dash free) of the line: " ++ fromList (takeWhile (¬? ∘ ('\n' ≟_)) cs))
101 ∷ []
102
103 -- You can compile and run this test by writing:
104 -- agda -c Haskell.agda
105 -- ../../Haskell
106
107 -- You should see the following text (without the indentation on the left):
108 -- First (non dash) character: ' '
109 -- Rest (dash free) of the line: The Agda standard library