-- |
-- 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 Data.Void
import Prettyprinter

import Cryptol.TypeCheck.AST
import Cryptol.Utils.Ident (Ident,unpackIdent)
import Cryptol.Utils.RecordMap (canonicalFields)
import Cryptol.Parser.AST ( Located(..))
import Cryptol.ModuleSystem.Name


infixl 5 $$
($$) :: Doc a -> Doc a -> Doc a
$$ :: Doc a -> Doc a -> Doc a
($$) Doc a
x Doc a
y = [Doc a] -> Doc a
forall ann. [Doc ann] -> Doc ann
sep [Doc a
x, Doc a
y]

text :: String -> Doc a
text :: String -> Doc a
text = String -> Doc a
forall a ann. Pretty a => a -> Doc ann
pretty

int :: Int -> Doc a
int :: Int -> Doc a
int = Int -> Doc a
forall a ann. Pretty a => a -> Doc ann
pretty

-- 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 Void

instance ShowParseable Expr where
  showParseable :: Expr -> Doc Void
showParseable (ELocated Range
_ Expr
e) = Expr -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable Expr
e -- TODO? emit range information
  showParseable (EList [Expr]
es Type
_) = Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann
parens (String -> Doc Void
forall a. String -> Doc a
text String
"EList" Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
<+> [Expr] -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable [Expr]
es)
  showParseable (ETuple [Expr]
es) = Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann
parens (String -> Doc Void
forall a. String -> Doc a
text String
"ETuple" Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
<+> [Expr] -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable [Expr]
es)
  showParseable (ERec RecordMap Ident Expr
ides) = Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann
parens (String -> Doc Void
forall a. String -> Doc a
text String
"ERec" Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
<+> [(Ident, Expr)] -> Doc Void
forall t. ShowParseable t => t -> Doc Void
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 Void -> Doc Void
forall ann. Doc ann -> Doc ann
parens (String -> Doc Void
forall a. String -> Doc a
text String
"ESel" Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Expr -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable Expr
e Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Selector -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable Selector
s)
  showParseable (ESet Type
_ty Expr
e Selector
s Expr
v) = Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann
parens (String -> Doc Void
forall a. String -> Doc a
text String
"ESet" Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
<+>
                                Expr -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable Expr
e Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Selector -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable Selector
s
                                                Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Expr -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable Expr
v)
  showParseable (EIf Expr
c Expr
t Expr
f) = Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann
parens (String -> Doc Void
forall a. String -> Doc a
text String
"EIf" Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Expr -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable Expr
c Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
$$ Expr -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable Expr
t Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
$$ Expr -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable Expr
f)
  showParseable (EComp Type
_ Type
_ Expr
e [[Match]]
mss) = Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann
parens (String -> Doc Void
forall a. String -> Doc a
text String
"EComp" Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
$$ Expr -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable Expr
e Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
$$ [[Match]] -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable [[Match]]
mss)
  showParseable (EVar Name
n) = Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann
parens (String -> Doc Void
forall a. String -> Doc a
text String
"EVar" Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Name -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable Name
n)
  showParseable (EApp Expr
fe Expr
ae) = Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann
parens (String -> Doc Void
forall a. String -> Doc a
text String
"EApp" Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
$$ Expr -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable Expr
fe Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
$$ Expr -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable Expr
ae)
  showParseable (EAbs Name
n Type
_ Expr
e) = Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann
parens (String -> Doc Void
forall a. String -> Doc a
text String
"EAbs" Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Name -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable Name
n Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
$$ Expr -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable Expr
e)
  showParseable (EWhere Expr
e [DeclGroup]
dclg) = Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann
parens (String -> Doc Void
forall a. String -> Doc a
text String
"EWhere" Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
$$ Expr -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable Expr
e Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
$$ [DeclGroup] -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable [DeclGroup]
dclg)
  showParseable (ETAbs TParam
tp Expr
e) = Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann
parens (String -> Doc Void
forall a. String -> Doc a
text String
"ETAbs" Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
<+> TParam -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable TParam
tp
                                        Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
$$ Expr -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable Expr
e)
  showParseable (ETApp Expr
e Type
t) = Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann
parens (String -> Doc Void
forall a. String -> Doc a
text String
"ETApp" Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
$$ Expr -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable Expr
e Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
$$ Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann
parens (String -> Doc Void
forall a. String -> Doc a
text String
"ETyp" Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Type -> Doc Void
forall t. ShowParseable t => t -> Doc Void
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 Void
forall t. ShowParseable t => t -> Doc Void
showParseable Expr
e --"(EProofAbs " ++ show p ++ showParseable e ++ ")"
  showParseable (EProofApp Expr
e) = Expr -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable Expr
e --"(EProofApp " ++ showParseable e ++ ")"

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

instance ShowParseable Int where
  showParseable :: Int -> Doc Void
showParseable Int
i = Int -> Doc Void
forall a. Int -> Doc a
int Int
i

instance ShowParseable Ident where
  showParseable :: Ident -> Doc Void
showParseable Ident
i = String -> Doc Void
forall a. String -> Doc a
text (String -> Doc Void) -> String -> Doc Void
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 Void
showParseable (TUser Name
n [Type]
lt Type
t) = Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann
parens (String -> Doc Void
forall a. String -> Doc a
text String
"TUser" Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Name -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable Name
n Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
<+> [Type] -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable [Type]
lt Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Type -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable Type
t)
  showParseable (TRec RecordMap Ident Type
lidt) = Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann
parens (String -> Doc Void
forall a. String -> Doc a
text String
"TRec" Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
<+> [(Ident, Type)] -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable (RecordMap Ident Type -> [(Ident, Type)]
forall a b. RecordMap a b -> [(a, b)]
canonicalFields RecordMap Ident Type
lidt))
  showParseable Type
t = Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann
parens (Doc Void -> Doc Void) -> Doc Void -> Doc Void
forall a b. (a -> b) -> a -> b
$ String -> Doc Void
forall a. String -> Doc a
text (String -> Doc Void) -> String -> Doc Void
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 Void
showParseable (TupleSel Int
n Maybe Int
_) = Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann
parens (String -> Doc Void
forall a. String -> Doc a
text String
"TupleSel" Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Int -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable Int
n)
  showParseable (RecordSel Ident
n Maybe [Ident]
_) = Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann
parens (String -> Doc Void
forall a. String -> Doc a
text String
"RecordSel" Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Ident -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable Ident
n)
  showParseable (ListSel Int
n Maybe Int
_) = Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann
parens (String -> Doc Void
forall a. String -> Doc a
text String
"ListSel" Doc Void -> Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Int -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable Int
n)

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

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

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

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

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

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

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

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

maybeNameDoc :: Maybe Name -> Doc Void
maybeNameDoc :: Maybe Name -> Doc Void
maybeNameDoc Maybe Name
Nothing = Doc Void -> Doc Void
forall ann. Doc ann -> Doc ann
dquotes Doc Void
forall a. Monoid a => a
mempty
maybeNameDoc (Just Name
n) = Ident -> Doc Void
forall t. ShowParseable t => t -> Doc Void
showParseable (Name -> Ident
nameIdent Name
n)

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