Skip to content

Instantly share code, notes, and snippets.

@TOTBWF
Created December 1, 2021 07:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TOTBWF/98f8f98fa31cbb48e0f92c0c9967f0ee to your computer and use it in GitHub Desktop.
Save TOTBWF/98f8f98fa31cbb48e0f92c0c9967f0ee to your computer and use it in GitHub Desktop.
Compile Time File Embedding in Agda
{-# OPTIONS --allow-exec #-}
-- Embed a file as a string inside of an agda program.
-- To use this, make sure to add '/bin/cat' to '~/.agda/executables'
module Static where
open import Function
open import Data.Unit
open import Data.Product
open import Data.Nat
open import Data.List
open import Data.String
open import Relation.Binary.PropositionalEquality
open import Reflection
postulate
execTC : (exe : String) (args : List String) (stdIn : String)
→ TC (Σ ℕ (λ _ → Σ String (λ _ → String)))
{-# BUILTIN AGDATCMEXEC execTC #-}
macro
file-contents : String → Term → TC ⊤
file-contents path hole = do
(exitCode , (stdOut , stdErr)) ← execTC "cat" (path ∷ []) ""
case exitCode of λ
{ zero → unify hole (lit (string stdOut))
; _ → typeError (strErr "file-contents failed:" ∷ strErr stdErr ∷ [])
}
example : String
example = file-contents "/Users/reedmullanix/Documents/Projects/advent-of-code-2021/day1_input.txt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment