Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
KonjacSource committed Oct 8, 2024
1 parent 4059719 commit 873084b
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Draft.shitt
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ fun nomatchTest (n : N) (_: Id (succ (succ n)) (succ n)) : N where


mutual

fun even (_ : N) : Bool
fun odd (_ : N) : Bool

begin

fun even
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,17 @@ def addComm (x y : N) : Id (add x y) (add y x) where


```

## Reference

Pattern matching and inductive type:

- Ulf Norell. [Towards a practical programming language based on dependent type theory](https://www.cse.chalmers.se/~ulfn/papers/thesis.pdf).

Solving meta variables:

- András Kovács. [elaboration-zoo](https://github.com/AndrasKovacs/elaboration-zoo).

Termination checking:

- Karl Mehltretter. [Termination Checking for a Dependently Typed Language](https://www.cse.chalmers.se/~abela/mehltret-da.pdf).
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies:
- exception-transformers
- megaparsec
- mtl
- matrix

ghc-options:
- -Wall
Expand Down
5 changes: 5 additions & 0 deletions shitt.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ library
ShiTT.Meta
ShiTT.Parser
ShiTT.Syntax
ShiTT.Termination.Call
ShiTT.Termination.Ord
ShiTT.TermParser
Test
other-modules:
Expand Down Expand Up @@ -68,6 +70,7 @@ library
base >=4.7 && <5
, containers
, exception-transformers
, matrix
, megaparsec
, mtl
, transformers
Expand Down Expand Up @@ -108,6 +111,7 @@ executable shitt-exe
base >=4.7 && <5
, containers
, exception-transformers
, matrix
, megaparsec
, mtl
, shitt
Expand Down Expand Up @@ -150,6 +154,7 @@ test-suite shitt-test
base >=4.7 && <5
, containers
, exception-transformers
, matrix
, megaparsec
, mtl
, shitt
Expand Down
6 changes: 6 additions & 0 deletions src/ShiTT/Context.hs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ data Fun = Fun
, funClauses :: Maybe [Clause]
}

instance Eq Fun where
f == g = f.funName == g.funName

instance Ord Fun where
compare f g = compare f.funName g.funName

instance Show Fun where
show = funName

Expand Down
21 changes: 21 additions & 0 deletions src/ShiTT/Termination/Call.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module ShiTT.Termination.Call where

import ShiTT.Eval
import ShiTT.Context
import ShiTT.Syntax
import Data.Matrix hiding (trace)
import qualified Data.Set as S
import ShiTT.Termination.Ord

instance Ord e => Ord (Matrix e) where
compare a b = compare (toList a) (toList b)

data CallMat = CallMat
{ fromFun :: Fun
, callFun :: Fun
, callMat :: Matrix Order
} deriving (Eq, Ord, Show)

-- | The cm function from paper.
-- getCallMat :: (Fun, [Pattern]) -> (Fun, Spine) -> CallMat
-- getCallMat
70 changes: 70 additions & 0 deletions src/ShiTT/Termination/Ord.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
module ShiTT.Termination.Ord where

import ShiTT.Context
import ShiTT.Syntax

data Order = Unsure | LessEq | Less
deriving (Eq, Ord)

instance Show Order where
show = \case
Unsure -> "?"
Less -> "<"
LessEq -> "<="

-- | Unsure as zero, LessEq as one, Less as infinity
instance Num Order where
fromInteger 0 = Unsure
fromInteger _ = LessEq

x + y = max x y
-- case (x, y) of
-- (Unsure, x) -> x -- Unsure as zero
-- (x, Unsure) -> x
-- (Less, x) -> Less -- Less as inf
-- (x, Less) -> Less
-- (LessEq, LessEq) -> LessEq

x * y = case (x, y) of
(Unsure, _) -> Unsure -- Unsure as zero
(_, Unsure) -> Unsure
(LessEq, x) -> x -- LessEq as one
(x, LessEq) -> x
(Less, Less) -> Less

abs = error "I hate Num"
signum = error "I hate Num"
negate = error "I hate Num"

maxOf :: Foldable f => f Order -> Order
maxOf = foldr (+) Unsure

minOf :: Foldable f => f Order -> Order
minOf = foldr min Less

-- | We would simply ignore the inaccessible patterns, so no need for context.
-- It would not be a big problem, since we (usually) could show the decreasing by other accessible pattern .
compareTP :: Context -> Value -> Pattern -> Order
compareTP ctx e p = case (e, p) of
(VVar x, p) -> compareNP ctx x p -- This case might be useless
(VPatVar x [], p) -> compareNP ctx x p
(VCon con' [], PCon con ps _)
| con' == con -> LessEq
(VCon con' con_arg, PCon con pcon_arg _)
| con' == con ->
case lookupCon' con ctx of
Just (dat, con) ->
let data_para_leng = length dat.dataPara
con_arg' = map fst $ drop data_para_leng con_arg in
if (length con_arg - data_para_leng) == length pcon_arg then
minOf $ zipWith (compareTP ctx) con_arg' pcon_arg
else
Unsure
_ -> Unsure
_ -> Unsure

compareNP :: Context -> Name -> Pattern -> Order
compareNP ctx x = \case
PVar x' _ | x == x' -> LessEq
PCon con ps _ -> Less * maxOf (map (compareNP ctx x) ps)
_ -> Unsure
1 change: 1 addition & 0 deletions src/ShiTT/Termination/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This part is mainly based on [Termination Checking for a Dependently Typed Language](https://www.cse.chalmers.se/~abela/mehltret-da.pdf).

0 comments on commit 873084b

Please sign in to comment.