~ubuntu-branches/ubuntu/raring/agda/raring-proposed

« back to all changes in this revision

Viewing changes to src/full/Agda/TypeChecking/Monad/Open.hs

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane
  • Date: 2009-07-20 19:49:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090720194941-hcjy91vrn16csh7d
Tags: upstream-2.2.4+dfsg
ImportĀ upstreamĀ versionĀ 2.2.4+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{-# LANGUAGE CPP #-}
 
2
 
 
3
module Agda.TypeChecking.Monad.Open
 
4
        ( makeOpen
 
5
        , makeClosed
 
6
        , getOpen
 
7
        , tryOpen
 
8
        ) where
 
9
 
 
10
import Control.Applicative
 
11
import Control.Monad
 
12
import Control.Monad.Error
 
13
import Data.List
 
14
 
 
15
import Agda.Syntax.Common
 
16
 
 
17
import Agda.TypeChecking.Substitute
 
18
import Agda.TypeChecking.Monad.Base
 
19
 
 
20
import {-# SOURCE #-} Agda.TypeChecking.Monad.Context
 
21
 
 
22
#include "../../undefined.h"
 
23
import Agda.Utils.Impossible
 
24
 
 
25
-- | Create an open term in the current context.
 
26
makeOpen :: MonadTCM tcm => a -> tcm (Open a)
 
27
makeOpen x = do
 
28
    ctx <- getContextId
 
29
    return $ OpenThing ctx x
 
30
 
 
31
-- | Create an open term which is closed.
 
32
makeClosed :: a -> Open a
 
33
makeClosed = OpenThing []
 
34
 
 
35
 
 
36
-- | Extract the value from an open term. Must be done in an extension of the
 
37
--   context in which the term was created.
 
38
getOpen :: (MonadTCM tcm, Raise a) => Open a -> tcm a
 
39
getOpen (OpenThing []  x) = return x
 
40
getOpen (OpenThing ctx x) = do
 
41
  ctx' <- getContextId
 
42
  unless (ctx `isSuffixOf` ctx') $ fail $ "thing out of context (" ++ show ctx ++ " is not a sub context of " ++ show ctx' ++ ")"
 
43
  return $ raise (genericLength ctx' - genericLength ctx) x
 
44
 
 
45
tryOpen :: (MonadTCM tcm, Raise a) => Open a -> tcm (Maybe a)
 
46
tryOpen o = liftTCM $
 
47
  (Just <$> getOpen o)
 
48
  `catchError` \_ -> return Nothing
 
49