{- Free-ing Hutton: Generic Programming and Modular Reasoning "`a la carte" James McKinna, LFCS, Edinburgh WG2.1 #77 2018-01-01--06 Seehotel Brandenburg a.d. Havel -} {-# OPTIONS --sized-types #-} -- towards eventual inlining of all map constructs open import Function open import Data.Nat open import Data.Unit open import Data.Char hiding (show) open import Data.String hiding (show) open import Data.Nat.Show open import Relation.Binary.PropositionalEquality -- This talk describes an approach to reducing the burden of -- proving metatheoretic properties of programming languages via -- generic programming/proving. -- It is a revised and reorganised version of an original presentation -- by James Chapman, Strathclyde MSP group '101' talk, 2018-06-13 -- It is a heavily simplified version of the ideas in the paper: -- "Type-and-Scope Safe Programs and Their Proofs" -- Guillaume Allais, James Chapman, Conor McBride and James McKinna. -- In Yves Bertot and Viktor Vafeiadis, eds., Proceedings of -- The 6th ACM SIGPLAN Conference on Certified Programs and Proofs, -- CPP 2017 (Paris, Jan. 2017). -- Instead of scope and type safe lambda calculus we use -- Hutton's Razor, and its extension with Variables -- Hutton's Razor is the very simple expression language -- containing just whole numbers and addition. -- It can represent expressions such as "2 + (3 + 4)". -- Adding variables lets us have "x + (y + 1)" etc. -------------------------------------------------------------------------------- -- preamble: basics of algebraic structure -------------------------------------------------------------------------------- -- progressively richer structure defined 'Pebble-style' -- i.e. parameterised over the preceding structure, so that -- e.g. FunctorLaws make no sense without already being a Functor, -- a Monad should presuppose a Functor, MonadLaws a Monad etc. record Functor (F : Set -> Set) : Set₁ where field fmap : ∀{X Y} -> (X -> Y) -> F X -> F Y record FunctorLaws {F : Set -> Set}(FF : Functor F) : Set₁ where open Functor FF field fmap-id : ∀{X} (x : F X) -> fmap id x ≡ x fmap-comp : ∀{X Y Z} (f : Y -> Z) (g : X -> Y) (x : F X) -> fmap f (fmap g x) ≡ fmap (f ∘ g) x record Monad {H : Set -> Set}(HF : Functor H) : Set₁ where field unit : ∀{X} -> X -> H X bind : ∀{X Y} -> (X -> H Y) -> H X -> H Y record MonadLaws {H : Set -> Set}{HF : Functor H}(HM : Monad HF) : Set₁ where open Functor HF ; open Monad HM field bind-unit : ∀{X Y} (h : X -> H Y) (x : X) -> bind h (unit x) ≡ h x unit-bind : ∀{X} (x : H X) -> bind unit x ≡ x bind-bind : ∀{X Y Z} (h : Y -> H Z) (k : X -> H Y) (t : H X) -> bind h (bind k t) ≡ bind (bind h ∘ k) t -------------------------------------------------------------------------------- -- the following is for illustration only, -- and plays no subsequent role module HuttonsRazorAndSomeTraversals where -- Language data Exp (X : Set) : Set where Var : X -> Exp X Val : ℕ -> Exp X Plus : Exp X -> Exp X -> Exp X -- 2 + (3 + 4) ex1 : ∀{X} -> Exp X ex1 = Plus (Val 2) (Plus (Val 3) (Val 4)) -- x + (y + 1) ex2 : Exp Char ex2 = Plus (Var 'x') (Plus (Var 'y') (Val 1)) -- Programming -- Renaming variables ren : ∀{X Y} -> (X -> Y) -> Exp X -> Exp Y ren f (Var x) = Var (f x) ren f (Val n) = Val n ren f (Plus e e') = Plus (ren f e) (ren f e') -- x + (y + 1) :=> a + (a + 1) ex3 : Exp Char ex3 = ren (\ _ -> 'a') ex2 -- Substituting variables for expressions sub : ∀{X Y} -> (X -> Exp Y) -> Exp X -> Exp Y sub f (Var x) = f x sub f (Val n) = Val n sub f (Plus e e') = Plus (sub f e) (sub f e') -- x + (y + 1) :=> z + (3 + 1) ex4 : Exp Char ex4 = sub (\{'x' -> Var 'z'; 'y' -> Val 3; x -> Var x}) ex2 -- Evaluating an expression to yield a number eval : ∀{X} -> (X -> ℕ) -> Exp X -> ℕ eval f (Var x) = f x eval f (Val n) = n eval f (Plus e e') = eval f e + eval f e' -- x + (y + 1) :=> 1 + (2 + 1) :=> 4 ex5 : ℕ ex5 = eval (\{'x' -> 1; 'y' -> 2; _ -> 0}) ex2 -- Printing an expression to yield a String print : ∀{X} -> (X -> String) -> Exp X -> String print f (Var x) = "Var " ++ f x print f (Val n) = "Val " ++ show n print f (Plus e e') = "Plus (" ++ print f e ++ ") (" ++ print f e' ++ ")" -- x + (y + 1) :=> Plus (Var x) (Plus (Var x) (Val 1)) ex6 : String ex6 = print (\ _ -> "x") ex2 -- The functions, ren, sub, eval and print are very similar. -- Rule 1 of programming: don't repeat yourself! -- Can we abstract out the differences -- and write one generic version? -- Yes! -------------------------------------------------------------------------------- -- Generic Language Implementation -------------------------------------------------------------------------------- -- a bit free monad-y; a bit a la carte-y -- we'll re-express Exp as the Free monad on a functor -- what's the functor for which Hutton's Exp is the free monad? data Hutton (X : Set) : Set where Val : ℕ -> Hutton X Plus : X -> X -> Hutton X -- Hutton does indeed define a Functor Hmap : ∀{X Y} -> (X -> Y) -> Hutton X -> Hutton Y Hmap h (Val n) = Val n Hmap h (Plus s t) = Plus (h s) (h t) HuttonFunctor : Functor Hutton HuttonFunctor = record { fmap = Hmap } HuttonFunctorLaws : FunctorLaws HuttonFunctor HuttonFunctorLaws = record { fmap-id = Hmap-id ; fmap-comp = Hmap-comp } where Hmap-id : ∀{X} x -> Hmap {Y = X} id x ≡ x Hmap-id (Val n) = refl Hmap-id (Plus s t) = refl Hmap-comp : ∀{X Y Z} (f : Y -> Z) (g : X -> Y) x -> Hmap f (Hmap g x) ≡ Hmap (f ∘ g) x Hmap-comp f g (Val n) = refl Hmap-comp f g (Plus s t) = refl -- what's the free monad on Hutton? data HExp (X : Set) : Set where Var : X -> HExp X Con : Hutton (HExp X) -> HExp X -- smart constructors to simplify writing examples HVal : ∀{X} -> ℕ -> HExp X HVal = Con ∘ Val HPlus : ∀{X} (s t : HExp X) -> HExp X HPlus s t = Con (Plus s t) -- 2 + (3 + 4) hex1 : ∀{X} -> HExp X hex1 = HPlus (HVal 2) (HPlus (HVal 3) (HVal 4)) -- x + (y + 1) hex2 : HExp Char hex2 = HPlus (Var 'x') (HPlus (Var 'y') (HVal 1)) -- modules over Hutton record HMod (G : Set -> Set) : Set₁ where -- G is what the function returns field hcon : ∀{X} -> Hutton (G X) -> G X {- this is equivalent to having hval : ∀{X} -> ℕ -> G X -- what we do to the after extracting the value hplus : ∀{X} -> G X -> G X -> G X -- what we do after recursively processing the subexpressions. -} -- HExp is such a Module and we'll see it's initial later HExpMod : HMod HExp HExpMod = record { hcon = Con } -- TODO: no discussion of algebra/module homomorphisms here -------------------------------------------------------------------------------- -- Generic Programming -------------------------------------------------------------------------------- -- data to be able to lift a Hutton module to an HExp module homomorphism record HVar {H : Set -> Set} (HM : HMod H) (J : Set -> Set) : Set₁ where -- J is what the environment provides -- H is what the function returns field hvar : ∀{X} -> J X -> H X -- what we do after projecting the variable RenHVar : HVar HExpMod id RenHVar = record { hvar = Var } -- generic construction of the lifting traversal module HKit {H J : Set -> Set} {HM : HMod H} (HV : HVar HM J) where -- we presuppose a module (Mendler algebra) structure HM on H -- and then extend it by HV to take care of variables open HMod HM -- what to do with Con open HVar HV -- what to do with Var -- a generic action/environment machine -- generic traversal usually dubbed a 'kit', following McBride hkit : ∀{X Y} -> (X -> J Y) -> HExp X -> H Y hkitmap : ∀{X Y} -> (X -> J Y) -> Hutton (HExp X) -> Hutton (H Y) hkit h (Var x) = hvar (h x) hkit h (Con t) = hcon (hkitmap h t) -- hcon (Hmap (hkit h) t) -- forced on us by the termination checker hkitmap h (Val n) = Val n hkitmap h (Plus s t) = Plus (hkit h s) (hkit h t) -- should be able to write -- hkitmap h = fmap (hkit h) where open Functor FunctorHutton -- then the following lemma would be a *definitional* equality -- if the termination checker accepted hkitmap h ≡ Hmap (hkit h) -- ... hliftmap is extensionally what we know it to be ... lem : ∀{X Y} -> (h : X -> J Y) -> (t : Hutton (HExp X)) -> hkitmap h t ≡ Hmap (hkit h) t lem h (Val n) = refl lem h (Plus s t) = refl -- Homomorphism lemma: -- hkit h actually defines a homomorphism between HExpMod and HM -- as with lem/defn of hkitmap above, -- this would be a *definitional* equality hom : ∀{X Y} -> (h : X -> J Y) -> (t : Hutton (HExp X)) -> hcon (Hmap (hkit h) t) ≡ hkit h (Con t) hom h t rewrite lem h t = refl -------------------------------------------------------------------------------- -- Instances of the generic 'kit' traversal -------------------------------------------------------------------------------- -- 'renaming': Functor instance for HExp hren : ∀{X Y} -> (X -> Y) -> HExp X -> HExp Y hren = hkit where open HKit RenHVar HExpFunctor : Functor HExp HExpFunctor = record { fmap = hren } -- x + (y + 1) :=> a + (a + 1) hex3 : HExp Char hex3 = hren (\ _ -> 'a') hex2 -- 'substitution': -- Monad.bind instance for HExp; -- Monad.unit instance is Var SubHVar : HVar HExpMod HExp SubHVar = record { hvar = id } hsub : ∀{X Y} -> (X -> HExp Y) -> HExp X -> HExp Y hsub = hkit where open HKit SubHVar HExpMonad : Monad HExpFunctor HExpMonad = record { unit = Var ; bind = hsub } -- x + (y + 1) :=> z + (3 + 1) hex4 : HExp Char hex4 = hsub (\{'x' -> Var 'z'; 'y' -> HVal 3; x -> Var x}) hex2 -- 'evaluation': Hutton module from Hutton algebra structure on ℕ EvalHMod : HMod (const ℕ) EvalHMod = record { hcon = ⟦_⟧ℕ } where ⟦_⟧ℕ : Hutton ℕ -> ℕ ⟦ Val n ⟧ℕ = n ⟦ Plus s t ⟧ℕ = s + t EvalHVar : HVar EvalHMod (const ℕ) EvalHVar = record { hvar = id } heval : ∀{X Y : Set} -> (X -> ℕ) -> HExp X -> ℕ heval {Y = Y} = hkit {Y = Y} where open HKit EvalHVar -- x + (y + 1) :=> 1 + (2 + 1) :=> 4 hex5 : ℕ hex5 = heval {Y = ⊤} (\{'x' -> 1; 'y' -> 2; _ -> 0}) hex2 -- 'pretty printing': Hutton module from Hutton algebra structure on String PrintHMod : HMod (const String) PrintHMod = record { hcon = ⟦_⟧Str } where ⟦_⟧Str : Hutton String -> String ⟦ Val n ⟧Str = "Val " ++ (show n) ⟦ Plus s t ⟧Str = "Plus (" ++ s ++ ") (" ++ t ++ ")" PrintHVar : HVar PrintHMod (const String) PrintHVar = record { hvar = \x -> "Var " ++ x } hprint : ∀{X} -> (X -> String) -> HExp X -> String hprint = hkit {Y = ⊤} where open HKit PrintHVar -- x + (y + 1) :=> Plus (Var x) (Plus (Var x) (Val 1)) hex6 : String hex6 = hprint (\ _ -> "x") hex2 -- Can we do the same for proofs? -- Yes, but what do we want to prove? -- hren is fmap for the functor HExp: -- hren : ∀{X Y} -> (X -> Y) -> HExp X -> HExp Y -- (1) hren id e = e * Fresh -- (2) hren f (hren g e) = hren (f ∘ g) e * Fusion -- hsub is bind for the monad HExp, Var is unit: -- hsub : ∀{X Y} -> (X -> HExp Y) -> HExp X -> HExp Y -- (1) hsub f (Var x) = f x * varLemma -- (2) hsub Var e = e * Fresh -- (3) hsub f (hsub g e) = hsub (hsub f ∘ g) e * Fusion -- note: if we were using lambda calculus instead of Hutton's Razor -- where we need renaming and weakening to define substitution, -- we get *lots* of generic lemmas and need various combinations of -- substitution and then renaming/renaming and then substitution etc. -- heval is algebra map for the monad HExp: -- heval : ∀{X} -> (X -> ℕ) -> HExp X -> ℕ -- (1) heval f (var x) = f x * varLemma -- (2) heval f (hsub g e) = heval (heval f ∘ g) e * Fusion -- Note: -- algebras are usually presented as just something like alg : Free F ℕ -> ℕ -- (induced by an F-module structure on (const ℕ) -- eval below is isomorphic, just as join is isomorphic to bind for monads -- alg = heval id : HExp ℕ -> ℕ -- join = hsub id : HExp (HExp X) -> HExp X -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -- Generic proving -- this trivial lemma has several interesting special cases hvarLemma : ∀{H J X Y}{HM : HMod H}(k : HVar HM J)(h : X -> J Y)(x : X) -> let open HKit k ; open HVar k in hkit h (Var x) ≡ hvar (h x) hvarLemma k h x = refl -- requires no proof as it is the first line of hkit -- Monad Law 1 hmonadlaw1 : ∀{X Y}(h : X -> HExp Y)(x : X) -> hsub h (Var x) ≡ h x hmonadlaw1 h x = hvarLemma SubHVar h x -- Algebra Law 1 halglaw1 : ∀{X Y}(h : X -> ℕ)(x : X) -> heval {Y = Y} h (Var x) ≡ h x halglaw1 {Y = Y} h x = hvarLemma {Y = Y} EvalHVar h x -- What about the rest? As we abstracted over differences in recursive -- functions to give hkit, we can do the same for differences in proofs -- by induction. This yields theorems such as: -- if you use the identity renaming/substitution nothing happens. -- This gives us the data needed to give a generic proof of such properties record HFresh {J}(HV : HVar HExpMod J) : Set₁ where open HVar HV ; open HKit HV field hfresh : ∀{X} -> X -> J X -- identity/do nothing environment -- the required property will be proved by induction on HExp -- so we need to explain what is needed in the Var case -- the Hutton cases are expected to look after themselves -- by existing functoriality of Hutton hlawVar : ∀{X}(x : X) -> hkit hfresh (Var x) ≡ Var x -- as hkit is a generic function, this will be a generic proof hlaw : ∀{X}(t : HExp X) -> hkit hfresh t ≡ t hlawmap : ∀{X}(t : Hutton (HExp X)) -> hkitmap hfresh t ≡ t -- hlawmap is itself essentially an explicit unfolding of Hmap-id: -- using the *inductive* assumption that hkit hfresh is id, -- we obtain that 'hkitmap hfresh = Hmap id = id' -- implicitly supplying HKit.lem for the first equation, -- and fmap-id for the second, -- but here *without* explicit appeal to either... hlaw (Var x) = hlawVar x hlaw (Con t) = cong Con (hlawmap t) -- rewrite hlawmap t = refl hlawmap (Val n) = refl hlawmap (Plus s t) = cong₂ Plus (hlaw s) (hlaw t) -- rewrite hlaw s | hlaw t = refl -- HVar equipment for renaming hrenid : HFresh RenHVar hrenid = record { hfresh = id ; hlawVar = λ _ -> refl } -- Functor Law 1: functoriality of Hutton lifts to functoriality of HExp hfunlaw1 : ∀{X}(t : HExp X) -> hren id t ≡ t hfunlaw1 = hlaw where open HFresh hrenid -- HVar equipment for substitution hsubvar : HFresh SubHVar hsubvar = record { hfresh = Var ; hlawVar = λ _ -> refl } -- Monad Law 2 hmonadlaw2 : ∀{X}(t : HExp X) -> hsub Var t ≡ t hmonadlaw2 = hlaw where open HFresh hsubvar -- Theorems such as: we can combine two traversals (e.g. two subs, two -- rens, or one of each) into one single traversal record HFuse {H}{HM : HMod H}{F G K} (kF : HVar HExpMod F)(kG : HVar HM G)(kK : HVar HM K) : Set₁ where field hcomp : {X Y Z : Set} → (X -> F Y) -> (Y -> G Z) -> (X -> K Z) -- project out/abbreviate the various pieces of kit hconM = HMod.hcon HM hF = HKit.hkit kF hmapF = HKit.hkitmap kF hG = HKit.hkit kG hmapG = HKit.hkitmap kG hK = HKit.hkit kK hmapK = HKit.hkitmap kK -- equivalence relation on fused environments hlawT : ∀{X Y Z}(f : X -> F Y)(g : Y -> G Z) → (t : HExp X) -> Set hlawT f g t = hG g (hF f t) ≡ hK (hcomp f g) t hlawmapT : ∀{X Y Z}(f : X -> F Y)(g : Y -> G Z) → (t : Hutton (HExp X)) -> Set hlawmapT f g t = hmapG g (hmapF f t) ≡ hmapK (hcomp f g) t field hlawVar : ∀{X Y Z}(f : X -> F Y)(g : Y -> G Z) -> ∀ x -> hlawT f g (Var x) -- similar remarks apply to Fuse.hlaw/hlawmap as above for Fresh -- just as HFresh.hlaw extends identity law for Hutton functor -- HFuse.hlaw is the analogue of (the extension of) the composition law hlaw : ∀{X Y Z}(f : X -> F Y)(g : Y -> G Z) t -> hlawT f g t hlawmap : ∀{X Y Z}(f : X -> F Y)(g : Y -> G Z) t -> hlawmapT f g t hlaw f g (Var x) = hlawVar f g x hlaw f g (Con t) = cong hconM (hlawmap f g t) -- rewrite hlawmap f g t = refl hlawmap f g (Val n) = refl hlawmap f g (Plus s t) = cong₂ Plus (hlaw f g s) (hlaw f g t) -- rewrite hlaw f g s | hlaw f g t = refl -- composition of renamings hrenrenren : HFuse RenHVar RenHVar RenHVar hrenrenren = record { hcomp = λ f g → g ∘ f ; hlawVar = λ f g x → refl } -- Functor Law 2 hfunlaw2 : ∀{X Y Z}(f : Y -> Z)(g : X -> Y)(t : HExp X) -> hren f (hren g t) ≡ hren (f ∘ g) t hfunlaw2 f g = hlaw g f where open HFuse hrenrenren HExpFunctorLaws : FunctorLaws HExpFunctor HExpFunctorLaws = record { fmap-id = hfunlaw1 ; fmap-comp = hfunlaw2 } -- renaming and then substitution can be fused into one substitution hrensubsub : HFuse RenHVar SubHVar SubHVar hrensubsub = record { hcomp = λ f g → g ∘ f ; hlawVar = λ f g x -> refl } -- substitution and then renaming can also be fused into one substitution hsubrensub : HFuse SubHVar RenHVar SubHVar hsubrensub = record { hcomp = λ f g → hren g ∘ f ; hlawVar = λ f g x → refl } hsubrensublaw : ∀{X Y Z}(f : Y -> Z)(g : X -> HExp Y)(t : HExp X) -> hren f (hsub g t) ≡ hsub (hren f ∘ g) t hsubrensublaw f g = hlaw g f where open HFuse hsubrensub -- composition of substitutions hsubsubsub : HFuse SubHVar SubHVar SubHVar hsubsubsub = record { hcomp = λ f g → hsub g ∘ f ; hlawVar = λ f g x → refl } -- Monad Law 3 hmonadlaw3 : ∀{X Y Z}(f : Y -> HExp Z)(g : X -> HExp Y)(t : HExp X) -> hsub f (hsub g t) ≡ hsub (hsub f ∘ g) t hmonadlaw3 f g = hlaw g f where open HFuse hsubsubsub HExpMonadLaws : MonadLaws HExpMonad HExpMonadLaws = record { bind-unit = hmonadlaw1 ; unit-bind = hmonadlaw2 ; bind-bind = hmonadlaw3 } -- substitution and then evaluation amounts to an iterated evaluation hsubevaleval : HFuse SubHVar EvalHVar EvalHVar hsubevaleval = record { hcomp = λ f g → heval g ∘ f ; hlawVar = λ f g x → refl } -- Algebra Law 2 a.k.a. "the substitution law" halglaw2 : ∀{X Y}(f : Y -> ℕ)(g : X -> HExp Y)(t : HExp X) -> heval {Y = Y} f (hsub g t) ≡ heval (heval f ∘ g) t halglaw2 f g = hlaw g f where open HFuse hsubevaleval -- ... to be continued ...