Skip to content

Auxiliary Functions

The \(\lambda\)-notation in the Agda definitions of auxiliary functions for Scm corresponds closely to that in its published denotational semantics (Mosses2025CSE).

{-# OPTIONS --rewriting --confluence-check --lossy-unification #-}

module Examples.Scm.Auxiliary-Functions where

  open import Examples.Scm.Abstract-Syntax
  open import Examples.Scm.Domain-Equations
  open import Notation.Domains
  open import Notation.Functions
  open import Notation.Flat
  open import Notation.Flat.Booleans
  open import Notation.Flat.Naturals
  open import Notation.Sums
  open import Notation.Products
  open import Notation.Products.Sequences
  open import Notation.Updates
  open import Agda.Builtin.Nat using (Nat)
  open import Data.String.Properties renaming (_==_ to _==Λ’_) using ()

  postulate
    _<α΄Ώ_  : βŸͺ 𝐑 β†’αΆœ 𝐑 β†’αΆœ 𝐓 ⟫
    _+α΄Ώ_  : βŸͺ 𝐑 β†’αΆœ 𝐑 β†’αΆœ 𝐑 ⟫
    _βˆ§α΅€_  : βŸͺ 𝐓 β†’αΆœ 𝐓 β†’αΆœ 𝐓 ⟫

  instance
    eqIde : Eq Ide
    _==_ {{eqIde}} = _==Λ’_

  postulate unknown : Loc

  postulate initial-env : βŸͺ 𝐔 ⟫

  assign : βŸͺ 𝐋 β†’αΆœ 𝐄 β†’αΆœ 𝐂 β†’αΆœ 𝐂 ⟫      -- assign Ξ± Ο΅ stores Ο΅ at location Ξ±
  assign Ξ± Ο΅ ΞΈ Οƒ = ΞΈ (Οƒ [ Ο΅ / Ξ± ]βŠ₯)

  hold : βŸͺ 𝐋 β†’αΆœ (𝐄 β†’αΆœ 𝐂) β†’αΆœ 𝐂 ⟫      -- hold Ξ± gives the value stored at Ξ±
  hold Ξ± ΞΊ Οƒ = ΞΊ (Οƒ Ξ±) Οƒ

In the continuation-passing style used to define auxilary functions for Scm, giving explicit continuity proofs would be particulary tedious. For example, the function hold is simply a combination of \(\lambda\)-abstraction and application, which is well-known to ensure continuity.

  postulate new : βŸͺ (𝐋 β†’αΆœ 𝐂) β†’αΆœ 𝐂 ⟫  -- new gives an unallocated location

  alloc : βŸͺ 𝐄 β†’αΆœ (𝐋 β†’αΆœ 𝐂) β†’αΆœ 𝐂 ⟫     -- alloc Ο΅ allocates a location for Ο΅
  alloc Ο΅ ΞΊ = new (Ξ» Ξ± β†’ assign Ξ± Ο΅ (ΞΊ Ξ±))

  postulate initial-store : βŸͺ 𝐒 ⟫    -- may have initialised locations

  postulate finished : βŸͺ 𝐒 β†’αΆœ 𝐀 ⟫    -- obtain answer from the final store

Conventional denotational definitions usually leave the injection function ↑ from sets into flat domains implicit, in contrast to the following embedding of the definition of truish:

  truish : βŸͺ 𝐄 β†’αΆœ 𝐓 ⟫                -- truish Ξ΅ is true for all Ξ΅ except false
  truish Ο΅ =  (Ο΅ ∈βŠ₯ 𝐓) ⟢ (((Ο΅ |βŠ₯ 𝐓) ==βŠ₯ ↑ false) ⟢ ↑ false , ↑ true) ,
              ↑ true

The remaining auxiliary function definitions shown here involve the operations for (finite) sequences ϡ⋆ declared in the module for sequence domains.

  cons : βŸͺ 𝐅 ⟫                       -- cons ⟨ ϡ₁ , Ο΅β‚‚ ⟩ allocates and initialises a pair
  cons ϡ⋆ ΞΊ =  (# ϡ⋆ ==βŠ₯ ↑ 2) ⟢
                  alloc (ϡ⋆ ↓ 1) (Ξ» α₁ β†’
                    alloc (ϡ⋆ ↓ 2) (Ξ» Ξ±β‚‚ β†’ ΞΊ ((α₁ , Ξ±β‚‚) inβŠ₯ 𝐄))) ,
                βŠ₯

In (Mosses2025CSE) the auxiliary function \(\textit{list}\) is defined by recursion on ϡ⋆. Agda accepts recursive definitions only when it can mechanically prove that the recursion terminates, which is not the case for arguments in postulated types. The following definition uses the postulated operation fix to avoid recursion.

  list : βŸͺ 𝐅 ⟫                       -- list ϡ⋆ allocates and initialises a list
  list =   fix Ξ» (listβ€² : βŸͺ 𝐅 ⟫) β†’ Ξ» ϡ⋆ ΞΊ β†’
            (# ϡ⋆ ==βŠ₯ ↑ 0) ⟢ ΞΊ (↑ null inβŠ₯ 𝐄) ,
            listβ€² (ϡ⋆ † 1) (Ξ» Ο΅ β†’ cons ⟨ (ϡ⋆ ↓ 1) , Ο΅ ⟩ ΞΊ)

  car : βŸͺ 𝐅 ⟫                        -- car ⟨ Ο΅ ⟩ gives the head of the list Ο΅
  car ϡ⋆ ΞΊ = (# ϡ⋆ ==βŠ₯ ↑ 1) ⟢ hold (((ϡ⋆ ↓ 1) |βŠ₯ 𝐏) ↓₁) ΞΊ , βŠ₯

  cdr : βŸͺ 𝐅 ⟫                        -- cdr ⟨ Ο΅ ⟩ gives the tail of the list Ο΅
  cdr ϡ⋆ ΞΊ = (# ϡ⋆ ==βŠ₯ ↑ 1) ⟢ hold (((ϡ⋆ ↓ 1) |βŠ₯ 𝐏) ↓₂) ΞΊ , βŠ₯

  setcar : βŸͺ 𝐅 ⟫                     -- setcar ⟨ ϡ₁ , Ο΅β‚‚ ⟩ stores Ο΅β‚‚ in the head of list ϡ₁
  setcar ϡ⋆ ΞΊ =
    (# ϡ⋆ ==βŠ₯ ↑ 2) ⟢
      assign (((ϡ⋆ ↓ 1) |βŠ₯ 𝐏) ↓₁) (ϡ⋆ ↓ 2) (ΞΊ (↑ unspecified inβŠ₯ 𝐄)) ,
    βŠ₯

  setcdr : βŸͺ 𝐅 ⟫                     -- setcdr ⟨ ϡ₁ , Ο΅β‚‚ ⟩ stores Ο΅β‚‚ in the tail of list ϡ₁
  setcdr ϡ⋆ ΞΊ =
    (# ϡ⋆ ==βŠ₯ ↑ 2) ⟢
      assign (((ϡ⋆ ↓ 1) |βŠ₯ 𝐏) ↓₂) (ϡ⋆ ↓ 2) (ΞΊ (↑ unspecified inβŠ₯ 𝐄)) ,
    βŠ₯