Liquid haskell表示,服务器是安全的

5nd*_*ndG 8 haskell liquid-haskell

我在Haskell中有一个响应Json输入的服务器.问题是,有些情况下服务器会因部分功能而崩溃,但Liquid Haskell表示这是安全的.

这是一个最小的工作示例:

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}

module Main where

import qualified Web.Scotty as Scot
import GHC.Generics (Generic)
import qualified Data.Aeson as Json
import Data.Text.Internal.Lazy (Text)

main :: IO ()
main =
  Scot.scotty 3000 $
    Scot.get "/:queryJson" $ do
      rawRequest <- Scot.param "queryJson"
      case Json.decode rawRequest of
        Nothing -> Scot.text "Could not decode input."
        Just input -> Scot.text $ makeOutput (dim1 input)

{-@ type Dim = { x : Int | x >= 0 && x <= 1 } @-}

{-@ makeOutput :: Dim -> Text @-}
makeOutput :: Int -> Text
makeOutput dim =
  case dim of
    0 -> "a"
    1 -> "b"
    _ -> error "Liquid haskell should stop this happening."

{-@ dim1 :: Input -> Dim @-}
data Input = Input
  { dim1 :: Int
  } deriving (Generic)

instance Json.FromJSON Input
Run Code Online (Sandbox Code Playgroud)

Liquid Haskell说这是安全的,但我可以通过访问http:// localhost:3000 / {"dim1":2} 来崩溃它.

我想要的是Liquid Haskell告诉我'dim1'函数的注释无效,因为它无法确定输入是0还是1.

编辑:

我发现如果我在Input中为dim1手动创建一个访问函数,例如:

{-@ dim1get :: Input -> Dim @-}
dim1get :: Input -> Int
dim1get (Input x) = x
Run Code Online (Sandbox Code Playgroud)

并使用它代替'dim1'函数,然后我从Liquid Haskell得到代码不安全的必要警告.