-- |
-- Module      :  Cryptol.TypeCheck.Parseable
-- Copyright   :  (c) 2013-2017 Galois, Inc.
-- License     :  BSD3
-- Maintainer  :  cryptol@galois.com
-- Stability   :  provisional
-- Portability :  portable

{-# LANGUAGE Safe                                #-}

{-# LANGUAGE RecordWildCards                     #-}
{-# LANGUAGE PatternGuards                       #-}
{-# LANGUAGE FlexibleInstances, FlexibleContexts #-}
{-# LANGUAGE DeriveAnyClass, DeriveGeneric       #-}
module Cryptol.TypeCheck.Parseable
  ( module Cryptol.TypeCheck.Parseable
  , ShowParseable(..)
  ) where

import Cryptol.TypeCheck.AST
import Cryptol.Utils.Ident (Ident,unpackIdent)
import Cryptol.Utils.RecordMap (canonicalFields)
import Cryptol.Parser.AST ( Located(..))
import Cryptol.ModuleSystem.Name
import Text.PrettyPrint hiding ((<>))
import qualified Text.PrettyPrint as PP ((<>))

-- ShowParseable prints out a cryptol program in a way that it's parseable by Coq (and likely other things)
-- Used mainly for reasoning about the semantics of cryptol programs in Coq (https://github.com/GaloisInc/cryptol-semantics)
class ShowParseable t where
  showParseable :: t -> Doc

instance ShowParseable Expr where
  showParseable :: Expr -> Doc
showParseable (ELocated Range
_ Expr
e) = Expr -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Expr
e -- TODO? emit range information
  showParseable (EList [Expr]
es Type
_) = Doc -> Doc
parens (String -> Doc
text String
"EList" Doc -> Doc -> Doc
<+> [Expr] -> Doc
forall t. ShowParseable t => t -> Doc
showParseable [Expr]
es)
  showParseable (ETuple [Expr]
es) = Doc -> Doc
parens (String -> Doc
text String
"ETuple" Doc -> Doc -> Doc
<+> [Expr] -> Doc
forall t. ShowParseable t => t -> Doc
showParseable [Expr]
es)
  showParseable (ERec RecordMap Ident Expr
ides) = Doc -> Doc
parens (String -> Doc
text String
"ERec" Doc -> Doc -> Doc
<+> [(Ident, Expr)] -> Doc
forall t. ShowParseable t => t -> Doc
showParseable (RecordMap Ident Expr -> [(Ident, Expr)]
forall a b. RecordMap a b -> [(a, b)]
canonicalFields RecordMap Ident Expr
ides))
  showParseable (ESel Expr
e Selector
s) = Doc -> Doc
parens (String -> Doc
text String
"ESel" Doc -> Doc -> Doc
<+> Expr -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Expr
e Doc -> Doc -> Doc
<+> Selector -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Selector
s)
  showParseable (ESet Type
_ty Expr
e Selector
s Expr
v) = Doc -> Doc
parens (String -> Doc
text String
"ESet" Doc -> Doc -> Doc
<+>
                                Expr -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Expr
e Doc -> Doc -> Doc
<+> Selector -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Selector
s
                                                Doc -> Doc -> Doc
<+> Expr -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Expr
v)
  showParseable (EIf Expr
c Expr
t Expr
f) = Doc -> Doc
parens (String -> Doc
text String
"EIf" Doc -> Doc -> Doc
<+> Expr -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Expr
c Doc -> Doc -> Doc
$$ Expr -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Expr
t Doc -> Doc -> Doc
$$ Expr -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Expr
f)
  showParseable (EComp Type
_ Type
_ Expr
e [[Match]]
mss) = Doc -> Doc
parens (String -> Doc
text String
"EComp" Doc -> Doc -> Doc
$$ Expr -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Expr
e Doc -> Doc -> Doc
$$ [[Match]] -> Doc
forall t. ShowParseable t => t -> Doc
showParseable [[Match]]
mss)
  showParseable (EVar Name
n) = Doc -> Doc
parens (String -> Doc
text String
"EVar" Doc -> Doc -> Doc
<+> Name -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Name
n)
  showParseable (EApp Expr
fe Expr
ae) = Doc -> Doc
parens (String -> Doc
text String
"EApp" Doc -> Doc -> Doc
$$ Expr -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Expr
fe Doc -> Doc -> Doc
$$ Expr -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Expr
ae)
  showParseable (EAbs Name
n Type
_ Expr
e) = Doc -> Doc
parens (String -> Doc
text String
"EAbs" Doc -> Doc -> Doc
<+> Name -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Name
n Doc -> Doc -> Doc
$$ Expr -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Expr
e)
  showParseable (EWhere Expr
e [DeclGroup]
dclg) = Doc -> Doc
parens (String -> Doc
text String
"EWhere" Doc -> Doc -> Doc
$$ Expr -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Expr
e Doc -> Doc -> Doc
$$ [DeclGroup] -> Doc
forall t. ShowParseable t => t -> Doc
showParseable [DeclGroup]
dclg)
  showParseable (ETAbs TParam
tp Expr
e) = Doc -> Doc
parens (String -> Doc
text String
"ETAbs" Doc -> Doc -> Doc
<+> TParam -> Doc
forall t. ShowParseable t => t -> Doc
showParseable TParam
tp
                                        Doc -> Doc -> Doc
$$ Expr -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Expr
e)
  showParseable (ETApp Expr
e Type
t) = Doc -> Doc
parens (String -> Doc
text String
"ETApp" Doc -> Doc -> Doc
$$ Expr -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Expr
e Doc -> Doc -> Doc
$$ Doc -> Doc
parens (String -> Doc
text String
"ETyp" Doc -> Doc -> Doc
<+> Type -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Type
t))
  --NOTE: erase all "proofs" for now (change the following two lines to change that)
  showParseable (EProofAbs {-p-}Type
_ Expr
e) = Expr -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Expr
e --"(EProofAbs " ++ show p ++ showParseable e ++ ")"
  showParseable (EProofApp Expr
e) = Expr -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Expr
e --"(EProofApp " ++ showParseable e ++ ")"

instance (ShowParseable a, ShowParseable b) => ShowParseable (a,b) where
  showParseable :: (a, b) -> Doc
showParseable (a
x,b
y) = Doc -> Doc
parens (a -> Doc
forall t. ShowParseable t => t -> Doc
showParseable a
x Doc -> Doc -> Doc
PP.<> Doc
comma Doc -> Doc -> Doc
PP.<> b -> Doc
forall t. ShowParseable t => t -> Doc
showParseable b
y)

instance ShowParseable Int where
  showParseable :: Int -> Doc
showParseable Int
i = Int -> Doc
int Int
i

instance ShowParseable Ident where
  showParseable :: Ident -> Doc
showParseable Ident
i = String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ String -> String
forall a. Show a => a -> String
show (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ Ident -> String
unpackIdent Ident
i

instance ShowParseable Type where
  showParseable :: Type -> Doc
showParseable (TUser Name
n [Type]
lt Type
t) = Doc -> Doc
parens (String -> Doc
text String
"TUser" Doc -> Doc -> Doc
<+> Name -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Name
n Doc -> Doc -> Doc
<+> [Type] -> Doc
forall t. ShowParseable t => t -> Doc
showParseable [Type]
lt Doc -> Doc -> Doc
<+> Type -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Type
t)
  showParseable (TRec RecordMap Ident Type
lidt) = Doc -> Doc
parens (String -> Doc
text String
"TRec" Doc -> Doc -> Doc
<+> [(Ident, Type)] -> Doc
forall t. ShowParseable t => t -> Doc
showParseable (RecordMap Ident Type -> [(Ident, Type)]
forall a b. RecordMap a b -> [(a, b)]
canonicalFields RecordMap Ident Type
lidt))
  showParseable Type
t = Doc -> Doc
parens (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ Type -> String
forall a. Show a => a -> String
show Type
t

instance ShowParseable Selector where
  showParseable :: Selector -> Doc
showParseable (TupleSel Int
n Maybe Int
_) = Doc -> Doc
parens (String -> Doc
text String
"TupleSel" Doc -> Doc -> Doc
<+> Int -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Int
n)
  showParseable (RecordSel Ident
n Maybe [Ident]
_) = Doc -> Doc
parens (String -> Doc
text String
"RecordSel" Doc -> Doc -> Doc
<+> Ident -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Ident
n)
  showParseable (ListSel Int
n Maybe Int
_) = Doc -> Doc
parens (String -> Doc
text String
"ListSel" Doc -> Doc -> Doc
<+> Int -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Int
n)

instance ShowParseable Match where
  showParseable :: Match -> Doc
showParseable (From Name
n Type
_ Type
_ Expr
e) = Doc -> Doc
parens (String -> Doc
text String
"From" Doc -> Doc -> Doc
<+> Name -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Name
n Doc -> Doc -> Doc
<+> Expr -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Expr
e)
  showParseable (Let Decl
d) = Doc -> Doc
parens (String -> Doc
text String
"MLet" Doc -> Doc -> Doc
<+> Decl -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Decl
d)

instance ShowParseable Decl where
  showParseable :: Decl -> Doc
showParseable Decl
d = Doc -> Doc
parens (String -> Doc
text String
"Decl" Doc -> Doc -> Doc
<+> Name -> Doc
forall t. ShowParseable t => t -> Doc
showParseable (Decl -> Name
dName Decl
d)
                              Doc -> Doc -> Doc
$$ DeclDef -> Doc
forall t. ShowParseable t => t -> Doc
showParseable (Decl -> DeclDef
dDefinition Decl
d))

instance ShowParseable DeclDef where
  showParseable :: DeclDef -> Doc
showParseable DeclDef
DPrim = String -> Doc
text (DeclDef -> String
forall a. Show a => a -> String
show DeclDef
DPrim)
  showParseable (DExpr Expr
e) = Doc -> Doc
parens (String -> Doc
text String
"DExpr" Doc -> Doc -> Doc
$$ Expr -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Expr
e)

instance ShowParseable DeclGroup where
  showParseable :: DeclGroup -> Doc
showParseable (Recursive [Decl]
ds) =
    Doc -> Doc
parens (String -> Doc
text String
"Recursive" Doc -> Doc -> Doc
$$ [Decl] -> Doc
forall t. ShowParseable t => t -> Doc
showParseable [Decl]
ds)
  showParseable (NonRecursive Decl
d) =
    Doc -> Doc
parens (String -> Doc
text String
"NonRecursive" Doc -> Doc -> Doc
$$ Decl -> Doc
forall t. ShowParseable t => t -> Doc
showParseable Decl
d)

instance (ShowParseable a) => ShowParseable [a] where
  showParseable :: [a] -> Doc
showParseable [a]
a = case [a]
a of
                      [] -> String -> Doc
text String
"[]"
                      [a
x] -> Doc -> Doc
brackets (a -> Doc
forall t. ShowParseable t => t -> Doc
showParseable a
x)
                      a
x : [a]
xs -> String -> Doc
text String
"[" Doc -> Doc -> Doc
<+> a -> Doc
forall t. ShowParseable t => t -> Doc
showParseable a
x Doc -> Doc -> Doc
$$
                                [Doc] -> Doc
vcat [ Doc
comma Doc -> Doc -> Doc
<+> a -> Doc
forall t. ShowParseable t => t -> Doc
showParseable a
y | a
y <- [a]
xs ] Doc -> Doc -> Doc
$$
                                String -> Doc
text String
"]"

instance (ShowParseable a) => ShowParseable (Maybe a) where
  showParseable :: Maybe a -> Doc
showParseable Maybe a
Nothing = String -> Doc
text String
"(0,\"\")" --empty ident, won't shadow number
  showParseable (Just a
x) = a -> Doc
forall t. ShowParseable t => t -> Doc
showParseable a
x

instance (ShowParseable a) => ShowParseable (Located a) where
  showParseable :: Located a -> Doc
showParseable Located a
l = a -> Doc
forall t. ShowParseable t => t -> Doc
showParseable (Located a -> a
forall a. Located a -> a
thing Located a
l)

instance ShowParseable TParam where
  showParseable :: TParam -> Doc
showParseable TParam
tp = Doc -> Doc
parens (String -> Doc
text (Int -> String
forall a. Show a => a -> String
show (TParam -> Int
tpUnique TParam
tp)) Doc -> Doc -> Doc
PP.<> Doc
comma Doc -> Doc -> Doc
PP.<> Maybe Name -> Doc
maybeNameDoc (TParam -> Maybe Name
tpName TParam
tp))

maybeNameDoc :: Maybe Name -> Doc
maybeNameDoc :: Maybe Name -> Doc
maybeNameDoc Maybe Name
Nothing = Doc -> Doc
doubleQuotes Doc
empty
maybeNameDoc (Just Name
n) = Ident -> Doc
forall t. ShowParseable t => t -> Doc
showParseable (Name -> Ident
nameIdent Name
n)

instance ShowParseable Name where
  showParseable :: Name -> Doc
showParseable Name
n = Doc -> Doc
parens (String -> Doc
text (Int -> String
forall a. Show a => a -> String
show (Name -> Int
nameUnique Name
n)) Doc -> Doc -> Doc
PP.<> Doc
comma Doc -> Doc -> Doc
PP.<> Ident -> Doc
forall t. ShowParseable t => t -> Doc
showParseable (Name -> Ident
nameIdent Name
n))