module Haskell.Extra.Refinement where

open import Haskell.Prelude
open import Agda.Primitive

private variable
   ℓ′ : Level

record  (a : Set ) (@0 P : a  Set ℓ′) : Set (  ℓ′) where
  constructor _⟨_⟩
  field
    value    : a
    @0 proof : P value
open  public
{-# COMPILE AGDA2HS  unboxed #-}

mapRefine : {@0 P Q : a  Set } (@0 f :  {x}  P x  Q x)   a P   a Q
mapRefine f (x  p ) = x  f p 

{-# COMPILE AGDA2HS mapRefine transparent #-}

refineMaybe : {@0 P : a  Set } 
             (mx : Maybe a)  @0 (∀ {x}  mx  Just x  P x)
             Maybe ( a P)
refineMaybe Nothing f = Nothing
refineMaybe (Just x) f = Just (x  f refl )

{-# COMPILE AGDA2HS refineMaybe transparent #-}