wip in getting IO type and its hash from the Unison source, rather than from the codebase

This commit is contained in:
Paul Chiusano 2019-03-11 17:12:16 -04:00
parent e3d59384fb
commit e12bd6a4f7
3 changed files with 204 additions and 3 deletions

View File

@ -0,0 +1,194 @@
{-# Language TemplateHaskell #-}
{-# Language QuasiQuotes #-}
module Unison.Runtime.IOSource where
import Control.Monad.Identity (runIdentity, Identity)
import Data.String (fromString)
import Data.Text (Text)
import Text.RawString.QQ (r)
import Unison.FileParsers (parseAndSynthesizeFile)
import Unison.Parser (Ann(..))
import Unison.Symbol (Symbol)
import qualified Unison.Builtin as Builtin
import qualified Unison.Result as Result
import qualified Unison.Typechecker.TypeLookup as TL
import qualified Unison.UnisonFile as UF
typecheckedFile :: UF.TypecheckedUnisonFile Symbol Ann
typecheckedFile = let
tl :: a -> Identity (TL.TypeLookup Symbol Ann)
tl = const $ pure (External <$ TL.builtinTypeLookup)
r = parseAndSynthesizeFile [] tl Builtin.names "<IO.u builtin>" source
in case runIdentity $ Result.runResultT r of
(Nothing, notes) -> error $ "parsing failed: " <> show notes
(Just (_ppe, Nothing), notes) -> error $ "typechecking failed" <> show notes
(Just (_, Just file), _) -> file
source :: Text
source = fromString [r|
-- Handles are unique identifiers.
-- The implementation of IO in the runtime will supply Haskell
-- file handles and map those to Unison handles.
-- A pure implementation of I/O might use some kind of pure supply
-- of unique IDs instead.
type Handle = Handle Text
-- Ditto for sockets
type Socket = Socket Text
-- Builtin handles: standard in, out, error
namespace IO where
stdin: Handle
stdin = Handle "stdin"
stdout: Handle
stdout = Handle "stdout"
stderr: Handle
stderr = Handle "stderr"
printLine : Text -> {IO} ()
printLine t =
IO.putText stdout t
IO.putText stdout "\n"
-- IO Modes from the Haskell API
type IOMode = Read | Write | Append | ReadWrite
-- IO error types from the Haskell API
type IOErrorType
= AlreadyExists
| NoSuchThing
| ResourceBusy
| ResourceExhausted
| EOF
| IllegalOperation
| PermissionDenied
| UserError
type ErrorLocation = ErrorLocation Text
type ErrorDescription = ErrorDescription Text
type FilePath = FilePath Text
type IOError =
IOError
(Optional Handle)
IOErrorType
ErrorLocation
ErrorDescription
(Optional FilePath)
type SeekMode = Absolute | Relative | FromEnd
-- If the buffer size is not specified,
-- use an implementation-specific size.
type BufferMode = Line | Block (Optional Nat)
type EpochTime = EpochTime Nat
-- Either a host name e.g., "unisonweb.org" or a numeric host address
-- string consisting of a dotted decimal IPv4 address or an IPv6 address
-- e.g., "192.168.0.1".
type HostName = HostName Text
type PortNumber = Nat
-- Represents a 32-bit host address
type HostAddress = HostAddress Int
-- Internet protocol v4 socket address
type SocketAddress = SocketAddress HostAddress PortNumber
ability IO where
-- Basic file IO
openFile : FilePath -> IOMode ->{IO} Handle
closeFile : Handle ->{IO} ()
isEOF : Handle ->{IO} Boolean
isFileOpen : Handle ->{IO} Boolean
-- Text input and output
--getChar : Handle ->{IO} Char
getLine : Handle ->{IO} Text
-- Get the entire contents of the file as text
getText : Handle ->{IO} Text
-- putChar : Handle -> Char ->{IO} ()
putText : Handle -> Text ->{IO} ()
-- Handling I/O errors.
-- Question: can we do better?
throw : IOError ->{IO} a
catch : '{IO} a -> (IOError ->{IO} a) ->{IO} a
-- File positioning
isSeekable : Handle ->{IO} Boolean
seek : Handle -> SeekMode -> Int ->{IO} ()
position : Handle ->{IO} Int
-- File buffering
getBuffering : Handle ->{IO} (Optional BufferMode)
setBuffering : Handle -> Optional BufferMode ->{IO} ()
-- Should we expose mutable arrays for byte buffering?
-- Inclined to say no, although that sounds a lot like
-- a decision to just be slow.
-- We'll need a byte buffer manipulation library in that case.
-- getBytes : Handle -> Nat ->{IO} Bytes
-- putBytes : Handle -> Bytes ->{IO} ()
-- getBytes : Handle -> Nat -> ByteArray ->{IO} Nat
-- putBytes : Handle -> Nat -> ByteArray ->{IO} ()
systemTime : {IO} EpochTime
-- File system operations
getCurrentDirectory : {IO} FilePath
setCurrentDirectory : FilePath ->{IO} ()
directoryContents : FilePath ->{IO} [FilePath]
fileExists : FilePath -> {IO} Boolean
isDirectory : FilePath ->{IO} Boolean
createDirectory : FilePath ->{IO} ()
removeDirectory : FilePath ->{IO} ()
renameDirectory : FilePath -> FilePath -> {IO} ()
removeFile : FilePath ->{IO} ()
renameFile : FilePath -> FilePath ->{IO} ()
getFileTimestamp : FilePath ->{IO} EpochTime
getFileSize : FilePath ->{IO} Nat
-- Network I/O
-- Glossing over address families (ipv4, ipv6),
-- and socket types (stream, raw, etc)
-- Creates a socket and binds it to a the given local port
serverSocket : SocketAddress -> {IO} Socket
-- Creates a socket connected to the given remote address
clientSocket : SocketAddress -> {IO} Socket
socketToHandle : Socket ->{IO} Handle
handleToSocket : Handle ->{IO} Socket
closeSocket : Socket ->{IO} ()
-- Accept a connection on a socket.
-- Returns a socket that can send and receive data on a new connection,
-- together with the remote host information.
accept : Socket ->{IO} (Socket, SocketAddress)
-- Returns the number of bytes actually sent
-- send : Socket -> Bytes ->{IO} Int
-- scatter/gather mode network I/O
-- sendMany : Socket -> [Bytes] ->{IO} Int
-- Read the spefified number of bytes from the socket.
-- receive : Socket -> Int ->{IO} Bytes
|]

View File

@ -2,13 +2,14 @@ module Unison.Typechecker.TypeLookup where
import Control.Applicative ((<|>))
import Data.Map (Map)
import qualified Data.Map as Map
import Unison.Reference (Reference)
import Unison.Referent (Referent)
import qualified Unison.Referent as Referent
import Unison.Type (AnnotatedType)
import qualified Unison.DataDeclaration as DD
import Unison.Var (Var)
import qualified Data.Map as Map
import qualified Unison.ConstructorType as CT
import qualified Unison.DataDeclaration as DD
import qualified Unison.Referent as Referent
type Type v a = AnnotatedType v a
type DataDeclaration v a = DD.DataDeclaration' v a
@ -23,6 +24,10 @@ data TypeLookup v a =
, effectDecls :: Map Reference (EffectDeclaration v a) }
deriving Show
builtinTypeLookup :: Var v => TypeLookup v ()
builtinTypeLookup = TypeLookup mempty decls mempty where
decls = Map.fromList [ (r, dd) | (_, r, dd) <- DD.builtinDataDecls ]
typeOfReferent :: TypeLookup v a -> Referent -> Maybe (Type v a)
typeOfReferent tl r = case r of
Referent.Ref r -> typeOfTerm tl r

View File

@ -85,6 +85,7 @@ library
Unison.Runtime.IR
Unison.Runtime.Rt1
Unison.Runtime.Rt1IO
Unison.Runtime.IOSource
Unison.Runtime.Vector
Unison.Runtime.SparseVector
Unison.Settings
@ -155,6 +156,7 @@ library
megaparsec,
prelude-extras,
random,
raw-strings-qq,
regex-base,
regex-tdfa,
safe,